Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge commit 'mainstream/master'

  • Loading branch information...
commit c99e54f4ec67cefe11787aa9ab9d8bd006e6ca71 2 parents e6afd8b + dc4eec1
@lifo lifo authored
View
4 actionpack/CHANGELOG
@@ -1,5 +1,9 @@
*SVN*
+* InstanceTag#default_time_from_options with hash args uses Time.current as default; respects hash settings when time falls in system local spring DST gap [Geoff Buesing]
+
+* select_date defaults to Time.zone.today when config.time_zone is set [Geoff Buesing]
+
* Fixed that TextHelper#text_field would corrypt when raw HTML was used as the value (mchenryc, Kevin Glowacz) [#80]
* Added ActionController::TestCase#rescue_action_in_public! to control whether the action under test should use the regular rescue_action path instead of simply raising the exception inline (great for error testing) [DHH]
View
9 actionpack/lib/action_view/helpers/date_helper.rb
@@ -283,7 +283,7 @@ def select_datetime(datetime = Time.current, options = {}, html_options = {})
# # prefixed with 'payday' rather than 'date'
# select_datetime(my_date_time, :prefix => 'payday')
#
- def select_date(date = Date.today, options = {}, html_options = {})
+ def select_date(date = Date.current, options = {}, html_options = {})
options[:order] ||= []
[:year, :month, :day].each { |o| options[:order].push(o) unless options[:order].include?(o) }
@@ -683,12 +683,13 @@ def default_time_from_options(default)
default[:min] ||= default[:minute]
default[:sec] ||= default[:second]
+ time = Time.current
+
[:year, :month, :day, :hour, :min, :sec].each do |key|
- default[key] ||= Time.now.send(key)
+ default[key] ||= time.send(key)
end
- Time.mktime(default[:year], default[:month], default[:day],
- default[:hour], default[:min], default[:sec])
+ Time.utc(default[:year], default[:month], default[:day], default[:hour], default[:min], default[:sec])
end
end
end
View
32 actionpack/test/template/date_helper_test.rb
@@ -950,6 +950,15 @@ def test_select_time_uses_time_current_as_default
expects(:select_minute).with(time, anything, anything).returns('')
select_time
end
+
+ def test_select_date_uses_date_current_as_default
+ date = stub(:year => 2004, :month => 6, :day => 15)
+ Date.expects(:current).returns date
+ expects(:select_year).with(date, anything, anything).returns('')
+ expects(:select_month).with(date, anything, anything).returns('')
+ expects(:select_day).with(date, anything, anything).returns('')
+ select_date
+ end
end
def test_date_select
@@ -1699,4 +1708,27 @@ def test_datetime_select_with_html_options
assert_dom_equal expected, datetime_select("post", "updated_at", {}, :class => 'selector')
end
+ uses_mocha 'TestInstanceTagDefaultTimeFromOptions' do
+ def test_instance_tag_default_time_from_options_uses_time_current_as_default_when_hash_passed_as_arg
+ dummy_instance_tag = ActionView::Helpers::InstanceTag.new(1,2,3)
+ Time.expects(:current).returns Time.now
+ dummy_instance_tag.send!(:default_time_from_options, :hour => 2)
+ end
+
+ def test_instance_tag_default_time_from_options_respects_hash_arg_settings_when_time_falls_in_system_local_dst_spring_gap
+ with_env_tz('US/Central') do
+ dummy_instance_tag = ActionView::Helpers::InstanceTag.new(1,2,3)
+ Time.stubs(:now).returns Time.local(2006, 4, 2, 1)
+ assert_equal 2, dummy_instance_tag.send!(:default_time_from_options, :hour => 2).hour
+ end
+ end
+ end
+
+ protected
+ def with_env_tz(new_tz = 'US/Eastern')
+ old_tz, ENV['TZ'] = ENV['TZ'], new_tz
+ yield
+ ensure
+ old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
+ end
end
View
4 activerecord/CHANGELOG
@@ -1,5 +1,9 @@
*SVN*
+* Base#instantiate_time_object: eliminate check for Time.zone, since we can assume this is set if time_zone_aware_attributes is set to true [Geoff Buesing]
+
+* Time zone aware attribute methods use Time.zone.parse instead of #to_time for String arguments, so that offset information in String is respected. Resolves #105. [Scott Fleckenstein, Geoff Buesing]
+
* Added change_table for migrations (Jeff Dean) [#71]. Example:
change_table :videos do |t|
View
4 activerecord/lib/active_record/associations/association_proxy.rb
@@ -118,7 +118,7 @@ def target=(target)
end
def inspect
- reload unless loaded?
+ load_target
@target.inspect
end
@@ -167,7 +167,7 @@ def merge_options_from_reflection!(options)
def with_scope(*args, &block)
@reflection.klass.send :with_scope, *args, &block
end
-
+
private
def method_missing(method, *args)
if load_target
View
6 activerecord/lib/active_record/attribute_methods.rb
@@ -179,10 +179,10 @@ def define_write_method(attr_name)
def define_write_method_for_time_zone_conversion(attr_name)
method_body = <<-EOV
def #{attr_name}=(time)
- if time
- time = time.to_time rescue time unless time.acts_like?(:time)
- time = time.in_time_zone if time.acts_like?(:time)
+ unless time.acts_like?(:time)
+ time = time.is_a?(String) ? Time.zone.parse(time) : time.to_time rescue time
end
+ time = time.in_time_zone rescue nil if time
write_attribute(:#{attr_name}, time)
end
EOV
View
2  activerecord/lib/active_record/base.rb
@@ -2588,7 +2588,7 @@ def assign_multiparameter_attributes(pairs)
end
def instantiate_time_object(name, values)
- if Time.zone && self.class.time_zone_aware_attributes && !self.class.skip_time_zone_conversion_for_attributes.include?(name.to_sym)
+ if self.class.time_zone_aware_attributes && !self.class.skip_time_zone_conversion_for_attributes.include?(name.to_sym)
Time.zone.local(*values)
else
Time.time_with_datetime_fallback(@@default_timezone, *values)
View
6 activerecord/test/cases/associations_test.rb
@@ -149,6 +149,12 @@ def test_save_on_parent_does_not_load_target
assert !david.projects.loaded?
end
+ def test_inspect_does_not_reload_a_not_yet_loaded_target
+ andreas = Developer.new :name => 'Andreas', :log => 'new developer added'
+ assert !andreas.audit_logs.loaded?
+ assert_match(/message: "new developer added"/, andreas.audit_logs.inspect)
+ end
+
def test_save_on_parent_saves_children
developer = Developer.create :name => "Bryan", :salary => 50_000
assert_equal 1, developer.reload.audit_logs.size
View
35 activerecord/test/cases/attribute_methods_test.rb
@@ -173,6 +173,41 @@ def test_setting_time_zone_aware_attribute_in_other_time_zone
end
end
+ def test_setting_time_zone_aware_attribute_with_string
+ utc_time = Time.utc(2008, 1, 1)
+ (-11..13).each do |timezone_offset|
+ time_string = utc_time.in_time_zone(timezone_offset).to_s
+ in_time_zone "Pacific Time (US & Canada)" do
+ record = @target.new
+ record.written_on = time_string
+ assert_equal Time.zone.parse(time_string), record.written_on
+ assert_equal TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone
+ assert_equal Time.utc(2007, 12, 31, 16), record.written_on.time
+ end
+ end
+ end
+
+ def test_setting_time_zone_aware_attribute_to_blank_string_returns_nil
+ in_time_zone "Pacific Time (US & Canada)" do
+ record = @target.new
+ record.written_on = ' '
+ assert_nil record.written_on
+ end
+ end
+
+ def test_setting_time_zone_aware_attribute_interprets_time_zone_unaware_string_in_time_zone
+ time_string = 'Tue Jan 01 00:00:00 2008'
+ (-11..13).each do |timezone_offset|
+ in_time_zone timezone_offset do
+ record = @target.new
+ record.written_on = time_string
+ assert_equal Time.zone.parse(time_string), record.written_on
+ assert_equal TimeZone[timezone_offset], record.written_on.time_zone
+ assert_equal Time.utc(2008, 1, 1), record.written_on.time
+ end
+ end
+ end
+
def test_setting_time_zone_aware_attribute_in_current_time_zone
utc_time = Time.utc(2008, 1, 1)
in_time_zone "Pacific Time (US & Canada)" do
View
4 activerecord/test/models/developer.rb
@@ -49,6 +49,10 @@ def find_least_recent
before_create do |developer|
developer.audit_logs.build :message => "Computer created"
end
+
+ def log=(message)
+ audit_logs.build :message => message
+ end
end
class AuditLog < ActiveRecord::Base
View
8 activesupport/CHANGELOG
@@ -1,5 +1,13 @@
*SVN*
+* Adding Date.current, which returns Time.zone.today if config.time_zone is set; otherwise returns Date.today [Geoff Buesing]
+
+* TimeWithZone: date part getter methods (#year #mon #day etc) are defined on class; no longer relying on method_missing [Geoff Buesing]
+
+* Time.zone.parse return nil for strings with no date information [Geoff Buesing]
+
+* Time.zone.parse respects offset information in string. Resolves #105. [Scott Fleckenstein, Geoff Buesing]
+
* Added Ruby 1.8 implementation of Process.daemon
* Duration #since and #ago with no argument (e.g., 5.days.ago) return TimeWithZone when config.time_zone is set. Introducing Time.current, which returns Time.zone.now if config.time_zone is set, otherwise just returns Time.now [Geoff Buesing]
View
5 activesupport/lib/active_support/core_ext/date/calculations.rb
@@ -25,6 +25,11 @@ def yesterday
def tomorrow
::Date.today.tomorrow
end
+
+ # Returns Time.zone.today when config.time_zone is set, otherwise just returns Date.today.
+ def current
+ ::Time.zone_default ? ::Time.zone.today : ::Date.today
+ end
end
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
View
8 activesupport/lib/active_support/time_with_zone.rb
@@ -163,6 +163,14 @@ def advance(options)
utc.advance(options).in_time_zone(time_zone)
end
+ %w(year mon month day mday hour min sec).each do |method_name|
+ class_eval <<-EOV
+ def #{method_name}
+ time.#{method_name}
+ end
+ EOV
+ end
+
def usec
time.respond_to?(:usec) ? time.usec : 0
end
View
8 activesupport/lib/active_support/values/time_zone.rb
@@ -213,8 +213,14 @@ def at(secs)
# Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00
# Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00
def parse(str, now=now)
+ date_parts = Date._parse(str)
+ return if date_parts.blank?
time = Time.parse(str, now) rescue DateTime.parse(str)
- ActiveSupport::TimeWithZone.new(nil, self, time)
+ if date_parts[:offset].nil?
+ ActiveSupport::TimeWithZone.new(nil, self, time)
+ else
+ time.in_time_zone(self)
+ end
end
# Returns an ActiveSupport::TimeWithZone instance representing the current time
View
23 activesupport/test/core_ext/date_ext_test.rb
@@ -208,6 +208,29 @@ def test_xmlschema
end
end
end
+
+ uses_mocha 'TestDateCurrent' do
+ def test_current_returns_date_today_when_zone_default_not_set
+ with_env_tz 'US/Central' do
+ Time.stubs(:now).returns Time.local(1999, 12, 31, 23)
+ assert_equal Date.new(1999, 12, 31), Date.today
+ assert_equal Date.new(1999, 12, 31), Date.current
+ end
+ end
+
+ def test_current_returns_time_zone_today_when_zone_default_set
+ silence_warnings do # silence warnings raised by tzinfo gem
+ Time.zone_default = TimeZone['Eastern Time (US & Canada)']
+ with_env_tz 'US/Central' do
+ Time.stubs(:now).returns Time.local(1999, 12, 31, 23)
+ assert_equal Date.new(1999, 12, 31), Date.today
+ assert_equal Date.new(2000, 1, 1), Date.current
+ end
+ end
+ ensure
+ Time.zone_default = nil
+ end
+ end
protected
def with_env_tz(new_tz = 'US/Eastern')
View
10 activesupport/test/core_ext/time_with_zone_test.rb
@@ -328,10 +328,18 @@ def test_marshal_dump_and_load_with_tzinfo_identifier
assert_equal Time.utc(1999, 12, 31, 19), mtime.time
end
end
-
+
def test_method_missing_with_non_time_return_value
silence_warnings do # silence warnings raised by tzinfo gem
+ @twz.time.expects(:foo).returns('bar')
+ assert_equal 'bar', @twz.foo
+ end
+ end
+
+ def test_date_part_value_methods
+ silence_warnings do # silence warnings raised by tzinfo gem
twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone)
+ twz.stubs(:method_missing).returns(nil) #ensure these methods are defined directly on class
assert_equal 1999, twz.year
assert_equal 12, twz.month
assert_equal 31, twz.day
View
25 activesupport/test/time_zone_test.rb
@@ -173,6 +173,14 @@ def test_parse
assert_equal zone, twz.time_zone
end
+ def test_parse_string_with_timezone
+ (-11..13).each do |timezone_offset|
+ zone = TimeZone[timezone_offset]
+ twz = zone.parse('1999-12-31 19:00:00')
+ assert_equal twz, zone.parse(twz.to_s)
+ end
+ end
+
def test_parse_with_old_date
silence_warnings do # silence warnings raised by tzinfo gem
zone = TimeZone['Eastern Time (US & Canada)']
@@ -181,6 +189,23 @@ def test_parse_with_old_date
assert_equal zone, twz.time_zone
end
end
+
+ def test_parse_far_future_date_with_time_zone_offset_in_string
+ silence_warnings do # silence warnings raised by tzinfo gem
+ zone = TimeZone['Eastern Time (US & Canada)']
+ twz = zone.parse('2050-12-31 19:00:00 -10:00') # i.e., 2050-01-01 05:00:00 UTC
+ assert_equal [0,0,0,1,1,2051], twz.to_a[0,6]
+ assert_equal zone, twz.time_zone
+ end
+ end
+
+ def test_parse_returns_nil_when_string_without_date_information_is_passed_in
+ silence_warnings do # silence warnings raised by tzinfo gem
+ zone = TimeZone['Eastern Time (US & Canada)']
+ assert_nil zone.parse('foobar')
+ assert_nil zone.parse(' ')
+ end
+ end
uses_mocha 'TestParseWithIncompleteDate' do
def test_parse_with_incomplete_date
View
2  railties/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* script/dbconsole fires up the command-line database client. #102 [Steve Purcell]
+
* Fix bug where plugin init.rb files from frozen gem specs weren't being run. (pjb3) [#122 state:resolved]
* Made the location of the routes file configurable with config.routes_configuration_file (Scott Fleckenstein) [#88]
View
3  railties/bin/dbconsole
@@ -0,0 +1,3 @@
+#!/usr/bin/env ruby
+require File.dirname(__FILE__) + '/../config/boot'
+require 'commands/dbconsole'
View
55 railties/lib/commands/dbconsole.rb
@@ -0,0 +1,55 @@
+require 'yaml'
+require 'optparse'
+
+OptionParser.new do |opt|
+ opt.banner = "Usage: dbconsole [environment]"
+ opt.parse!(ARGV)
+ abort opt.to_s unless (0..1).include?(ARGV.size)
+end
+
+env = ARGV.first || ENV['RAILS_ENV'] || 'development'
+unless config = YAML.load_file(RAILS_ROOT + "/config/database.yml")[env]
+ abort "No database is configured for the environment '#{env}'"
+end
+
+
+def find_cmd(*commands)
+ dirs_on_path = ENV['PATH'].to_s.split(File::PATH_SEPARATOR)
+ commands += commands.map{|cmd| "#{cmd}.exe"} if RUBY_PLATFORM =~ /win32/
+ commands.detect do |cmd|
+ dirs_on_path.detect do |path|
+ File.executable? File.join(path, cmd)
+ end
+ end || abort("Couldn't find database client: #{commands.join(', ')}. Check your $PATH and try again.")
+end
+
+case config["adapter"]
+when "mysql"
+ args = {
+ 'host' => '--host',
+ 'port' => '--port',
+ 'socket' => '--socket',
+ 'username' => '--user',
+ 'password' => '--password',
+ 'encoding' => '--default-character-set'
+ }.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact
+
+ args << config['database']
+
+ exec(find_cmd('mysql5', 'mysql'), *args)
+
+when "postgresql"
+ ENV['PGHOST'] = config["host"] if config["host"]
+ ENV['PGPORT'] = config["port"].to_s if config["port"]
+ ENV['PGPASSWORD'] = config["password"].to_s if config["password"]
+ exec(find_cmd('psql'), '-U', config["username"], config["database"])
+
+when "sqlite"
+ exec(find_cmd('sqlite'), config["database"])
+
+when "sqlite3"
+ exec(find_cmd('sqlite3'), config["database"])
+
+else
+ abort "Unknown command-line client for #{config['database']}. Submit a Rails patch to add support!"
+end
View
2  railties/lib/rails_generator/generators/applications/app/app_generator.rb
@@ -72,7 +72,7 @@ def manifest
m.file "environments/test.rb", "config/environments/test.rb"
# Scripts
- %w( about console destroy generate performance/benchmarker performance/profiler performance/request process/reaper process/spawner process/inspector runner server plugin ).each do |file|
+ %w( about console dbconsole destroy generate performance/benchmarker performance/profiler performance/request process/reaper process/spawner process/inspector runner server plugin ).each do |file|
m.file "bin/#{file}", "script/#{file}", script_options
end
Please sign in to comment.
Something went wrong with that request. Please try again.