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, 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



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/api/v2/application_controller.rb', line 63

def create
    # Normal Create Action
    @record = @model.new(@body)
    authorize! :create, @record
    # 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.save!
    render json: @record.to_json(json_attrs), status: 201
end

#destroyObject



90
91
92
93
94
95
96
97
98
99
100
# File 'app/controllers/api/v2/application_controller.rb', line 90

def destroy
    authorize! :destroy, @record

    # 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

#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
# 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])
    # Paging
    @records = @records_all.page(page).per(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 !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: MultiJson.dump(@records, 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: MultiJson.dump({count: @records_all.count}) if !count.blank?
    #puts "Default"
    json_out = MultiJson.dump(@records_all, json_attrs)
    #puts "JSON ATTRS: #{json_attrs}"
    #puts "JSON OUT: #{json_out}"
    render json: json_out, status: status #(@json_attrs || {})
end

#showObject



51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/api/v2/application_controller.rb', line 51

def show
    authorize! :show, @record_id

    # 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



77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/api/v2/application_controller.rb', line 77

def update
    authorize! :update, @record

    # 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
    # Raisl 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