thecore_api 1.2.9 → 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ee696aacf1b0d3c09bfd32c87b50c0a80b23885aa8e9c2d502feb18c8143acfe
4
- data.tar.gz: 334177918179c064d4e4befd95706689d4b956352259bb80088bbb9bca2c3e90
3
+ metadata.gz: 652a914141b6f13ca7a4c4bb38cd2d1f5d18ad2176f172d4d41c97b8957a6052
4
+ data.tar.gz: 4751c908c3b8ae4fdf0b241654e9e201a14d3041cb8e8082dc97f03b28ddfb6e
5
5
  SHA512:
6
- metadata.gz: 04750ed5e942de5053e953c2019d5184bc5c04aeb0fcf9cb6759301c7456ad467fb4a93218d8d7fedcf5c88e468f389e71cf332c0f91d2f2da56b87305d1fff2
7
- data.tar.gz: 19af2ccd66a3a0873ae2555d51704e042461e2397605079bec154733806f6862d620346c1c09758e3af6a6f9fad84740bfa89deebacdfb5aaec6adaac8e39d42
6
+ metadata.gz: a556839a53c08cd73e3fd041e276028df41f5cfcc322a8a1bec46873f091be6ed010ca4ef736adb0fbd627c0ed1a19b06039addc035af4c4b2a049ba5ba0bdaa
7
+ data.tar.gz: 1ca71d563bdb7e1676ce5f9e004a881e30554ad6c990223da23569297be3f09e1df1c75cb98e21435ea488ae47188a8ed6a4236722ccc2410e473cf2e40104a9
@@ -1,3 +1,11 @@
1
+ # Rails.application.routes.disable_clear_and_finalize = true
2
+ # Rails.application.routes.draw do
3
+ # namespace :api do
4
+ # namespace :v1, defaults: { format: :json } do
5
+
6
+ # end
7
+ # end
8
+ # end
1
9
  # https://labs.kollegorna.se/blog/2015/04/build-an-api-now/
2
10
  class Api::V1::BaseController < ActionController::API
3
11
  include CanCan::ControllerAdditions
@@ -14,7 +22,7 @@ class Api::V1::BaseController < ActionController::API
14
22
  before_action :destroy_session
15
23
 
16
24
  before_action :authenticate_user!
17
- before_action :find_model, except: [ :version, :token, :available_roles ]
25
+ before_action :find_model, except: [ :version, :token, :available_roles, :check ]
18
26
  before_action :find_record, only: [ :show, :update, :destroy ]
19
27
 
20
28
  rescue_from ActiveRecord::RecordNotFound, with: :not_found!
@@ -31,11 +39,54 @@ class Api::V1::BaseController < ActionController::API
31
39
  # request.parameters
32
40
  # end
33
41
 
42
+ def check
43
+ path = params[:path].split("/")
44
+ find_model path.first
45
+ if request.get?
46
+ if path.second.blank?
47
+ @page = params[:page]
48
+ @per = params[:per]
49
+ @pages_info = params[:pages_info]
50
+ @count = params[:count]
51
+ @query = params[:q]
52
+ index
53
+ elsif path.second.to_i.zero?
54
+ # String, so it's a custom action I must find in the @model (as an singleton method)
55
+ # Like: :controller/:custom_action
56
+ return render json: MultiJson.dump(@model.send(path.second, params))
57
+ elsif !path.second.to_i.zero? && path.third.blank?
58
+ # Integer, so it's an ID, I must show it
59
+ find_record path.second.to_i
60
+ show
61
+ elsif !path.second.to_i.zero? && !path.third.blank?
62
+ # Like :controller/:id/:custom_action
63
+ return render json: MultiJson.dump(@model.send(path.third, path.second.to_i, params))
64
+ end
65
+ elsif request.post?
66
+ # Non sono certo che i request params gli arrivino... Domani da testare
67
+ # Il body come glielo passo?
68
+ create
69
+ elsif request.put?
70
+ # Non sono certo che i request params gli arrivino... Domani da testare
71
+ # Il body come glielo passo?
72
+ find_record path.second.to_i
73
+ update
74
+ elsif request.delete?
75
+ find_record path.second.to_i
76
+ destroy
77
+ end
78
+ end
79
+
34
80
  def index
81
+ # Rails.logger.debug params.inspect
35
82
  # find the records
36
- @q = (@model.column_names.include?("user_id") ? @model.where(user_id: current_user.id) : @model).ransack(params[:q])
83
+ @q = (@model.column_names.include?("user_id") ? @model.where(user_id: current_user.id) : @model).ransack(@query.presence|| params[:q])
37
84
  @records_all = @q.result(distinct: true)
38
- @records = @records_all.page(params[:page]).per(params[:per])
85
+ page = (@page.presence || params[:page])
86
+ per = (@per.presence || params[:per])
87
+ pages_info = (@pages_info.presence || params[:pages_info])
88
+ count = (@count.presence || params[:count])
89
+ @records = @records_all.page(page).per(per)
39
90
 
40
91
  # If there's the keyword pagination_info, then return a pagination info object
