module ActAsPermissionControllable::Model

Public Class Methods

total_permission_count() click to toggle source
# File lib/act_as_permission_controllable/model.rb, line 27
def self.total_permission_count
  Controller.get_controllers.sum { |controller| controller.actions.size }
end

Public Instance Methods

assign_permissions(attributes) click to toggle source
# File lib/act_as_permission_controllable/model.rb, line 16
def assign_permissions(attributes)
  return if !attributes.respond_to?(:each)
  perms, controllers = {}, Controller.get_controllers.map { |c| [ c.to_s, c ] }.to_h
  attributes.each do |name, actions|
    next if !(Array === actions)
    next if (controller = controllers[name.to_s]).nil?
    perms[name.to_s] = controller.actions.map(&:to_s) & actions.map(&:to_s)
  end
  self.permissions = perms
end
ban(subject, *actions) click to toggle source
# File lib/act_as_permission_controllable/model.rb, line 6
def ban(subject, *actions)
  control_permissions :ban, subject, *actions
  self
end
can?(*args) click to toggle source
# File lib/act_as_permission_controllable/model.rb, line 41
def can?(*args)
  @current_ability ||= ::Ability.new(self)
  @current_ability.can?(*args)
end
permission_count() click to toggle source
# File lib/act_as_permission_controllable/model.rb, line 31
def permission_count
  c = 0
  Controller.get_controllers.each do |controller|
    if actions = self.permissions[controller.to_s]
      c += (controller.actions.map(&:to_s) & actions.map(&:to_s)).size
    end
  end
  c
end
permit(subject, *actions) click to toggle source
# File lib/act_as_permission_controllable/model.rb, line 11
def permit(subject, *actions)
  control_permissions :permit, subject, *actions
  self
end

Private Instance Methods

control_permissions(type, subject, *actions) click to toggle source
# File lib/act_as_permission_controllable/model.rb, line 52
def control_permissions(type, subject, *actions)
  type = type.to_s

  if subject == :all
    Controller.get_controllers.each do |controller|
      if type == 'permit'
        self.permissions[controller.to_s] = controller.actions.map(&:to_s)
      elsif type == 'ban'
        self.permissions.delete(controller.to_s)
      end
    end
    return
  end

  subject = subject.to_s
  controller = Controller.get_controllers.find do |controller|
    name, key = controller.controller_name, controller.to_s
    subject == key || subject == name || subject == name.singularize
  end

  return if controller.nil?

  key = controller.to_s
  self.permissions[key] ||= []
  if actions == [ :all ]
    actions = controller.actions.map(&:to_s)
  else
    actions = actions.flatten.map(&:to_s)
  end
  if type == 'permit'
    self.permissions[key] += actions
  elsif type == 'ban'
    self.permissions[key] -= actions
  end
  self.permissions[key].uniq!
  self.permissions.delete(key) if self.permissions[key].empty?

  return
end