diff options
17 files changed, 151 insertions, 83 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 8e1820e810..8298a199d8 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,3 +1,23 @@ +* Explicitly ignored wildcard verbs when searching for HEAD routes before fallback + + Fixes an issue where a mounted rack app at root would intercept the HEAD + request causing an incorrect behavior during the fall back to GET requests. + + Example: + ```ruby + draw do + get '/home' => 'test#index' + mount rack_app, at: '/' + end + head '/home' + assert_response :success + ``` + In this case, a HEAD request runs through the routes the first time and fails + to match anything. Then, it runs through the list with the fallback and matches + `get '/home'`. The original behavior would match the rack app in the first pass. + + *Terence Sun* + * Migrating xhr methods to keyword arguments syntax in `ActionController::TestCase` and `ActionDispatch::Integration` diff --git a/actionpack/lib/action_dispatch/journey/router.rb b/actionpack/lib/action_dispatch/journey/router.rb index 2b036796ab..e9df984c86 100644 --- a/actionpack/lib/action_dispatch/journey/router.rb +++ b/actionpack/lib/action_dispatch/journey/router.rb @@ -121,6 +121,7 @@ module ActionDispatch end def match_head_routes(routes, req) + routes.delete_if { |route| route.verb == // } head_routes = match_routes(routes, req) if head_routes.empty? diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index 3a95a9025f..ca5de05814 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -3477,6 +3477,18 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest assert_equal '/post/comments/new', new_comment_path end + def test_head_fetch_with_mount_on_root + draw do + get '/home' => 'test#index' + mount lambda { |env| [404, {"Content-Type" => "text/html"}, ["testing"]] }, at: '/' + end + head '/home' + assert_response :success + + head '/' + assert_response :not_found + end + private def draw(&block) diff --git a/activejob/CHANGELOG.md b/activejob/CHANGELOG.md index 05ece5ddb2..d4e19274fa 100644 --- a/activejob/CHANGELOG.md +++ b/activejob/CHANGELOG.md @@ -1,3 +1,36 @@ +* Add an `:only` option to `perform_enqueued_jobs` to filter jobs based on + type. + + This allows specific jobs to be tested, while preventing others from + being performed unnecessarily. + + Example: + + def test_hello_job + assert_performed_jobs 1, only: HelloJob do + HelloJob.perform_later('jeremy') + LoggingJob.perform_later + end + end + + An array may also be specified, to support testing multiple jobs. + + Example: + + def test_hello_and_logging_jobs + assert_nothing_raised do + assert_performed_jobs 2, only: [HelloJob, LoggingJob] do + HelloJob.perform_later('jeremy') + LoggingJob.perform_later('stewie') + RescueJob.perform_later('david') + end + end + end + + Fixes #18802. + + *Michael Ryan* + * Allow keyword arguments to be used with Active Job. Fixes #18741. @@ -44,39 +77,4 @@ *Isaac Seymour* -* Add an `:only` option to `perform_enqueued_jobs` to filter jobs based on - type. - - This allows specific jobs to be tested, while preventing others from - being performed unnecessarily. - - Example: - - def test_hello_job - assert_performed_jobs 1, only: HelloJob do - HelloJob.perform_later('jeremy') - LoggingJob.perform_later - end - end - - An array may also be specified, to support testing multiple jobs. - - Example: - - def test_hello_and_logging_jobs - assert_nothing_raised do - assert_performed_jobs 2, only: [HelloJob, LoggingJob] do - HelloJob.perform_later('jeremy') - LoggingJob.perform_later('stewie') - RescueJob.perform_later('david') - end - end - end - - Fixes #18802. - - *Michael Ryan* - - - Please check [4-2-stable](https://github.com/rails/rails/blob/4-2-stable/activejob/CHANGELOG.md) for previous changes. diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 46d10d5bff..1470c6dec1 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,18 @@ +* Fix rounding problem for PostgreSQL timestamp column. + + If timestamp column have the precision, it need to format according to + the precision of timestamp column. + + *Ryuta Kamizono* + +* Respect the database default charset for `schema_migrations` table. + + The charset of `version` column in `schema_migrations` table is depend + on the database default charset and collation rather than the encoding + of the connection. + + *Ryuta Kamizono* + * Raise `ArgumentError` when passing `nil` or `false` to `Relation#merge`. These are not valid values to merge in a relation so it should warn the users diff --git a/activerecord/lib/active_record/attributes.rb b/activerecord/lib/active_record/attributes.rb index 70b8c1c635..2c475f3cda 100644 --- a/activerecord/lib/active_record/attributes.rb +++ b/activerecord/lib/active_record/attributes.rb @@ -147,7 +147,7 @@ module ActiveRecord # # +type_cast_from_user+. Assumed to be in instance of +Money+ in # # this case. # def type_cast_for_database(value) - # value_in_bitcoins = currency_converter.convert_to_bitcoins(value) + # value_in_bitcoins = @currency_converter.convert_to_bitcoins(value) # value_in_bitcoins.amount # end # end diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 61bac6741f..c29692d6ca 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -63,7 +63,7 @@ module ActiveRecord def column_spec_for_primary_key(column) spec = {} - if column.extra == 'auto_increment' + if column.auto_increment? return unless column.limit == 8 spec[:id] = ':bigint' else @@ -103,6 +103,10 @@ module ActiveRecord collation && !collation.match(/_ci$/) end + def auto_increment? + extra == 'auto_increment' + end + private # MySQL misreports NOT NULL column default when none is given. @@ -808,7 +812,7 @@ module ActiveRecord options = { default: column.default, null: column.null, - auto_increment: column.extra == "auto_increment" + auto_increment: column.auto_increment? } current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'", 'SCHEMA')["Type"] @@ -913,18 +917,10 @@ module ActiveRecord end class MysqlDateTime < Type::DateTime # :nodoc: - def type_cast_for_database(value) - if value.acts_like?(:time) && value.respond_to?(:usec) - result = super.to_s(:db) - case precision - when 1..6 - "#{result}.#{sprintf("%0#{precision}d", value.usec / 10 ** (6 - precision))}" - else - result - end - else - super - end + private + + def has_precision? + precision || 0 end end diff --git a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb index 75f244b3f3..fac6f81540 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb @@ -39,7 +39,7 @@ module ActiveRecord MAX_INDEX_LENGTH_FOR_UTF8MB4 = 191 def initialize_schema_migrations_table - if @config[:encoding] == 'utf8mb4' + if charset == 'utf8mb4' ActiveRecord::SchemaMigration.create_table(MAX_INDEX_LENGTH_FOR_UTF8MB4) else ActiveRecord::SchemaMigration.create_table diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/date_time.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/date_time.rb index b9e7894e5c..2fe61eeb77 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/date_time.rb @@ -5,6 +5,15 @@ module ActiveRecord class DateTime < Type::DateTime # :nodoc: include Infinity + def type_cast_for_database(value) + if has_precision? && value.acts_like?(:time) && value.year <= 0 + bce_year = format("%04d", -value.year + 1) + super.sub(/^-?\d+/, bce_year) + " BC" + else + super + end + end + def cast_value(value) if value.is_a?(::String) case value diff --git a/activerecord/lib/active_record/type/date_time.rb b/activerecord/lib/active_record/type/date_time.rb index 5646ee61db..e8614b16e0 100644 --- a/activerecord/lib/active_record/type/date_time.rb +++ b/activerecord/lib/active_record/type/date_time.rb @@ -11,21 +11,28 @@ module ActiveRecord end def type_cast_for_database(value) + return super unless value.acts_like?(:time) + zone_conversion_method = ActiveRecord::Base.default_timezone == :utc ? :getutc : :getlocal - if value.acts_like?(:time) - if value.respond_to?(zone_conversion_method) - value.send(zone_conversion_method) - else - value - end + if value.respond_to?(zone_conversion_method) + value = value.send(zone_conversion_method) + end + + return value unless has_precision? + + result = value.to_s(:db) + if value.respond_to?(:usec) && (1..6).cover?(precision) + "#{result}.#{sprintf("%0#{precision}d", value.usec / 10 ** (6 - precision))}" else - super + result end end private + alias has_precision? precision + def cast_value(string) return string unless string.is_a?(::String) return if string.empty? diff --git a/activerecord/test/cases/adapters/mysql2/schema_migrations_test.rb b/activerecord/test/cases/adapters/mysql2/schema_migrations_test.rb index 06b0cb5515..271b570eb5 100644 --- a/activerecord/test/cases/adapters/mysql2/schema_migrations_test.rb +++ b/activerecord/test/cases/adapters/mysql2/schema_migrations_test.rb @@ -18,21 +18,29 @@ module ActiveRecord smtn = ActiveRecord::Migrator.schema_migrations_table_name connection.drop_table smtn, if_exists: true - config = connection.instance_variable_get(:@config) - original_encoding = config[:encoding] + database_name = connection.current_database + database_info = connection.select_one("SELECT * FROM information_schema.schemata WHERE schema_name = '#{database_name}'") + + original_charset = database_info["DEFAULT_CHARACTER_SET_NAME"] + original_collation = database_info["DEFAULT_COLLATION_NAME"] + + execute("ALTER DATABASE #{database_name} DEFAULT CHARACTER SET utf8mb4") - config[:encoding] = 'utf8mb4' connection.initialize_schema_migrations_table assert connection.column_exists?(smtn, :version, :string, limit: Mysql2Adapter::MAX_INDEX_LENGTH_FOR_UTF8MB4) ensure - config[:encoding] = original_encoding + execute("ALTER DATABASE #{database_name} DEFAULT CHARACTER SET #{original_charset} COLLATE #{original_collation}") end private def connection @connection ||= ActiveRecord::Base.connection end + + def execute(sql) + connection.execute(sql) + end end end end diff --git a/activerecord/test/cases/adapters/postgresql/array_test.rb b/activerecord/test/cases/adapters/postgresql/array_test.rb index 19ebef4981..3d5b7e5137 100644 --- a/activerecord/test/cases/adapters/postgresql/array_test.rb +++ b/activerecord/test/cases/adapters/postgresql/array_test.rb @@ -40,7 +40,6 @@ class PostgresqlArrayTest < ActiveRecord::TestCase ratings_column = PgArray.columns_hash['ratings'] assert_equal :integer, ratings_column.type - type = PgArray.type_for_attribute("ratings") assert ratings_column.array? end diff --git a/activerecord/test/cases/adapters/postgresql/timestamp_test.rb b/activerecord/test/cases/adapters/postgresql/timestamp_test.rb index eb32c4d2c2..8246b14b93 100644 --- a/activerecord/test/cases/adapters/postgresql/timestamp_test.rb +++ b/activerecord/test/cases/adapters/postgresql/timestamp_test.rb @@ -46,6 +46,8 @@ end class TimestampTest < ActiveRecord::TestCase fixtures :topics + class Foo < ActiveRecord::Base; end + def test_group_by_date keys = Topic.group("date_trunc('month', created_at)").count.keys assert_operator keys.length, :>, 0 @@ -135,6 +137,20 @@ class TimestampTest < ActiveRecord::TestCase assert_equal date, Developer.find_by_name("yahagi").updated_at end + def test_formatting_timestamp_according_to_precision + ActiveRecord::Base.connection.create_table(:foos, force: true) do |t| + t.datetime :created_at, precision: 0 + t.datetime :updated_at, precision: 4 + end + date = ::Time.utc(2014, 8, 17, 12, 30, 0, 999999) + Foo.create!(created_at: date, updated_at: date) + assert foo = Foo.find_by(created_at: date) + assert_equal date.to_s, foo.created_at.to_s + assert_equal date.to_s, foo.updated_at.to_s + assert_equal 000000, foo.created_at.usec + assert_equal 999900, foo.updated_at.usec + end + private def pg_datetime_precision(table_name, column_name) diff --git a/activerecord/test/cases/migration/columns_test.rb b/activerecord/test/cases/migration/columns_test.rb index 54d3a729c3..6f65288ac0 100644 --- a/activerecord/test/cases/migration/columns_test.rb +++ b/activerecord/test/cases/migration/columns_test.rb @@ -65,7 +65,7 @@ module ActiveRecord if current_adapter?(:MysqlAdapter, :Mysql2Adapter) def test_mysql_rename_column_preserves_auto_increment rename_column "test_models", "id", "id_test" - assert_equal "auto_increment", connection.columns("test_models").find { |c| c.name == "id_test" }.extra + assert connection.columns("test_models").find { |c| c.name == "id_test" }.auto_increment? TestModel.reset_column_information ensure rename_column "test_models", "id_test", "id" diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 5ca3f91cf0..bafc9fa81b 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -78,11 +78,6 @@ class SchemaDumperTest < ActiveRecord::TestCase end end.compact - if lengths.uniq.length != 1 - p lengths.uniq.length - puts column_set - end - assert_equal 1, lengths.uniq.length end end diff --git a/railties/lib/rails/generators/rails/plugin/plugin_generator.rb b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb index ab050fc246..68c3829515 100644 --- a/railties/lib/rails/generators/rails/plugin/plugin_generator.rb +++ b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb @@ -317,9 +317,9 @@ task default: :test @modules ||= namespaced_name.camelize.split("::") end - def wrap_in_modules(content) - content = "#{content}".strip.gsub(/\W$\n/, '') - modules.reverse.inject(content) do |content, mod| + def wrap_in_modules(unwrapped_code) + unwrapped_code = "#{unwrapped_code}".strip.gsub(/\W$\n/, '') + modules.reverse.inject(unwrapped_code) do |content, mod| str = "module #{mod}\n" str += content.lines.map { |line| " #{line}" }.join str += content.present? ? "\nend" : "end" diff --git a/railties/test/generators/named_base_test.rb b/railties/test/generators/named_base_test.rb index 4199e00b0d..18a26fde05 100644 --- a/railties/test/generators/named_base_test.rb +++ b/railties/test/generators/named_base_test.rb @@ -2,16 +2,6 @@ require 'generators/generators_test_helper' require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator' require 'mocha/setup' # FIXME: stop using mocha -# Mock out what we need from AR::Base. -module ActiveRecord - class Base - class << self - attr_accessor :pluralize_table_names - end - self.pluralize_table_names = true - end -end - class NamedBaseTest < Rails::Generators::TestCase include GeneratorsTestHelper tests Rails::Generators::ScaffoldControllerGenerator @@ -59,11 +49,13 @@ class NamedBaseTest < Rails::Generators::TestCase end def test_named_generator_attributes_without_pluralized + original_pluralize_table_names = ActiveRecord::Base.pluralize_table_names ActiveRecord::Base.pluralize_table_names = false + g = generator ['admin/foo'] assert_name g, 'admin_foo', :table_name ensure - ActiveRecord::Base.pluralize_table_names = true + ActiveRecord::Base.pluralize_table_names = original_pluralize_table_names end def test_scaffold_plural_names |