Class: License::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cancan-permits/license/base_license.rb

Direct Known Subclasses

BloggingLicense, UserAdminLicense

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(permit, licenses_file = nil) ⇒ Base

Returns a new instance of Base.



7
8
9
10
# File 'lib/cancan-permits/license/base_license.rb', line 7

def initialize permit, licenses_file = nil
  @permit = permit
  @licenses = ::PermissionsLoader.load_licenses licenses_file
end

Instance Attribute Details

#licensesObject (readonly)

Returns the value of attribute licenses.



5
6
7
# File 'lib/cancan-permits/license/base_license.rb', line 5

def licenses
  @licenses
end

#permitObject (readonly)

Returns the value of attribute permit.



5
6
7
# File 'lib/cancan-permits/license/base_license.rb', line 5

def permit
  @permit
end

Instance Method Details

#can(action, subject, conditions = nil, &block) ⇒ Object



30
31
32
# File 'lib/cancan-permits/license/base_license.rb', line 30

def can(action, subject, conditions = nil, &block)
  permit.can action, subject, conditions, &block
end

#cannot(action, subject, conditions = nil, &block) ⇒ Object



34
35
36
# File 'lib/cancan-permits/license/base_license.rb', line 34

def cannot(action, subject, conditions = nil, &block)
  permit.cannot action, subject, conditions, &block
end

#enforce!Object



12
13
14
# File 'lib/cancan-permits/license/base_license.rb', line 12

def enforce!
  raise "enforce! must be implemented by subclass of License::Base"
end

#load_rules(name = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cancan-permits/license/base_license.rb', line 16

def load_rules name = nil
  return if !licenses || licenses.empty?      

  name ||= self.class.to_s.gsub(/License$/, "").underscore.to_sym
        
  licenses[name].can_statement do |permission_statement|
    instance_eval permission_statement
  end

  licenses[name].cannot_statement do |permission_statement|
    instance_eval permission_statement
  end
end

#owns(user, clazz, ownership_relation = :user_id, user_id_attribute = :id) ⇒ Object



38
39
40
# File 'lib/cancan-permits/license/base_license.rb', line 38

def owns(user, clazz, ownership_relation = :user_id, user_id_attribute = :id)
  permit.owns user, clazz, ownership_relation, user_id_attribute
end