Class: Tint::Decorator
- Inherits:
-
Draper::Decorator
- Object
- Draper::Decorator
- Tint::Decorator
- Includes:
- JsonConversion
- Defined in:
- lib/tint.rb,
lib/tint/decorator.rb
Class Attribute Summary collapse
-
._attributes ⇒ Object
Returns the value of attribute _attributes.
-
.parent_association ⇒ Object
Returns the value of attribute parent_association.
-
.parent_decorator ⇒ Object
Returns the value of attribute parent_decorator.
Class Method Summary collapse
- .already_eager_loaded_associations?(object) ⇒ Boolean
- .attributes(*options) ⇒ Object
- .decorate(object, options = {}) ⇒ Object
- .decorate_collection(collection, options = {}) ⇒ Object
- .decorates_association(association_name, options = {}) ⇒ Object
- .decorates_associations(*arguments) ⇒ Object
- .eager_load(*schema) ⇒ Object
- .eager_loads ⇒ Object
- .eager_loads=(value) ⇒ Object
- .link_delegations_to_object(delegated_attrs) ⇒ Object
- .link_mappings_to_object(mapped_attrs) ⇒ Object
- .parent_eager_loads_include_own?(context = {}) ⇒ Boolean
- .responds_to_methods?(object, *methods) ⇒ Boolean
Instance Method Summary collapse
- #column_names ⇒ Object
- #decorate_as_association(association_name, association, options = {}) ⇒ Object
-
#initialize(object, options = {}) ⇒ Decorator
constructor
A new instance of Decorator.
- #persisted? ⇒ Boolean
Methods included from JsonConversion
Constructor Details
#initialize(object, options = {}) ⇒ Decorator
Returns a new instance of Decorator.
9 10 11 12 13 |
# File 'lib/tint/decorator.rb', line 9 def initialize(object, = {}) super(object, .except(:parent_decorator, :parent_association)) @context = @context.merge(.slice(:parent_decorator, :parent_association)) end |
Class Attribute Details
._attributes ⇒ Object
Returns the value of attribute _attributes.
41 42 43 |
# File 'lib/tint/decorator.rb', line 41 def _attributes @_attributes end |
.parent_association ⇒ Object
Returns the value of attribute parent_association.
41 42 43 |
# File 'lib/tint/decorator.rb', line 41 def parent_association @parent_association end |
.parent_decorator ⇒ Object
Returns the value of attribute parent_decorator.
41 42 43 |
# File 'lib/tint/decorator.rb', line 41 def parent_decorator @parent_decorator end |
Class Method Details
.already_eager_loaded_associations?(object) ⇒ Boolean
163 164 165 166 167 168 169 |
# File 'lib/tint/decorator.rb', line 163 def already_eager_loaded_associations?(object) if object.respond_to?(:association_cache) object.association_cache.any? else true end end |
.attributes(*options) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/tint/decorator.rb', line 57 def attributes(*) @_attributes ||= Set.new return unless && .any? mapped_attrs = . link_mappings_to_object(mapped_attrs) delegated_attrs = link_delegations_to_object(delegated_attrs) end |
.decorate(object, options = {}) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/tint/decorator.rb', line 144 def decorate(object, = {}) object_class = object.class unless already_eager_loaded_associations?(object) object = if responds_to_methods?(object_class, :includes, :find) && eager_loads.present? object_class.includes(*eager_loads).find(object.id) else object end end super(object, ) end |
.decorate_collection(collection, options = {}) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/tint/decorator.rb', line 129 def decorate_collection(collection, = {}) collection_with_eager_loads = if collection.respond_to?(:includes) && eager_loads.present? && !parent_eager_loads_include_own?([:context]) collection.includes(eager_loads) else collection end super(collection_with_eager_loads, ) end |
.decorates_association(association_name, options = {}) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/tint/decorator.rb', line 86 def decorates_association(association_name, = {}) [:with] ||= (association_name.to_s.camelize.singularize + 'Decorator').constantize association_alias = .delete(:as) || association_name .assert_valid_keys(:with, :scope, :context) define_method(association_alias) do context_with_association = context.merge({ parent_decorator: self, parent_association: association_name }) decorated_associations[association_alias] ||= Draper::DecoratedAssociation.new( self, association_name, .merge(context: context_with_association) ) decorated_associations[association_alias].call end attributes(association_alias) association_eager_loads = [:with].eager_loads if association_eager_loads.present? eager_load({ association_name => association_eager_loads}) else eager_load(association_name) end end |
.decorates_associations(*arguments) ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'lib/tint/decorator.rb', line 119 def decorates_associations(*arguments) = arguments. association_list = arguments association_list.each do |association_name| decorates_association(association_name, .dup) end end |
.eager_load(*schema) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/tint/decorator.rb', line 71 def eager_load(*schema) new_eager_loads = schema.inject({}) do |memo, schema_item| if schema_item.kind_of?(Hash) memo = memo.merge(schema_item) else memo[schema_item] = {} end memo end self.eager_loads = self.eager_loads.deeper_merge(new_eager_loads) end |
.eager_loads ⇒ Object
43 44 45 |
# File 'lib/tint/decorator.rb', line 43 def eager_loads {} end |
.eager_loads=(value) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/tint/decorator.rb', line 47 def eager_loads=(value) singleton_class.class_eval do remove_possible_method(:eager_loads) define_method(:eager_loads){ value } end end |
.link_delegations_to_object(delegated_attrs) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/tint/decorator.rb', line 179 def link_delegations_to_object(delegated_attrs) delegated_attrs.each do |delegate_method| @_attributes.add(delegate_method) unless method_defined?(delegate_method) define_method(delegate_method) do if object.respond_to?(delegate_method) object.send(delegate_method) end end end end end |
.link_mappings_to_object(mapped_attrs) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/tint/decorator.rb', line 193 def link_mappings_to_object(mapped_attrs) mapped_attrs.each do |decorator_attribute, object_method| @_attributes.add(decorator_attribute) define_method(decorator_attribute) do if object.respond_to?(object_method) object.send(object_method) end end end end |
.parent_eager_loads_include_own?(context = {}) ⇒ Boolean
159 160 161 |
# File 'lib/tint/decorator.rb', line 159 def parent_eager_loads_include_own?(context = {}) !!(context && context[:parent_decorator] && context[:parent_decorator].class.eager_loads[context[:parent_association]]) end |
.responds_to_methods?(object, *methods) ⇒ Boolean
171 172 173 174 175 176 177 |
# File 'lib/tint/decorator.rb', line 171 def responds_to_methods?(object, *methods) methods.each do |method_name| return false unless object.respond_to?(method_name) end true end |
Instance Method Details
#column_names ⇒ Object
15 16 17 |
# File 'lib/tint/decorator.rb', line 15 def column_names object.class.column_names end |
#decorate_as_association(association_name, association, options = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/tint/decorator.rb', line 23 def decorate_as_association(association_name, association, = {}) .assert_valid_keys(:with) association_decorator = [:with] association_context = .except(:with).merge(context: context. merge(parent_decorator: self, parent_association: association_name.to_sym)) self.class.eager_load(association_name => {}) if association.respond_to?(:each) association_decorator.decorate_collection(association, association_context) else association_decorator.decorate(association, association_context) end end |
#persisted? ⇒ Boolean
19 20 21 |
# File 'lib/tint/decorator.rb', line 19 def persisted? object.persisted? end |