Module: Api::Controllers::Base

Extended by:
ActiveSupport::Concern
Included in:
V1::ApplicationController
Defined in:
app/controllers/concerns/api/controllers/base.rb

Defined Under Namespace

Classes: NotAuthenticatedError

Instance Method Summary collapse

Instance Method Details

#apply_filtersObject



114
115
116
117
118
119
120
121
122
# File 'app/controllers/concerns/api/controllers/base.rb', line 114

def apply_filters
  # An empty method that descendant controllers can override
  # A possible implementation might look like:
  #
  # self.collection = collection.where(status: params[:filter_status]) if params[:filter_status]
  #
  # Keep in mind that for adding filters to auto-generated documentation, you
  # will need to override index.yaml.erb or _paths.yaml.erbs.
end

#apply_paginationObject



124
125
126
127
128
129
130
131
# File 'app/controllers/concerns/api/controllers/base.rb', line 124

def apply_pagination
  pagination_collection = collection.order(id: :asc)
  if params[:after]
    pagination_collection = pagination_collection.where("id > ?", params[:after])
  end
  @pagy, pagination_collection = pagy(pagination_collection)
  self.collection = pagination_collection
end

#collectionObject



105
106
107
# File 'app/controllers/concerns/api/controllers/base.rb', line 105

def collection
  @collection ||= instance_variable_get(collection_variable)
end

#collection=(new_collection) ⇒ Object



109
110
111
112
# File 'app/controllers/concerns/api/controllers/base.rb', line 109

def collection=(new_collection)
  @collection = new_collection
  instance_variable_set collection_variable, new_collection
end

#collection_variableObject



101
102
103
# File 'app/controllers/concerns/api/controllers/base.rb', line 101

def collection_variable
  @collection_variable ||= "@#{self.class.name.split("::").last.gsub("Controller", "").underscore}"
end

#current_membershipObject



97
98
99
# File 'app/controllers/concerns/api/controllers/base.rb', line 97

def current_membership
  current_user.memberships.where(team: current_team).first
end

#current_teamObject



92
93
94
95
# File 'app/controllers/concerns/api/controllers/base.rb', line 92

def current_team
  # Application agents are users but only have one team.
  current_user&.teams&.first
end

#current_userObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/concerns/api/controllers/base.rb', line 72

def current_user
  @current_user ||= if doorkeeper_token
    User.find_by(id: doorkeeper_token[:resource_owner_id])
  else
    warden.authenticate(scope: :user)
  end

  # TODO Remove this rescue once workspace clusters can write to this column on the identity server.
  if doorkeeper_token
    begin
      doorkeeper_token.update(last_used_at: Time.zone.now)
    rescue ActiveRecord::StatementInvalid => _
    end
  end

  raise NotAuthenticatedError unless @current_user

  @current_user
end

#permitted_arraysObject



65
66
67
# File 'app/controllers/concerns/api/controllers/base.rb', line 65

def permitted_arrays
  {}
end

#permitted_fieldsObject



61
62
63
# File 'app/controllers/concerns/api/controllers/base.rb', line 61

def permitted_fields
  []
end

#process_params(strong_params) ⇒ Object



69
70
# File 'app/controllers/concerns/api/controllers/base.rb', line 69

def process_params(strong_params)
end

#set_default_response_formatObject



133
134
135
# File 'app/controllers/concerns/api/controllers/base.rb', line 133

def set_default_response_format
  request.format = :json
end