Module: RoleFu::Roleable::ClassMethods

Defined in:
lib/role_fu/roleable.rb

Instance Method Summary collapse

Instance Method Details

#role_fu_alias(name) ⇒ Object

Dynamically create aliases for role methods

Examples:

role_fu_alias :group
# Creates: add_group, remove_group, has_group?, with_group, in_group, etc.

Parameters:

  • name (Symbol, String)

    The alias name (e.g. :group)



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/role_fu/roleable.rb', line 70

def role_fu_alias(name)
  singular = name.to_s.singularize
  plural = name.to_s.pluralize

  # Instance methods
  alias_method "add_#{singular}", :add_role
  alias_method "remove_#{singular}", :remove_role
  alias_method "has_#{singular}?", :has_role?
  alias_method "only_has_#{singular}?", :only_has_role?
  alias_method "has_any_#{singular}?", :has_any_role?
  alias_method "has_all_#{plural}?", :has_all_roles?
  alias_method "#{plural}_name", :roles_name

  # Class methods (Scopes)
  singleton_class.class_eval do
    alias_method "with_#{singular}", :with_role
    alias_method "without_#{singular}", :without_role
    alias_method "with_any_#{singular}", :with_any_role
    alias_method "with_all_#{plural}", :with_all_roles

    # Additional natural aliases
    alias_method "in_#{singular}", :with_role
    alias_method "not_in_#{singular}", :without_role
  end
end

#role_fu_options(options = {}) ⇒ Object



19
20
21
# File 'lib/role_fu/roleable.rb', line 19

def role_fu_options(options = {})
  self.role_fu_callbacks = options.slice(:before_add, :after_add, :before_remove, :after_remove)
end

#with_all_roles(*args) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/role_fu/roleable.rb', line 55

def with_all_roles(*args)
  ids = nil
  args.each do |arg|
    current_ids = arg.is_a?(Hash) ? with_role(arg[:name], arg[:resource]).pluck(:id) : with_role(arg).pluck(:id)
    ids = ids.nil? ? current_ids : ids & current_ids
    return none if ids.empty?
  end
  where(id: ids)
end

#with_any_role(*args) ⇒ Object



48
49
50
51
52
53
# File 'lib/role_fu/roleable.rb', line 48

def with_any_role(*args)
  ids = args.flat_map do |arg|
    arg.is_a?(Hash) ? with_role(arg[:name], arg[:resource]).pluck(:id) : with_role(arg).pluck(:id)
  end
  where(id: ids.uniq)
end

#with_role(role_name, resource = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/role_fu/roleable.rb', line 23

def with_role(role_name, resource = nil)
  role_table = RoleFu.role_class.table_name
  assignment_table = RoleFu.role_assignment_class.table_name

  query = joins(:roles).where(role_table => {name: role_name.to_s})

  if RoleFu.role_assignment_class.column_names.include?("expires_at")
    query = query.where("#{assignment_table}.expires_at IS NULL OR #{assignment_table}.expires_at > ?", Time.current)
  end

  if resource.nil?
    query.where(role_table => {resource_type: nil, resource_id: nil})
  elsif resource == :any
    query
  elsif resource.is_a?(Class)
    query.where(role_table => {resource_type: resource.to_s, resource_id: nil})
  else
    query.where(role_table => {resource_type: resource.class.name, resource_id: resource.id})
  end.distinct
end

#without_role(role_name, resource = nil) ⇒ Object



44
45
46
# File 'lib/role_fu/roleable.rb', line 44

def without_role(role_name, resource = nil)
  where.not(id: with_role(role_name, resource).select(:id))
end