aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2012-09-02 17:19:50 +0200
committerYves Senn <yves.senn@garaio.com>2012-09-03 16:35:38 +0200
commit01d09a6079b0af387c722ba2148cf7af308d3f49 (patch)
treebee01cb37bfa634b2348d5ede8e8e68d43d6876e /activerecord
parentcabab374bfc6c1bd1d783ecc3c674047f17523c6 (diff)
downloadrails-01d09a6079b0af387c722ba2148cf7af308d3f49.tar.gz
rails-01d09a6079b0af387c722ba2148cf7af308d3f49.tar.bz2
rails-01d09a6079b0af387c722ba2148cf7af308d3f49.zip
set the configured #inheritance_column on #become (#7503)
I had to create a new table because I needed an STI table, which does not have both a "type" and a "custom_type" the test fails with: 1) Error: test_alt_becomes_works_with_sti(InheritanceTest): NoMethodError: undefined method `type=' for #<Cabbage id: 1, name: "my cucumber", custom_type: "Cucumber"> /Users/username/Projects/rails/activemodel/lib/active_model/attribute_methods.rb:432:in `method_missing' /Users/username/Projects/rails/activerecord/lib/active_record/attribute_methods.rb:100:in `method_missing' /Users/username/Projects/rails/activerecord/lib/active_record/persistence.rb:165:in `becomes' test/cases/inheritance_test.rb:134:in `test_becomes_works_with_sti' test/cases/inheritance_test.rb:140:in `test_alt_becomes_works_with_sti' Conflicts: activerecord/test/cases/inheritance_test.rb
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md4
-rw-r--r--activerecord/lib/active_record/persistence.rb2
-rw-r--r--activerecord/test/cases/inheritance_test.rb10
-rw-r--r--activerecord/test/fixtures/vegetables.yml9
-rw-r--r--activerecord/test/models/vegetables.rb14
-rw-r--r--activerecord/test/schema/schema.rb5
6 files changed, 42 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 3c1a400cc6..c836886c08 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,5 +1,9 @@
## Rails 3.2.9 (unreleased)
+* Fix `becomes` when using a configured `inheritance_column`.
+
+ *Yves Senn*
+
* Fix `reset_counters` when there are multiple `belongs_to` association with the
same foreign key and one of them have a counter cache.
Fixes #5200.
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb
index 038355deaa..8a3c3fed7d 100644
--- a/activerecord/lib/active_record/persistence.rb
+++ b/activerecord/lib/active_record/persistence.rb
@@ -162,7 +162,7 @@ module ActiveRecord
became.instance_variable_set("@new_record", new_record?)
became.instance_variable_set("@destroyed", destroyed?)
became.instance_variable_set("@errors", errors)
- became.type = klass.name unless self.class.descends_from_active_record?
+ became.send("#{klass.inheritance_column}=", klass.name) unless self.class.descends_from_active_record?
became
end
diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb
index fab858e09c..54c9152c06 100644
--- a/activerecord/test/cases/inheritance_test.rb
+++ b/activerecord/test/cases/inheritance_test.rb
@@ -2,9 +2,10 @@ require "cases/helper"
require 'models/company'
require 'models/project'
require 'models/subscriber'
+require 'models/vegetables'
class InheritanceTest < ActiveRecord::TestCase
- fixtures :companies, :projects, :subscribers, :accounts
+ fixtures :companies, :projects, :subscribers, :accounts, :vegetables
def test_class_with_store_full_sti_class_returns_full_name
old = ActiveRecord::Base.store_full_sti_class
@@ -98,6 +99,13 @@ class InheritanceTest < ActiveRecord::TestCase
switch_to_default_inheritance_column
end
+ def test_alt_becomes_works_with_sti
+ vegetable = Vegetable.find(1)
+ assert_kind_of Vegetable, vegetable
+ cabbage = vegetable.becomes(Cabbage)
+ assert_kind_of Cabbage, cabbage
+ end
+
def test_inheritance_find_all
companies = Company.find(:all, :order => 'id')
assert_kind_of Firm, companies[0], "37signals should be a firm"
diff --git a/activerecord/test/fixtures/vegetables.yml b/activerecord/test/fixtures/vegetables.yml
new file mode 100644
index 0000000000..82bd607701
--- /dev/null
+++ b/activerecord/test/fixtures/vegetables.yml
@@ -0,0 +1,9 @@
+first_cucumber:
+ id: 1
+ custom_type: Cucumber
+ name: 'my cucumber'
+
+first_cabbage:
+ id: 2
+ custom_type: Cabbage
+ name: 'my cabbage' \ No newline at end of file
diff --git a/activerecord/test/models/vegetables.rb b/activerecord/test/models/vegetables.rb
new file mode 100644
index 0000000000..59cedfd9f5
--- /dev/null
+++ b/activerecord/test/models/vegetables.rb
@@ -0,0 +1,14 @@
+class Vegetable < ActiveRecord::Base
+
+ validates_presence_of :name
+
+ def self.inheritance_column
+ 'custom_type'
+ end
+end
+
+class Cucumber < Vegetable
+end
+
+class Cabbage < Vegetable
+end
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index b3ae2abbc4..b8150c50b9 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -179,6 +179,11 @@ ActiveRecord::Schema.define do
add_index :companies, [:firm_id, :type, :rating, :ruby_type], :name => "company_index"
+ create_table :vegetables, :force => true do |t|
+ t.string :name
+ t.string :custom_type
+ end
+
create_table :computers, :force => true do |t|
t.integer :developer, :null => false
t.integer :extendedWarranty, :null => false