module Charcoal::ControllerFilter::ClassMethods

Public Instance Methods

allow(filter, &block) click to toggle source
# 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