lib/sugarcrm/base.rb in sugarcrm-0.8.1 vs lib/sugarcrm/base.rb in sugarcrm-0.8.2
- old
+ new
@@ -127,12 +127,27 @@
def query_from_options(options)
# If we dont have conditions, just return an empty query
return "" unless options[:conditions]
conditions = []
- options[:conditions].each_pair do |column, value|
- conditions << "#{self._module.table_name}.#{column} = \'#{value}\'"
+ options[:conditions].each_pair do |column, v|
+ v = [] << v unless v.class == Array
+
+ v.each{|value|
+ # parse operator in cases where (e.g.) :attribute => '>= some_value', fallback to '=' operator as default
+ operator = value.to_s[/^([<>=]*)(.*)$/,1]
+ operator = '=' if operator.nil? || operator.strip == ''
+
+ value = $2 # strip the operator from value passed to query
+ value = value.strip[/'?([^']*)'?/,1]
+ unless column =~ /_c$/ # attribute name ending with _c implies a custom attribute
+ condition_attribute = "#{self._module.table_name}.#{column}"
+ else
+ condition_attribute = column # if setting a condition on a customer attribute, don't add model table name (or query breaks)
+ end
+ conditions << "#{condition_attribute} #{operator} \'#{value}\'"
+ }
end
conditions.join(" AND ")
end
# Enables dynamic finders like <tt>find_by_user_name(user_name)</tt> and <tt>find_by_user_name_and_password(user_name, password)</tt>
@@ -268,10 +283,10 @@
attribute_names.each_with_index { |name, idx| attributes[name] = arguments[idx] }
attributes
end
VALID_FIND_OPTIONS = [ :conditions, :include, :joins, :limit, :offset,
- :order, :select, :readonly, :group, :having, :from, :lock ]
+ :order_by, :select, :readonly, :group, :having, :from, :lock ]
def validate_find_options(options) #:nodoc:
options.assert_valid_keys(VALID_FIND_OPTIONS)
end
\ No newline at end of file