Class: PermissionConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/cancan-permits/loader/permission_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ PermissionConfig

Returns a new instance of PermissionConfig.



4
5
6
# File 'lib/cancan-permits/loader/permission_config.rb', line 4

def initialize name
  @name = name
end

Instance Attribute Details

#canObject

Returns the value of attribute can.



2
3
4
# File 'lib/cancan-permits/loader/permission_config.rb', line 2

def can
  @can
end

#cannotObject

Returns the value of attribute cannot.



2
3
4
# File 'lib/cancan-permits/loader/permission_config.rb', line 2

def cannot
  @cannot
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/cancan-permits/loader/permission_config.rb', line 2

def name
  @name
end

Instance Method Details

#can_eval {|statements| ... } ⇒ Object

Should take @can=href=""all"">manage”=>, @cannot=“Profile”] and create string ready to ruby evaluate like: can(:manage, :all) cannot(:update, [User, Profile])

Yields:

  • (statements)


12
13
14
15
16
17
18
# File 'lib/cancan-permits/loader/permission_config.rb', line 12

def can_eval &block
  statements = [:manage, :read, :update, :create, :write].map do |action|
    targets = can[action]
    targets ? "can(:#{action}, #{parse_targets(targets)})" : nil
  end.compact.join("\n")
  yield statements if !statements.empty? && block
end

#parse_targets(targets) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cancan-permits/loader/permission_config.rb', line 28

def parse_targets targets
  targets.map do |target|
    if target == 'all'
      ':all'
    else
      begin
        "#{target.constantize}"
      rescue
        puts "[permission] target #{target} does not have a class so it was skipped"
      end
    end
  end
end