Class: AccessControlEntry

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.can(verb, resource, options = {}) ⇒ Object



77
78
79
# File 'app/models/access_control_entry.rb', line 77

def self.can(verb, resource, options={})
  AccessControlEntry.new(:can=>true, :verb=> verb, :resource=>resource, :options=>options )
end

.cannot(verb, resource, options) ⇒ Object



80
81
82
# File 'app/models/access_control_entry.rb', line 80

def self.cannot(verb, resource, options)
  AccessControlEntry.new(:can=>false, :verb=> verb, :resource=>resource, :options=>options )
end

Instance Method Details

#able_optionsObject



69
70
71
72
73
74
75
# File 'app/models/access_control_entry.rb', line 69

def able_options
  if resource.is_a?(ActiveRecord::Base)
    options.merge({:id=>resource.id})
  else
    options
  end
end

#configure(ability) ⇒ Object



63
64
65
66
67
# File 'app/models/access_control_entry.rb', line 63

def configure(ability)
  raise "I only like Ability" unless ability.class==Ability
  able = self.can ? :can : :cannot
  ability.send able, verb.to_sym, resource_type.try(:constantize), able_options
end

#method_nameObject



84
85
86
# File 'app/models/access_control_entry.rb', line 84

def method_name
  self.can ? :can : :cannot
end

#optionsObject



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

def options
  @options ||= (self.serialized_options ? eval(self.serialized_options) : {})
end

#options_strObject



46
47
48
# File 'app/models/access_control_entry.rb', line 46

def options_str
  options.map{|u,v| "#{u.inspect}=>#{v.is_numeric? ? v : v.inspect}"}.join(", ")
end

#options_str=(str) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/access_control_entry.rb', line 49

def options_str=(str)
  opts = str.split(",").map(&:strip).map_hash{|keyval|
    key, val = keyval.split("=>").map(&:strip)
    val = val.to_i if val.is_numeric?
    if key.index(":")
      key = key.gsub(/\:/, "").to_sym
    end
    {key=>val}
  }

  @options = opts
  self.serialized_options = opts.inspect
end

#resourceObject



29
30
31
32
33
34
35
36
37
38
# File 'app/models/access_control_entry.rb', line 29

def resource
  unless @res
    if resource_id
      @res = resource_type.constantize.find(resource_id) unless resource_type.empty?
    else
      @res = resource_type.constantize unless resource_type.empty?
    end
  end
  @res
end

#resource=(res) ⇒ Object



20
21
22
23
24
25
26
27
# File 'app/models/access_control_entry.rb', line 20

def resource=(res)
  @res = res
  if res.is_a? Class then self.resource_type = res.name
  else
    self.resource_id = res.id
    self.resource_type = res.class.name
  end
end

#resource_classObject



40
# File 'app/models/access_control_entry.rb', line 40

def resource_class; resource_type.constantize unless resource_type.empty?; end

#role=(role) ⇒ Object



95
96
97
98
99
100
101
102
# File 'app/models/access_control_entry.rb', line 95

def role=(role)
  if role.kind_of? Role
    self.role_id = role.id
  else
    role = role.to_s.camelize
    self.role_id = (role.is_numeric? ? role : Role.find_by_name(role).id) unless role.empty?
  end
end

#user_emailObject



88
89
90
# File 'app/models/access_control_entry.rb', line 88

def user_email
  self.user.email if self.user
end

#user_email=(email) ⇒ Object



91
92
93
# File 'app/models/access_control_entry.rb', line 91

def user_email=(email)
  self.user = User.find_by_email(email) unless email.blank?
end