Module: Cell::ClassMethods
- Defined in:
- lib/cell.rb
Instance Method Summary collapse
-
#build(&block) ⇒ Object
Adds a builder to the cell class.
- #build_class_for(controller, target_class, *args) ⇒ Object
- #builders ⇒ Object
-
#class_from_cell_name(cell_name) ⇒ Object
The cell class constant for
cell_name. -
#create_cell_for(controller, name, *args) ⇒ Object
Creates a cell instance.
- #render_cell_for(controller, name, state, *args) {|cell| ... } ⇒ Object
-
#setup_view_paths! ⇒ Object
Called in Railtie at initialization time.
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.
58 59 60 |
# File 'lib/cell.rb', line 58 def build(&block) builders << block end |
#build_class_for(controller, target_class, *args) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/cell.rb', line 62 def build_class_for(controller, target_class, *args) target_class.builders.each do |blk| res = controller.instance_exec(*args, &blk) and return res end target_class end |
#builders ⇒ Object
69 70 71 |
# File 'lib/cell.rb', line 69 def builders @builders ||= [] end |
#class_from_cell_name(cell_name) ⇒ Object
The cell class constant for cell_name.
74 75 76 |
# File 'lib/cell.rb', line 74 def class_from_cell_name(cell_name) "#{cell_name}_cell".classify.constantize end |
#create_cell_for(controller, name, *args) ⇒ Object
Creates a cell instance. Note that this method calls builders which were attached to the class with Cell::Base.build - this might lead to a different cell being returned.
27 28 29 30 |
# File 'lib/cell.rb', line 27 def create_cell_for(controller, name, *args) build_class_for(controller, class_from_cell_name(name), *args). new(controller, *args) end |
#render_cell_for(controller, name, state, *args) {|cell| ... } ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/cell.rb', line 17 def render_cell_for(controller, name, state, *args) cell = create_cell_for(controller, name, *args) # DISCUSS: we always save options. yield cell if block_given? return cell.render_state(state, *args) if cell.state_accepts_args?(state) cell.render_state(state) # backward-compat. end |
#setup_view_paths! ⇒ Object
Called in Railtie at initialization time.
13 14 15 |
# File 'lib/cell.rb', line 13 def setup_view_paths! self.view_paths = self::DEFAULT_VIEW_PATHS end |