Module: Garage::ControllerHelper
- Extended by:
- ActiveSupport::Concern
- Includes:
- Utils
- Included in:
- Meta::DocsController, Meta::ServicesController
- Defined in:
- lib/garage/controller_helper.rb
Instance Attribute Summary collapse
-
#field_selector ⇒ Object
Returns the value of attribute field_selector.
-
#representation ⇒ Object
Returns the value of attribute representation.
Instance Method Summary collapse
- #allow_access?(klass, action = :read) ⇒ Boolean
- #cache_context ⇒ Object
-
#current_resource_owner ⇒ Object
Implement by using
resource_owner_idlike:. -
#doorkeeper_token ⇒ Object
For backword compatiblility.
-
#has_scope?(scope) ⇒ Boolean
Public: returns if the current request includes the given OAuth scope.
-
#requested_by?(resource) ⇒ Boolean
Check if the current resource is the same as the requester.
- #resource_owner_id ⇒ Object
-
#unauthorized_render_options(error: nil) ⇒ Hash
Use this method to render ‘unauthorized’.
Instance Attribute Details
#field_selector ⇒ Object
Returns the value of attribute field_selector.
76 77 78 |
# File 'lib/garage/controller_helper.rb', line 76 def field_selector @field_selector end |
#representation ⇒ Object
Returns the value of attribute representation.
76 77 78 |
# File 'lib/garage/controller_helper.rb', line 76 def representation @representation end |
Instance Method Details
#allow_access?(klass, action = :read) ⇒ Boolean
78 79 80 |
# File 'lib/garage/controller_helper.rb', line 78 def allow_access?(klass, action = :read) ability_from_token.allow?(klass, action) end |
#cache_context ⇒ Object
72 73 74 |
# File 'lib/garage/controller_helper.rb', line 72 def cache_context { t: access_token.try(:id) } end |
#current_resource_owner ⇒ Object
Implement by using resource_owner_id like:
def current_resource_owner
@current_resource_owner ||= User.find(resource_owner_id) if resource_owner_id
end
47 48 49 |
# File 'lib/garage/controller_helper.rb', line 47 def current_resource_owner raise "Your ApplicationController needs to implement current_resource_owner!" end |
#doorkeeper_token ⇒ Object
For backword compatiblility.
26 27 28 |
# File 'lib/garage/controller_helper.rb', line 26 def doorkeeper_token access_token end |
#has_scope?(scope) ⇒ Boolean
Public: returns if the current request includes the given OAuth scope
68 69 70 |
# File 'lib/garage/controller_helper.rb', line 68 def has_scope?(scope) access_token && access_token.scopes.include?(scope) end |
#requested_by?(resource) ⇒ Boolean
Check if the current resource is the same as the requester. The resource must respond to resource.id method.
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/garage/controller_helper.rb', line 53 def requested_by?(resource) user = resource.respond_to?(:owner) ? resource.owner : resource case when current_resource_owner.nil? false when !user.is_a?(current_resource_owner.class) false when current_resource_owner.id == user.id true else false end end |
#resource_owner_id ⇒ Object
30 31 32 |
# File 'lib/garage/controller_helper.rb', line 30 def resource_owner_id access_token.try(:resource_owner_id) end |
#unauthorized_render_options(error: nil) ⇒ Hash
Use this method to render ‘unauthorized’. Garage user may overwrite this method to response custom unauthorized response.
37 38 39 |
# File 'lib/garage/controller_helper.rb', line 37 def (error: nil) { json: { status_code: 401, error: "Unauthorized (invalid token)" } } end |