Module: Acl9::ControllerExtensions::ClassMethods

Defined in:
lib/acl9/controller_extensions.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) access_control(*args, &block)

Raises:

  • (ArgumentError)


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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/acl9/controller_extensions.rb', line 10

def access_control(*args, &block)
  opts = args.extract_options!

  case args.size
  when 0 then true
  when 1
    meth = args.first

    if meth.is_a? Symbol
      opts[:as_method] = meth
    else
      raise ArgumentError, "access_control argument must be a :symbol!"
    end
  else
    raise ArgumentError, "Invalid arguments for access_control"
  end

  subject_method = opts[:subject_method] || Acl9::config[:default_subject_method]

  raise ArgumentError, "Block must be supplied to access_control" unless block

  filter = opts[:filter]
  filter = true if filter.nil?

  case helper = opts[:helper]
  when true
    raise ArgumentError, "you should specify :helper => :method_name" if !opts[:as_method]
  when nil then nil
  else
    if opts[:as_method]
      raise ArgumentError, "you can't specify both method name and helper name" 
    else
      opts[:as_method] = helper
      filter = false
    end
  end

  method = opts[:as_method]

  query_method_available = true
  generator = case
              when method && filter
                Acl9::Dsl::Generators::FilterMethod.new(subject_method, method)
              when method && !filter
                query_method_available = false
                Acl9::Dsl::Generators::BooleanMethod.new(subject_method, method)
              else
                Acl9::Dsl::Generators::FilterLambda.new(subject_method)
              end

  generator.acl_block!(&block)

  generator.install_on(self, opts)

  if query_method_available && (query_method = opts.delete(:query_method))
    case query_method
    when true
      if method
        query_method = "#{method}?"
      else
        raise ArgumentError, "You must specify :query_method as Symbol"
      end
    when Symbol, String
      # okay here
    else
      raise ArgumentError, "Invalid value for :query_method"
    end

    second_generator = Acl9::Dsl::Generators::BooleanMethod.new(subject_method, query_method)
    second_generator.acl_block!(&block)
    second_generator.install_on(self, opts)
  end
end