Module: Cell::Builder::ClassMethods
- Included in:
- Cell::Base
- Defined in:
- lib/cell/builder.rb
Instance Method Summary collapse
-
#build(&block) ⇒ Object
Adds a builder to the cell class.
- #builders ⇒ Object
Instance Method Details
#build(&block) ⇒ Object
Adds a builder to the cell class. Builders are used in #render_cell to find out the concrete class for rendering. This is helpful if you frequently want to render subclasses according to different circumstances (e.g. login situations) and you don’t want to place these deciders in your view code.
Passes the opts hash from #render_cell into the block. The block is executed in controller context. Multiple build blocks are ORed, if no builder matches the building cell is used.
Example:
Consider two different user box cells in your app.
class AuthorizedUserBox < UserInfoBox
end
class AdminUserBox < UserInfoBox
end
Now you don’t want to have deciders all over your views - use a declarative builder.
UserInfoBox.build do |opts|
AuthorizedUserBox if user_signed_in?
AdminUserBox if admin_signed_in?
end
In your view #render_cell will instantiate the right cell for you now.
62 63 64 |
# File 'lib/cell/builder.rb', line 62 def build(&block) builders << block end |
#builders ⇒ Object
66 67 68 |
# File 'lib/cell/builder.rb', line 66 def builders @builders ||= [] end |