diff options
11 files changed, 74 insertions, 96 deletions
diff --git a/actionpack/lib/action_controller/metal/strong_parameters.rb b/actionpack/lib/action_controller/metal/strong_parameters.rb index a78a24622d..d028c7d8c4 100644 --- a/actionpack/lib/action_controller/metal/strong_parameters.rb +++ b/actionpack/lib/action_controller/metal/strong_parameters.rb @@ -251,7 +251,7 @@ module ActionController end end - unpermitted_parameters!(params) + unpermitted_parameters!(params) if self.class.action_on_unpermitted_parameters params.permit! end diff --git a/actionpack/test/controller/parameters/nested_parameters_test.rb b/actionpack/test/controller/parameters/nested_parameters_test.rb index 8aec159499..91df527dec 100644 --- a/actionpack/test/controller/parameters/nested_parameters_test.rb +++ b/actionpack/test/controller/parameters/nested_parameters_test.rb @@ -16,7 +16,7 @@ class NestedParametersTest < ActiveSupport::TestCase }, { name: "Christopher Marlowe" }, { - :name => %w(malicious injected names) + name: %w(malicious injected names) }], details: { pages: 200, @@ -71,25 +71,25 @@ class NestedParametersTest < ActiveSupport::TestCase test "nested arrays with strings" do params = ActionController::Parameters.new({ - :book => { - :genres => ["Tragedy"] + book: { + genres: ["Tragedy"] } }) - permitted = params.permit :book => {:genres => []} + permitted = params.permit book: {genres: []} assert_equal ["Tragedy"], permitted[:book][:genres] end test "permit may specify symbols or strings" do params = ActionController::Parameters.new({ - :book => { - :title => "Romeo and Juliet", - :author => "William Shakespeare" + book: { + title: "Romeo and Juliet", + author: "William Shakespeare" }, - :magazine => "Shakespeare Today" + magazine: "Shakespeare Today" }) - permitted = params.permit({:book => ["title", :author]}, "magazine") + permitted = params.permit({book: ["title", :author]}, "magazine") assert_equal "Romeo and Juliet", permitted[:book][:title] assert_equal "William Shakespeare", permitted[:book][:author] assert_equal "Shakespeare Today", permitted[:magazine] @@ -132,15 +132,15 @@ class NestedParametersTest < ActiveSupport::TestCase test "fields_for-style nested params" do params = ActionController::Parameters.new({ - :book => { - :authors_attributes => { - :'0' => { :name => 'William Shakespeare', :age_of_death => '52' }, - :'1' => { :name => 'Unattributed Assistant' }, - :'2' => { :name => %w(injected names)} + book: { + authors_attributes: { + :'0' => { name: 'William Shakespeare', age_of_death: '52' }, + :'1' => { name: 'Unattributed Assistant' }, + :'2' => { name: %w(injected names)} } } }) - permitted = params.permit :book => { :authors_attributes => [ :name ] } + permitted = params.permit book: { authors_attributes: [ :name ] } assert_not_nil permitted[:book][:authors_attributes]['0'] assert_not_nil permitted[:book][:authors_attributes]['1'] @@ -153,14 +153,14 @@ class NestedParametersTest < ActiveSupport::TestCase test "fields_for-style nested params with negative numbers" do params = ActionController::Parameters.new({ - :book => { - :authors_attributes => { - :'-1' => { :name => 'William Shakespeare', :age_of_death => '52' }, - :'-2' => { :name => 'Unattributed Assistant' } + book: { + authors_attributes: { + :'-1' => { name: 'William Shakespeare', age_of_death: '52' }, + :'-2' => { name: 'Unattributed Assistant' } } } }) - permitted = params.permit :book => { :authors_attributes => [:name] } + permitted = params.permit book: { authors_attributes: [:name] } assert_not_nil permitted[:book][:authors_attributes]['-1'] assert_not_nil permitted[:book][:authors_attributes]['-2'] diff --git a/actionpack/test/controller/parameters/parameters_permit_test.rb b/actionpack/test/controller/parameters/parameters_permit_test.rb index 303c13eb25..734662ae11 100644 --- a/actionpack/test/controller/parameters/parameters_permit_test.rb +++ b/actionpack/test/controller/parameters/parameters_permit_test.rb @@ -20,7 +20,7 @@ class ParametersPermitTest < ActiveSupport::TestCase end test 'if nothing is permitted, the hash becomes empty' do - params = ActionController::Parameters.new(:id => '1234') + params = ActionController::Parameters.new(id: '1234') permitted = params.permit assert permitted.permitted? assert permitted.empty? @@ -34,7 +34,7 @@ class ParametersPermitTest < ActiveSupport::TestCase values += [StringIO.new] values.each do |value| - params = ActionController::Parameters.new(:id => value) + params = ActionController::Parameters.new(id: value) permitted = params.permit(:id) assert_equal value, permitted[:id] @@ -47,7 +47,7 @@ class ParametersPermitTest < ActiveSupport::TestCase end test 'key: unknown keys are filtered out' do - params = ActionController::Parameters.new(:id => '1234', :injected => 'injected') + params = ActionController::Parameters.new(id: '1234', injected: 'injected') permitted = params.permit(:id) assert_equal '1234', permitted[:id] assert_filtered_out permitted, :injected @@ -55,7 +55,7 @@ class ParametersPermitTest < ActiveSupport::TestCase test 'key: arrays are filtered out' do [[], [1], ['1']].each do |array| - params = ActionController::Parameters.new(:id => array) + params = ActionController::Parameters.new(id: array) permitted = params.permit(:id) assert_filtered_out permitted, :id @@ -68,8 +68,8 @@ class ParametersPermitTest < ActiveSupport::TestCase end test 'key: hashes are filtered out' do - [{}, {:foo => 1}, {:foo => 'bar'}].each do |hash| - params = ActionController::Parameters.new(:id => hash) + [{}, {foo: 1}, {foo: 'bar'}].each do |hash| + params = ActionController::Parameters.new(id: hash) permitted = params.permit(:id) assert_filtered_out permitted, :id @@ -82,7 +82,7 @@ class ParametersPermitTest < ActiveSupport::TestCase end test 'key: non-permitted scalar values are filtered out' do - params = ActionController::Parameters.new(:id => Object.new) + params = ActionController::Parameters.new(id: Object.new) permitted = params.permit(:id) assert_filtered_out permitted, :id @@ -94,37 +94,37 @@ class ParametersPermitTest < ActiveSupport::TestCase end test 'key: it is not assigned if not present in params' do - params = ActionController::Parameters.new(:name => 'Joe') + params = ActionController::Parameters.new(name: 'Joe') permitted = params.permit(:id) assert !permitted.has_key?(:id) end test 'key to empty array: empty arrays pass' do - params = ActionController::Parameters.new(:id => []) - permitted = params.permit(:id => []) + params = ActionController::Parameters.new(id: []) + permitted = params.permit(id: []) assert_equal [], permitted[:id] end test 'key to empty array: arrays of permitted scalars pass' do [['foo'], [1], ['foo', 'bar'], [1, 2, 3]].each do |array| - params = ActionController::Parameters.new(:id => array) - permitted = params.permit(:id => []) + params = ActionController::Parameters.new(id: array) + permitted = params.permit(id: []) assert_equal array, permitted[:id] end end test 'key to empty array: permitted scalar values do not pass' do ['foo', 1].each do |permitted_scalar| - params = ActionController::Parameters.new(:id => permitted_scalar) - permitted = params.permit(:id => []) + params = ActionController::Parameters.new(id: permitted_scalar) + permitted = params.permit(id: []) assert_filtered_out permitted, :id end end test 'key to empty array: arrays of non-permitted scalar do not pass' do - [[Object.new], [[]], [[1]], [{}], [{:id => '1'}]].each do |non_permitted_scalar| - params = ActionController::Parameters.new(:id => non_permitted_scalar) - permitted = params.permit(:id => []) + [[Object.new], [[]], [[1]], [{}], [{id: '1'}]].each do |non_permitted_scalar| + params = ActionController::Parameters.new(id: non_permitted_scalar) + permitted = params.permit(id: []) assert_filtered_out permitted, :id end end diff --git a/activerecord/activerecord.gemspec b/activerecord/activerecord.gemspec index bfc2e54aba..d523c1eca1 100644 --- a/activerecord/activerecord.gemspec +++ b/activerecord/activerecord.gemspec @@ -25,5 +25,5 @@ Gem::Specification.new do |s| s.add_dependency 'activemodel', version s.add_dependency 'arel', '~> 3.0.2' - s.add_dependency 'activerecord-deprecated_finders', '0.0.2' + s.add_dependency 'activerecord-deprecated_finders', '~> 0.0.3' end diff --git a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb index 47a8b576c0..7701001da9 100644 --- a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb +++ b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb @@ -39,14 +39,11 @@ module ActiveRecord unless time.acts_like?(:time) time = time.is_a?(String) ? Time.zone.parse(time) : time.to_time rescue time end - zoned_time = time && time.in_time_zone rescue nil - rounded_time = round_usec(zoned_time) - rounded_value = round_usec(read_attribute("#{attr_name}")) - if (rounded_value != rounded_time) || (!rounded_value && original_time) - write_attribute("#{attr_name}", original_time) - #{attr_name}_will_change! - @attributes_cache["#{attr_name}"] = zoned_time - end + time = time.in_time_zone rescue nil if time + changed = read_attribute(:#{attr_name}) != time + write_attribute(:#{attr_name}, original_time) + #{attr_name}_will_change! if changed + @attributes_cache["#{attr_name}"] = time end EOV generated_attribute_methods.module_eval(method_body, __FILE__, line) @@ -62,11 +59,6 @@ module ActiveRecord [:datetime, :timestamp].include?(column.type) end end - - private - def round_usec(value) - value.change(usec: 0) if value - end end end end diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index b9961a4420..4ac82f6880 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -551,18 +551,17 @@ class DirtyTest < ActiveRecord::TestCase end end - def test_setting_time_attributes_with_time_zone_field_to_same_time_should_not_be_marked_as_a_change + def test_datetime_attribute_can_be_updated_with_fractional_seconds in_time_zone 'Paris' do target = Class.new(ActiveRecord::Base) - target.table_name = 'pirates' + target.table_name = 'topics' - created_on = Time.now + written_on = Time.utc(2012, 12, 1, 12, 0, 0).in_time_zone('Paris') - pirate = target.create(:created_on => created_on) - pirate.reload # Here mysql truncate the usec value to 0 + topic = target.create(:written_on => written_on) + topic.written_on += 0.3 - pirate.created_on = created_on - assert !pirate.created_on_changed? + assert topic.written_on_changed?, 'Fractional second update not detected' end end diff --git a/activesupport/lib/active_support/railtie.rb b/activesupport/lib/active_support/railtie.rb index 72ac597d99..133aa6a054 100644 --- a/activesupport/lib/active_support/railtie.rb +++ b/activesupport/lib/active_support/railtie.rb @@ -13,6 +13,20 @@ module ActiveSupport end end + # Sets the default value for Time.zone + # If assigned value cannot be matched to a TimeZone, an exception will be raised. + initializer "active_support.initialize_time_zone" do |app| + require 'active_support/core_ext/time/zones' + zone_default = Time.find_zone!(app.config.time_zone) + + unless zone_default + raise 'Value assigned to config.time_zone not recognized. ' \ + 'Run "rake -D time" for a list of tasks for finding appropriate time zone names.' + end + + Time.zone_default = zone_default + end + # Sets the default week start # If assigned value is not a valid day symbol (e.g. :sunday, :monday, ...), an exception will be raised. initializer "active_support.initialize_beginning_of_week" do |app| @@ -28,21 +42,5 @@ module ActiveSupport ActiveSupport.send(k, v) if ActiveSupport.respond_to? k end end - - # Sets the default value for Time.zone after initialization since the default configuration - # lives in application initializers. - # If assigned value cannot be matched to a TimeZone, an exception will be raised. - config.after_initialize do |app| - require 'active_support/core_ext/time/zones' - zone_default = Time.find_zone!(app.config.time_zone) - - unless zone_default - raise 'Value assigned to config.time_zone not recognized. ' \ - 'Run "rake -D time" for a list of tasks for finding appropriate time zone names.' - end - - Time.zone_default = zone_default - end - end end diff --git a/guides/source/i18n.md b/guides/source/i18n.md index 69232d9bd4..5304ca4285 100644 --- a/guides/source/i18n.md +++ b/guides/source/i18n.md @@ -102,7 +102,7 @@ The **translations load path** (`I18n.load_path`) is just a Ruby Array of paths NOTE: The backend will lazy-load these translations when a translation is looked up for the first time. This makes it possible to just swap the backend with something else even after translations have already been announced. -The default initializer `locale.rb` file has instructions on how to add locales from another directory and how to set a different default locale. Just uncomment and edit the specific lines. +The default `application.rb` files has instructions on how to add locales from another directory and how to set a different default locale. Just uncomment and edit the specific lines. ```ruby # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. diff --git a/railties/lib/rails/generators/rails/app/templates/config/application.rb b/railties/lib/rails/generators/rails/app/templates/config/application.rb index f5d7d698a3..d149413e2e 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/application.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/application.rb @@ -22,6 +22,14 @@ module <%= app_const_base %> # Custom directories with classes and modules you want to be autoloadable. # config.autoload_paths += %W(#{config.root}/extras) + + # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. + # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. + # config.time_zone = 'Central Time (US & Canada)' + + # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. + # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] + # config.i18n.default_locale = :de <% if options.skip_sprockets? -%> # Disable the asset pipeline. diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/locale.rb b/railties/lib/rails/generators/rails/app/templates/config/initializers/locale.rb deleted file mode 100644 index d89dac7c6a..0000000000 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/locale.rb +++ /dev/null @@ -1,9 +0,0 @@ -# Be sure to restart your server when you modify this file. - -# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. -# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. -# Rails.application.config.time_zone = 'Central Time (US & Canada)' - -# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. -# Rails.application.config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] -# Rails.application.config.i18n.default_locale = :de diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 78aefa1437..7b45623f6c 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -417,17 +417,7 @@ module ApplicationTests require "#{app_path}/config/environment" - assert_equal Time.find_zone!("Wellington"), Time.zone_default - end - - test "timezone can be set on initializers" do - app_file "config/initializers/locale.rb", <<-RUBY - Rails.application.config.time_zone = "Central Time (US & Canada)" - RUBY - - require "#{app_path}/config/environment" - - assert_equal Time.find_zone!("Central Time (US & Canada)"), Time.zone_default + assert_equal "Wellington", Rails.application.config.time_zone end test "raises when an invalid timezone is defined in the config" do |