3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/action_requires.rb', line 3
def self.action_requires(options)
unless options.is_a? Hash
options = {:names => [options].flatten}
end
options = {:type => :and}.merge(options)
options[:names] = [options[:names]].flatten
big_meth = "action_requires_wrapper_" << options[:names].join("_#{options[:type].to_s}_")
options[:names].each do |meth|
unless method_defined?(meth)
define_method("action_requires_#{meth}") do
return !params[meth.to_sym].blank?
end
end
end
unless method_defined?(big_meth)
delim = case options[:type]
when :and
" && "
when :or
" || "
end
eval %{
def #{big_meth}
unless #{options[:names].collect {|meth| "action_requires_#{meth}"}.join(delim)}
send_to_unknown
# return false
end
end
}
end
options.delete(:names)
options.delete(:type)
before_filter big_meth.to_sym, options
end
|