Permalink
Browse files

Refactored away all the legacy validate_options calls and replaced th…

…em with Hash#assert_valid_keys

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2167 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 5583315 commit 0439769324ef889e5e353caacb92a025a8ce2629 @dhh dhh committed Sep 9, 2005
@@ -82,7 +82,7 @@ module ClassMethods
# make <tt>scaffold :post, :suffix => true</tt> use method names like list_post, show_post, and create_post
# instead of just list, show, and post. If suffix is used, then no index method is added.
def scaffold(model_id, options = {})
- validate_options([ :class_name, :suffix ], options.keys)
+ options.assert_valid_keys(:class_name, :suffix)
singular_name = model_id.to_s
class_name = options[:class_name] || singular_name.camelize
@@ -176,14 +176,7 @@ def caller_method_name(caller)
caller.first.scan(/`(.*)'/).first.first # ' ruby-mode
end
end_eval
- 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(ActionController::ActionControllerError, "Unknown options: #{unknown_option_keys}") unless unknown_option_keys.empty?
- end
+ end
end
end
end
@@ -120,7 +120,7 @@ module ClassMethods
# 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 @@ def composed_of(part_id, options = {})
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
@@ -284,8 +284,12 @@ module ClassMethods
# '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 @@ def has_many(association_id, options = {})
# 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 @@ def has_one(association_id, options = {})
# 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 @@ def belongs_to(association_id, options = {})
# 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 @@ def has_and_belongs_to_many(association_id, options = {})
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}"
@@ -14,7 +14,7 @@
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
@@ -9,7 +9,7 @@
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
@@ -315,4 +315,23 @@ def test_create_turned_off
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

0 comments on commit 0439769

Please sign in to comment.