Skip to content
Browse files

Implement nebolsin commit to fix or conditions bug

  • Loading branch information...
1 parent c24e942 commit 00aab642be72171aa098108002d2fd43b6092f05 @binarylogic committed Jan 14, 2010
Showing with 17 additions and 2 deletions.
  1. +9 −1 lib/searchlogic/named_scopes/or_conditions.rb
  2. +7 −0 spec/named_scopes/or_conditions_spec.rb
  3. +1 −1 spec/spec_helper.rb
View
10 lib/searchlogic/named_scopes/or_conditions.rb
@@ -10,6 +10,10 @@ def condition?(name) # :nodoc:
super || or_condition?(name)
end
+ def named_scope_options(name) # :nodoc:
+ super || super(or_conditions(name).join("_or_"))
+ end
+
private
def or_condition?(name)
!or_conditions(name).nil?
@@ -114,7 +118,11 @@ def full_association_path(part, last_condition, given_assoc)
end
def create_or_condition(scopes, args)
- named_scope scopes.join("_or_"), lambda { |*args|
+ scopes_options = scopes.collect { |scope, *args| send(scope, *args).proxy_options }
+ # We're using first scope to determine column's type
+ scope = named_scope_options(scopes.first)
+ column_type = scope.respond_to?(:searchlogic_arg_type) ? scope.searchlogic_arg_type : :string
+ named_scope scopes.join("_or_"), searchlogic_lambda(column_type) { |*args|
merge_scopes_with_or(scopes.collect { |scope| [scope, *args] })
}
end
View
7 spec/named_scopes/or_conditions_spec.rb
@@ -56,4 +56,11 @@
User.find_or_create_by_name_and_username("Fred", "fredb").should be_a_kind_of User
end
+ it "should work with User.search(conditions) method" do
+ User.search(:username_or_name_like => 'ben').proxy_options.should == {:conditions => "(users.username LIKE '%ben%') OR (users.name LIKE '%ben%')"}
+ end
+
+ it "should convert types properly when used with User.search(conditions) method" do
+ User.search(:id_or_age_lte => '10').proxy_options.should == {:conditions => "(users.id <= 10) OR (users.age <= 10)"}
+ end
end
View
2 spec/spec_helper.rb
@@ -1,7 +1,7 @@
require 'spec'
require 'rubygems'
require 'ruby-debug'
-require 'activerecord'
+require 'active_record'
ENV['TZ'] = 'UTC'
Time.zone = 'Eastern Time (US & Canada)'

0 comments on commit 00aab64

Please sign in to comment.
Something went wrong with that request. Please try again.