Class: Api::V3::ApplicationController

Inherits:
ActionController::API
  • Object
show all
Defined in:
app/controllers/api/v3/application_controller.rb

Defined Under Namespace

Classes: AccessDenied, PageNotFound, ParameterValueNotAllowed

Instance Method Summary collapse

Instance Method Details

#admin?Boolean

Returns:

  • (Boolean)

71
72
73
74
# File 'app/controllers/api/v3/application_controller.rb', line 71

def admin?
  return false if current_user.blank?
  current_user.admin?
end

#can?(*args) ⇒ Boolean

Returns:

  • (Boolean)

67
68
69
# File 'app/controllers/api/v3/application_controller.rb', line 67

def can?(*args)
  current_ability.can?(*args)
end

#current_abilityObject


63
64
65
# File 'app/controllers/api/v3/application_controller.rb', line 63

def current_ability
  @current_ability ||= Ability.new(current_user)
end

#current_userObject


59
60
61
# File 'app/controllers/api/v3/application_controller.rb', line 59

def current_user
  @current_user ||= User.find_by_id(doorkeeper_token.resource_owner_id) if doorkeeper_token
end

#error!(data, status_code = 400) ⇒ Object


51
52
53
# File 'app/controllers/api/v3/application_controller.rb', line 51

def error!(data, status_code = 400)
  render json: data, status: status_code
end

#error_404!Object


55
56
57
# File 'app/controllers/api/v3/application_controller.rb', line 55

def error_404!
  error!({ 'error' => 'Page not found' }, 404)
end

#optional!(name, opts = {}) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/api/v3/application_controller.rb', line 34

def optional!(name, opts = {})
  if params[name].blank? && opts[:require] == true
    raise ActionController::ParameterMissing.new(name)
  end

  if opts[:values] && params[name].present?
    values = opts[:values].to_a
    if !values.include?(params[name]) && !values.include?(params[name].to_i)
      raise ParameterValueNotAllowed.new(name, opts[:values])
    end
  end

  if params[name].blank? && opts[:default].present?
    params[name] = opts[:default]
  end
end

#requires!(name, opts = {}) ⇒ Object


29
30
31
32
# File 'app/controllers/api/v3/application_controller.rb', line 29

def requires!(name, opts = {})
  opts[:require] = true
  optional!(name, opts)
end