Class: Services::CanCan::LoadAbilities

Inherits:
ApiMaker::BaseService
  • Object
show all
Defined in:
app/api_maker/services/can_can/load_abilities.rb

Instance Method Summary collapse

Instance Method Details

#performObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/api_maker/services/can_can/load_abilities.rb', line 2

def perform
  result = []

  request.each do |ability_data|
    # Sometimes Rails passes a hash instead of an array
    ability_data = ability_data.fetch(1) if ability_data.is_a?(Array)

    ability = ability_data.fetch("ability")
    subject = ability_data.fetch("subject")
    subject_to_check = subject

    # Convert subject to original model class if resource is given
    subject_to_check = subject.model_class if subject.is_a?(Class) && subject < ApiMaker::BaseResource

    can = current_ability.can?(ability.to_sym, subject_to_check)
    result << {
      ability: ability,
      can: can,
      subject: subject
    }
  end

  succeed!(abilities: result)
end

#requestObject



27
28
29
# File 'app/api_maker/services/can_can/load_abilities.rb', line 27

def request
  @request ||= args.fetch(:request)
end