Method: Api::V2::ApplicationController#index

Defined in:
app/controllers/api/v2/application_controller.rb

#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
# File 'app/controllers/api/v2/application_controller.rb', line 15

def index
    authorize! :index, @model

    # Custom Action
    status, result = check_for_custom_action
    return render json: result, status: 200 if status == true

    # Normal Index Action with Ransack querying
    @q = (@model.column_names.include?("user_id") ? @model.where(user_id: current_user.id) : @model).ransack(@query.presence|| params[:q])
    @records_all = @q.result(distinct: true)
    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