module ActiveDynamic::HasDynamicAttributes
Public Instance Methods
dynamic_attributes()
click to toggle source
# File lib/active_dynamic/has_dynamic_attributes.rb, line 14 def dynamic_attributes if persisted? && any_dynamic_attributes? should_resolve_persisted? ? resolve_combined : resolve_from_db else resolve_from_provider end end
dynamic_attributes_loaded?()
click to toggle source
# File lib/active_dynamic/has_dynamic_attributes.rb, line 22 def dynamic_attributes_loaded? @dynamic_attributes_loaded ||= false end
method_missing(method_name, *arguments, &block)
click to toggle source
Calls superclass method
# File lib/active_dynamic/has_dynamic_attributes.rb, line 35 def method_missing(method_name, *arguments, &block) if dynamic_attributes_loaded? super else load_dynamic_attributes send(method_name, *arguments, &block) end end
respond_to?(method_name, include_private = false)
click to toggle source
Calls superclass method
# File lib/active_dynamic/has_dynamic_attributes.rb, line 26 def respond_to?(method_name, include_private = false) if super true else load_dynamic_attributes unless dynamic_attributes_loaded? dynamic_attributes.find { |attr| attr.name == method_name.to_s.delete('=') }.present? end end
Private Instance Methods
_custom_fields()
click to toggle source
# File lib/active_dynamic/has_dynamic_attributes.rb, line 100 def _custom_fields @_custom_fields ||= ActiveSupport::HashWithIndifferentAccess.new end
add_presence_validator(attribute)
click to toggle source
# File lib/active_dynamic/has_dynamic_attributes.rb, line 94 def add_presence_validator(attribute) singleton_class.instance_eval do validates_presence_of(attribute) end end
any_dynamic_attributes?()
click to toggle source
# File lib/active_dynamic/has_dynamic_attributes.rb, line 58 def any_dynamic_attributes? active_dynamic_attributes.any? end
generate_accessors(fields)
click to toggle source
# File lib/active_dynamic/has_dynamic_attributes.rb, line 78 def generate_accessors(fields) fields.each do |field| add_presence_validator(field.name) if field.required? define_singleton_method(field.name) do _custom_fields[field.name] end define_singleton_method("#{field.name}=") do |value| _custom_fields[field.name] = value && value.to_s.strip end end end
load_dynamic_attributes()
click to toggle source
# File lib/active_dynamic/has_dynamic_attributes.rb, line 104 def load_dynamic_attributes dynamic_attributes.each do |ticket_field| _custom_fields[ticket_field.name] = ticket_field.value end generate_accessors dynamic_attributes @dynamic_attributes_loaded = true end
resolve_combined()
click to toggle source
# File lib/active_dynamic/has_dynamic_attributes.rb, line 62 def resolve_combined attributes = resolve_from_db resolve_from_provider.each do |attribute| attributes << ActiveDynamic::Attribute.new(attribute.as_json) unless attributes.find { |attr| attr.name == attribute.name } end attributes end
resolve_from_db()
click to toggle source
# File lib/active_dynamic/has_dynamic_attributes.rb, line 70 def resolve_from_db active_dynamic_attributes end
resolve_from_provider()
click to toggle source
# File lib/active_dynamic/has_dynamic_attributes.rb, line 74 def resolve_from_provider ActiveDynamic.configuration.provider_class.new(self).call end
save_dynamic_attributes()
click to toggle source
# File lib/active_dynamic/has_dynamic_attributes.rb, line 113 def save_dynamic_attributes dynamic_attributes.each do |field| next unless _custom_fields[field.name] attr = active_dynamic_attributes.find_or_initialize_by(field.as_json) if persisted? attr.update(value: _custom_fields[field.name]) else attr.assign_attributes(value: _custom_fields[field.name]) end end end
should_resolve_persisted?()
click to toggle source
# File lib/active_dynamic/has_dynamic_attributes.rb, line 46 def should_resolve_persisted? value = ActiveDynamic.configuration.resolve_persisted case value when TrueClass, FalseClass value when Proc value.call(self) else raise "Invalid configuration for resolve_persisted. Value should be Bool or Proc, got #{value.class}" end end