class Cell::ViewModel

Attributes

model[R]
options[R]

Public Class Methods

call(model=nil, options={}, &block) click to toggle source

Public entry point. Use this to instantiate cells with builders.

SongCell.(@song)
SongCell.(collection: Song.all)
# File lib/cell/view_model.rb, line 47
def call(model=nil, options={}, &block)
  if model.is_a?(Hash) and array = model[:collection]
    return Collection.new(array, model.merge(options), self)
  end

  build(model, options)
end
controller_path() click to toggle source
# File lib/cell/view_model.rb, line 23
def self.controller_path
  @controller_path ||= util.underscore(name.sub(/Cell$/, ''))
end
new(model=nil, options={}) click to toggle source
# File lib/cell/view_model.rb, line 70
def initialize(model=nil, options={})
  setup!(model, options)
end
property(*names) click to toggle source
# File lib/cell/view_model.rb, line 39
def property(*names)
  delegates :model, *names # Uber::Delegates.
end
templates() click to toggle source
# File lib/cell/view_model.rb, line 15
def templates
  @templates ||= Templates.new # note: this is shared in subclasses. do we really want this?
end

Private Class Methods

class_from_cell_name(name) click to toggle source
# File lib/cell/view_model.rb, line 58
def class_from_cell_name(name)
  util.constant_for("#{name}_cell")
end

Public Instance Methods

cell(name, model=nil, options={}) click to toggle source

Build nested cell in instance.

# File lib/cell/view_model.rb, line 64
def cell(name, model=nil, options={})
  context = Context[options[:context], self.context]

  self.class.cell(name, model, options.merge(context: context))
end
context() click to toggle source
# File lib/cell/view_model.rb, line 74
def context
  @options[:context]
end
to_s() click to toggle source
# File lib/cell/view_model.rb, line 124
def to_s
  call
end

Private Instance Methods

normalize_options(options) click to toggle source
# File lib/cell/view_model.rb, line 178
def normalize_options(options)
  options = if options.is_a?(Hash)
    options[:view] ||= state_for_implicit_render(options)
    options
  else
    {view: options.to_s}
  end

  options[:prefixes] ||= _prefixes

  process_options!(options)
  options
end
output_buffer() click to toggle source
# File lib/cell/view_model.rb, line 153
def output_buffer # called from the precompiled template. FIXME: this is currently not used in Haml.
  OutputBuffer.new # don't cache output_buffer, for every #render call we get a fresh one.
end
setup!(model, options) click to toggle source
# File lib/cell/view_model.rb, line 132
def setup!(model, options)
  @model   = model
  @options = options
  # or: create_twin(model, options)
end
state_for_implicit_render(options) click to toggle source

Computes the view name from the call stack in which `render` was invoked.

# File lib/cell/view_model.rb, line 200
def state_for_implicit_render(options)
  _caller = RUBY_VERSION < "2.0" ? caller(3) : caller(3, 1) # TODO: remove case in 5.0 when dropping 1.9.
  _caller[0].match(/`(\w+)/)[1]
end