Browse files

Merge branch 'master' of github.com:portablemind/compass_agile_enterp…

…rise
  • Loading branch information...
2 parents cd02a08 + 6c2a67a commit 0bbe93d5696d1c93c97af2a764c8bf02ad1607b1 @cwoodward cwoodward committed Jun 21, 2012
View
10 erp_base_erp_svcs/app/models/party.rb
@@ -177,12 +177,16 @@ def get_contact_by_method(m)
end
def respond_to?(m)
- ((get_contact_by_method(m.to_s).nil? ? super : true)) rescue super
+ (super ? true : get_contact_by_method(m.to_s)) rescue super
end
def method_missing(m, *args, &block)
- value = get_contact_by_method(m.to_s)
- (value.nil?) ? super : (return value)
+ if self.respond_to?(m)
+ value = get_contact_by_method(m.to_s)
+ (value.nil?) ? super : (return value)
+ else
+ super
+ end
end
#************************************************************************************************
View
38 ...tech_svcs/lib/erp_tech_svcs/extensions/active_record/has_relational_dynamic_attributes.rb
@@ -21,20 +21,34 @@ def has_relational_dynamic_attributes
module SingletonMethods
def find_by_dynamic_attribute(value, options={})
- arel_query = AttributeValue.where('attributed_record_type = ?', self.name)
+ arel_query = AttributeValue.select("DISTINCT id,attributed_record_id, attributed_record_type,
+ attribute_type_id, value")
+ .where('attributed_record_type = ?', self.name)
.where(AttributeValue.arel_table[:value].matches("%#{value}%"))
+ #if type_iids are based add or clauses for the types
or_clauses = nil
options[:type_iids].each do |type_iid|
- type = AttributeType.where('description = ? or internal_identifier = ?',type_iid,type_iid).first
+ 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 = or_clauses.nil? ? AttributeValue.arel_table[:attribute_type_id].eq(type.id) : or_clauses.or(AttributeValue.arel_table[:attribute_type_id].eq(type.id))
+ 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[:type_iids]
-
+
arel_query = arel_query.where(or_clauses) if or_clauses
+
+ #get total_count if we need to return it
+ total_count = arel_query.count('attribute_values.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]
- arel_query.all.collect(&:attributed_record)
+ records = arel_query.all.collect(&:attributed_record)
+
+ #return total_count if option passed
+ options[:return_total_count] ? (return records, total_count) : records
end
end
@@ -71,7 +85,7 @@ 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
-
+
def assign_dynamic_attribute_on_save
#template method overridden in implementing class
end
@@ -83,7 +97,7 @@ def has_dynamic_attribute_of_type? (attribute_type_iid)
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
-
+
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
@@ -97,8 +111,8 @@ def method_missing(m, *args, &block)
end
end
-
- end#HasRelationalDynamicAttributes
- end#ActiveRecord
- end#Extensions
-end#ErpTechSvcs
+
+ end #HasRelationalDynamicAttributes
+ end #ActiveRecord
+ end #Extensions
+end #ErpTechSvcs

0 comments on commit 0bbe93d

Please sign in to comment.