active_delegate 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +10 -11
- data/lib/active_delegate/attributes.rb +43 -11
- data/lib/active_delegate/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db4ce7e906bb62440ecead0e66d21d611b9354ac
|
4
|
+
data.tar.gz: e6e9e1b63faaf10d0f7522f7c19cc22563976554
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 11d848253fb51ced3c37e97806dcd15ef71634a501ae6003a65e18a074347406d1b1c12241904b74d58b5ebc37a386d97baf119250fe83fcd0d16ad26686265d
|
7
|
+
data.tar.gz: 51202fe31b97ac2fa6bf6dfa0966681d9b75fc87ef3845b9959674f59403c6f9d7dce06a62682175c14fee417f7cdfdc000c51798557f53a8c1001925dd55df9
|
data/Gemfile.lock
CHANGED
@@ -1,29 +1,28 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
active_delegate (0.1.
|
4
|
+
active_delegate (0.1.1)
|
5
5
|
activerecord (~> 5.0)
|
6
6
|
i18n (~> 0.8)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
activemodel (5.1.
|
12
|
-
activesupport (= 5.1.
|
13
|
-
activerecord (5.1.
|
14
|
-
activemodel (= 5.1.
|
15
|
-
activesupport (= 5.1.
|
11
|
+
activemodel (5.1.1)
|
12
|
+
activesupport (= 5.1.1)
|
13
|
+
activerecord (5.1.1)
|
14
|
+
activemodel (= 5.1.1)
|
15
|
+
activesupport (= 5.1.1)
|
16
16
|
arel (~> 8.0)
|
17
|
-
activesupport (5.1.
|
17
|
+
activesupport (5.1.1)
|
18
18
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
19
19
|
i18n (~> 0.7)
|
20
20
|
minitest (~> 5.1)
|
21
21
|
tzinfo (~> 1.1)
|
22
22
|
arel (8.0.0)
|
23
23
|
concurrent-ruby (1.0.5)
|
24
|
-
i18n (0.
|
25
|
-
|
26
|
-
minitest (5.10.3)
|
24
|
+
i18n (0.8.4)
|
25
|
+
minitest (5.10.2)
|
27
26
|
rake (10.5.0)
|
28
27
|
thread_safe (0.3.6)
|
29
28
|
tzinfo (1.2.3)
|
@@ -39,4 +38,4 @@ DEPENDENCIES
|
|
39
38
|
rake (~> 10.0)
|
40
39
|
|
41
40
|
BUNDLED WITH
|
42
|
-
1.
|
41
|
+
1.14.6
|
@@ -18,7 +18,11 @@ module ActiveDelegate
|
|
18
18
|
|
19
19
|
# Get default options
|
20
20
|
def default_options
|
21
|
-
{
|
21
|
+
{
|
22
|
+
except: [], only: [], allow_nil: false, to: [],
|
23
|
+
prefix: nil, localized: false, finder: false,
|
24
|
+
scope: false, cast: false
|
25
|
+
}
|
22
26
|
end
|
23
27
|
|
24
28
|
# Get association reflection
|
@@ -117,12 +121,22 @@ module ActiveDelegate
|
|
117
121
|
|
118
122
|
# Get attribute default
|
119
123
|
def attribute_default(attribute)
|
120
|
-
@options
|
124
|
+
@options.fetch :default, association_class.column_defaults["#{attribute}"]
|
121
125
|
end
|
122
126
|
|
123
127
|
# Get attribute cast type
|
124
128
|
def attribute_cast_type(attribute)
|
125
|
-
@options
|
129
|
+
@options.fetch :cast_type, association_class.type_for_attribute("#{attribute}")
|
130
|
+
end
|
131
|
+
|
132
|
+
# Check if attribute types are not the same
|
133
|
+
def cast_types_mismatch?(attribute)
|
134
|
+
attribute_cast_type(attribute) != association_class.type_for_attribute("#{attribute}")
|
135
|
+
end
|
136
|
+
|
137
|
+
# Check if attribute needs type cast
|
138
|
+
def needs_type_cast?(attribute)
|
139
|
+
@options[:cast] != false && cast_types_mismatch?(attribute)
|
126
140
|
end
|
127
141
|
|
128
142
|
# Check if should define attribute finders
|
@@ -163,6 +177,7 @@ module ActiveDelegate
|
|
163
177
|
attr_name = unprefix_attribute(attrib)
|
164
178
|
|
165
179
|
define_attribute_default_value(attrib, attr_name)
|
180
|
+
define_attribute_type_cast(attrib, attr_name)
|
166
181
|
define_attribute_and_alias(attrib, attr_name)
|
167
182
|
define_attribute_finders_and_scopes(attrib, attr_name)
|
168
183
|
end
|
@@ -188,6 +203,29 @@ module ActiveDelegate
|
|
188
203
|
end
|
189
204
|
end
|
190
205
|
|
206
|
+
# Define attribute type casting
|
207
|
+
def define_attribute_type_cast(attrib, attr_name)
|
208
|
+
attr_assoc = @options[:to]
|
209
|
+
attr_cattr = :"_attribute_#{attrib}_default"
|
210
|
+
|
211
|
+
if needs_type_cast?(attr_name)
|
212
|
+
@model.class_eval do
|
213
|
+
class_eval <<-EOM, __FILE__, __LINE__ + 1
|
214
|
+
def #{attrib}
|
215
|
+
assoc_value = send(:#{attr_assoc}).try(:#{attr_name})
|
216
|
+
self.class.type_for_attribute('#{attrib}').cast(assoc_value) ||
|
217
|
+
self.class.try(:#{attr_cattr})
|
218
|
+
end
|
219
|
+
|
220
|
+
def #{attrib}=(value)
|
221
|
+
assoc_value = self.class.type_for_attribute('#{attrib}').cast(value)
|
222
|
+
send(:#{attr_assoc}).send(:#{attr_name}=, assoc_value)
|
223
|
+
end
|
224
|
+
EOM
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
191
229
|
# Define delegated attribute alias
|
192
230
|
def define_attribute_and_alias(attrib, attr_name)
|
193
231
|
cast_type = attribute_cast_type(attr_name)
|
@@ -213,15 +251,11 @@ module ActiveDelegate
|
|
213
251
|
|
214
252
|
# Define attribute finder methods
|
215
253
|
def define_attribute_finder_methods(attrib, attr_name, attr_assoc, attr_table)
|
216
|
-
|
217
|
-
|
218
|
-
@model.send(:define_singleton_method, attr_method) do |value|
|
219
|
-
value = type_caster.type_cast_for_database(attr_name, value)
|
254
|
+
@model.send(:define_singleton_method, :"find_by_#{attrib}") do |value|
|
220
255
|
joins(attr_assoc).find_by(attr_table => { attr_name => value })
|
221
256
|
end
|
222
257
|
|
223
|
-
@model.send(:define_singleton_method, :"#{
|
224
|
-
value = type_caster.type_cast_for_database(attr_name, value)
|
258
|
+
@model.send(:define_singleton_method, :"find_by_#{attrib}!") do |value|
|
225
259
|
joins(attr_assoc).find_by!(attr_table => { attr_name => value })
|
226
260
|
end
|
227
261
|
end
|
@@ -229,12 +263,10 @@ module ActiveDelegate
|
|
229
263
|
# Define attribute scope methods
|
230
264
|
def define_attribute_scope_methods(attrib, attr_name, attr_assoc, attr_table)
|
231
265
|
@model.scope :"with_#{attrib}", -> (*names) do
|
232
|
-
names = names.map { |n| type_caster.type_cast_for_database(attr_name, n) }
|
233
266
|
joins(attr_assoc).where(attr_table => { attr_name => names })
|
234
267
|
end
|
235
268
|
|
236
269
|
@model.scope :"without_#{attrib}", -> (*names) do
|
237
|
-
names = names.map { |n| type_caster.type_cast_for_database(attr_name, n) }
|
238
270
|
joins(attr_assoc).where.not(attr_table => { attr_name => names })
|
239
271
|
end
|
240
272
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_delegate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonian Guveli
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-11
|
11
|
+
date: 2017-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|