Class: Lightrail::Wrapper::Model
- Inherits:
-
Object
- Object
- Lightrail::Wrapper::Model
- Defined in:
- lib/lightrail/wrapper/model.rb
Instance Attribute Summary collapse
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Class Method Summary collapse
- .around_association(collection, scope) ⇒ Object
-
.has_many(*associations) ⇒ Object
Declares that this object is associated to the given association with cardinality 1..*.
-
.has_one(*associations) ⇒ Object
Declares that this object is associated to the given association with cardinality 1..1.
- .inherited(base) ⇒ Object
-
.valid_includes(includes) ⇒ Object
Based on the declared associations and the given parameters, generate an includes clause that can be passed down to the relation object to avoid doing N+1 queries.
-
.wrapped_class ⇒ Object
Returns the original class wrapped by this wrapper.
Instance Method Summary collapse
-
#initialize(resource, scope) ⇒ Model
constructor
A new instance of Model.
-
#render(options = {}, result = Result.new) ⇒ Object
Gets the view and render the associated object.
-
#render_many(options = {}, result = Result.new) ⇒ Object
Gets the view and render the associated considering it is part of a collection.
- #view ⇒ Object
Constructor Details
#initialize(resource, scope) ⇒ Model
Returns a new instance of Model.
69 70 71 72 |
# File 'lib/lightrail/wrapper/model.rb', line 69 def initialize(resource, scope) @resource = resource @scope = scope end |
Instance Attribute Details
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
67 68 69 |
# File 'lib/lightrail/wrapper/model.rb', line 67 def resource @resource end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
67 68 69 |
# File 'lib/lightrail/wrapper/model.rb', line 67 def scope @scope end |
Class Method Details
.around_association(collection, scope) ⇒ Object
16 17 18 |
# File 'lib/lightrail/wrapper/model.rb', line 16 def around_association(collection, scope) yield end |
.has_many(*associations) ⇒ Object
Declares that this object is associated to the given association with cardinality 1..*.
30 31 32 33 34 |
# File 'lib/lightrail/wrapper/model.rb', line 30 def has_many(*associations) = associations. self.associations += associations.map { |a| Associations::HasManyConfig.new(a, ) } define_association_methods(associations) end |
.has_one(*associations) ⇒ Object
Declares that this object is associated to the given association with cardinality 1..1.
22 23 24 25 26 |
# File 'lib/lightrail/wrapper/model.rb', line 22 def has_one(*associations) = associations. self.associations += associations.map { |a| Associations::HasOneConfig.new(a, ) } define_association_methods(associations) end |
.inherited(base) ⇒ Object
10 11 12 13 14 |
# File 'lib/lightrail/wrapper/model.rb', line 10 def inherited(base) base.class_eval do alias_method wrapped_class.model_name.underscore, :resource end end |
.valid_includes(includes) ⇒ Object
Based on the declared associations and the given parameters, generate an includes clause that can be passed down to the relation object to avoid doing N+1 queries.
39 40 41 42 43 44 45 46 47 |
# File 'lib/lightrail/wrapper/model.rb', line 39 def valid_includes(includes) result = [] includes.each do |i| if association = associations.find { |a| a.includes == i } result << association.name end end result end |
.wrapped_class ⇒ Object
Returns the original class wrapped by this wrapper.
50 51 52 |
# File 'lib/lightrail/wrapper/model.rb', line 50 def wrapped_class @wrapped_class ||= name.sub(/Wrapper$/, "").constantize end |
Instance Method Details
#render(options = {}, result = Result.new) ⇒ Object
Gets the view and render the associated object.
79 80 81 82 83 |
# File 'lib/lightrail/wrapper/model.rb', line 79 def render(={}, result=Result.new) name = [:as] || wrapped_model_name.underscore result[name] = view = self.view _include_associations(result, view, [:include]) end |
#render_many(options = {}, result = Result.new) ⇒ Object
Gets the view and render the associated considering it is part of a collection.
86 87 88 89 90 91 |
# File 'lib/lightrail/wrapper/model.rb', line 86 def render_many(={}, result=Result.new) name = [:as] || wrapped_model_name.plural view = self.view array = (result[name] ||= []) << view _include_associations(result, view, [:include]) end |
#view ⇒ Object
74 75 76 |
# File 'lib/lightrail/wrapper/model.rb', line 74 def view raise NotImplementedError, "No view implemented for #{self}" end |