Class: ViewModels::Base

Inherits:
Object
  • Object
show all
Extended by:
Extensions::ModelReader
Includes:
AbstractController::Helpers
Defined in:
lib/view_models/base.rb

Overview

Base class from which all view_models inherit.

Examples:

Create Your first View Model

class ViewModels::MyViewModel < ViewModels::Base
  # your code
end

Since:

Class Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Extensions::ModelReader

model_reader

Constructor Details

- (Base) initialize(model, app_or_controller_or_view)

Create a view_model. To create a view_model, you need to have a model (to present) and a context. The context is usually a view, a controller, or an app, but doesn't need to be.

The @context = @controller is really only needed because some Rails helpers access @controller directly. It's really bad.

Examples:

Initialize a view model without the mapping helper

ViewModel::YourModel.new(@model, @context)

Parameters:

  • model (ActiveRecord)

    The model which the view model is based upon

  • app_or_controller_or_view (ActionView, ActionController, Rails::Application)

    The context of the view model

Since:

  • 1.0.0



38
39
40
41
# File 'lib/view_models/base.rb', line 38

def initialize model, app_or_controller_or_view
  @model                 = model
  @context = @controller = ContextExtractor.new(app_or_controller_or_view).extract
end

Class Attribute Details

+ (Object) path_store

The path store accessor, storing the view paths for the view model

Since:

  • 1.0.0



47
48
49
# File 'lib/view_models/base.rb', line 47

def path_store
  @path_store
end

Class Method Details

+ (Object) context_method(*methods) Also known as: controller_method

Delegates method calls to the context. In the view_model:

self.current_user

will call

context.current_user

Examples:

delegate one method to the context

context_method :current_user

delegate multiple methods to the context

context_method :current_user, :current_profile

Parameters:

  • methods (Symbol)

    A list of symbols representing methods to be delegated

Since:

  • 1.0.0



71
72
73
# File 'lib/view_models/base.rb', line 71

def context_method *methods
  delegate *methods << { :to => :context }
end

+ (Object) inherited(subclass)

Installs a path store, a specific store for template inheritance, to remember specific

path, name, format

tuples, pointing to a template path

so the view models don't have to always traverse the inheritance chain.

Parameters:

  • subclass (ViewModel)

    The subclass of the view model

Since:

  • 1.0.0



54
55
56
57
# File 'lib/view_models/base.rb', line 54

def inherited subclass
  ViewModels::PathStore.install_in subclass
  super
end

+ (Object) render(renderer, options)

Note:

Also caches [path, name, format] => template path.

Sets the view format and tries to render the given options.

Parameters:

  • renderer (ActionView)

    The view renderer

  • options (Hash)

    The options to pass to the view

Options Hash (options):

  • :locals (Hash)

    The locals to pass to the view

Since:

  • 1.0.0



105
106
107
108
109
110
111
# File 'lib/view_models/base.rb', line 105

def render renderer, options
  options.format! renderer
  path_store.cached options do
    options.file = template_path renderer, options
    renderer.render_with options
  end
end

Instance Method Details

- (Object) add_template_helper(helper_module)

TODO:

extract into module

Wrapper for add_template_helper in ActionController::Helpers, also includes given helper in the view_model

Parameters:

  • helper_module (Module)

    the helper to be added

Since:

  • 1.0.0



93
94
95
96
# File 'lib/view_models/base.rb', line 93

def add_template_helper helper_module
  include helper_module
  old_add_template_helper helper_module
end

- (Object) dom_id(*args)

Delegate dom id to the action controller record identifier.

Since:

  • 1.0.0



226
227
228
229
230
231
232
# File 'lib/view_models/base.rb', line 226

def dom_id *args
  if args.present?
    context.dom_id *args
  else
    ActionController::RecordIdentifier.dom_id model
  end
end

- (Object) old_add_template_helper

Since:

  • 1.0.0



85
# File 'lib/view_models/base.rb', line 85

alias old_add_template_helper add_template_helper

- (Object) render_as(name, options = {}) Also known as: render_the

Renders the given partial in the view_model's view root in the format given.

Example:

views/view_models/this/view_model/_partial.haml
views/view_models/this/view_model/_partial.text.erb

The following options are supported:

  • :format - Calling view_model.render_as('partial') will render the haml partial, calling view_model.render_as('partial', :format => :text) will render the text erb.

  • All other options are passed on to the render call. I.e. if you want to specify locals you can call view_model.render_as(:partial, :locals => { :name => :value })

  • If no format is given, it will render the default format, which is (currently) html.

Since:

  • 1.0.0



258
259
260
# File 'lib/view_models/base.rb', line 258

def render_as name, options = {}
  render_internal RenderOptions::Partial.new(name, options)
end

- (Object) render_template(name, options = {})

Renders the given template in the view_model's view root in the format given.

Example:

views/view_models/this/view_model/template.haml
views/view_models/this/view_model/template name.text.erb

The following options are supported:

  • :format - Calling view_model.render_template('template') will render the haml template, calling view_model.render_template('template', :format => :text) will render the text erb template.

  • All other options are passed on to the render call. I.e. if you want to specify locals you can call view_model.render_template(:template, :locals => { :name => :value })

  • If no format is given, it will render the default format, which is (currently) html.

Since:

  • 1.0.0



284
285
286
# File 'lib/view_models/base.rb', line 284

def render_template name, options = {}
  render_internal RenderOptions::Template.new(name, options)
end