Module: Blush::HasPresenter
- Defined in:
- lib/blush/has_presenter.rb
Overview
Tell a model that it has a presenter
This is included in all models
Use with defaults:
has_presenter
Use with custom accessor:
has_presenter :accessor_name
Use with options:
has_presenter { class: 'ThingPresenter', helper_name: :foo }
Instance Method Summary collapse
Instance Method Details
#has_presenter(accessor_name = {}, options = nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/blush/has_presenter.rb', line 17 def has_presenter(accessor_name = {}, = nil) class_attribute :blush_options , accessor_name = accessor_name, unless self. = self.[:accessor_name] = accessor_name || Blush.config.accessor_name # defines the accessor define_method(self.[:accessor_name]) do accessor_name = "@#{self.[:accessor_name]}" presenter_class = (self.[:class] || "#{self.model_name}Presenter").constantize instance_variable_get(accessor_name) || instance_variable_set(accessor_name, presenter_class.new(self)) end # defines the helper define_method(self.[:helper_name] || Blush.config.helper_name) do |method, *args, &block| presenter.try(method, *args, &block) || send(method, *args, &block) end end |