tint 0.0.4 → 0.0.5.pre1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tint/decorated_association.rb +16 -0
- data/lib/tint/decorator.rb +76 -56
- data/lib/tint/version.rb +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd0ed750b6e6c9ad99e7bccd4bd2581bed714ff4
|
4
|
+
data.tar.gz: f5f07e5ddb68ce731449dcba1b1d608768912cb8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 030e7bb630e095ad2432da014b3d74e66f11b3e648cb2df9145016cd479c9612476cf65030c9b95bd4c0eb67ce021b8af6ecb500b15e4d912a4b25a8c98b52ea
|
7
|
+
data.tar.gz: 98908fd1d9909ffcd72e618cb91770ec4f8969dd94d02cb1263b3d975c2bbdf54d40b92b5a2e6507d292b5aeecd9b2462307e0708ae0609bc2f673904c1b1aab
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Tint
|
2
|
+
class DecoratedAssociation < Draper::DecoratedAssociation
|
3
|
+
def decorate
|
4
|
+
association_chain = @association
|
5
|
+
association_chain = Array.wrap(association_chain) unless association_chain.kind_of?(Array)
|
6
|
+
|
7
|
+
associated = association_chain.inject(owner.object) do |memo, method_name|
|
8
|
+
memo.send(method_name)
|
9
|
+
end
|
10
|
+
|
11
|
+
associated = associated.sent(scope) if scope
|
12
|
+
|
13
|
+
@decorated = factory.decorate(associated, context_args: owner.context)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/tint/decorator.rb
CHANGED
@@ -2,6 +2,8 @@ require 'draper'
|
|
2
2
|
require 'deep_merge/rails_compat'
|
3
3
|
require_relative 'json_conversion.rb'
|
4
4
|
|
5
|
+
require 'tint/decorated_association'
|
6
|
+
|
5
7
|
module Tint
|
6
8
|
class Decorator < Draper::Decorator
|
7
9
|
include JsonConversion
|
@@ -20,23 +22,6 @@ module Tint
|
|
20
22
|
object.persisted?
|
21
23
|
end
|
22
24
|
|
23
|
-
def decorate_as_association(association_name, association, options = {})
|
24
|
-
options.assert_valid_keys(:with)
|
25
|
-
|
26
|
-
association_decorator = options[:with]
|
27
|
-
|
28
|
-
association_context = options.except(:with).merge(context: context.
|
29
|
-
merge(parent_decorator: self, parent_association: association_name.to_sym))
|
30
|
-
|
31
|
-
self.class.eager_load(association_name => {})
|
32
|
-
|
33
|
-
if association.respond_to?(:each)
|
34
|
-
association_decorator.decorate_collection(association, association_context)
|
35
|
-
else
|
36
|
-
association_decorator.decorate(association, association_context)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
25
|
class << self
|
41
26
|
attr_accessor :_attributes, :parent_decorator, :parent_association
|
42
27
|
|
@@ -69,51 +54,24 @@ module Tint
|
|
69
54
|
end
|
70
55
|
|
71
56
|
def eager_load(*schema)
|
72
|
-
|
73
|
-
schema.inject({}) do |memo, schema_item|
|
74
|
-
if schema_item.kind_of?(Hash)
|
75
|
-
memo = memo.merge(schema_item)
|
76
|
-
else
|
77
|
-
memo[schema_item] = {}
|
78
|
-
end
|
79
|
-
|
80
|
-
memo
|
81
|
-
end
|
82
|
-
|
83
|
-
self.eager_loads = self.eager_loads.deeper_merge(new_eager_loads)
|
57
|
+
self.eager_loads = self.eager_loads.deeper_merge(expand_schema(schema))
|
84
58
|
end
|
85
59
|
|
86
|
-
def decorates_association(
|
87
|
-
options
|
60
|
+
def decorates_association(*args)
|
61
|
+
options = args.extract_options!
|
62
|
+
association_chain = args
|
88
63
|
|
89
|
-
|
64
|
+
association_tail = association_chain.last
|
90
65
|
|
91
|
-
options
|
66
|
+
options[:with] ||= (association_tail.to_s.camelize.singularize + 'Decorator').constantize
|
92
67
|
|
93
|
-
|
94
|
-
context_with_association = context.merge({
|
95
|
-
parent_decorator: self,
|
96
|
-
parent_association: association_name
|
97
|
-
})
|
68
|
+
association_alias = options.delete(:as) || association_tail
|
98
69
|
|
99
|
-
|
100
|
-
self,
|
101
|
-
association_name,
|
102
|
-
options.merge(context: context_with_association)
|
103
|
-
)
|
104
|
-
|
105
|
-
decorated_associations[association_alias].call
|
106
|
-
end
|
107
|
-
|
108
|
-
attributes(association_alias)
|
70
|
+
options.assert_valid_keys(:with, :scope, :context)
|
109
71
|
|
110
|
-
|
72
|
+
define_association_method(association_alias, association_chain, options)
|
111
73
|
|
112
|
-
|
113
|
-
eager_load({ association_name => association_eager_loads})
|
114
|
-
else
|
115
|
-
eager_load(association_name)
|
116
|
-
end
|
74
|
+
eager_load_association(association_chain, options)
|
117
75
|
end
|
118
76
|
|
119
77
|
def decorates_associations(*arguments)
|
@@ -147,7 +105,7 @@ module Tint
|
|
147
105
|
unless already_eager_loaded_associations?(object)
|
148
106
|
object =
|
149
107
|
if responds_to_methods?(object_class, :includes, :find) && eager_loads.present?
|
150
|
-
object_class.includes(
|
108
|
+
object_class.includes(eager_loads).find(object.id)
|
151
109
|
else
|
152
110
|
object
|
153
111
|
end
|
@@ -157,7 +115,15 @@ module Tint
|
|
157
115
|
end
|
158
116
|
|
159
117
|
def parent_eager_loads_include_own?(context = {})
|
160
|
-
|
118
|
+
if context && context[:parent_decorator]
|
119
|
+
if (parent_eager_loads = context[:parent_decorator].class.eager_loads)
|
120
|
+
context[:parent_association].inject(parent_eager_loads) do |memo, chain_link|
|
121
|
+
memo[chain_link] if memo
|
122
|
+
end
|
123
|
+
end
|
124
|
+
else
|
125
|
+
false
|
126
|
+
end
|
161
127
|
end
|
162
128
|
|
163
129
|
def already_eager_loaded_associations?(object)
|
@@ -201,6 +167,60 @@ module Tint
|
|
201
167
|
end
|
202
168
|
end
|
203
169
|
end
|
170
|
+
|
171
|
+
private
|
172
|
+
|
173
|
+
def expand_schema(schema)
|
174
|
+
if schema.kind_of?(Hash)
|
175
|
+
schema.inject({}) do |memo, (schema_key, schema_value)|
|
176
|
+
memo[schema_key] = expand_schema(schema_value)
|
177
|
+
memo
|
178
|
+
end
|
179
|
+
elsif schema.kind_of?(Array)
|
180
|
+
schema.inject({}) do |memo, schema_item|
|
181
|
+
memo.merge(expand_schema(schema_item))
|
182
|
+
end
|
183
|
+
else
|
184
|
+
{ schema => {} }
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def define_association_method(association_alias, association_chain, options)
|
189
|
+
define_method(association_alias) do
|
190
|
+
context_with_association = context.merge({
|
191
|
+
parent_decorator: self,
|
192
|
+
parent_association: association_chain
|
193
|
+
})
|
194
|
+
|
195
|
+
decorated_associations[association_alias] ||= Tint::DecoratedAssociation.new(
|
196
|
+
self,
|
197
|
+
association_chain,
|
198
|
+
options.merge(context: context_with_association)
|
199
|
+
)
|
200
|
+
|
201
|
+
decorated_associations[association_alias].call
|
202
|
+
end
|
203
|
+
|
204
|
+
attributes(association_alias)
|
205
|
+
end
|
206
|
+
|
207
|
+
def eager_load_association(association_chain, options)
|
208
|
+
association_eager_loads = options[:with].eager_loads
|
209
|
+
schema = association_schema(association_chain, association_eager_loads)
|
210
|
+
|
211
|
+
eager_load(schema)
|
212
|
+
end
|
213
|
+
|
214
|
+
def association_schema(association_chain, eager_loads = {})
|
215
|
+
association_chain.reverse.reduce({}) do |memo, chain_link|
|
216
|
+
|
217
|
+
if chain_link == association_chain.last
|
218
|
+
{ chain_link => eager_loads }
|
219
|
+
else
|
220
|
+
{ chain_link => memo }
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
204
224
|
end
|
205
225
|
|
206
226
|
end
|
data/lib/tint/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tint
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5.pre1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aleck Greenham
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: draper
|
@@ -95,6 +95,7 @@ files:
|
|
95
95
|
- README.md
|
96
96
|
- Rakefile
|
97
97
|
- lib/tint.rb
|
98
|
+
- lib/tint/decorated_association.rb
|
98
99
|
- lib/tint/decorator.rb
|
99
100
|
- lib/tint/json_conversion.rb
|
100
101
|
- lib/tint/version.rb
|
@@ -117,9 +118,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
117
118
|
version: '0'
|
118
119
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
120
|
requirements:
|
120
|
-
- - "
|
121
|
+
- - ">"
|
121
122
|
- !ruby/object:Gem::Version
|
122
|
-
version:
|
123
|
+
version: 1.3.1
|
123
124
|
requirements: []
|
124
125
|
rubyforge_project:
|
125
126
|
rubygems_version: 2.2.2
|