1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #7506 from senny/issue_7503

#becomes works with configured inheritance_column
This commit is contained in:
Rafael Mendonça França 2012-09-03 07:12:18 -07:00
commit 30a8f0d5b6
5 changed files with 38 additions and 2 deletions

View file

@ -161,7 +161,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.public_send("#{klass.inheritance_column}=", klass.name) unless self.class.descends_from_active_record?
became
end

View file

@ -5,9 +5,10 @@ require 'models/post'
require 'models/project'
require 'models/subscriber'
require 'models/teapot'
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
@ -127,6 +128,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.all.merge!(:order => 'id').to_a
assert_kind_of Firm, companies[0], "37signals should be a firm"

View file

@ -0,0 +1,9 @@
first_cucumber:
id: 1
custom_type: Cucumber
name: 'my cucumber'
first_cabbage:
id: 2
custom_type: Cabbage
name: 'my cabbage'

View file

@ -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

View file

@ -184,6 +184,11 @@ ActiveRecord::Schema.define do
add_index :companies, [:firm_id, :type, :rating, :ruby_type], :name => "company_index"
add_index :companies, [:firm_id, :type], :name => "company_partial_index", :where => "rating > 10"
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