Class: Cell::Rails
- Inherits:
-
AbstractController::Base
- Object
- AbstractController::Base
- Cell::Rails
- Includes:
- AbstractController, ActionController::RequestForgeryProtection, Callbacks, Cell, Caching, Metal, Rendering, VersionStrategy, Helpers, Layouts, Logger, Translation
- Defined in:
- lib/cell/rails.rb
Defined Under Namespace
Modules: Metal, Rendering Classes: View
Constant Summary
Constants included from Cell
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#parent_controller ⇒ Object
readonly
Returns the value of attribute parent_controller.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(parent_controller, *args) ⇒ Rails
constructor
A new instance of Rails.
-
#render(*args) ⇒ Object
Renders the view for the current state and returns the markup.
-
#setup_backwardibility(*args) ⇒ Object
Some people still like #options and assume it’s a hash.
Methods included from Caching
Methods included from Rendering
Constructor Details
#initialize(parent_controller, *args) ⇒ Rails
Returns a new instance of Rails.
52 53 54 55 56 |
# File 'lib/cell/rails.rb', line 52 def initialize(parent_controller, *args) super() @parent_controller = parent_controller setup_backwardibility(*args) end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
47 48 49 |
# File 'lib/cell/rails.rb', line 47 def @options end |
#parent_controller ⇒ Object (readonly)
Returns the value of attribute parent_controller.
46 47 48 |
# File 'lib/cell/rails.rb', line 46 def parent_controller @parent_controller end |
Class Method Details
.controller_path ⇒ Object
64 65 66 |
# File 'lib/cell/rails.rb', line 64 def self.controller_path @controller_path ||= name.sub(/Cell$/, '').underscore unless anonymous? end |
Instance Method Details
#render(*args) ⇒ Object
Renders the view for the current state and returns the markup. Don’t forget to return the markup itself from the state method.
Options
:view-
Specifies the name of the view file to render. Defaults to the current state name.
:layout-
Renders the state wrapped in the layout. Layouts reside in
app/cells/layouts. :locals-
Makes the named parameters available as variables in the view.
:text-
Just renders plain text.
:inline-
Renders an inline template as state view. See ActionView::Base#render for details.
:file-
Specifies the name of the file template to render.
:nothing-
Doesn’t invoke the rendering process.
:state-
Instantly invokes another rendering cycle for the passed state and returns. You may pass arbitrary state-args to the called state.
:format-
Sets a different template format, e.g.
:json.
Example:
class MusicianCell < ::Cell::Base
def sing
# ... laalaa
render
end
renders the view musician/sing.html.
def sing
# ... laalaa
render :view => :shout, :layout => 'metal'
end
renders musician/shout.html and wrap it in app/cells/layouts/metal.html.erb.
#render is explicit!
You can also alter the markup from #render. Just remember to return it.
def sing
render + render + render
end
will render three concated views.
Partials?
In Cells we abandoned the term ‘partial’ in favor of plain ‘views’ - we don’t need to distinguish between both terms. A cell view is both, a view and a kind of partial as it represents only a fragment of the page.
Just use :view and enjoy.
Using states instead of helpers
Sometimes it’s useful to not only render a view but also invoke the associated state. This is especially helpful when replacing helpers. Do that with render :state.
def show_cheap_item(item)
render if item.price <= 1
end
A view could use this state in place of an odd helper.
- @items.each do |item|
= render({:state => :show_cheap_item}, item)
This calls the state method which in turn will render its view - if the item isn’t too expensive.
130 131 132 |
# File 'lib/cell/rails.rb', line 130 def render(*args) render_view_for(self.action_name, *args) end |
#setup_backwardibility(*args) ⇒ Object
Some people still like #options and assume it’s a hash.
59 60 61 62 |
# File 'lib/cell/rails.rb', line 59 def setup_backwardibility(*args) @options = (args.first.is_a?(Hash) and args.size == 1) ? args.first : args @opts = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :options) end |