model_driven_api 2.5.3 → 3.0.0

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: 8698a1f65e7b3cae8a9d27c3c610435b11d23102f86e90719b45b1a480628279
4
- data.tar.gz: bddef848a94fd0a23b555f44e7596606fc1ed630afcd3d91179bc8d6ac0fe601
3
+ metadata.gz: 67995baff4158d601e25d872aaa500a7aa2481898da5dc3b11efa2ec34d57664
4
+ data.tar.gz: 6a7da3c92f698689ac9c1c6262400acf65d84d5cd2ea3db0577d7e8e7bba4443
5
5
  SHA512:
6
- metadata.gz: 483e239527128317d747a7f2621379b35d7ed0fe24947c135d5e13154364e530baf9bf63a022e56dd0ca70196cc3f21a13acdeeaf4496408e698afab075fee15
7
- data.tar.gz: 7396686c68fbfd721ed82728e3d6d2de9c6e2170362a9432b24da98b1cff3c8667d822a187f6b9d2cdc1f0509fc6437cceb65e795a444f161d93d320572a57d5
6
+ metadata.gz: bd49743f7008d52c27b8f283f12913df13fe091ac5222f71ce4ad05cba246132cd3488d32f7509097dc88baec47bab7ba3425074c4b58e009d0a357754c52640
7
+ data.tar.gz: b8bff5b3af92837c21109f3918b8c278219c73c05258623796824a3f2e2bfc299b6de5af634bdf9a6f2cc96cd8217f29e37318aa88c12c0e8bb630eb304db6ac
data/Rakefile CHANGED
@@ -1,32 +1,14 @@
1
- begin
2
- require 'bundler/setup'
3
- rescue LoadError
4
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
- end
6
-
7
- require 'rdoc/task'
8
-
9
- RDoc::Task.new(:rdoc) do |rdoc|
10
- rdoc.rdoc_dir = 'rdoc'
11
- rdoc.title = 'ModelDrivenApi'
12
- rdoc.options << '--line-numbers'
13
- rdoc.rdoc_files.include('README.md')
14
- rdoc.rdoc_files.include('lib/**/*.rb')
15
- end
1
+ # frozen_string_literal: true
16
2
 
17
- APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
- load 'rails/tasks/engine.rake'
19
-
20
- load 'rails/tasks/statistics.rake'
21
-
22
- require 'bundler/gem_tasks'
23
-
24
- require 'rake/testtask'
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
25
5
 
26
6
  Rake::TestTask.new(:test) do |t|
27
- t.libs << 'test'
28
- t.pattern = 'test/**/*_test.rb'
29
- t.verbose = false
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
30
10
  end
31
11
 
32
- task default: :test
12
+ # require "standard/rake"
13
+
14
+ task default: %i[test standard]
@@ -27,22 +27,26 @@ class Api::V2::ApplicationController < ActionController::API
27
27
  @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)
28
28
  page = (@page.presence || params[:page])
29
29
  per = (@per.presence || params[:per])
30
- pages_info = (@pages_info.presence || params[:pages_info])
30
+ # pages_info = (@pages_info.presence || params[:pages_info])
31
31
  count = (@count.presence || params[:count])
32
- # Paging
32
+ # Pagination
33
33
  @records = @records_all.page(page).per(per)
34
+ # Content-Range: posts 0-4/27
35
+ range_start = [(page.to_i - 1) * per.to_i, 0].max;
36
+ range_end = [0, page.to_i * per.to_i - 1].max;
37
+ response.set_header('Content-Range', "#{@model.table_name} #{range_start}-#{range_end}/#{@records.total_count}")
34
38
 
35
39
  # If there's the keyword pagination_info, then return a pagination info object
36
- 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?
40
+ # 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?
37
41
 
38
42
  # puts "ALL RECORDS FOUND: #{@records_all.inspect}"
39
43
  status = @records_all.blank? ? 404 : 200
40
44
  # puts "If it's asked for page number, then paginate"
41
- return render json: MultiJson.dump(@records, json_attrs), status: status if !page.blank? # (@json_attrs || {})
45
+ return render json: @records.as_json(json_attrs), status: status if !page.blank? # (@json_attrs || {})
42
46
  #puts "if you ask for count, then return a json object with just the number of objects"
43
- return render json: MultiJson.dump({count: @records_all.count}) if !count.blank?
47
+ return render json: {count: @records_all.count}if !count.blank?
44
48
  #puts "Default"
45
- json_out = MultiJson.dump(@records_all, json_attrs)
49
+ json_out = @records_all.as_json(json_attrs)
46
50
  #puts "JSON ATTRS: #{json_attrs}"
47
51
  #puts "JSON OUT: #{json_out}"
48
52
  render json: json_out, status: status #(@json_attrs || {})
@@ -82,10 +86,17 @@ class Api::V2::ApplicationController < ActionController::API
82
86
  return render json: result, status: (status_number.presence || 200) if status == true
83
87
 
