23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/erp_tech_svcs/extensions/active_record/has_relational_dynamic_attributes.rb', line 23
def find_by_dynamic_attribute(value, options={})
arel_query = AttributeValue.where('attributed_record_type = ?', self.name)
.where(AttributeValue.arel_table[:value].matches("%#{value}%"))
or_clauses = nil
options[:included_type_iids].each do |type_iid|
type = AttributeType.where('description = ? or internal_identifier = ?', type_iid, type_iid).first
raise "Attribute Type '#{type_iid}' does not exist" if type.nil?
or_clauses = if or_clauses.nil?
AttributeValue.arel_table[:attribute_type_id].eq(type.id)
else
or_clauses.or(AttributeValue.arel_table[:attribute_type_id].eq(type.id))
end
end if options[:included_type_iids]
or_clauses = nil
options[:excluded_type_iids].each do |type_iid|
type = AttributeType.where('description = ? or internal_identifier = ?', type_iid, type_iid).first
raise "Attribute Type '#{type_iid}' does not exist" if type.nil?
or_clauses = if or_clauses.nil?
AttributeValue.arel_table[:attribute_type_id].eq(type.id).not
else
or_clauses.or(AttributeValue.arel_table[:attribute_type_id].eq(type.id).not)
end
end if options[:excluded_type_iids]
arel_query = arel_query.where(or_clauses) if or_clauses
total_count = arel_query.count('attributed_record_id') if options[:return_total_count]
arel_query = arel_query.limit(options[:limit]) if options[:limit]
arel_query = arel_query.offset(options[:offset]) if options[:offset]
records = arel_query.all.collect(&:attributed_record)
options[:return_total_count] ? (return records, total_count) : records
end
|