Refactored away all the legacy validate_options calls and replaced them with Hash#assert_valid_keys

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2167 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson
2005-09-09 08:32:47 +00:00
parent 558331596f
commit 0439769324
6 changed files with 38 additions and 31 deletions

View File

@@ -82,7 +82,7 @@ module ActionController
# 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 @@ module ActionController
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

View File

@@ -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

View File

@@ -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}"

View File

@@ -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

View File

@@ -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

View File

@@ -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