Class: Clerk::Account::RolesWrapper

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

Instance Method Summary collapse

Constructor Details

#initialize(instance, target) ⇒ RolesWrapper

Returns a new instance of RolesWrapper.



108
109
110
111
# File 'lib/clerk/account.rb', line 108

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



138
139
140
# File 'lib/clerk/account.rb', line 138

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

Instance Method Details

#inspectObject



142
143
144
# File 'lib/clerk/account.rb', line 142

def inspect
  no_with.inspect
end

#no_withObject



132
133
134
135
136
# File 'lib/clerk/account.rb', line 132

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/clerk/account.rb', line 113

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