Skip to content
This repository
Browse code

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]
  • Loading branch information...
commit 72483c0d4c1e4ea794919974100acc2f255f6fd2 1 parent a6e7908
risk danger olson authored May 31, 2008
2  activerecord/CHANGELOG
... ...
@@ -1,3 +1,5 @@
  1
+* Add ActiveRecord::Base.sti_name that checks ActiveRecord::Base#store_full_sti_class? and returns either the full or demodulized name. [rick]
  2
+
1 3
 * Add first/last methods to associations/named_scope. Resolved #226. [Ryan Bates]
2 4
 
3 5
 *2.1.0 RC1 (May 11th, 2008)*
2  activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -237,7 +237,7 @@ def build_through_conditions
237 237
         end
238 238
         
239 239
         def build_sti_condition
240  
-          "#{@reflection.through_reflection.quoted_table_name}.#{@reflection.through_reflection.klass.inheritance_column} = #{@reflection.klass.quote_value(@reflection.through_reflection.klass.name.demodulize)}"
  240
+          "#{@reflection.through_reflection.quoted_table_name}.#{@reflection.through_reflection.klass.inheritance_column} = #{@reflection.klass.quote_value(@reflection.through_reflection.klass.sti_name)}"
241 241
         end
242 242
 
243 243
         alias_method :sql_conditions, :conditions
16  activerecord/lib/active_record/base.rb
@@ -1293,6 +1293,10 @@ def respond_to?(method_id, include_private = false)
1293 1293
         super
1294 1294
       end
1295 1295
 
  1296
+      def sti_name
  1297
+        store_full_sti_class ? name : name.demodulize
  1298
+      end
  1299
+
1296 1300
       private
1297 1301
         def find_initial(options)
1298 1302
           options.update(:limit => 1)
@@ -1452,7 +1456,11 @@ def instantiate(record)
1452 1456
         # Nest the type name in the same module as this class.
1453 1457
         # Bar is "MyApp::Business::Bar" relative to MyApp::Business::Foo
1454 1458
         def type_name_with_module(type_name)
1455  
-          (/^::/ =~ type_name) ? type_name : "#{parent.name}::#{type_name}"
  1459
+          if store_full_sti_class
  1460
+            type_name
  1461
+          else
  1462
+            (/^::/ =~ type_name) ? type_name : "#{parent.name}::#{type_name}"
  1463
+          end
1456 1464
         end
1457 1465
 
1458 1466
         def construct_finder_sql(options)
@@ -1571,8 +1579,8 @@ def add_conditions!(sql, conditions, scope = :auto)
1571 1579
 
1572 1580
         def type_condition
1573 1581
           quoted_inheritance_column = connection.quote_column_name(inheritance_column)
1574  
-          type_condition = subclasses.inject("#{quoted_table_name}.#{quoted_inheritance_column} = '#{store_full_sti_class ? name : name.demodulize}' ") do |condition, subclass|
1575  
-            condition << "OR #{quoted_table_name}.#{quoted_inheritance_column} = '#{store_full_sti_class ? subclass.name : subclass.name.demodulize}' "
  1582
+          type_condition = subclasses.inject("#{quoted_table_name}.#{quoted_inheritance_column} = '#{sti_name}' ") do |condition, subclass|
  1583
+            condition << "OR #{quoted_table_name}.#{quoted_inheritance_column} = '#{subclass.sti_name}' "
1576 1584
           end
1577 1585
 
1578 1586
           " (#{type_condition}) "
@@ -2508,7 +2516,7 @@ def create
2508 2516
       # Message class in that example.
2509 2517
       def ensure_proper_type
2510 2518
         unless self.class.descends_from_active_record?
2511  
-          write_attribute(self.class.inheritance_column, store_full_sti_class ? self.class.name : self.class.name.demodulize)
  2519
+          write_attribute(self.class.inheritance_column, self.class.sti_name)
2512 2520
         end
2513 2521
       end
2514 2522
 
18  activerecord/test/cases/inheritance_test.rb
@@ -5,7 +5,23 @@
5 5
 
6 6
 class InheritanceTest < ActiveRecord::TestCase
7 7
   fixtures :companies, :projects, :subscribers, :accounts
8  
-  
  8
+
  9
+  def test_class_with_store_full_sti_class_returns_full_name
  10
+    old = ActiveRecord::Base.store_full_sti_class
  11
+    ActiveRecord::Base.store_full_sti_class = true
  12
+    assert_equal 'Namespaced::Company', Namespaced::Company.sti_name
  13
+  ensure
  14
+    ActiveRecord::Base.store_full_sti_class = old
  15
+  end
  16
+
  17
+  def test_class_without_store_full_sti_class_returns_demodulized_name
  18
+    old = ActiveRecord::Base.store_full_sti_class
  19
+    ActiveRecord::Base.store_full_sti_class = false
  20
+    assert_equal 'Company', Namespaced::Company.sti_name
  21
+  ensure
  22
+    ActiveRecord::Base.store_full_sti_class = old
  23
+  end
  24
+
9 25
   def test_should_store_demodulized_class_name_with_store_full_sti_class_option_disabled
10 26
     old = ActiveRecord::Base.store_full_sti_class
11 27
     ActiveRecord::Base.store_full_sti_class = false

0 notes on commit 72483c0

Please sign in to comment.
Something went wrong with that request. Please try again.