Class: ResourceDisplayHelper
- Inherits:
-
Object
- Object
- ResourceDisplayHelper
- Defined in:
- lib/resource_display_helper.rb
Overview
This helper class is meant to get yielded to a block of code in a show template. We hold the model object whose attributes are to be displayed, and handle substituting values into a partial template, abstracting HTML boilerplate from the resource’s show template itself.
We are very similar to the form that gets yielded by a form_for / simple_form_for call except that we are making a show template easier to build, instead of a form.
Instance Attribute Summary collapse
-
#model_object ⇒ Object
Attributes.
-
#resource_helper ⇒ Object
Returns the value of attribute resource_helper.
Instance Method Summary collapse
-
#attribute(name, options = {}) ⇒ Object
This is the main method of this class that callers will use.
-
#initialize(model_object, resource_helper) ⇒ ResourceDisplayHelper
constructor
Provide the model object we’ll be invoking attribute method calls on, as well as the resource helper that we can ask to render a view partial for us.
Constructor Details
#initialize(model_object, resource_helper) ⇒ ResourceDisplayHelper
Provide the model object we’ll be invoking attribute method calls on, as well as the resource helper that we can ask to render a view partial for us.
20 21 22 23 |
# File 'lib/resource_display_helper.rb', line 20 def initialize(model_object, resource_helper) @model_object = model_object @resource_helper = resource_helper end |
Instance Attribute Details
#model_object ⇒ Object
Attributes
12 13 14 |
# File 'lib/resource_display_helper.rb', line 12 def model_object @model_object end |
#resource_helper ⇒ Object
Returns the value of attribute resource_helper.
13 14 15 |
# File 'lib/resource_display_helper.rb', line 13 def resource_helper @resource_helper end |
Instance Method Details
#attribute(name, options = {}) ⇒ Object
This is the main method of this class that callers will use. They provide the attribute whose label and value is to be displayed. Optional overrides for the label and value are also allowed.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/resource_display_helper.rb', line 31 def attribute(name, ={}) label = [:label] || name.to_s.humanize.titleize value = [:value] || model_object.send(name) link_path = [:link_path] if value && value.kind_of?(String) && [:truncate] value = value.truncate([:truncate], :omission => "...", :separator => ' ') end # We don't have access to the view renderer, but our provider resource helper does. # Use it to render the partial that holds the boilerplate template for how we like # to show a single attribute: resource_helper.render partial: 'mock/common/show_attribute', locals: {label: label, value: value, link_path: link_path} end |