41
92
  return render json: MultiJson.dump({
@@ -48,13 +99,13 @@ class Api::V1::BaseController < ActionController::API
48
99
  is_out_of_range: @records.out_of_range?,
49
100
  pages_count: @records.total_pages,
50
101
  current_page_number: @records.current_page
51
- }) if !params[:pages_info].nil?
102
+ }) if !pages_info.blank?
52
103
  # If it's asked for page number, the paginate
53
- return render json: MultiJson.dump(@records, @json_attrs || {}) if !params[:page].nil? # (@json_attrs || {})
104
+ return render json: MultiJson.dump(@records, json_attrs) if !page.blank? # (@json_attrs || {})
54
105
  # if you ask for count, then return a json object with just the number of objects
55
- return render json: MultiJson.dump({count: @records_all.count}) if !params[:count].nil?
106
+ return render json: MultiJson.dump({count: @records_all.count}) if !count.blank?
56
107
  # Default
57
- render json: MultiJson.dump(@records_all, @json_attrs || {}) #(@json_attrs || {})
108
+ render json: MultiJson.dump(@records_all, json_attrs) #(@json_attrs || {})
58
109
  end
59
110
 
60
111
  # def count
@@ -71,7 +122,7 @@ class Api::V1::BaseController < ActionController::API
71
122
  end
72
123
 
73
124
  def show
74
- render json: @record.to_json(@json_attrs || {}), status: 200
125
+ render json: @record.to_json(json_attrs), status: 200
75
126
  end
76
127
 
77
128
  def create
@@ -80,13 +131,13 @@ class Api::V1::BaseController < ActionController::API
80
131
 
81
132
  @record.save!
82
133
 
83
- render json: @record.to_json(@json_attrs || {}), status: 201
134
+ render json: @record.to_json(json_attrs), status: 201
84
135
  end
85
136
 
86
137
  def update
87
138
  @record.update_attributes(request_params)
88
139
 
89
- render json: @record.to_json(@json_attrs || {}), status: 200
140
+ render json: @record.to_json(json_attrs), status: 200
90
141
  end
91
142
 
92
143
  def destroy
@@ -181,17 +232,23 @@ class Api::V1::BaseController < ActionController::API
181
232
 
182
233
  # private
183
234
 
184
- def find_record
235
+ def find_record id
185
236
  # find the records
186
- @record = @model.column_names.include?("user_id") ? @model.where(id: params[:id], user_id: current_user.id).first : @model.find(params[:id])
237
+ @record = @model.column_names.include?("user_id") ? @model.where(id: (id.presence || params[:id]), user_id: current_user.id).first : @model.find((id.presence || params[:id]))
187
238
  end
188
239
 
189
- def find_model
240
+ def find_model path=nil
190
241
  # Find the name of the model from controller
191
- @model = controller_path.classify.constantize rescue controller_name.classify.constantize
242
+ @singular_controller = (path.presence || controller_name).singularize.to_sym
243
+ @model = (path.presence || controller_path).classify.constantize rescue controller_name.classify.constantize
192
244
  end
193
245
 
194
246
  def request_params
195
- params.require(controller_name.singularize.to_sym).permit!
247
+ # controller_name.singularize.to_sym
248
+ params.require(@singular_controller).permit!
249
+ end
250
+
251
+ def json_attrs
252
+ @model.json_attrs.presence || @json_attrs.presence || {}
196
253
  end
197
254
  end
@@ -0,0 +1,7 @@
1
+
2
+ Rails.application.configure do
3
+ config.after_initialize do
4
+
5
+ # Rails.application.reload_routes!
6
+ end
7
+ end
data/config/routes.rb CHANGED
@@ -17,6 +17,19 @@ Rails.application.routes.draw do
17
17
  resources :users, only: [:index, :create, :show, :update, :destroy] do
18
18
  match 'search' => 'users#search', via: [:get, :post], as: :search, on: :collection
19
19
  end
20
+
21
+ namespace :base do
22
+ get :check
23
+ post :check
24
+ put :check
25
+ delete :check
26
+ end
27
+
28
+ # Catchall routes
29
+ get '*path', to: 'base#check'
30
+ post '*path', to: 'base#check'
31
+ put '*path', to: 'base#check'
32
+ delete '*path', to: 'base#check'
20
33
  end
21
34
  end
22
35
  end
@@ -1,3 +1,3 @@
1
1
  module ThecoreApi
2
- VERSION = "1.2.9".freeze
2
+ VERSION = "1.3.3".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thecore_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.9
4
+ version: 1.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriele Tassoni
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-30 00:00:00.000000000 Z
11
+ date: 2019-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thecore
@@ -93,6 +93,7 @@ files:
93
93
  - app/controllers/api/v1/info_controller.rb
94
94
  - app/controllers/api/v1/sessions_controller.rb
95
95
  - app/controllers/api/v1/users_controller.rb
96
+ - config/initializers/after_initialize_for_thecore_api.rb
96
97
  - config/initializers/cors_api_thecore.rb
97
98
  - config/initializers/wrap_parameters.rb
98
99
  - config/routes.rb