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 +4 -4
- data/Rakefile +9 -27
- data/app/controllers/api/v2/application_controller.rb +28 -9
- data/config/initializers/cors_api_thecore.rb +16 -6
- data/config/routes.rb +3 -1
- data/lib/concerns/model_driven_api_role.rb +2 -2
- data/lib/concerns/model_driven_api_user.rb +2 -3
- data/lib/model_driven_api/version.rb +2 -1
- data/lib/model_driven_api.rb +1 -1
- metadata +20 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67995baff4158d601e25d872aaa500a7aa2481898da5dc3b11efa2ec34d57664
|
4
|
+
data.tar.gz: 6a7da3c92f698689ac9c1c6262400acf65d84d5cd2ea3db0577d7e8e7bba4443
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd49743f7008d52c27b8f283f12913df13fe091ac5222f71ce4ad05cba246132cd3488d32f7509097dc88baec47bab7ba3425074c4b58e009d0a357754c52640
|
7
|
+
data.tar.gz: b8bff5b3af92837c21109f3918b8c278219c73c05258623796824a3f2e2bfc299b6de5af634bdf9a6f2cc96cd8217f29e37318aa88c12c0e8bb630eb304db6ac
|
data/Rakefile
CHANGED
@@ -1,32 +1,14 @@
|
|
1
|
-
|
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
|
-
|
18
|
-
|
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 <<
|
28
|
-
t.
|
29
|
-
t.
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/test_*.rb"]
|
30
10
|
end
|
31
11
|
|
32
|
-
|
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
|
-
#
|
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:
|
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:
|
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:
|
47
|
+
return render json: {count: @records_all.count}if !count.blank?
|
44
48
|
#puts "Default"
|
45
|
-
json_out =
|
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
|
-
#
|
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.
|
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
|
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
|
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
|
-
|
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
|
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
|
-
|
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]
|
data/lib/model_driven_api.rb
CHANGED
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:
|
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:
|
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: '
|
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: '
|
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.
|
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.
|
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
|
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.
|
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
|
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
|
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:
|
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.
|
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.
|
96
|
+
version: '1.2'
|
111
97
|
- !ruby/object:Gem::Dependency
|
112
|
-
name:
|
98
|
+
name: sqlite3
|
113
99
|
requirement: !ruby/object:Gem::Requirement
|
114
100
|
requirements:
|
115
|
-
- - "
|
101
|
+
- - ">="
|
116
102
|
- !ruby/object:Gem::Version
|
117
|
-
version: '
|
118
|
-
type: :
|
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: '
|
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.
|
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
|