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



72
73
74
75
76
77
78
79
# File 'app/controllers/api/v1/base_controller.rb', line 72

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



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

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

  head status: 204
end

#indexObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/api/v1/base_controller.rb', line 29

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



63
64
65
66
# File 'app/controllers/api/v1/base_controller.rb', line 63

def search
  index
  render :index
end

#showObject



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

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

#updateObject



81
82
83
84
85
# File 'app/controllers/api/v1/base_controller.rb', line 81

def update
  @record.update_attributes(request_params)

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