From a0fb9a6f7c0614358e59bb5f95eef00cba79ab50 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sun, 13 Sep 2009 21:24:04 +0200 Subject: AS guide: documents assert_valid_keys --- .../guides/source/active_support_overview.textile | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'railties') diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile index 2e7fbe7cea..a498849633 100644 --- a/railties/guides/source/active_support_overview.textile +++ b/railties/guides/source/active_support_overview.textile @@ -1258,6 +1258,37 @@ h5. +to_options+ and +to_options!+ The methods +to_options+ and +to_options!+ are respectively aliases of +symbolize_keys+ and +symbolize_keys!+. +h5. +assert_valid_keys+ + +The method +assert_valid_keys+ receives an arbitrary number of arguments, and checks whether the receiver has any key outside that white list. If it does +ArgumentError+ is raised. + + +{:a => 1}.assert_valid_keys(:a) # passes +{:a => 1}.assert_valid_keys("a") # ArgumentError + + +Active Record does not accept unknown options when building associations for example. It implements that control via +assert_valid_keys+: + + +mattr_accessor :valid_keys_for_has_many_association +@@valid_keys_for_has_many_association = [ + :class_name, :table_name, :foreign_key, :primary_key, + :dependent, + :select, :conditions, :include, :order, :group, :having, :limit, :offset, + :as, :through, :source, :source_type, + :uniq, + :finder_sql, :counter_sql, + :before_add, :after_add, :before_remove, :after_remove, + :extend, :readonly, + :validate, :inverse_of +] + +def create_has_many_reflection(association_id, options, &extension) + options.assert_valid_keys(valid_keys_for_has_many_association) + ... +end + + h4. Indifferent Access The method +with_indifferent_access+ returns an +ActiveSupport::HashWithIndifferentAccess+ out of its receiver: -- cgit v1.2.3