diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2014-02-18 15:20:15 +0100 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2014-02-18 16:35:07 +0100 |
commit | 4edca106daacc5a159289eae255207d160f22396 (patch) | |
tree | 26bea685d0493162fed61944ec9989ada169e573 /activesupport/lib/active_support | |
parent | 5d037819ca80606638212f83de741cc2041db28f (diff) | |
download | rails-4edca106daacc5a159289eae255207d160f22396.tar.gz rails-4edca106daacc5a159289eae255207d160f22396.tar.bz2 rails-4edca106daacc5a159289eae255207d160f22396.zip |
Added Object#present_in to simplify value whitelisting
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/inclusion.rb | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/inclusion.rb b/activesupport/lib/active_support/core_ext/object/inclusion.rb index b5671f66d0..141f19e7b3 100644 --- a/activesupport/lib/active_support/core_ext/object/inclusion.rb +++ b/activesupport/lib/active_support/core_ext/object/inclusion.rb @@ -12,4 +12,16 @@ class Object rescue NoMethodError raise ArgumentError.new("The parameter passed to #in? must respond to #include?") end + + # Returns the receiver if it's included in the argument otherwise returns +nil+. + # Argument must be any object which responds to +#include?+. Usage: + # + # params[:bucket_type].present_in %w( project calendar ) + # + # This will throw an ArgumentError if the argument doesn't respond to +#include?+. + # + # @return [Object] + def present_in(another_object) + self.in?(another_object) ? self : nil + end end |