Module: ViewModels::Extensions::ModelReader

Included in:
Base
Defined in:
lib/view_models/extensions/model_reader.rb

Overview

Model Reader extension. Allows to define model readers on view models, accessing attributes and methods on models

Defined Under Namespace

Classes: FilteredDelegationInstaller, Options

Instance Method Summary (collapse)

Instance Method Details

- (Object) model_reader(*attributes_and_options)

Define a reader for a model attribute. Acts as a filtered delegation to the model.

You may specify a :filter_through option that is either a symbol or an array of symbols. The return value from the model will be filtered through the functions (arity 1) and then passed back to the receiver.

Examples:

install different model readers

model_reader :foobar                                        # same as delegate :foobar, :to => :model
model_reader :foobar, :filter_through => :h                 # html escape foobar 
model_reader :foobar, :filter_through => [:textilize, :h]   # first textilize, then html escape

Parameters:

  • attributes_and_options (Symbol, Hash)

    The attributes and options for the model reader



27
28
29
30
# File 'lib/view_models/extensions/model_reader.rb', line 27

def model_reader *attributes_and_options
  options = Options.new *attributes_and_options
  FilteredDelegationInstaller.new(self, options).install
end