Skip to content

ViewModels#model_reader

floere edited this page Sep 13, 2010 · 1 revision

Getting values from the model

Either you explicitly call the model, like so model.some_attribute, or you install model readers.

Model readers are just delegates to the model, with the added functionality that they can have filters on them, like the h method, or upcase for example.

Examples

Without Filter

model_reader :first_name, :last_name

Creates two delegate methods first_name and last_name that delegate to the model.

Calling first_name in the view model will simply give you the model value.
model.first_name # => "Joe"
view_model.first_name # => "Joe"

With Filter

model_reader :description, :filter_through => :h

Creates a description delegate method that filters the model value through h.

Calling description in the view model will give you the html escaped model value.
model.description # => "<script & more>"
view_model.description # => "&lt;script &amp; more&gt;"

With Multiple Filters

model_reader :description, :filter_through => [:textilize, :h]

Creates a description delegate method that filters the model value through first textilize, then h.

With Multiple Filter and Delegate Methods

model_reader :first_name, :last_name, :filter_through => [:textilize, :h]

Creates both a first_name and last_name delegate method that filters the model value through first textilize, then h.

Note: Filter methods can be any method on the view_model with arity 1.

Clone this wiki locally