From a6435d0a55b5316aa23824043e87473f8efc66ac Mon Sep 17 00:00:00 2001 From: Darius Date: Sat, 10 Jan 2009 21:16:33 -0500 Subject: [PATCH] changed punctuation --- activerecord/lib/active_record/base.rb | 12 ++++++++++++ activerecord/test/cases/base_test.rb | 6 ++++++ 2 files changed, 18 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index cca012e..f8ba89c 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2247,6 +2247,18 @@ module ActiveRecord #:nodoc: expanded_attrs end + # Accepts a hash of attributes. Replaces strings which match as ranges + # { :age => "13..18" } + # # => { :age => 13..18 } + def expand_hash_conditions_for_ranges(attrs) + attrs.inject({}) do |expanded_attrs, (attr, value)| + expanded_attrs.merge attr => case value + when /^(\d+)..(\d+)$/i; Range.new($1.to_f,$2.to_f) + else; value + end + end + end + # Sanitizes a hash of attribute/value pairs into SQL conditions for a WHERE clause. # { :name => "foo'bar", :group_id => 4 } # # => "name='foo''bar' and group_id= 4" diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 0f03dae..d6ca830 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1680,6 +1680,12 @@ class BasicsTest < ActiveRecord::TestCase assert_equal clients.map(&:name).to_set, firm.clients.map(&:name).to_set end + def test_find_by_string_ranges + developers = Developer.find :all, :conditions => {:salary => '0..90000' } + assert_equal 3, developers.size + end + + def test_interpolate_sql assert_nothing_raised { Category.new.send(:interpolate_sql, 'foo@bar') } assert_nothing_raised { Category.new.send(:interpolate_sql, 'foo bar) baz') } -- 1.5.6.4 From 575f7ba69a36dc4c722bf81f1a705465338c7d2d Mon Sep 17 00:00:00 2001 From: Darius Date: Sat, 10 Jan 2009 22:11:53 -0500 Subject: [PATCH] added range matching for finders --- activerecord/lib/active_record/base.rb | 1 + activerecord/test/cases/base_test.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index f8ba89c..6846716 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2275,6 +2275,7 @@ module ActiveRecord #:nodoc: # # => "address_street='123 abc st.' and address_city='chicago'" def sanitize_sql_hash_for_conditions(attrs, table_name = quoted_table_name) attrs = expand_hash_conditions_for_aggregates(attrs) + attrs = expand_hash_conditions_for_ranges(attrs) conditions = attrs.map do |attr, value| unless value.is_a?(Hash) diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index d6ca830..ce96140 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1682,7 +1682,7 @@ class BasicsTest < ActiveRecord::TestCase def test_find_by_string_ranges developers = Developer.find :all, :conditions => {:salary => '0..90000' } - assert_equal 3, developers.size + assert_equal 2, developers.size end -- 1.5.6.4