Module: Charcoal::ControllerFilter::ClassMethods

Defined in:
lib/charcoal/controller_filter.rb

Instance Method Summary collapse

Instance Method Details

#allow(filter, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/charcoal/controller_filter.rb', line 8

def allow(filter, &block)
  action = "allow_#{filter}"
  define_method action do |*args|
    # If we don't need 1.8 compat then ->(options = {}) instead of *args and the next line
    options = args.last.is_a?(Hash) ? args.pop : {}
    options.assert_valid_keys(:only, :except, :if, :unless)

    methods = args.map(&:to_sym)
    methods = [:all] if methods.empty?

    directive = if options[:unless]
      lambda { |c| !parse_directive(options[:unless]).call(c) }
    else
      parse_directive(options[:if] || true)
    end

    methods.each do |method|
      instance_exec(method, directive, &block)
    end
  end
end