Module: Strongbolt::BoltedController::ClassMethods

Defined in:
lib/strongbolt/bolted_controller.rb

Instance Method Summary collapse

Instance Method Details

#actions_mappingObject

Returns the actions mapping of this controller

[View source]

120
121
122
123
# File 'lib/strongbolt/bolted_controller.rb', line 120

def actions_mapping
  # Defaults to a duplicate of the standard mapping
  @actions_mapping ||= ACTIONS_MAPPING.dup
end

#model_for_authorizationObject

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

[View source]

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

#model_for_authorization=(model) ⇒ Object

Allows defining a specific model for this controller, if it cannot be infer from the controller name

[View source]

21
22
23
24
25
26
27
28
29
# File 'lib/strongbolt/bolted_controller.rb', line 21

def model_for_authorization=(model)
  @model_for_authorization = case model
                             when Class then model
                             when String then constantize_model(model)
                             when nil then nil
                             else
                               raise ArgumentError, 'Model for authorization must be a Class or the name of the Class'
                             end
end

#render_with_authorizationObject

Render with authorization

[View source]

113
114
115
# File 'lib/strongbolt/bolted_controller.rb', line 113

def render_with_authorization
  self.actions_without_authorization = nil
end

#render_without_authorization(*actions) ⇒ Object

Render without authorization, for better performance

[View source]

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/strongbolt/bolted_controller.rb', line 92

def render_without_authorization(*actions)
  self.actions_without_authorization = [*actions]

  class_eval do
    #
    # It needs to be created afterward,
    # No idea why
    #
    def render(*args)
      if render_without_authorization?
        Strongbolt.without_authorization { _render(*args) }
      else
        _render(*args)
      end
    end
  end
end

#skip_all_authorization(opts = {}) ⇒ Object

Skip all authorization checking for the controller, or a subset of actions

[View source]

73
74
75
76
# File 'lib/strongbolt/bolted_controller.rb', line 73

def skip_all_authorization(opts = {})
  skip_controller_authorization opts
  around_action :disable_authorization, opts
end

#skip_controller_authorization(opts = {}) ⇒ Object

Skips controller authorization check for this controller No argument given will skip for all actions, and can be passed only: [] or except: []

[View source]

64
65
66
67
# File 'lib/strongbolt/bolted_controller.rb', line 64

def skip_controller_authorization(opts = {})
  opts = { raise: false }.merge(opts) if Rails::VERSION::MAJOR >= 5
  skip_before_action :check_authorization, opts
end