diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/aggregations.rb | 8 | ||||
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 27 | ||||
-rwxr-xr-x | activerecord/test/associations_test.rb | 2 | ||||
-rwxr-xr-x | activerecord/test/deprecated_associations_test.rb | 2 | ||||
-rw-r--r-- | activerecord/test/mixin_test.rb | 19 |
5 files changed, 36 insertions, 22 deletions
diff --git a/activerecord/lib/active_record/aggregations.rb b/activerecord/lib/active_record/aggregations.rb index 208dc35321..29cf5c3925 100644 --- a/activerecord/lib/active_record/aggregations.rb +++ b/activerecord/lib/active_record/aggregations.rb @@ -120,7 +120,7 @@ module ActiveRecord # composed_of :address, :mapping => [ %w(address_street street), %w(address_city city) ] # composed_of :gps_location def composed_of(part_id, options = {}) - validate_options([ :class_name, :mapping ], options.keys) + options.assert_valid_keys(:class_name, :mapping) name = part_id.id2name class_name = options[:class_name] || name_to_class_name(name) @@ -131,12 +131,6 @@ module ActiveRecord end private - # Raises an exception if an invalid option has been specified to prevent misspellings from slipping through - def validate_options(valid_option_keys, supplied_option_keys) - unknown_option_keys = supplied_option_keys - valid_option_keys - raise(ActiveRecordError, "Unknown options: #{unknown_option_keys}") unless unknown_option_keys.empty? - end - def name_to_class_name(name) name.capitalize.gsub(/_(.)/) { |s| $1.capitalize } end diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index ffb0d5679b..df9c66aa08 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -284,8 +284,12 @@ module ActiveRecord # 'WHERE ps.post_id = #{id} AND ps.person_id = p.id ' + # 'ORDER BY p.first_name' def has_many(association_id, options = {}) - validate_options([ :foreign_key, :class_name, :exclusively_dependent, :dependent, :conditions, :order, :finder_sql, :counter_sql, - :before_add, :after_add, :before_remove, :after_remove ], options.keys) + options.assert_valid_keys( + :foreign_key, :class_name, :exclusively_dependent, :dependent, + :conditions, :order, :finder_sql, :counter_sql, + :before_add, :after_add, :before_remove, :after_remove + ) + association_name, association_class_name, association_class_primary_key_name = associate_identification(association_id, options[:class_name], options[:foreign_key]) @@ -358,7 +362,7 @@ module ActiveRecord # has_one :last_comment, :class_name => "Comment", :order => "posted_on" # has_one :project_manager, :class_name => "Person", :conditions => "role = 'project_manager'" def has_one(association_id, options = {}) - validate_options([ :class_name, :foreign_key, :remote, :conditions, :order, :dependent, :counter_cache ], options.keys) + options.assert_valid_keys(:class_name, :foreign_key, :remote, :conditions, :order, :dependent, :counter_cache) association_name, association_class_name, association_class_primary_key_name = associate_identification(association_id, options[:class_name], options[:foreign_key], false) @@ -429,7 +433,7 @@ module ActiveRecord # belongs_to :valid_coupon, :class_name => "Coupon", :foreign_key => "coupon_id", # :conditions => 'discounts > #{payments_count}' def belongs_to(association_id, options = {}) - validate_options([ :class_name, :foreign_key, :remote, :conditions, :order, :dependent, :counter_cache ], options.keys) + options.assert_valid_keys(:class_name, :foreign_key, :remote, :conditions, :order, :dependent, :counter_cache) association_name, association_class_name, class_primary_key_name = associate_identification(association_id, options[:class_name], options[:foreign_key], false) @@ -544,9 +548,12 @@ module ActiveRecord # has_and_belongs_to_many :active_projects, :join_table => 'developers_projects', :delete_sql => # 'DELETE FROM developers_projects WHERE active=1 AND developer_id = #{id} AND project_id = #{record.id}' def has_and_belongs_to_many(association_id, options = {}) - validate_options([ :class_name, :table_name, :foreign_key, :association_foreign_key, :conditions, - :join_table, :finder_sql, :delete_sql, :insert_sql, :order, :uniq, :before_add, :after_add, - :before_remove, :after_remove ], options.keys) + options.assert_valid_keys( + :class_name, :table_name, :foreign_key, :association_foreign_key, :conditions, + :join_table, :finder_sql, :delete_sql, :insert_sql, :order, :uniq, :before_add, :after_add, + :before_remove, :after_remove + ) + association_name, association_class_name, association_class_primary_key_name = associate_identification(association_id, options[:class_name], options[:foreign_key]) @@ -570,12 +577,6 @@ module ActiveRecord end private - # Raises an exception if an invalid option has been specified to prevent misspellings from slipping through - def validate_options(valid_option_keys, supplied_option_keys) - unknown_option_keys = supplied_option_keys - valid_option_keys - raise(ActiveRecord::ActiveRecordError, "Unknown options: #{unknown_option_keys}") unless unknown_option_keys.empty? - end - def join_table_name(first_table_name, second_table_name) if first_table_name < second_table_name join_table = "#{first_table_name}_#{second_table_name}" diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index c8dc687106..fd4bd5cda8 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -14,7 +14,7 @@ require 'fixtures/author' bad_collection_keys = false begin class Car < ActiveRecord::Base; has_many :wheels, :name => "wheels"; end -rescue ActiveRecord::ActiveRecordError +rescue ArgumentError bad_collection_keys = true end raise "ActiveRecord should have barked on bad collection keys" unless bad_collection_keys diff --git a/activerecord/test/deprecated_associations_test.rb b/activerecord/test/deprecated_associations_test.rb index 9f4894beca..1d5908d774 100755 --- a/activerecord/test/deprecated_associations_test.rb +++ b/activerecord/test/deprecated_associations_test.rb @@ -9,7 +9,7 @@ require 'fixtures/reply' bad_collection_keys = false begin class Car < ActiveRecord::Base; has_many :wheels, :name => "wheels"; end -rescue ActiveRecord::ActiveRecordError +rescue ArgumentError bad_collection_keys = true end raise "ActiveRecord should have barked on bad collection keys" unless bad_collection_keys diff --git a/activerecord/test/mixin_test.rb b/activerecord/test/mixin_test.rb index dd29d94ab1..69789a0cec 100644 --- a/activerecord/test/mixin_test.rb +++ b/activerecord/test/mixin_test.rb @@ -315,4 +315,23 @@ class TouchTest < Test::Unit::TestCase Mixin.record_timestamps = true end + + def test_ancestors + assert_equal [], mixins(:tree_1).ancestors + assert_equal [mixins(:tree_1)], mixins(:tree_2).ancestors + assert_equal [mixins(:tree_2), mixins(:tree_1)], mixins(:tree_3).ancestors + assert_equal [mixins(:tree_1)], mixins(:tree_4).ancestors + assert_equal [], mixins(:tree2_1).ancestors + assert_equal [], mixins(:tree3_1).ancestors + end + + def test_root + assert_equal mixins(:tree_1), TreeMixin.root + assert_equal mixins(:tree_1), mixins(:tree_1).root + assert_equal mixins(:tree_1), mixins(:tree_2).root + assert_equal mixins(:tree_1), mixins(:tree_3).root + assert_equal mixins(:tree_1), mixins(:tree_4).root + assert_equal mixins(:tree2_1), mixins(:tree2_1).root + assert_equal mixins(:tree3_1), mixins(:tree3_1).root + end end |