aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xactiverecord/lib/active_record/base.rb10
-rwxr-xr-xactiverecord/test/cases/inheritance_test.rb28
-rwxr-xr-xactiverecord/test/models/company.rb4
-rw-r--r--railties/configs/initializers/new_rails_defaults.rb3
4 files changed, 42 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 392d187092..ac1a35dc91 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -439,6 +439,10 @@ module ActiveRecord #:nodoc:
cattr_accessor :schema_format , :instance_writer => false
@@schema_format = :ruby
+ # Determine whether to store the full constant name including namespace when using STI
+ superclass_delegating_accessor :store_full_sti_class
+ self.store_full_sti_class = false
+
class << self # Class methods
# Find operates with four different retrieval approaches:
#
@@ -1557,8 +1561,8 @@ module ActiveRecord #:nodoc:
def type_condition
quoted_inheritance_column = connection.quote_column_name(inheritance_column)
- type_condition = subclasses.inject("#{quoted_table_name}.#{quoted_inheritance_column} = '#{name.demodulize}' ") do |condition, subclass|
- condition << "OR #{quoted_table_name}.#{quoted_inheritance_column} = '#{subclass.name.demodulize}' "
+ type_condition = subclasses.inject("#{quoted_table_name}.#{quoted_inheritance_column} = '#{store_full_sti_class ? name : name.demodulize}' ") do |condition, subclass|
+ condition << "OR #{quoted_table_name}.#{quoted_inheritance_column} = '#{store_full_sti_class ? subclass.name : subclass.name.demodulize}' "
end
" (#{type_condition}) "
@@ -2492,7 +2496,7 @@ module ActiveRecord #:nodoc:
# Message class in that example.
def ensure_proper_type
unless self.class.descends_from_active_record?
- write_attribute(self.class.inheritance_column, Inflector.demodulize(self.class.name))
+ write_attribute(self.class.inheritance_column, store_full_sti_class ? self.class.name : Inflector.demodulize(self.class.name))
end
end
diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb
index c9eb83e371..27394924a1 100755
--- a/activerecord/test/cases/inheritance_test.rb
+++ b/activerecord/test/cases/inheritance_test.rb
@@ -5,6 +5,34 @@ require 'models/subscriber'
class InheritanceTest < ActiveRecord::TestCase
fixtures :companies, :projects, :subscribers, :accounts
+
+ def test_should_store_demodulized_class_name_with_store_full_sti_class_option_disabled
+ old = ActiveRecord::Base.store_full_sti_class
+ ActiveRecord::Base.store_full_sti_class = false
+ item = Namespaced::Company.new
+ assert_equal 'Company', item[:type]
+ ensure
+ ActiveRecord::Base.store_full_sti_class = old
+ end
+
+ def test_should_store_full_class_name_with_store_full_sti_class_option_enabled
+ old = ActiveRecord::Base.store_full_sti_class
+ ActiveRecord::Base.store_full_sti_class = true
+ item = Namespaced::Company.new
+ assert_equal 'Namespaced::Company', item[:type]
+ ensure
+ ActiveRecord::Base.store_full_sti_class = old
+ end
+
+ def test_different_namespace_subclass_should_load_correctly_with_store_full_sti_class_option
+ old = ActiveRecord::Base.store_full_sti_class
+ ActiveRecord::Base.store_full_sti_class = true
+ item = Namespaced::Company.create :name => "Wolverine 2"
+ assert_not_nil Company.find(item.id)
+ assert_not_nil Namespaced::Company.find(item.id)
+ ensure
+ ActiveRecord::Base.store_full_sti_class = old
+ end
def test_company_descends_from_active_record
assert_raise(NoMethodError) { ActiveRecord::Base.descends_from_active_record? }
diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb
index 3d76dfd398..f637490c59 100755
--- a/activerecord/test/models/company.rb
+++ b/activerecord/test/models/company.rb
@@ -15,6 +15,10 @@ class Company < AbstractCompany
end
end
+module Namespaced
+ class Company < ::Company
+ end
+end
class Firm < Company
has_many :clients, :order => "id", :dependent => :destroy, :counter_sql =>
diff --git a/railties/configs/initializers/new_rails_defaults.rb b/railties/configs/initializers/new_rails_defaults.rb
index 3a617e3ae3..b8f0e2cac2 100644
--- a/railties/configs/initializers/new_rails_defaults.rb
+++ b/railties/configs/initializers/new_rails_defaults.rb
@@ -7,6 +7,9 @@ ActiveRecord::Base.partial_updates = true
# Include ActiveRecord class name as root for JSON serialized output.
ActiveRecord::Base.include_root_in_json = true
+# Store the full class name (including module namespace) in STI type column
+ActiveRecord::Base.store_full_sti_class = true
+
# Use ISO 8601 format for JSON serialized times and dates
ActiveSupport.use_standard_json_time_format = true