Module: Cell::ClassMethods

Defined in:
lib/cell.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) build(&block)

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.



57
58
59
# File 'lib/cell.rb', line 57

def build(&block)
  builders << block
end

- (Object) build_class_for(controller, target_class, *args)



61
62
63
64
65
66
# File 'lib/cell.rb', line 61

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

- (Object) builders



68
69
70
# File 'lib/cell.rb', line 68

def builders
  @builders ||= []
end

- (Object) class_from_cell_name(cell_name)

The cell class constant for cell_name.



73
74
75
# File 'lib/cell.rb', line 73

def class_from_cell_name(cell_name)
  "#{cell_name}_cell".classify.constantize
end

- (Object) create_cell_for(controller, name, *args)

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.



26
27
28
29
# File 'lib/cell.rb', line 26

def create_cell_for(controller, name, *args)
  build_class_for(controller, class_from_cell_name(name), *args).
  new(controller, *args)
end

- (Object) render_cell_for(controller, name, state, *args) {|cell| ... }

Yields:

  • (cell)


17
18
19
20
21
22
# 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?
  
  cell.render_state_with_args(state, *args)
end

- (Object) setup_view_paths!

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