Skip to content
This repository
Browse code

Added keyword-style approach to defining the custom relational bindings

#545 [Jamis Buck]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@527 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 1aa82b3217d243c3074feb47baed07a620a270e2 1 parent b8e4c78
David Heinemeier Hansson authored February 07, 2005
8  activerecord/CHANGELOG
... ...
@@ -1,5 +1,13 @@
1 1
 *SVN*
2 2
 
  3
+* Added keyword-style approach to defining the custom relational bindings #545 [Jamis Buck]. Example:
  4
+
  5
+    class Project < ActiveRecord::Base
  6
+      primary_key "sysid"
  7
+      table_name "XYZ_PROJECT"
  8
+      inheritance_column { original_inheritance_column + "_id" }
  9
+    end
  10
+
3 11
 * Added an Oracle adapter that works with the Oracle bindings by Yoshida (http://raa.ruby-lang.org/project/oracle/) #564 [Maik Schmidt]
4 12
   
5 13
   A number of caveats:
71  activerecord/lib/active_record/base.rb
@@ -525,6 +525,75 @@ def inheritance_column
525 525
         "type"
526 526
       end
527 527
 
  528
+      # Defines an "attribute" method (like #inheritance_column or
  529
+      # #table_name). A new (class) method will be created with the
  530
+      # given name. If a value is specified, the new method will
  531
+      # return that value (as a string). Otherwise, the given block
  532
+      # will be used to compute the value of the method.
  533
+      #
  534
+      # The original method will be aliased, with the new name being
  535
+      # prefixed with "original_". This allows the new method to
  536
+      # access the original value.
  537
+      #
  538
+      # Example:
  539
+      #
  540
+      #   class A < ActiveRecord::Base
  541
+      #     define_attr_method :primary_key, "sysid"
  542
+      #     define_attr_method( :inheritance_column ) do
  543
+      #       original_inheritance_column + "_id"
  544
+      #     end
  545
+      #   end
  546
+      def define_attr_method( name, value=nil, &block )
  547
+        sing = class << self; self; end
  548
+        block = proc { value.to_s } if value
  549
+        sing.send( :alias_method, "original_#{name}", name )
  550
+        sing.send( :define_method, name, &block )
  551
+      end
  552
+
  553
+      # Sets the table name to use to the given value, or (if the value
  554
+      # is nil or false) to the value returned by the given block. (See
  555
+      # #define_attr_method).
  556
+      #
  557
+      # Example:
  558
+      #
  559
+      #   class Project < ActiveRecord::Base
  560
+      #     set_table_name "project"
  561
+      #   end
  562
+      def set_table_name( value=nil, &block )
  563
+        define_attr_method :table_name, value, &block
  564
+      end
  565
+      alias :table_name= :set_table_name
  566
+
  567
+      # Sets the name of the primary key column to use to the given value,
  568
+      # or (if the value is nil or false) to the value returned by the given
  569
+      # block. (See #define_attr_method).
  570
+      #
  571
+      # Example:
  572
+      #
  573
+      #   class Project < ActiveRecord::Base
  574
+      #     set_primary_key "sysid"
  575
+      #   end
  576
+      def set_primary_key( value=nil, &block )
  577
+        define_attr_method :primary_key, value, &block
  578
+      end
  579
+      alias :primary_key= :set_primary_key
  580
+
  581
+      # Sets the name of the inheritance column to use to the given value,
  582
+      # or (if the value # is nil or false) to the value returned by the
  583
+      # given block. (See # #define_attr_method).
  584
+      #
  585
+      # Example:
  586
+      #
  587
+      #   class Project < ActiveRecord::Base
  588
+      #     set_inheritance_column do
  589
+      #       original_inheritance_column + "_id"
  590
+      #     end
  591
+      #   end
  592
+      def set_inheritance_column( value=nil, &block )
  593
+        define_attr_method :inheritance_column, value, &block
  594
+      end
  595
+      alias :inheritance_column= :set_inheritance_column
  596
+
528 597
       # Turns the +table_name+ back into a class name following the reverse rules of +table_name+.
529 598
       def class_name(table_name = table_name) # :nodoc:
530 599
         # remove any prefix and/or suffix from the table name
@@ -1253,4 +1322,4 @@ def has_yaml_encoding_header?(string)
1253 1322
         string[0..3] == "--- "
1254 1323
       end
1255 1324
   end
1256  
-end
  1325
+end
54  activerecord/test/base_test.rb
@@ -706,4 +706,58 @@ def test_reload
706 706
     t2.reload
707 707
     assert_equal t1.title, t2.title
708 708
   end
  709
+
  710
+  def test_define_attr_method_with_value
  711
+    k = Class.new( ActiveRecord::Base )
  712
+    k.define_attr_method :table_name, "foo"
  713
+    assert_equal "foo", k.table_name
  714
+  end
  715
+
  716
+  def test_define_attr_method_with_block
  717
+    k = Class.new( ActiveRecord::Base )
  718
+    k.define_attr_method( :primary_key ) { "sys_" + original_primary_key }
  719
+    assert_equal "sys_id", k.primary_key
  720
+  end
  721
+
  722
+  def test_set_table_name_with_value
  723
+    k = Class.new( ActiveRecord::Base )
  724
+    k.table_name = "foo"
  725
+    assert_equal "foo", k.table_name
  726
+    k.set_table_name "bar"
  727
+    assert_equal "bar", k.table_name
  728
+  end
  729
+
  730
+  def test_set_table_name_with_block
  731
+    k = Class.new( ActiveRecord::Base )
  732
+    k.set_table_name { "ks" }
  733
+    assert_equal "ks", k.table_name
  734
+  end
  735
+
  736
+  def test_set_primary_key_with_value
  737
+    k = Class.new( ActiveRecord::Base )
  738
+    k.primary_key = "foo"
  739
+    assert_equal "foo", k.primary_key
  740
+    k.set_primary_key "bar"
  741
+    assert_equal "bar", k.primary_key
  742
+  end
  743
+
  744
+  def test_set_primary_key_with_block
  745
+    k = Class.new( ActiveRecord::Base )
  746
+    k.set_primary_key { "sys_" + original_primary_key }
  747
+    assert_equal "sys_id", k.primary_key
  748
+  end
  749
+
  750
+  def test_set_inheritance_column_with_value
  751
+    k = Class.new( ActiveRecord::Base )
  752
+    k.inheritance_column = "foo"
  753
+    assert_equal "foo", k.inheritance_column
  754
+    k.set_inheritance_column "bar"
  755
+    assert_equal "bar", k.inheritance_column
  756
+  end
  757
+
  758
+  def test_set_inheritance_column_with_block
  759
+    k = Class.new( ActiveRecord::Base )
  760
+    k.set_inheritance_column { original_inheritance_column + "_id" }
  761
+    assert_equal "type_id", k.inheritance_column
  762
+  end
709 763
 end

0 notes on commit 1aa82b3

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