Class: Api::V1::BaseController

Inherits:
ActionController::API
  • Object
show all
Includes:
ActiveHashRelation, CanCan::ControllerAdditions
Defined in:
app/controllers/api/v1/base_controller.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_userObject

rescue_from Pundit::NotAuthorizedError, with: :unauthorized!



27
28
29
# File 'app/controllers/api/v1/base_controller.rb', line 27

def current_user
  @current_user
end

Instance Method Details

#createObject



77
78
79
80
81
82
83
84
# File 'app/controllers/api/v1/base_controller.rb', line 77

def create
  @record =  @model.new(request_params)
  @record.user_id = current_user.id if @model.column_names.include? "user_id"

  @record.save!

  render json: @record.to_json(@json_attrs || {}), status: 201
end

#destroyObject



92
93
94
95
96
97
98
# File 'app/controllers/api/v1/base_controller.rb', line 92

def destroy
  unless @record.destroy
     return api_error(status: 500)
  end

  render json: {message: "Deleted"}, status: 200
end

#indexObject

Disabling Strong Parameters def params

request.parameters

end



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/api/v1/base_controller.rb', line 34

def index
  # find the records
  @q = (@model.column_names.include?("user_id") ? @model.where(user_id: current_user.id) : @model).ransack(params[:q])
  @records_all = @q.result(distinct: true)
  @records = @records_all.page(params[:page]).per(params[:per])

  # If there's the keyword pagination_info, then return a pagination info object
  return render json: MultiJson.dump({
    count: @records_all.count,
    current_page_count: @records.count,
    next_page: @records.next_page,
    prev_page: @records.prev_page,
    is_first_page: @records.first_page?,
    is_last_page: @records.last_page?,
    is_out_of_range: @records.out_of_range?,
    pages_count: @records.total_pages,
    current_page_number: @records.current_page
  }) if !params[:pages_info].nil?
  # If it's asked for page number, the paginate
  return render json: MultiJson.dump(@records, @json_attrs || {}) if !params[:page].nil? # (@json_attrs || {})
  # if you ask for count, then return a json object with just the number of objects
  return render json: MultiJson.dump({count: @records_all.count}) if !params[:count].nil?
  # Default
  render json: MultiJson.dump(@records_all, @json_attrs || {}) #(@json_attrs || {})
end

#searchObject

def count

# find the records
@q = (@model.column_names.include?("user_id") ? @model.where(user_id: current_user.id) : @model).ransack(params[:q])
@records_all = @q.result(distinct: true)
# if you ask for count, then return a json object with just the number of objects
return render json: {count: @records_all.count}.to_json

end



68
69
70
71
# File 'app/controllers/api/v1/base_controller.rb', line 68

def search
  index
  render :index
end

#showObject



73
74
75
# File 'app/controllers/api/v1/base_controller.rb', line 73

def show
  render json: @record.to_json(@json_attrs || {}), status: 200
end

#updateObject



86
87
88
89
90
# File 'app/controllers/api/v1/base_controller.rb', line 86

def update
  @record.update_attributes(request_params)

  render json: @record.to_json(@json_attrs || {}), status: 200
end