Skip to content

Commit

Permalink
set the configured #inheritance_column on #become (rails#7503)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
senny authored and Yves Senn committed Sep 3, 2012
1 parent cabab37 commit b864ded
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 2 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,9 @@
## Rails 3.2.9 (unreleased) ## 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 * Fix `reset_counters` when there are multiple `belongs_to` association with the
same foreign key and one of them have a counter cache. same foreign key and one of them have a counter cache.
Fixes #5200. Fixes #5200.
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/persistence.rb
Expand Up @@ -162,7 +162,7 @@ def becomes(klass)
became.instance_variable_set("@new_record", new_record?) became.instance_variable_set("@new_record", new_record?)
became.instance_variable_set("@destroyed", destroyed?) became.instance_variable_set("@destroyed", destroyed?)
became.instance_variable_set("@errors", errors) became.instance_variable_set("@errors", errors)
became.type = klass.name unless self.class.descends_from_active_record? became.public_send("#{klass.inheritance_column}=", klass.name) unless self.class.descends_from_active_record?
became became
end end


Expand Down
10 changes: 9 additions & 1 deletion activerecord/test/cases/inheritance_test.rb
Expand Up @@ -2,9 +2,10 @@
require 'models/company' require 'models/company'
require 'models/project' require 'models/project'
require 'models/subscriber' require 'models/subscriber'
require 'models/vegetables'


class InheritanceTest < ActiveRecord::TestCase 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 def test_class_with_store_full_sti_class_returns_full_name
old = ActiveRecord::Base.store_full_sti_class old = ActiveRecord::Base.store_full_sti_class
Expand Down Expand Up @@ -98,6 +99,13 @@ def test_alt_inheritance_find
switch_to_default_inheritance_column switch_to_default_inheritance_column
end 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 def test_inheritance_find_all
companies = Company.find(:all, :order => 'id') companies = Company.find(:all, :order => 'id')
assert_kind_of Firm, companies[0], "37signals should be a firm" assert_kind_of Firm, companies[0], "37signals should be a firm"
Expand Down
9 changes: 9 additions & 0 deletions 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'
14 changes: 14 additions & 0 deletions 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
5 changes: 5 additions & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -179,6 +179,11 @@ def create_table(*args, &block)


add_index :companies, [:firm_id, :type, :rating, :ruby_type], :name => "company_index" 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| create_table :computers, :force => true do |t|
t.integer :developer, :null => false t.integer :developer, :null => false
t.integer :extendedWarranty, :null => false t.integer :extendedWarranty, :null => false
Expand Down

0 comments on commit b864ded

Please sign in to comment.