active_delegate 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4d157fcd42a41e6a9106addbf103a551543d9443
4
- data.tar.gz: 181bc82d7f83e017d77438bee17a3fd1ff4225f5
3
+ metadata.gz: 35e32e13c8c9845a3a17e841ddb1eaa2d5f9121a
4
+ data.tar.gz: 0ed1a3d1c8ca242f17e4380b71580a67b5ee37c9
5
5
  SHA512:
6
- metadata.gz: 19ab087839bd3766cb65f23caa4e9fc33d5bc7043b6488ccab65b67b2b5f222fd480a95f5c31474d1af6b06f8ddc473bbde2851d997530565816052cda366b16
7
- data.tar.gz: 4c89f450c882dc11f0b0200c5631c82dabfbde717c319e2e107ed9a4fc8f559673595c65967a7294b0803061a097675de148565d3eb0cee8c819941a3b43321a
6
+ metadata.gz: 5cab38ba25465170241da07c01579281f56f7ad72e423fad6c42a22b3f1769d13963533b67347835fc2a93d1eccab2bbdd1157bae6c59ea9585829136629864a
7
+ data.tar.gz: '0782f2d0cc0d8f9585921d3fcf4dbedd32ecfcc5768fd45a90019be9343850b4c67733e9d82e1a6bf3cf7fb66558196a00c679e9ff574e1c0ee6b78c569ea2c0'
@@ -101,6 +101,11 @@ module ActiveDelegate
101
101
  prefix.is_a?(TrueClass) ? @options[:to] : prefix
102
102
  end
103
103
 
104
+ # Get unprefixed attribute
105
+ def unprefix_attribute(attribute)
106
+ attribute.to_s.sub("#{attribute_prefix}_", '')
107
+ end
108
+
104
109
  # Get prefixed attributes
105
110
  def prefix_attributes(attributes)
106
111
  if @options[:prefix].present?
@@ -110,13 +115,33 @@ module ActiveDelegate
110
115
  end
111
116
  end
112
117
 
118
+ # Get attribute default
119
+ def attribute_default(attribute)
120
+ @options[:default] || association_class.column_defaults["#{attribute}"]
121
+ end
122
+
123
+ # Get attribute cast type
124
+ def attribute_cast_type(attribute)
125
+ @options[:cast_type] || association_class.attribute_types["#{attribute}"]
126
+ end
127
+
128
+ # Check if should define attribute finders
129
+ def define_finders?(attribute)
130
+ @options[:finder] || Array(@options[:finder]).include?(attribute)
131
+ end
132
+
133
+ # Check if should define attribute scopes
134
+ def define_scopes?(attribute)
135
+ @options[:scope] || Array(@options[:scope]).include?(attribute)
136
+ end
137
+
113
138
  # Save delagated attributes in model class
114
139
  def save_delegated_attributes
115
140
  dl_atable = association_reflection.klass.table_name
116
141
  dl_method = :"#{dl_atable}_attribute_names"
117
142
 
118
143
  delegated = prefix_attributes(delegatable_attributes)
119
- define_attribute_names_and_types(delegated)
144
+ define_attribute_defaults_and_methods(delegated)
120
145
 
121
146
  delegated = @model.try(dl_method).to_a.concat(delegated)
122
147
  @model.send(:define_singleton_method, dl_method) { delegated }
@@ -129,54 +154,65 @@ module ActiveDelegate
129
154
  end
130
155
  end
131
156
 
132
- # Define attribute names and types
133
- def define_attribute_names_and_types(attributes)
157
+ # Define attribute default values, methods and scopes
158
+ def define_attribute_defaults_and_methods(attributes)
134
159
  existing = @model.attribute_names.map(&:to_sym)
135
160
  undefined = attributes.reject { |a| a.in? existing }
136
161
 
137
162
  undefined.each do |attrib|
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)
144
-
145
- @model.attribute(attrib, cast_type)
146
-
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
163
+ attr_name = unprefix_attribute(attrib)
164
+
165
+ define_attribute_default_value(attrib, attr_name)
166
+ define_attribute_and_alias(attrib, attr_name)
167
+ define_attribute_finders_and_scopes(attrib, attr_name)
151
168
  end
