Module: ErpTechSvcs::Extensions::ActiveRecord::HasRelationalDynamicAttributes::InstanceMethods
- Defined in:
- lib/erp_tech_svcs/extensions/active_record/has_relational_dynamic_attributes.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
118
119
120
121
|
# File 'lib/erp_tech_svcs/extensions/active_record/has_relational_dynamic_attributes.rb', line 118
def method_missing(m, *args, &block)
value = get_dynamic_value_of_type(m.to_s)
(value.nil?) ? super : (return value)
end
|
Instance Method Details
#add_dynamic_attribute(value, type, data_type) ⇒ Object
111
112
113
114
115
116
|
# File 'lib/erp_tech_svcs/extensions/active_record/has_relational_dynamic_attributes.rb', line 111
def add_dynamic_attribute(value, type, data_type)
attribute_type = AttributeType.where('description = ? or internal_identifier = ?', type, type).first
attribute_type = AttributeType.create(:description => type, :data_type => data_type) unless attribute_type
attribute_value = AttributeValue.create(:value => value, :attribute_type => attribute_type)
self.attribute_values << attribute_value
end
|
#assign_dynamic_attribute_on_save ⇒ Object
99
100
101
|
# File 'lib/erp_tech_svcs/extensions/active_record/has_relational_dynamic_attributes.rb', line 99
def assign_dynamic_attribute_on_save
end
|
#destroy_dynamic_attribute_of_type(attribute_type_iid) ⇒ Object
107
108
109
|
# File 'lib/erp_tech_svcs/extensions/active_record/has_relational_dynamic_attributes.rb', line 107
def destroy_dynamic_attribute_of_type (attribute_type_iid)
self.attribute_values.includes(:attribute_type).destroy_all("attribute_types.internal_identifier = #{attribute_type_iid.to_s} or attribute_types.description = #{attribute_type_iid.to_s}")
end
|
#get_dynamic_attribute_of_type(attribute_type_iid) ⇒ Object
94
95
96
97
|
# File 'lib/erp_tech_svcs/extensions/active_record/has_relational_dynamic_attributes.rb', line 94
def get_dynamic_attribute_of_type(attribute_type_iid)
attribute_value = self.attribute_values.includes(:attribute_type).where('attribute_types.internal_identifier = ? or attribute_types.description = ?', attribute_type_iid.to_s, attribute_type_iid.to_s).first
attribute_value.nil? ? nil : attribute_value
end
|
#get_dynamic_attributes ⇒ Object
81
82
83
84
85
86
87
|
# File 'lib/erp_tech_svcs/extensions/active_record/has_relational_dynamic_attributes.rb', line 81
def get_dynamic_attributes
{}.tap do |hash|
self.attribute_values.each do |value|
hash[value.attribute_type.description] = value.value
end
end
end
|
#get_dynamic_value_of_type(attribute_type_iid) ⇒ Object
89
90
91
92
|
# File 'lib/erp_tech_svcs/extensions/active_record/has_relational_dynamic_attributes.rb', line 89
def get_dynamic_value_of_type(attribute_type_iid)
attribute_value = get_dynamic_attribute_of_type(attribute_type_iid)
attribute_value.nil? ? nil : attribute_value.value
end
|
#has_dynamic_attribute_of_type?(attribute_type_iid) ⇒ Boolean
103
104
105
|
# File 'lib/erp_tech_svcs/extensions/active_record/has_relational_dynamic_attributes.rb', line 103
def has_dynamic_attribute_of_type? (attribute_type_iid)
!self.attribute_values.includes(:attribute_type).where('attribute_types.internal_identifier = ? or attribute_types.description = ?', attribute_type_iid.to_s, attribute_type_iid.to_s).first.nil?
end
|
#update_first_dynamic_attribute_value_of_type(value, type) ⇒ Object
74
75
76
77
78
79
|
# File 'lib/erp_tech_svcs/extensions/active_record/has_relational_dynamic_attributes.rb', line 74
def update_first_dynamic_attribute_value_of_type(value, type)
attribute_type = AttributeType.where('description = ? or internal_identifier = ?', type, type).first
attribute_value = self.attribute_values.where(:attribute_type_id => attribute_type.id).first
attribute_value.value = value
attribute_value.save!
end
|
#update_or_create_dynamic_attribute(value, type, data_type) ⇒ Object
66
67
68
69
70
71
72
|
# File 'lib/erp_tech_svcs/extensions/active_record/has_relational_dynamic_attributes.rb', line 66
def update_or_create_dynamic_attribute(value, type, data_type)
if self.has_dynamic_attribute_of_type?(type)
update_first_dynamic_attribute_value_of_type(value, type)
else
add_dynamic_attribute(value, type, data_type)
end
end
|