Class: Cell::ViewModel

Inherits:
Object
  • Object
show all
Extended by:
Abstract, SelfContained, Util, Helpers, Uber::Delegates, Uber::InheritableAttr
Includes:
Caching, Layout, Prefixes, ProcessOptions, Rendering, TemplateFor, Uber::Builder
Defined in:
lib/cell/view_model.rb

Direct Known Subclasses

Concept

Defined Under Namespace

Modules: Escaped, Helpers, Partial, ProcessOptions, Rendering, TemplateFor Classes: OutputBuffer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

util

Methods included from SelfContained

self_contained!

Methods included from Abstract

abstract!, abstract?

Methods included from Layout

included, #process_options!

Methods included from ProcessOptions

#process_options!

Methods included from TemplateFor

#find_template, #template_for

Methods included from Caching

#cache?, #cache_store, included, #render_state

Methods included from Rendering

#call, #render

Methods included from Prefixes

#_prefixes, included

Constructor Details

#initialize(model = nil, options = {}) ⇒ ViewModel

in Ruby 2: def m(model: nil, controller:nil, **options) that'll make the controller optional.



88
89
90
91
92
93
# File 'lib/cell/view_model.rb', line 88

def initialize(model=nil, options={}) # in Ruby 2: def m(model: nil, controller:nil, **options) that'll make the controller optional.
  # options            = options.clone # DISCUSS: this could be time consuming when rendering many of em.
  @parent_controller = options[:controller] # TODO: filter out controller in a performant way.

  setup!(model, options)
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model



33
34
35
# File 'lib/cell/view_model.rb', line 33

def model
  @model
end

#parent_controllerObject (readonly) Also known as: controller

Returns the value of attribute parent_controller



94
95
96
# File 'lib/cell/view_model.rb', line 94

def parent_controller
  @parent_controller
end

Class Method Details

.build(*args) ⇒ Object

semi-public.



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

def build(*args) # semi-public.
  class_builder.call(*args).new(*args) # Uber::Builder::class_builder.
end

.call(model = nil, options = {}, &block) ⇒ Object

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

SongCell.(@song)
SongCell.(collection: Song.all)


64
65
66
67
68
69
70
# File 'lib/cell/view_model.rb', line 64

def call(model=nil, options={}, &block)
  if model.is_a?(Hash) and array = model.delete(:collection)
    return render_collection(array, model.merge(options))
  end

  build(model, options)
end

.controller_pathObject



29
30
31
# File 'lib/cell/view_model.rb', line 29

def self.controller_path
  @controller_path ||= util.underscore(name.sub(/Cell$/, ''))
end

.property(*names) ⇒ Object



54
55
56
# File 'lib/cell/view_model.rb', line 54

def property(*names)
  delegates :model, *names # Uber::Delegates.
end

.templatesObject



19
20
21
# File 'lib/cell/view_model.rb', line 19

def templates
  @templates ||= Templates.new # note: this is shared in subclasses. do we really want this?
end

Instance Method Details

#cell(name, model = nil, options = {}) ⇒ Object

Get nested cell in instance.



83
84
85
# File 'lib/cell/view_model.rb', line 83

def cell(name, model=nil, options={})
  self.class.cell(name, model, options.merge(controller: parent_controller))
end

#controller_pathObject



7
8
9
# File 'lib/cell/view_model.rb', line 7

def controller_path
  self.class.controller_path
end

#to_sObject

alias_method :to_s, :call # FIXME: why doesn't this work?



144
145
146
# File 'lib/cell/view_model.rb', line 144

def to_s
  call
end