From cc317d0abcca3263f198ec288fa8c3514709e95d Mon Sep 17 00:00:00 2001 From: Alex Arnell Date: Fri, 23 May 2008 11:50:56 -0700 Subject: [PATCH] updates AR::Base to handle both inclusive and exclusive ranges in condition hashes --- activerecord/lib/active_record/base.rb | 6 +++++- activerecord/test/cases/finder_test.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index c393128..b6d8956 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2006,7 +2006,11 @@ module ActiveRecord #:nodoc: table_name = quoted_table_name end - "#{table_name}.#{connection.quote_column_name(attr)} #{attribute_condition(value)}" + if value.is_a?(Range) and value.exclude_end? then + "#{table_name}.#{connection.quote_column_name(attr)} >= ? AND #{table_name}.#{connection.quote_column_name(attr)} < ?" + else + "#{table_name}.#{connection.quote_column_name(attr)} #{attribute_condition(value)}" + end end.join(' AND ') replace_bind_variables(conditions, expand_range_bind_variables(attrs.values)) diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 80936d5..a57b785 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -216,11 +216,23 @@ class FinderTest < ActiveRecord::TestCase assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :id => 2..3 }) } end + def test_find_on_hash_conditions_with_exclusive_range + assert_equal [1,2], Topic.find(:all, :conditions => { :id => 1...3 }).map(&:id).sort + end + def test_find_on_hash_conditions_with_multiple_ranges assert_equal [1,2,3], Comment.find(:all, :conditions => { :id => 1..3, :post_id => 1..2 }).map(&:id).sort assert_equal [1], Comment.find(:all, :conditions => { :id => 1..1, :post_id => 1..10 }).map(&:id).sort end + def test_find_on_hash_conditions_with_multiple_exclusive_ranges + assert_equal [1,2], Comment.find(:all, :conditions => { :id => 1...3, :post_id => 1...2 }).map(&:id).sort + end + + def test_find_on_hash_conditions_with_mixed_ranges + assert_equal [1], Comment.find(:all, :conditions => { :id => 1...2, :post_id => 1..10 }).map(&:id).sort + end + def test_find_on_multiple_hash_conditions assert Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => false }) assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }) } -- 1.5.4.5