Class: UserRole

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/user_role.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



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
# File 'app/models/user_role.rb', line 9

def method_missing(method, *args)
  method = method.to_s

  if method.starts_with? 'can_'
    role_keyword = method[method.index('_')+1..method.rindex('_')-1]
    privelege    = method[method.rindex('_')+1..-1]

    self.priveleges = [] unless self.priveleges.is_a?(Array)

    if method.ends_with? '='
      privelege = privelege[0..-2]

      if role && role.keyword == role_keyword
        if args[0] && args[0] != 'false'
          self.priveleges = (self.priveleges + [privelege]).uniq
        else
          self.priveleges = self.priveleges.select{|x| x != privelege}
        end
      end
    else
      return false if !role || role.keyword != role_keyword
      self.priveleges.include?(privelege)
    end
  else
    super(method.to_sym, *args)
  end
end

Instance Method Details

#priveleged?(privelege) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/models/user_role.rb', line 42

def priveleged?(privelege)
  priveleges.include?(privelege.to_s)
end

#respond_to?(method, *args) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'app/models/user_role.rb', line 37

def respond_to?(method, *args)
  return true if method.to_s.starts_with? 'can_'
  super
end