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.



89
90
91
92
# File 'app/models/clerk/account.rb', line 89

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



119
120
121
# File 'app/models/clerk/account.rb', line 119

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

Instance Method Details

#inspectObject



123
124
125
# File 'app/models/clerk/account.rb', line 123

def inspect
  no_with.inspect
end

#no_withObject



113
114
115
116
117
# File 'app/models/clerk/account.rb', line 113

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/models/clerk/account.rb', line 94

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