84
88
  # Normal Update Action
85
- # Raisl 6 vs Rails 6.1
89
+ # Rails 6 vs Rails 6.1
86
90
  @record.respond_to?('update_attributes!') ? @record.update_attributes!(@body) : @record.update!(@body)
87
91
  render json: @record.to_json(json_attrs), status: 200
88
92
  end
93
+
94
+ def update_multi
95
+ authorize! :update, @model
96
+ ids = params[:ids].split(",")
97
+ @model.where(id: ids).update!(@body)
98
+ render json: ids.to_json, status: 200
99
+ end
89
100
 
90
101
  def destroy
91
102
  authorize! :destroy, @record
@@ -98,6 +109,15 @@ class Api::V2::ApplicationController < ActionController::API
98
109
  return api_error(status: 500) unless @record.destroy
99
110
  head :ok
100
111
  end
112
+
113
+ def destroy_multi
114
+ authorize! :destroy, @model
115
+
116
+ # Normal Destroy Action
117
+ ids = params[:ids].split(",")
118
+ @model.where(id: ids).destroy!(@body)
119
+ render json: ids.to_json, status: 200
120
+ end
101
121
 
102
122
  private
103
123
 
@@ -111,7 +131,6 @@ class Api::V2::ApplicationController < ActionController::API
111
131
  # call an unwanted method in the AR Model.
112
132
  resource = "custom_action_#{params[:do]}"
113
133
  raise NoMethodError unless @model.respond_to?(resource)
114
- # return true, MultiJson.dump(params[:id].blank? ? @model.send(resource, params) : @model.send(resource, params[:id].to_i, params))
115
134
  # puts json_attrs
116
135
  body, status = @model.send(resource, params)
117
136
  return true, body.to_json(json_attrs), status
@@ -141,7 +160,7 @@ class Api::V2::ApplicationController < ActionController::API
141
160
  params[:current_user_id] = @current_user.id
142
161
  # Now every time the user fires off a successful GET request,
143
162
  # a new token is generated and passed to them, and the clock resets.
144
- response.headers['Token'] = JsonWebToken.encode(user_id: current_user.id)
163
+ response.set_header('Token', JsonWebToken.encode(user_id: current_user.id))
145
164
  end
146
165
 
147
166
  def find_record
@@ -1,11 +1,21 @@
1
1
  # config/initializers/cors.rb
2
+ # Rails.application.config.middleware.insert_before 0, Rack::Cors do
3
+ # allow do
4
+ # origins '*'
5
+ # resource '*',
6
+ # headers: %w(Token),
7
+ # methods: :any,
8
+ # expose: %w(Token),
9
+ # max_age: 600
10
+ # end
11
+ # end
12
+
13
+ puts "Loading CORS"
2
14
  Rails.application.config.middleware.insert_before 0, Rack::Cors do
15
+ # Allow Everything
16
+ # Please override to your specific security needs in the actual application
3
17
  allow do
4
18
  origins '*'
5
- resource '*',
6
- headers: %w(Token),
7
- methods: :any,
8
- expose: %w(Token),
9
- max_age: 600
19
+ resource '*', headers: :any, methods: [:get, :post, :put, :patch, :delete, :options, :head]
10
20
  end
11
- end
21
+ end
data/config/routes.rb CHANGED
@@ -27,8 +27,10 @@ Rails.application.routes.draw do
27
27
  # # CRUD Create
28
28
  post '*path', to: 'application#create'
29
29
  # # CRUD Update
30
+ put '*path/:id/multi', to: 'application#update_multi'
30
31
  put '*path/:id', to: 'application#update'
31
- # # CRUD DElete
32
+ # # CRUD Delete
33
+ delete '*path/:id/multi', to: 'application#destroy_multi'
32
34
  delete '*path/:id', to: 'application#destroy'
33
35
  end
34
36
  end
@@ -3,7 +3,7 @@ module ModelDrivenApiRole
3
3
 
4
4
  included do
5
5
  ## DSL (AKA what to show in the returned JSON)
6
- # Use @@json_attrs to drive json rendering for
6
+ # Use self.json_attrs to drive json rendering for
7
7
  # API model responses (index, show and update ones).
8
8
  # For reference:
9
9
  # https://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html
@@ -17,7 +17,7 @@ module ModelDrivenApiRole
17
17
  # - include: include associated models, it's a list [] of hashes {} which also
18
18
  # accepts the [:only, :except, :methods, :include] keys.
19
19
  cattr_accessor :json_attrs
