Class: Api::V2::ApplicationController

Inherits:
ActionController::API
  • Object
show all
Includes:
ApiExceptionManagement, CanCan::ControllerAdditions, HttpAcceptLanguage::AutoLocale
Defined in:
app/controllers/api/v2/application_controller.rb

Direct Known Subclasses

InfoController, RawController, UsersController

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_userObject

Returns the value of attribute current_user.



8
9
10
# File 'app/controllers/api/v2/application_controller.rb', line 8

def current_user
  @current_user
end

Instance Method Details

#createObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/api/v2/application_controller.rb', line 67

def create
  # Normal Create Action
  Rails.logger.debug("Creating a new record #{@record}")
  authorize! :create, @record.presence || @model
  # Custom Action
  status, result, status_number = check_for_custom_action
  return render json: result, status: (status_number.presence || 200) if status == true
  # Keeping this automation can be too dangerous and lead to unpredicted results
  # TODO: Remove it
  # @record.user_id = current_user.id if @model.column_names.include? "user_id"
  @record = @model.new(@body)
  @record.save!
  render json: @record.to_json(json_attrs), status: 201
end

#destroyObject



105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/api/v2/application_controller.rb', line 105

def destroy
  authorize! :destroy, @record.presence || @model

  # Custom Action
  status, result, status_number = check_for_custom_action
  return render json: result, status: (status_number.presence || 200) if status == true

  # Normal Destroy Action
  return api_error(status: 500) unless @record.destroy
  head :ok
end

#destroy_multiObject



117
118
119
120
121
122
123
124
# File 'app/controllers/api/v2/application_controller.rb', line 117

def destroy_multi
  authorize! :destroy, @model

  # Normal Destroy Action
  ids = params[:ids].split(",")
  @model.where(id: ids).destroy!(@body)
  render json: ids.to_json, status: 200
end

#indexObject

GET :controller/



15
16
17
18
19
20
21
22
23
24
25
26
27
28
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/v2/application_controller.rb', line 15

def index
  authorize! :index, @model

  # Custom Action
  status, result, status_number = check_for_custom_action
  return render json: result, status: (status_number.presence || 200) if status == true

  # Normal Index Action with Ransack querying
  # Keeping this automation can be too dangerous and lead to unpredicted results
  # TODO: Remove it
  # @q = (@model.column_names.include?("user_id") ? @model.where(user_id: current_user.id) : @model).ransack(@query.presence|| params[:q])
  @q = @model.ransack(@query.presence || params[:q])
  @records_all = @q.result # (distinct: true) Removing, but I'm not sure, with it I cannot sort in postgres for associated records (throws an exception on misuse of sort with distinct)
  page = (@page.presence || params[:page])
  per = (@per.presence || params[:per])
  # pages_info = (@pages_info.presence || params[:pages_info])
  count = (@count.presence || params[:count])
  # Pagination
  @records = @records_all.page(page).per(per)
  # Content-Range: posts 0-4/27
  range_start = [(page.to_i - 1) * per.to_i, 0].max
  range_end = [0, page.to_i * per.to_i - 1].max
  response.set_header("Content-Range", "#{@model.table_name} #{range_start}-#{range_end}/#{@records.total_count}")

  # If there's the keyword pagination_info, then return a pagination info object
  # return render json: {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 !pages_info.blank?

  # puts "ALL RECORDS FOUND: #{@records_all.inspect}"
  status = @records_all.blank? ? 404 : 200
  # puts "If it's asked for page number, then paginate"
  return render json: @records.as_json(json_attrs), status: status if !page.blank? # (@json_attrs || {})
  #puts "if you ask for count, then return a json object with just the number of objects"
  return render json: { count: @records_all.count } if !count.blank?
  #puts "Default"
  json_out = @records_all.as_json(json_attrs)
  #puts "JSON ATTRS: #{json_attrs}"
  #puts "JSON OUT: #{json_out}"
  render json: json_out, status: status #(@json_attrs || {})
end

#showObject



55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/api/v2/application_controller.rb', line 55

def show
  authorize! :show, @record_id.presence || @model

  # Custom Show Action
  status, result, status_number = check_for_custom_action
  return render json: result, status: (status_number.presence || 200) if status == true

  # Normal Show
  result = @record.to_json(json_attrs)
  render json: result, status: 200
end

#updateObject Also known as: patch



82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/api/v2/application_controller.rb', line 82

def update
  authorize! :update, @record.presence || @model

  # Custom Action
  status, result, status_number = check_for_custom_action
  return render json: result, status: (status_number.presence || 200) if status == true

  # Normal Update Action
  # Rails 6 vs Rails 6.1
  @record.respond_to?("update_attributes!") ? @record.update_attributes!(@body) : @record.update!(@body)
  render json: @record.to_json(json_attrs), status: 200
end

#update_multiObject



98
99
100
101
102
103
# File 'app/controllers/api/v2/application_controller.rb', line 98

def update_multi
  authorize! :update, @model
  ids = params[:ids].split(",")
  @model.where(id: ids).update!(@body)
  render json: ids.to_json, status: 200
end