152
169
  end
153
170
 
154
171
  # 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"
172
+ def define_attribute_default_value(attrib, attr_name)
173
+ attr_default = attribute_default(attr_name)
158
174
 
159
- @model.send(:define_singleton_method, attr_cattr) { attr_default }
175
+ unless attr_default.nil?
176
+ attr_assoc = @options[:to]
177
+ attr_cattr = :"_attribute_#{attrib}_default"
160
178
 
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
179
+ @model.send(:define_singleton_method, attr_cattr) { attr_default }
180
+
181
+ @model.class_eval do
182
+ class_eval <<-EOM, __FILE__, __LINE__ + 1
183
+ def #{attrib}
184
+ send(:#{attr_assoc}).try(:#{attr_name}) || self.class.send(:#{attr_cattr})
185
+ end
186
+ EOM
187
+ end
167
188
  end
168
189
  end
169
190
 
170
191
  # 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)
192
+ def define_attribute_and_alias(attrib, attr_name)
193
+ cast_type = attribute_cast_type(attr_name)
194
+ attr_alias = @options[:alias]
195
+
196
+ @model.attribute(attrib, cast_type)
197
+
198
+ unless attr_alias.nil?
199
+ @model.attribute(attr_alias, cast_type)
200
+ @model.alias_attribute(attr_alias, attrib)
201
+ end
202
+ end
203
+
204
+ # Define attribute finders and scopes
205
+ def define_attribute_finders_and_scopes(attrib, attr_name)
206
+ attr_assoc = @options[:to]
207
+ attr_table = association_reflection.klass.table_name
208
+ attr_args = [@options[:alias] || attrib, attr_name, attr_assoc, attr_table]
209
+
210
+ define_attribute_finder_methods(*attr_args) if define_finders?(attr_name)
211
+ define_attribute_scope_methods(*attr_args) if define_scopes?(attr_name)
174
212
  end
175
213
 
176
214
  # 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
215
+ def define_attribute_finder_methods(attrib, attr_name, attr_assoc, attr_table)
180
216
  attr_method = :"find_by_#{attrib}"
181
217
 
182
218
  @model.send(:define_singleton_method, attr_method) do |value|
@@ -191,32 +227,15 @@ module ActiveDelegate
191
227
  end
192
228
 
193
229
  # Define attribute scope methods
194
- def define_attribute_scope_methods(attrib, attr_name)
195
- attr_assoc = @options[:to]
196
- attr_table = association_reflection.klass.table_name
197
- attr_blank = { attr_table => { attr_name => nil } }
198
-
230
+ def define_attribute_scope_methods(attrib, attr_name, attr_assoc, attr_table)
199
231
  @model.scope :"with_#{attrib}", -> (*names) do
200
- if names.empty?
201
- left_outer_joins(attr_assoc).where.not(attr_blank)
202
- else
203
- names = names.map { |n| type_caster.type_cast_for_database(attr_name, n) }
204
- named = { attr_table => { attr_name => names } }
205
-
206
- left_outer_joins(attr_assoc).where(named)
207
- end
232
+ names = names.map { |n| type_caster.type_cast_for_database(attr_name, n) }
233
+ joins(attr_assoc).where(attr_table => { attr_name => names })
208
234
  end
209
235
 
210
236
  @model.scope :"without_#{attrib}", -> (*names) do
211
- if names.empty?
212
- left_outer_joins(attr_assoc).where(attr_blank)
213
- else
214
- names = names.map { |n| type_caster.type_cast_for_database(attr_name, n) }
215
- named = { attr_table => { attr_name => names } }
216
-
217
- left_outer_joins(attr_assoc).where(attr_blank)
218
- .or(left_outer_joins(attr_assoc).where.not(named))
219
- end
237
+ names = names.map { |n| type_caster.type_cast_for_database(attr_name, n) }
238
+ joins(attr_assoc).where.not(attr_table => { attr_name => names })
220
239
  end
221
240
  end
222
241
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveDelegate
2
- VERSION = '0.1.9'
2
+ VERSION = '0.2.0'
3
3
  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.9
4
+ version: 0.2.0
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-16 00:00:00.000000000 Z
11
+ date: 2017-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord