diff options
author | Mehmet Emin İNAÇ <mehmetemininac@gmail.com> | 2016-02-14 01:59:11 +0200 |
---|---|---|
committer | Mehmet Emin İNAÇ <mehmetemininac@gmail.com> | 2016-03-08 14:56:00 +0200 |
commit | 0784dccc4fb8322ad88717efb31d37553542532b (patch) | |
tree | d6d72c74331ffa52ecae79acb13967683bcd0ffe /activerecord/test | |
parent | a61bf5f5b63780a3e0b4c2d4339967df82b370de (diff) | |
download | rails-0784dccc4fb8322ad88717efb31d37553542532b.tar.gz rails-0784dccc4fb8322ad88717efb31d37553542532b.tar.bz2 rails-0784dccc4fb8322ad88717efb31d37553542532b.zip |
Execute default_scope defined by abstract class within the scope of subclass
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/scoping/default_scoping_test.rb | 12 | ||||
-rw-r--r-- | activerecord/test/models/cat.rb | 13 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 5 |
3 files changed, 30 insertions, 0 deletions
diff --git a/activerecord/test/cases/scoping/default_scoping_test.rb b/activerecord/test/cases/scoping/default_scoping_test.rb index c918cbdef5..dcd09b6973 100644 --- a/activerecord/test/cases/scoping/default_scoping_test.rb +++ b/activerecord/test/cases/scoping/default_scoping_test.rb @@ -4,6 +4,7 @@ require 'models/comment' require 'models/developer' require 'models/computer' require 'models/vehicle' +require 'models/cat' class DefaultScopingTest < ActiveRecord::TestCase fixtures :developers, :posts, :comments @@ -485,4 +486,15 @@ class DefaultScopingTest < ActiveRecord::TestCase assert_equal 1, SubConditionalStiPost.all.to_a.size assert_equal 2, SubConditionalStiPost.unscope(where: :title).to_a.size end + + def test_with_abstract_class_scope_should_be_executed_in_correct_context + vegetarian_pattern, gender_pattern = if current_adapter?(:Mysql2Adapter) + [/`lions`.`is_vegetarian`/, /`lions`.`gender`/] + else + [/"lions"."is_vegetarian"/, /"lions"."gender"/] + end + + assert_match vegetarian_pattern, Lion.all.to_sql + assert_match gender_pattern, Lion.female.to_sql + end end diff --git a/activerecord/test/models/cat.rb b/activerecord/test/models/cat.rb new file mode 100644 index 0000000000..e543d3aadb --- /dev/null +++ b/activerecord/test/models/cat.rb @@ -0,0 +1,13 @@ +class Cat < ActiveRecord::Base + self.abstract_class = true + + enum gender: [:female, :male] + + scope :female, -> { where(gender: genders[:female]) } + scope :male, -> { where(gender: genders[:male]) } + + default_scope -> { where(is_vegetarian: false) } +end + +class Lion < Cat +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 2a8996f35c..01dbf2cee6 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -421,6 +421,11 @@ ActiveRecord::Schema.define do t.integer :amount end + create_table :lions, force: true do |t| + t.integer :gender + t.boolean :is_vegetarian, default: false + end + create_table :lock_without_defaults, force: true do |t| t.column :lock_version, :integer end |