Module: Rolify

Extended by:
Configure
Defined in:
lib/rolify.rb,
lib/rolify/role.rb,
lib/rolify/utils.rb,
lib/rolify/dynamic.rb,
lib/rolify/finders.rb,
lib/rolify/railtie.rb,
lib/rolify/version.rb,
lib/rolify/resource.rb,
lib/rolify/configure.rb,
lib/rolify/adapters/base.rb,
lib/rolify/adapters/mongoid/scopes.rb,
lib/generators/rolify/user_generator.rb,
lib/generators/rolify/rolify_generator.rb,
lib/rolify/adapters/active_record/scopes.rb,
lib/rolify/adapters/mongoid/role_adapter.rb,
lib/rolify/adapters/mongoid/resource_adapter.rb,
lib/rolify/adapters/active_record/role_adapter.rb,
lib/rolify/adapters/active_record/resource_adapter.rb

Defined Under Namespace

Modules: Adapter, Configure, Dynamic, Finders, Generators, Resource, Role, Utils Classes: Railtie

Constant Summary collapse

VERSION =
"6.0.1"
@@resource_types =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Configure

configure, dynamic_shortcuts, dynamic_shortcuts=, orm, orm=, remove_role_if_empty, remove_role_if_empty=, use_defaults, use_dynamic_shortcuts, use_mongoid

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



11
12
13
# File 'lib/rolify.rb', line 11

def adapter
  @adapter
end

#resource_adapterObject

Returns the value of attribute resource_adapter.



11
12
13
# File 'lib/rolify.rb', line 11

def resource_adapter
  @resource_adapter
end

#role_cnameObject

Returns the value of attribute role_cname.



11
12
13
# File 'lib/rolify.rb', line 11

def role_cname
  @role_cname
end

#role_join_table_nameObject

Returns the value of attribute role_join_table_name.



11
12
13
# File 'lib/rolify.rb', line 11

def role_join_table_name
  @role_join_table_name
end

#role_table_nameObject

Returns the value of attribute role_table_name.



11
12
13
# File 'lib/rolify.rb', line 11

def role_table_name
  @role_table_name
end

#strict_rolifyObject

Returns the value of attribute strict_rolify.



11
12
13
# File 'lib/rolify.rb', line 11

def strict_rolify
  @strict_rolify
end

Class Method Details

.resource_typesObject



72
73
74
# File 'lib/rolify.rb', line 72

def self.resource_types
  @@resource_types
end

Instance Method Details

#resourcify(association_name = :roles, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rolify.rb', line 43

def resourcify(association_name = :roles, options = {})
  include Resource

  options.reverse_merge!({ :role_cname => 'Role', :dependent => :destroy })
  resourcify_options = { :class_name => options[:role_cname].camelize, :as => :resource, :dependent => options[:dependent] }
  self.role_cname = options[:role_cname]
  self.role_table_name = self.role_cname.tableize.gsub(/\//, "_")

  has_many association_name, **resourcify_options

  self.resource_adapter = Rolify::Adapter::Base.create("resource_adapter", self.role_cname, self.name)
  @@resource_types << self.name
end

#role_classObject



67
68
69
70
# File 'lib/rolify.rb', line 67

def role_class
  return self.superclass.role_class unless self.instance_variable_defined? '@role_cname'
  self.role_cname.constantize
end

#rolify(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rolify.rb', line 14

def rolify(options = {})
  include Role
  extend Dynamic if Rolify.dynamic_shortcuts

  options.reverse_merge!({:role_cname => 'Role'})
  self.role_cname = options[:role_cname]
  self.role_table_name = self.role_cname.tableize.gsub(/\//, "_")

  default_join_table = "#{self.to_s.tableize.gsub(/\//, "_")}_#{self.role_table_name}"
  options.reverse_merge!({:role_join_table_name => default_join_table})
  self.role_join_table_name = options[:role_join_table_name]

  rolify_options = { :class_name => options[:role_cname].camelize }
  rolify_options.merge!({ :join_table => self.role_join_table_name }) if Rolify.orm == "active_record"
  rolify_options.merge!(options.reject{ |k,v| ![ :before_add, :after_add, :before_remove, :after_remove, :inverse_of ].include? k.to_sym })

  has_and_belongs_to_many :roles, **rolify_options

  self.adapter = Rolify::Adapter::Base.create("role_adapter", self.role_cname, self.name)

  #use strict roles
  self.strict_rolify = true if options[:strict]
end

#scopifyObject



62
63
64
65
# File 'lib/rolify.rb', line 62

def scopify
  require "rolify/adapters/#{Rolify.orm}/scopes.rb"
  extend Rolify::Adapter::Scopes
end