20
- @@json_attrs = ModelDrivenApi.smart_merge((json_attrs || {}), {
20
+ self.json_attrs = ModelDrivenApi.smart_merge((json_attrs || {}), {
21
21
  except: [
22
22
  :lock_version,
23
23
  :created_at,
@@ -4,7 +4,7 @@ module ModelDrivenApiUser
4
4
  included do
5
5
  has_many :used_tokens, inverse_of: :user, dependent: :destroy
6
6
  ## DSL (AKA what to show in the returned JSON)
7
- # Use @@json_attrs to drive json rendering for
7
+ # Use self.json_attrs to drive json rendering for
8
8
  # API model responses (index, show and update ones).
9
9
  # For reference:
10
10
  # https://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html
@@ -18,10 +18,9 @@ module ModelDrivenApiUser
18
18
  # - include: include associated models, it's a list [] of hashes {} which also
19
19
  # accepts the [:only, :except, :methods, :include] keys.
20
20
  cattr_accessor :json_attrs
21
- @@json_attrs = ModelDrivenApi.smart_merge((json_attrs || {}), {
21
+ self.json_attrs = ModelDrivenApi.smart_merge((json_attrs || {}), {
22
22
  except: [
23
23
  :lock_version,
24
- :created_at,
25
24
  :updated_at
26
25
  ],
27
26
  include: [:roles]
@@ -1,3 +1,4 @@
1
1
  module ModelDrivenApi
2
- VERSION = "#{`git describe --tags $(git rev-list --tags --max-count=1)`.chomp}"
2
+ # VERSION = "#{`git describe --tags $(git rev-list --tags --max-count=1)`.chomp}"
3
+ VERSION = "3.0.0"
3
4
  end
@@ -4,7 +4,7 @@ require 'ransack'
4
4
  require 'jwt'
5
5
  require 'json_web_token'
6
6
  require "kaminari"
7
- require "multi_json"
7
+ # require "multi_json"
8
8
  require "simple_command"
9
9
 
10
10
  require 'concerns/api_exception_management'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_driven_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.3
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriele Tassoni
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-03 00:00:00.000000000 Z
11
+ date: 2023-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thecore_backend_commons
@@ -16,70 +16,56 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.4'
19
+ version: '3.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.4'
26
+ version: '3.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: jwt
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '2.2'
33
+ version: '2.4'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '2.2'
40
+ version: '2.4'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: simple_command
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0.1'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '0.1'
55
- - !ruby/object:Gem::Dependency
56
- name: kaminari
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '1.2'
47
+ version: '1.0'
62
48
  type: :runtime
63
49
  prerelease: false
64
50
  version_requirements: !ruby/object:Gem::Requirement
65
51
  requirements:
66
52
  - - "~>"
67
53
  - !ruby/object:Gem::Version
68
- version: '1.2'
54
+ version: '1.0'
69
55
  - !ruby/object:Gem::Dependency
70
56
  name: ransack
71
57
  requirement: !ruby/object:Gem::Requirement
72
58
  requirements:
73
59
  - - "~>"
74
60
  - !ruby/object:Gem::Version
75
- version: '2.3'
61
+ version: '3.2'
76
62
  type: :runtime
77
63
  prerelease: false
78
64
  version_requirements: !ruby/object:Gem::Requirement
79
65
  requirements:
80
66
  - - "~>"
81
67
  - !ruby/object:Gem::Version
82
- version: '2.3'
68
+ version: '3.2'
83
69
  - !ruby/object:Gem::Dependency
84
70
  name: rack-cors
85
71
  requirement: !ruby/object:Gem::Requirement
@@ -95,33 +81,33 @@ dependencies:
95
81
  - !ruby/object:Gem::Version
96
82
  version: '1.1'
97
83
  - !ruby/object:Gem::Dependency
98
- name: multi_json
84
+ name: deep_merge
99
85
  requirement: !ruby/object:Gem::Requirement
100
86
  requirements:
101
87
  - - "~>"
102
88
  - !ruby/object:Gem::Version
103
- version: '1.14'
89
+ version: '1.2'
104
90
  type: :runtime
105
91
  prerelease: false
106
92
  version_requirements: !ruby/object:Gem::Requirement
107
93
  requirements:
108
94
  - - "~>"
109
95
  - !ruby/object:Gem::Version
110
- version: '1.14'
96
+ version: '1.2'
111
97
  - !ruby/object:Gem::Dependency
112
- name: deep_merge
98
+ name: sqlite3
113
99
  requirement: !ruby/object:Gem::Requirement
114
100
  requirements:
115
- - - "~>"
101
+ - - ">="
116
102
  - !ruby/object:Gem::Version
117
- version: '1.2'
118
- type: :runtime
103
+ version: '0'
104
+ type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
107
  requirements:
122
- - - "~>"
108
+ - - ">="
123
109
  - !ruby/object:Gem::Version
124
- version: '1.2'
110
+ version: '0'
125
111
  description: Ruby on Rails REST APIs built by convention using the DB schema as the
126
112
  foundation, please see README for mode of use.
127
113
  email:
@@ -177,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
163
  - !ruby/object:Gem::Version
178
164
  version: '0'
179
165
  requirements: []
180
- rubygems_version: 3.0.3.1
166
+ rubygems_version: 3.3.26
181
167
  signing_key:
182
168
  specification_version: 4
183
169
  summary: Convention based RoR engine which uses DB schema introspection to create