Module: ModelPresenter::HasMany
- Defined in:
- lib/model_presenter/has_many.rb
Instance Method Summary collapse
-
#has_many(relation, options) ⇒ Object
The DSL adds an instance method
relationto represnet ahas_manyrelationship.
Instance Method Details
#has_many(relation, options) ⇒ Object
The DSL adds an instance method relation to represnet a has_many relationship
class User
include ModelPresenter::Base
has_many :groups, presenter_class: Presenters::Group
end
The macro will generates a groups methods, which will return an array. Each element of the array is an instance of Presenters::Group whose model is one of the group models that the user has.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/model_presenter/has_many.rb', line 17 def has_many(relation, ) self.send :define_method, relation do instance_variable_name = "@#{relation}" instance_variable = instance_variable_get("@#{relation}") return instance_variable if instance_variable instance_variable_set( instance_variable_name, model.send(relation).map do |relation_model| [:presenter_class].new relation_model end ) end end |