class Oprah::Presenter
Attributes
@return [ActionView::Base] The view context
@return [ActionView::Base] The view context
Public Class Methods
Returns the shared presenter cache object.
@return [ActiveSupport::Cache::MemoryStore]
# File lib/oprah/presenter.rb, line 15 def cache @@cache end
Returns the default view context to use if no view context is explicitly passed to the presenter.
@return [ActionView::Context]
# File lib/oprah/presenter.rb, line 78 def default_view_context ActionController::Base.new.view_context end
Initializes a new Presenter
.
@param object [Object] The object to present @param view_context
[ActionView::Context] View context to assign
# File lib/oprah/presenter.rb, line 100 def initialize(object, view_context: self.class.default_view_context) __setobj__(object) @view_context = view_context end
Presents the given `object` with all it's matching presenters, following it's ancestors in reverse.
@param object [Object] The object to present @param view_context
[ActionView::Context] View context to assign @param only [Class] Class or Array of presenters to use @return [Presenter] Presented object
# File lib/oprah/presenter.rb, line 26 def present(object, view_context: default_view_context, only: nil) presenters = presenter_classes_for(object) presenters &= Array(only) if only presenters.inject(object) do |memo, presenter| presenter.new(memo, view_context: view_context) end end
Presents the given `objects` with all their matching presenters. The behaviour and parameters are identical to `.present`'s.
@param objects [Enumerable] The objects to present @see .present
# File lib/oprah/presenter.rb, line 40 def present_many(objects, **kwargs) objects.map { |object| present(object, **kwargs) } end
Automatically wrap the objects returned by the given one-to-many or many-to-many `association` method in presenters.
Presenters will re-use the parent's assigned view context.
@param association [Symbol] Name of the association @return [Symbol] Name of the association
# File lib/oprah/presenter.rb, line 66 def presents_many(association) define_method association do present_many(__getobj__.__send__(association)) end association end
Automatically wrap the objects returned by the given one-to-one `association` method in presenters.
Presenters will re-use the parent's assigned view context.
@param association [Symbol] Name of the association @return [Symbol] Name of the association
# File lib/oprah/presenter.rb, line 51 def presents_one(association) define_method association do present(__getobj__.__send__(association)) end association end
Private Class Methods
@since 0.2.0
# File lib/oprah/presenter.rb, line 85 def presenter_classes_for(object) klass = object.class @@cache.fetch klass.name do klass.ancestors.map do |ancestor| (ancestor.name + "Presenter").safe_constantize if ancestor.name end.compact.reverse end end
Public Instance Methods
Returns `true` if the presented object or the presenter is an instance of the given `class`.
@param klass [Class] @return [Boolean] result
# File lib/oprah/presenter.rb, line 145 def instance_of?(klass) super || __getobj__.instance_of?(klass) end
Returns true if `klass` is the class of the presented object or the presenter, or if `#class` is one of the superclasses of the presented object, the presenter or modules included in the presented object or the presenter.
@param other [Module] @return [Boolean] result
# File lib/oprah/presenter.rb, line 134 def kind_of?(other) super || __getobj__.kind_of?(other) end
Presents a single object.
Will re-use the presenter's assigned view context if no `view_context`: parameter is given.
@see .present
# File lib/oprah/presenter.rb, line 111 def present(*args, **kwargs, &block) kwargs = { view_context: view_context }.merge(kwargs) self.class.present(*args, **kwargs, &block) end
Presents a collection of objects.
Will re-use the presenter's assigned view context if no `view_context`: parameter is given.
@see .present_many
# File lib/oprah/presenter.rb, line 122 def present_many(*args, **kwargs, &block) kwargs = { view_context: view_context }.merge(kwargs) self.class.present_many(*args, **kwargs, &block) end