Class: ViewModels::Base
- Inherits:
-
Object
- Object
- ViewModels::Base
- Extended by:
- Extensions::ModelReader
- Includes:
- AbstractController::Helpers
- Defined in:
- lib/view_models/base.rb
Overview
Base class from which all view_models inherit.
Class Attribute Summary (collapse)
-
+ (Object) path_store
The path store accessor, storing the view paths for the view model.
Class Method Summary (collapse)
-
+ (Object) context_method(*methods)
(also: controller_method)
Delegates method calls to the context.
-
+ (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.
-
+ (Object) render(renderer, options)
Sets the view format and tries to render the given options.
Instance Method Summary (collapse)
-
- (Object) add_template_helper(helper_module)
Wrapper for add_template_helper in ActionController::Helpers, also includes given helper in the view_model.
-
- (Object) dom_id(*args)
Delegate dom id to the action controller record identifier.
-
- (Base) initialize(model, app_or_controller_or_view)
constructor
Create a view_model.
- - (Object) old_add_template_helper
-
- (Object) render_as(name, options = {})
(also: #render_the)
Renders the given partial in the view_model's view root in the format given.
-
- (Object) render_template(name, options = {})
Renders the given template in the view_model's view root in the format given.
Methods included from Extensions::ModelReader
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.
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
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
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.
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)
Also caches [path, name, format] => template path.
Sets the view format and tries to render the given options.
105 106 107 108 109 110 111 |
# File 'lib/view_models/base.rb', line 105 def render renderer, .format! renderer path_store.cached do .file = template_path renderer, renderer.render_with end end |
Instance Method Details
- (Object) add_template_helper(helper_module)
extract into module
Wrapper for add_template_helper in ActionController::Helpers, also includes given helper in the view_model
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.
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
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.
258 259 260 |
# File 'lib/view_models/base.rb', line 258 def render_as name, = {} render_internal RenderOptions::Partial.new(name, ) 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.
284 285 286 |
# File 'lib/view_models/base.rb', line 284 def render_template name, = {} render_internal RenderOptions::Template.new(name, ) end |