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

Returns the value of attribute current_user.



35
36
37
# File 'app/controllers/api/v1/base_controller.rb', line 35

def current_user
  @current_user
end

Instance Method Details

#checkObject

Disabling Strong Parameters def params

request.parameters

end



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/api/v1/base_controller.rb', line 42

def check
  # This method is only valid for ActiveRecords
  # For any other model-less controller, the actions must be 
  # defined in the route, and must exist in the controller definition.
  # So, if it's not an activerecord, the find model makes no sense at all
  # Thus must return 404
  path = params[:path].split("/")
  return not_found! if (!path.first.classify.constantize.new.is_a? ActiveRecord::Base rescue false)
  find_model path.first
  if request.get?
    if path.second.blank?
      @page = params[:page]
      @per = params[:per]
      @pages_info = params[:pages_info]
      @count = params[:count]
      @query = params[:q]
      index
    elsif path.second.to_i.zero?
      # String, so it's a custom action I must find in the @model (as a singleton method)
      # GET :controller/:custom_action
      return not_found! unless @model.respond_to?(path.second)
      return render json: MultiJson.dump(@model.send(path.second, params)), status: 200
    elsif !path.second.to_i.zero? && path.third.blank?
      # Integer, so it's an ID, I must show it
      # Rails.logger.debug "IL SECONDO è ID? #{path.second.inspect}"
      # find_record path.second.to_i
      @record_id =  path.second.to_i
      find_record
      show
    elsif !path.second.to_i.zero? && !path.third.blank?
      # GET :controller/:id/:custom_action
      return not_found! unless @model.respond_to?(path.third)
      return render json: MultiJson.dump(@model.send(path.third, path.second.to_i, params)), status: 200
    end
  elsif request.post?
    if path.second.blank?
      @params = params
      create
    elsif path.second.to_i.zero?
      # POST :controller/:custom_action
      return not_found! unless @model.respond_to?(path.second)
      return render json: MultiJson.dump(@model.send(path.second, params)), status: 200
    end
  elsif request.put?
    if !path.second.to_i.zero? && path.third.blank?
      @params = params
      # Rails.logger.debug "IL SECONDO è ID in PUT? #{path.second.inspect}"
      # find_record path.second.to_i
      @record_id =  path.second.to_i
      find_record
      update
    elsif !path.second.to_i.zero? && !path.third.blank?
      # PUT :controller/:id/:custom_action
      return not_found! unless @model.respond_to?(path.third)
      return render json: MultiJson.dump(@model.send(path.third, path.second.to_i, params)), status: 200
    end
  elsif request.delete?
    # Rails.logger.debug "IL SECONDO è ID in delete? #{path.second.inspect}"
    # find_record path.second.to_i
    @record_id =  path.second.to_i
    find_record
    destroy
  end
end

#createObject



158
159
160
161
162
163
164
165
# File 'app/controllers/api/v1/base_controller.rb', line 158

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



173
174
175
176
177
178
179
# File 'app/controllers/api/v1/base_controller.rb', line 173

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

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

#indexObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/controllers/api/v1/base_controller.rb', line 107

def index
  # Rails.logger.debug params.inspect
  # find the records
  @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])
  @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?
  
  status = @records_all.blank? ? 404 : 200
  # If it's asked for page number, then paginate
  return render json: MultiJson.dump(@records, json_attrs), status: status if !page.blank? # (@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 !count.blank?
  # Default
  render json: MultiJson.dump(@records_all, json_attrs), status: status #(@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



148
149
150
151
# File 'app/controllers/api/v1/base_controller.rb', line 148

def search
  index
  render :index
end

#showObject



153
154
155
156
# File 'app/controllers/api/v1/base_controller.rb', line 153

def show
  result = @record.to_json(json_attrs)
  render json: result, status: result.blank? ? 404 : 200
end

#updateObject



167
168
169
170
171
# File 'app/controllers/api/v1/base_controller.rb', line 167

def update
  @record.update_attributes(request_params)

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