Class: Prepro::Presenter

Inherits:
Object
  • Object
show all
Defined in:
lib/prepro/presenter.rb

Defined Under Namespace

Modules: DecoratorMixin

Class Method Summary collapse

Class Method Details

.enforce_permissions(has_permission) ⇒ Object

Raises an AuthorizationError if actor doesn’t have permission @param has_permission indicates whether actor has permission @return nil, or raises AuthorizationError



93
94
95
# File 'lib/prepro/presenter.rb', line 93

def self.enforce_permissions(has_permission)
  raise Prepro::AuthorizationError  unless has_permission
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_classObject

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, options = {})
  options = {
    :enforce_permissions => true
  }.merge(options)
  case id_model_hash_collection
  when Array, ActiveRecord::Relation
    present_collection(id_model_hash_collection, actor, view_context, options)
  else
    present_single(id_model_hash_collection, actor, view_context, options)
  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, options = {})
  presenter_attrs = OpenStruct.new(
    :actor => actor, :view_context => view_context, :options => options
  )
  if options[:enforce_permissions]
    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, options = {})
  presenter_attrs = OpenStruct.new(:actor => actor, :view_context => view_context, :options => options)
  model_instance = load_model_instance(id_hash_model)
  if options[:enforce_permissions]
    enforce_permissions(model_instance.viewable_by?(actor))
  end
  make_presentable!(model_instance, presenter_attrs)
  model_instance
end