aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-10-09 17:52:42 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-10-09 17:52:42 +0000
commit5ef931a4b879ed2f380b393000edbdb4c50f8150 (patch)
tree155b090e0059303d0b4a14a94ce0032ee87af2d4 /activesupport
parent2ed689745c14f7f02e5e4a970240e206dcee1b33 (diff)
downloadrails-5ef931a4b879ed2f380b393000edbdb4c50f8150.tar.gz
rails-5ef931a4b879ed2f380b393000edbdb4c50f8150.tar.bz2
rails-5ef931a4b879ed2f380b393000edbdb4c50f8150.zip
Added to_s(:db) to Range
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2506 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/date/conversions.rb3
-rw-r--r--activesupport/lib/active_support/core_ext/range.rb5
-rw-r--r--activesupport/lib/active_support/core_ext/range/conversions.rb21
-rw-r--r--activesupport/test/core_ext/range_ext_test.rb16
5 files changed, 45 insertions, 2 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 630c8bdae9..493575d0fa 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added to_s(:db) to Range, so you can get "BETWEEN '2005-12-10' AND '2005-12-12'" from Date.new(2005, 12, 10)..Date.new(2005, 12, 12) (and likewise with Times)
+
* Moved require_library_or_gem into Kernel. #1992 [Michael Schuerig <michael@schuerig.de>]
* Add :rfc822 as an option for Time#to_s (to get rfc822-formatted times)
diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb
index 048a260ada..a9d7eb0976 100644
--- a/activesupport/lib/active_support/core_ext/date/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/date/conversions.rb
@@ -8,8 +8,7 @@ module ActiveSupport #:nodoc:
:long => "%B %e, %Y"
}
- def self.append_features(klass) #:nodoc:
- super
+ def self.included(klass) #:nodoc:
klass.send(:alias_method, :to_default_s, :to_s)
klass.send(:alias_method, :to_s, :to_formatted_s)
end
diff --git a/activesupport/lib/active_support/core_ext/range.rb b/activesupport/lib/active_support/core_ext/range.rb
new file mode 100644
index 0000000000..ca77511521
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/range.rb
@@ -0,0 +1,5 @@
+require File.dirname(__FILE__) + '/range/conversions'
+
+class Range #:nodoc:
+ include ActiveSupport::CoreExtensions::Range::Conversions
+end
diff --git a/activesupport/lib/active_support/core_ext/range/conversions.rb b/activesupport/lib/active_support/core_ext/range/conversions.rb
new file mode 100644
index 0000000000..677ba639ec
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/range/conversions.rb
@@ -0,0 +1,21 @@
+module ActiveSupport #:nodoc:
+ module CoreExtensions #:nodoc:
+ module Range #:nodoc:
+ # Getting dates in different convenient string representations and other objects
+ module Conversions
+ DATE_FORMATS = {
+ :db => Proc.new { |start, stop| "BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'" }
+ }
+
+ def self.included(klass) #:nodoc:
+ klass.send(:alias_method, :to_default_s, :to_s)
+ klass.send(:alias_method, :to_s, :to_formatted_s)
+ end
+
+ def to_formatted_s(format = :default)
+ DATE_FORMATS[format] ? DATE_FORMATS[format].call(first, last) : to_default_s
+ end
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/activesupport/test/core_ext/range_ext_test.rb b/activesupport/test/core_ext/range_ext_test.rb
new file mode 100644
index 0000000000..49cbb10be6
--- /dev/null
+++ b/activesupport/test/core_ext/range_ext_test.rb
@@ -0,0 +1,16 @@
+require 'test/unit'
+require File.dirname(__FILE__) + '/../../lib/active_support/core_ext/date'
+require File.dirname(__FILE__) + '/../../lib/active_support/core_ext/time'
+require File.dirname(__FILE__) + '/../../lib/active_support/core_ext/range'
+
+class RangeTest < Test::Unit::TestCase
+ def test_to_s_from_dates
+ date_range = Date.new(2005, 12, 10)..Date.new(2005, 12, 12)
+ assert_equal "BETWEEN '2005-12-10' AND '2005-12-12'", date_range.to_s(:db)
+ end
+
+ def test_to_s_from_times
+ date_range = Time.utc(2005, 12, 10, 15, 30)..Time.utc(2005, 12, 10, 17, 30)
+ assert_equal "BETWEEN '2005-12-10 15:30:00' AND '2005-12-10 17:30:00'", date_range.to_s(:db)
+ end
+end