Class: Prepro::Presenter
- Inherits:
-
Object
- Object
- Prepro::Presenter
- Defined in:
- lib/prepro/presenter.rb
Defined Under Namespace
Modules: DecoratorMixin
Class Method Summary collapse
- .enforce_permissions(has_permission) ⇒ Object
-
.load_model_instance(id_hash_model) ⇒ Object
Returns a model_instance, based on given id_hash_model.
-
.model_class ⇒ Object
Override this in your concrete presenters with the class presented by self.
-
.new(id_model_hash_collection, actor, view_context, options = {}) ⇒ Object
(also: new_original)
Prepares collection of model instances or a single model instance for presentation @param[Integer, String(number), Hash, Model, Array<Model>] id_model_hash_collection id of model, attributes for new model, existing model or collection of models to present.
- .present_collection(model_instances, actor, view_context, options = {}) ⇒ Object
-
.present_single(id_hash_model, actor, view_context, options = {}) ⇒ Object
Prepares a model instance for presentation @param[Integer, String(number), Model] id_hash_model id of model, attributes for model, or model to present @param[User, AnonymousUser] actor the actor who will view the model @param view_context An instance of a view class.
Class Method Details
.enforce_permissions(has_permission) ⇒ Object
93 94 95 |
# File 'lib/prepro/presenter.rb', line 93 def self.() raise Prepro::AuthorizationError unless end |
.load_model_instance(id_hash_model) ⇒ Object
Returns a model_instance, based on given id_hash_model
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/prepro/presenter.rb', line 74 def self.load_model_instance(id_hash_model) case id_hash_model when Integer, /\A\d+/ model_class.find(id_hash_model) when Hash model_class.new(id_hash_model) else id_hash_model end end |
.model_class ⇒ Object
Override this in your concrete presenters with the class presented by self.
86 87 88 |
# File 'lib/prepro/presenter.rb', line 86 def self.model_class raise "Implement me in concrete presenter" end |
.new(id_model_hash_collection, actor, view_context, options = {}) ⇒ Object Also known as: new_original
Prepares collection of model instances or a single model instance for presentation @param[Integer, String(number), Hash, Model, Array<Model>] id_model_hash_collection id of model,
attributes for new model, existing model or collection of models to present.
@param[User, AnonymousUser] actor the actor who will view the model @param view_context An instance of a view class. The default view class is
ActionView::Base
@param[Hash, optional] options:
* :enforce_permissions - default true
@return[DecoratedModel, Array<DecoratedModel>] a model or collection thereof, decorated for
presentation
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/prepro/presenter.rb', line 17 def self.new(id_model_hash_collection, actor, view_context, = {}) = { :enforce_permissions => true }.merge() case id_model_hash_collection when Array, ActiveRecord::Relation present_collection(id_model_hash_collection, actor, view_context, ) else present_single(id_model_hash_collection, actor, view_context, ) end end |
.present_collection(model_instances, actor, view_context, options = {}) ⇒ Object
Prepares collection of model instances for presentation @param model_instances A collection of model instances @param[User, AnonymousUser] actor the actor who will view the model @param view_context An instance of a view class. The default view class is
ActionView::Base
@param[Hash, optional] options @return An array of models, each decorated for presentation
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/prepro/presenter.rb', line 44 def self.present_collection(model_instances, actor, view_context, = {}) presenter_attrs = OpenStruct.new( :actor => actor, :view_context => view_context, :options => ) if [:enforce_permissions] (model_class.listable_by?(actor)) end model_instances.each { |e| make_presentable!(e, presenter_attrs) } model_instances end |
.present_single(id_hash_model, actor, view_context, options = {}) ⇒ Object
Prepares a model instance for presentation @param[Integer, String(number), Model] id_hash_model id of model, attributes for model, or model
to present
@param[User, AnonymousUser] actor the actor who will view the model @param view_context An instance of a view class. The default view class is
ActionView::Base
@param[Hash, optional] options @return a model, decorated for presentation
63 64 65 66 67 68 69 70 71 |
# File 'lib/prepro/presenter.rb', line 63 def self.present_single(id_hash_model, actor, view_context, = {}) presenter_attrs = OpenStruct.new(:actor => actor, :view_context => view_context, :options => ) model_instance = load_model_instance(id_hash_model) if [:enforce_permissions] (model_instance.viewable_by?(actor)) end make_presentable!(model_instance, presenter_attrs) model_instance end |