Sha256: 7a5750838aceebf9dc53036289864d5d91d70d1a9d539b7cbcebbc9a83e25f52

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

require "effective_roles/engine"
require "effective_roles/version"

module EffectiveRoles
  mattr_accessor :roles
  mattr_accessor :assignable_roles
  mattr_accessor :role_descriptions

  def self.setup
    yield self
  end

  def self.roles_collection(obj = nil, user = nil)
    raise ArgumentError.new('EffectiveRoles config.role_descriptions must be a Hash.  The Array syntax is deprecated.') unless EffectiveRoles.role_descriptions.kind_of?(Hash)

    descriptions = role_descriptions[obj.try(:class).to_s] || role_descriptions || {}

    assignable_roles_for(user, obj).map do |role|
      ["#{role}<br>#{descriptions[role]}".html_safe, role]
    end
  end

  def self.assignable_roles_for(user, obj = nil)
    return roles unless user.respond_to?(:is_role_restricted?) # All roles, if the user (or object) is not role_resticted

    assignable = assignable_roles[obj.try(:class).to_s] || assignable_roles || {}

    if assignable.present?
      user.roles.map { |role| assignable[role] }.flatten.compact.uniq
    else
      roles
    end
  end


end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
effective_roles-1.0.1 lib/effective_roles.rb
effective_roles-1.0.0 lib/effective_roles.rb