aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrick <technoweenie@gmail.com>2008-05-31 17:13:11 -0700
committerrick <technoweenie@gmail.com>2008-05-31 17:13:11 -0700
commit72483c0d4c1e4ea794919974100acc2f255f6fd2 (patch)
tree1519e9e4dd95c3c0ac13e1d0bf8bc0e2b15fc5a9
parenta6e79083273dfb1a62aa8ff02db07454c65729ff (diff)
downloadrails-72483c0d4c1e4ea794919974100acc2f255f6fd2.tar.gz
rails-72483c0d4c1e4ea794919974100acc2f255f6fd2.tar.bz2
rails-72483c0d4c1e4ea794919974100acc2f255f6fd2.zip
Add ActiveRecord::Base.sti_name that checks ActiveRecord::Base#store_full_sti_class? and returns either the full or demodulized name. [rick] [#114 state:resolved]
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb2
-rwxr-xr-xactiverecord/lib/active_record/base.rb16
-rwxr-xr-xactiverecord/test/cases/inheritance_test.rb18
4 files changed, 32 insertions, 6 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 78082665a4..31e1c22f0d 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,3 +1,5 @@
+* Add ActiveRecord::Base.sti_name that checks ActiveRecord::Base#store_full_sti_class? and returns either the full or demodulized name. [rick]
+
* Add first/last methods to associations/named_scope. Resolved #226. [Ryan Bates]
*2.1.0 RC1 (May 11th, 2008)*
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb
index ebcf462f2e..52ced36d16 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -237,7 +237,7 @@ module ActiveRecord
end
def build_sti_condition
- "#{@reflection.through_reflection.quoted_table_name}.#{@reflection.through_reflection.klass.inheritance_column} = #{@reflection.klass.quote_value(@reflection.through_reflection.klass.name.demodulize)}"
+ "#{@reflection.through_reflection.quoted_table_name}.#{@reflection.through_reflection.klass.inheritance_column} = #{@reflection.klass.quote_value(@reflection.through_reflection.klass.sti_name)}"
end
alias_method :sql_conditions, :conditions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index d2d9bda6ba..92a24ecc5f 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1293,6 +1293,10 @@ module ActiveRecord #:nodoc:
super
end
+ def sti_name
+ store_full_sti_class ? name : name.demodulize
+ end
+
private
def find_initial(options)
options.update(:limit => 1)
@@ -1452,7 +1456,11 @@ module ActiveRecord #:nodoc:
# Nest the type name in the same module as this class.
# Bar is "MyApp::Business::Bar" relative to MyApp::Business::Foo
def type_name_with_module(type_name)
- (/^::/ =~ type_name) ? type_name : "#{parent.name}::#{type_name}"
+ if store_full_sti_class
+ type_name
+ else
+ (/^::/ =~ type_name) ? type_name : "#{parent.name}::#{type_name}"
+ end
end
def construct_finder_sql(options)
@@ -1571,8 +1579,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} = '#{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}' "
+ type_condition = subclasses.inject("#{quoted_table_name}.#{quoted_inheritance_column} = '#{sti_name}' ") do |condition, subclass|
+ condition << "OR #{quoted_table_name}.#{quoted_inheritance_column} = '#{subclass.sti_name}' "
end
" (#{type_condition}) "
@@ -2508,7 +2516,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, store_full_sti_class ? self.class.name : self.class.name.demodulize)
+ write_attribute(self.class.inheritance_column, self.class.sti_name)
end
end
diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb
index 27394924a1..f09b617e6d 100755
--- a/activerecord/test/cases/inheritance_test.rb
+++ b/activerecord/test/cases/inheritance_test.rb
@@ -5,7 +5,23 @@ require 'models/subscriber'
class InheritanceTest < ActiveRecord::TestCase
fixtures :companies, :projects, :subscribers, :accounts
-
+
+ def test_class_with_store_full_sti_class_returns_full_name
+ old = ActiveRecord::Base.store_full_sti_class
+ ActiveRecord::Base.store_full_sti_class = true
+ assert_equal 'Namespaced::Company', Namespaced::Company.sti_name
+ ensure
+ ActiveRecord::Base.store_full_sti_class = old
+ end
+
+ def test_class_without_store_full_sti_class_returns_demodulized_name
+ old = ActiveRecord::Base.store_full_sti_class
+ ActiveRecord::Base.store_full_sti_class = false
+ assert_equal 'Company', Namespaced::Company.sti_name
+ ensure
+ ActiveRecord::Base.store_full_sti_class = old
+ end
+
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