diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2014-01-13 16:16:19 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2014-01-13 16:16:19 -0800 |
commit | bf48af53600e3e535cc4d0cfe9654c40023a7f70 (patch) | |
tree | 94db1f852c79c75d100f627615c2ba9cb8d440ec /activerecord | |
parent | aec4aa1702753f29ae79787c743f6f047ad9a80d (diff) | |
parent | f1764a2de17bba07bad3ec0d88de152d8279d3bb (diff) | |
download | rails-bf48af53600e3e535cc4d0cfe9654c40023a7f70.tar.gz rails-bf48af53600e3e535cc4d0cfe9654c40023a7f70.tar.bz2 rails-bf48af53600e3e535cc4d0cfe9654c40023a7f70.zip |
Merge branch 'master' into set_binds
* master: (24 commits)
unscope should remove bind values associated with the where
reverse_order_value= is not private, so no need to send
avoid more dynamic symbols
no need to to_sym
recover from test runs that leave the database in a bad state
updates screenshot data [ci skip]
"serie" => "series"
revises a few things in the getting started guide [ci skip]
Favor canonical environment variables for secrets
removed extra comma [ci skip]
Only lookup `config.log_level` for stdlib `::Logger`. Closes #11665.
Updated Changelog to reflect removal of :dependent => :restrict
standardize on jruby_skip & rbx_skip
fix bug in becomes! when changing from base to subclass. Closes #13272.
highlight http://localhost:3000 in README.md. Closes #13643. [ci skip]
doc proc/lambda arg on inclusion validation. Closes #13689. [ci skip]
Skip Spring App Generator tests on JRuby
fixes a typo in a CHANGELOG
upgrade SDoc
fixes the Gemfile generator templates
...
Conflicts:
activerecord/test/cases/hot_compatibility_test.rb
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/enum.rb | 3 | ||||
-rw-r--r-- | activerecord/lib/active_record/persistence.rb | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 10 | ||||
-rw-r--r-- | activerecord/test/cases/enum_test.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/hot_compatibility_test.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/inheritance_test.rb | 11 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 9 |
8 files changed, 45 insertions, 6 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index d9f8ee7097..f57158f38e 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,9 @@ +* Fix bug in `becomes!` when changing from the base model to a STI sub-class. + + Fixes #13272. + + *the-web-dev*, *Yves Senn* + * Currently Active Record can be configured via the environment variable `DATABASE_URL` or by manually injecting a hash of values which is what Rails does, reading in `database.yml` and setting Active Record appropriately. Active Record diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index 9ad718a02a..d1bc785bee 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -87,6 +87,9 @@ module ActiveRecord # def status() STATUS.key self[:status] end define_method(name) { enum_values.key self[name] } + # def status_before_type_cast() STATUS.key self[:status] end + define_method("#{name}_before_type_cast") { enum_values.key self[name] } + pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index pairs.each do |value, i| enum_values[value] = i diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 4669c072a1..460fbdb3f8 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -196,7 +196,11 @@ module ActiveRecord # share the same set of attributes. def becomes!(klass) became = becomes(klass) - became.public_send("#{klass.inheritance_column}=", klass.sti_name) unless self.class.descends_from_active_record? + sti_type = nil + if !klass.descends_from_active_record? + sti_type = klass.sti_name + end + became.public_send("#{klass.inheritance_column}=", sti_type) became end diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 7571208a88..0f5fe7ea51 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -858,11 +858,11 @@ module ActiveRecord end single_val_method = Relation::SINGLE_VALUE_METHODS.include?(scope) - unscope_code = :"#{scope}_value#{'s' unless single_val_method}=" + unscope_code = "#{scope}_value#{'s' unless single_val_method}=" case scope when :order - self.send(:reverse_order_value=, false) + self.reverse_order_value = false result = [] else result = [] unless single_val_method @@ -872,17 +872,19 @@ module ActiveRecord end def where_unscoping(target_value) - target_value_sym = target_value.to_sym + target_value = target_value.to_s where_values.reject! do |rel| case rel when Arel::Nodes::In, Arel::Nodes::NotIn, Arel::Nodes::Equality, Arel::Nodes::NotEqual subrelation = (rel.left.kind_of?(Arel::Attributes::Attribute) ? rel.left : rel.right) - subrelation.name.to_sym == target_value_sym + subrelation.name == target_value else raise "unscope(where: #{target_value.inspect}) failed: unscoping #{rel.class} is unimplemented." end end + + bind_values.reject! { |col,_| col.name == target_value } end def custom_join_ast(table, joins) diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb index c09e58fbf1..0075df8b8d 100644 --- a/activerecord/test/cases/enum_test.rb +++ b/activerecord/test/cases/enum_test.rb @@ -88,4 +88,8 @@ class EnumTest < ActiveRecord::TestCase assert Book.written.create.written? assert Book.read.create.read? end + + test "_before_type_cast returns the enum label (required for form fields)" do + assert_equal "proposed", @book.status_before_type_cast + end end diff --git a/activerecord/test/cases/hot_compatibility_test.rb b/activerecord/test/cases/hot_compatibility_test.rb index e671979bcf..367d04a154 100644 --- a/activerecord/test/cases/hot_compatibility_test.rb +++ b/activerecord/test/cases/hot_compatibility_test.rb @@ -5,7 +5,7 @@ class HotCompatibilityTest < ActiveRecord::TestCase setup do @klass = Class.new(ActiveRecord::Base) do - connection.create_table :hot_compatibilities, :force => true do |t| + connection.create_table :hot_compatibilities, force: true do |t| t.string :foo t.string :bar end diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb index cb0d374ef7..7fd7d42354 100644 --- a/activerecord/test/cases/inheritance_test.rb +++ b/activerecord/test/cases/inheritance_test.rb @@ -128,6 +128,17 @@ class InheritanceTest < ActiveRecord::TestCase assert_kind_of Cabbage, cabbage end + def test_alt_becomes_bang_resets_inheritance_type_column + vegetable = Vegetable.create!(name: "Red Pepper") + assert_nil vegetable.custom_type + + cabbage = vegetable.becomes!(Cabbage) + assert_equal "Cabbage", cabbage.custom_type + + vegetable = cabbage.becomes!(Vegetable) + assert_nil cabbage.custom_type + end + def test_inheritance_find_all companies = Company.all.merge!(:order => 'id').to_a assert_kind_of Firm, companies[0], "37signals should be a firm" diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 031da8e6d6..a0896f7f8d 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -1524,6 +1524,15 @@ class RelationTest < ActiveRecord::TestCase assert merged.to_sql.include?("bbq") end + def test_unscope_removes_binds + left = Post.where(id: Arel::Nodes::BindParam.new('?')) + column = Post.columns_hash['id'] + left.bind_values += [[column, 20]] + + relation = left.unscope(where: :id) + assert_equal [], relation.bind_values + end + def test_merging_removes_rhs_bind_parameters left = Post.where(id: Arel::Nodes::BindParam.new('?')) column = Post.columns_hash['id'] |