From 4edca106daacc5a159289eae255207d160f22396 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 18 Feb 2014 15:20:15 +0100 Subject: Added Object#present_in to simplify value whitelisting --- .../lib/active_support/core_ext/object/inclusion.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'activesupport/lib/active_support') 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 -- cgit v1.2.3