Class: Clerk::Account::RolesWrapper

Inherits:
Object
  • Object
show all
Defined in:
app/models/clerk/account.rb

Instance Method Summary collapse

Constructor Details

#initialize(instance, target) ⇒ RolesWrapper

Returns a new instance of RolesWrapper.



68
69
70
71
# File 'app/models/clerk/account.rb', line 68

def initialize(instance, target)
  @instance = instance
  @target_class = target.to_s.classify.constantize
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



98
99
100
# File 'app/models/clerk/account.rb', line 98

def method_missing(m, *args, &block)
  no_with.send(m, *args, &block)
end

Instance Method Details

#inspectObject



102
103
104
# File 'app/models/clerk/account.rb', line 102

def inspect
  no_with.inspect
end

#no_withObject



92
93
94
95
96
# File 'app/models/clerk/account.rb', line 92

def no_with
  @target_class.where( 
    id: @instance.roles.where(scope_class: @target_class.name).pluck(:scope_id) 
  )
end

#with(role: nil, permission: nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/clerk/account.rb', line 73

def with(role: nil, permission: nil)
  if (role.nil? and permission.nil?) or (not role.nil? and not permission.nil?)
    raise ArgumentError.new("Invalid argument, must supply either a role or permission")
  end

  if not role.nil?
    return @target_class.where( 
      id: @instance.roles.where(scope_class: @target_class.name, name: role).pluck(:scope_id) 
    )
  end

  if not permission.nil?
    roles = @target_class.roles_with_permission(permission)
    return @target_class.where(
      id: @instance.roles.where(scope_class: @target_class.name, name: roles).pluck(:scope_id)
    )            
  end
end