active_delegate 0.1.7 → 0.1.8
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 +75 -6
- 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: 59b7e693e22c66982f05158ef1df7ac3a1f5f1f2
|
4
|
+
data.tar.gz: 198ecbb8547fe818fea48ddf07eb1ca5fdb879ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 168cede4634ec480bab04354409d72f6306cceccfb27ad5c18e6a51ea686d3e308da13a81572540cb461ab4735e73c46124f4f10d217d094f1d1707edbd3b7ff
|
7
|
+
data.tar.gz: e71530500911af1ac072a82c4e0d29feb0e03a76ef0f23dfcd0b5ac9ec6a7ad79e1bd5286da0c29e41b4e4b9eeb6168b32d7bd07fb4e142fbe5aa85e81b20e66
|
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,7 @@ module ActiveDelegate
|
|
18
18
|
|
19
19
|
# Get default options
|
20
20
|
def default_options
|
21
|
-
{ except: [], only: [], allow_nil: false, to: [], prefix: nil, localized: false }
|
21
|
+
{ except: [], only: [], allow_nil: false, to: [], prefix: nil, localized: false, finder: false, scope: false }
|
22
22
|
end
|
23
23
|
|
24
24
|
# Get association reflection
|
@@ -135,14 +135,83 @@ module ActiveDelegate
|
|
135
135
|
undefined = attributes.reject { |a| a.in? existing }
|
136
136
|
|
137
137
|
undefined.each do |attrib|
|
138
|
-
attr_name
|
139
|
-
cast_type
|
138
|
+
attr_name = attrib.to_s.sub("#{attribute_prefix}_", '')
|
139
|
+
cast_type = @options[:cast_type] || association_class.attribute_types["#{attr_name}"]
|
140
|
+
attr_alias = @options[:alias]
|
141
|
+
attr_default = @options[:default] || association_class.column_defaults["#{attr_name}"]
|
142
|
+
attr_finder = @options[:finder] || Array(@options[:finder]).include?(attr_name)
|
143
|
+
attr_scope = @options[:scope] || Array(@options[:scope]).include?(attr_name)
|
140
144
|
|
141
145
|
@model.attribute(attrib, cast_type)
|
142
146
|
|
143
|
-
|
144
|
-
|
145
|
-
|
147
|
+
define_attribute_default_value(attrib, attr_name, attr_default) unless attr_default.nil?
|
148
|
+
define_attribute_alias_method(attrib, attr_alias, cast_type) unless attr_alias.nil?
|
149
|
+
define_attribute_finder_methods(attr_alias || attrib, attr_name) if attr_finder == true
|
150
|
+
define_attribute_scope_methods(attr_alias || attrib, attr_name) if attr_scope == true
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
# Define delegated attribute default
|
155
|
+
def define_attribute_default_value(attrib, attr_name, attr_default)
|
156
|
+
attr_assoc = @options[:to]
|
157
|
+
attr_cattr = :"_attribute_#{attrib}_default"
|
158
|
+
|
159
|
+
@model.send(:define_singleton_method, attr_cattr) { attr_default }
|
160
|
+
|
161
|
+
@model.class_eval do
|
162
|
+
class_eval <<-EOM, __FILE__, __LINE__ + 1
|
163
|
+
def #{attrib}
|
164
|
+
send(:#{attr_assoc}).try(:#{attr_name}) || self.class.send(:#{attr_cattr})
|
165
|
+
end
|
166
|
+
EOM
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
# Define delegated attribute alias
|
171
|
+
def define_attribute_alias_method(attrib, attr_alias, cast_type)
|
172
|
+
@model.attribute(attr_alias, cast_type)
|
173
|
+
@model.alias_attribute(attr_alias, attrib)
|
174
|
+
end
|
175
|
+
|
176
|
+
# Define attribute finder methods
|
177
|
+
def define_attribute_finder_methods(attrib, attr_name)
|
178
|
+
attr_assoc = @options[:to]
|
179
|
+
attr_table = association_reflection.klass.table_name
|
180
|
+
attr_method = :"find_by_#{attrib}"
|
181
|
+
|
182
|
+
@model.send(:define_singleton_method, attr_method) do |value|
|
183
|
+
includes(attr_assoc).find_by(attr_table => { attr_name => value })
|
184
|
+
end
|
185
|
+
|
186
|
+
@model.send(:define_singleton_method, :"#{attr_method}!") do |value|
|
187
|
+
includes(attr_assoc).find_by!(attr_table => { attr_name => value })
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
# Define attribute scope methods
|
192
|
+
def define_attribute_scope_methods(attrib, attr_name)
|
193
|
+
attr_assoc = @options[:to]
|
194
|
+
attr_table = association_reflection.klass.table_name
|
195
|
+
attr_blank = { attr_table => { attr_name => nil } }
|
196
|
+
|
197
|
+
@model.scope :"with_#{attrib}", -> (*names) do
|
198
|
+
named = { attr_table => { attr_name => names } }
|
199
|
+
|
200
|
+
if names.empty?
|
201
|
+
includes(attr_assoc).where.not(attr_blank)
|
202
|
+
else
|
203
|
+
includes(attr_assoc).where(named)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
@model.scope :"without_#{attrib}", -> (*names) do
|
208
|
+
named = { attr_table => { attr_name => names } }
|
209
|
+
|
210
|
+
if names.empty?
|
211
|
+
includes(attr_assoc).where(attr_blank)
|
212
|
+
else
|
213
|
+
includes(attr_assoc).where(attr_blank)
|
214
|
+
.or(includes(attr_assoc).where.not(named))
|
146
215
|
end
|
147
216
|
end
|
148
217
|
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.1.
|
4
|
+
version: 0.1.8
|
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-11-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|