Module: HasDynamicFields::Base::InstanceMethods
- Defined in:
- lib/has_dynamic_fields/base.rb,
lib/has_dynamic_fields/has_dynamic_fields.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
# File 'lib/has_dynamic_fields/has_dynamic_fields.rb', line 251
def method_missing(name, *args)
return if self.send(self.aad_options[:fieldgroup_singular].to_sym).blank?
m = name.to_s
type = :reader
attribute_name = name.to_s.sub(/=$/) do
type = :writer
""
end
fieldgroup_fields = self.send(self.aad_options[:fieldgroup_singular].to_sym).send(self.aad_options[:field_plural].to_sym)
@dynamic_field_keys ||= instance_variable_set("@dynamic_field_keys", fieldgroup_fields.collect {|o| o.name.to_sym })
@dynamic_fields ||= instance_variable_set("@dynamic_fields", fieldgroup_fields)
if @dynamic_field_keys.include?(attribute_name.to_sym)
field = @dynamic_fields.select { |field| field.name.to_sym == attribute_name.to_sym && field.send(self.aad_options[:fieldgroup_singular]).id == self.send(self.aad_options[:fieldgroup_singular]).id }.first
case(type)
when :writer
self.update_dynamic_attributes=(true)
self.class.send(:define_method, name) do |value|
self.send(self.aad_options[:value_singular].to_sym).send("field_#{field.id}=".to_sym, value)
end
else
self.class.send(:define_method, name) do
self.send(self.aad_options[:value_singular]).send("field_#{field.id}".to_sym, *args)
end
end
send(name, *args)
else
end
end
|
Instance Method Details
#create_dynamic_field_value ⇒ Object
222
223
224
225
226
227
228
229
230
|
# File 'lib/has_dynamic_fields/has_dynamic_fields.rb', line 222
def create_dynamic_field_value
value_model = self.aad_options[:value_klass]
value_model = value_model.create!({ self.aad_options[:fieldgroup_singular].to_sym => self.send(self.aad_options[:fieldgroup_singular]) })
self.send("#{self.aad_options[:value_singular]}=".to_sym, value_model)
end
|
243
244
245
246
247
248
249
|
# File 'lib/has_dynamic_fields/has_dynamic_fields.rb', line 243
def dynamic_field_inputs
fields = []
dynamic_fields = self.send(self.aad_options[:fieldgroup_singular].to_sym).send(self.aad_options[:field_plural].to_sym)
dynamic_fields.each do |field|
fields << field
end
end
|
#dynamic_field_keys ⇒ Object
232
233
234
235
236
237
238
239
240
241
|
# File 'lib/has_dynamic_fields/has_dynamic_fields.rb', line 232
def dynamic_field_keys
return if self.send(self.aad_options[:fieldgroup_singular].to_sym).blank?
keys = []
dynamic_fieldgroup_field_keys = self.send(self.aad_options[:fieldgroup_singular].to_sym).send(self.aad_options[:field_plural].to_sym)
dynamic_fieldgroup_field_keys.each do |o|
keys << o.name.to_sym
end
keys
end
|
#show_options ⇒ Object
205
206
207
208
|
# File 'lib/has_dynamic_fields/has_dynamic_fields.rb', line 205
def show_options
puts self.class.aad_options.inspect
puts self.aad_options.inspect
end
|
#update_dynamic_fields ⇒ Object
218
219
220
|
# File 'lib/has_dynamic_fields/has_dynamic_fields.rb', line 218
def update_dynamic_fields
self.send(self.aad_options[:value_singular].to_sym).save
end
|
#update_dynamic_fields=(val) ⇒ Object
214
215
216
|
# File 'lib/has_dynamic_fields/has_dynamic_fields.rb', line 214
def update_dynamic_fields=(val)
instance_variable_set("@update_dynamic_fields",val)
end
|
#update_dynamic_fields? ⇒ Boolean
210
211
212
|
# File 'lib/has_dynamic_fields/has_dynamic_fields.rb', line 210
def update_dynamic_fields?
instance_variable_get("@update_dynamic_fields")
end
|