Method: Strongbolt::BoltedController::ClassMethods#model_for_authorization

Defined in:
lib/strongbolt/bolted_controller.rb

#model_for_authorizationObject

Returns the model used for authorization, using controller name if not defined



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/strongbolt/bolted_controller.rb', line 35

def model_for_authorization
  if @model_for_authorization.present?
    @model_for_authorization
  else
    # We cannot just do controller_name.classify as it doesn't keep the modules
    # We'll also check demoduling one module after the other for case when
    # the controller and/or the model have different modules
    full_name = name.sub('Controller', '').classify
    # Split by ::
    splits = full_name.split('::')
    # While we still have modules to test
    while splits.size >= 1
      begin
        return constantize_model splits.join('::')
      rescue Strongbolt::ModelNotFound
      ensure
        # Removes first element
        splits.shift
      end
    end
    raise Strongbolt::ModelNotFound, "Model for controller #{controller_name} wasn't found"
  end
end