stockpot 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/stockpot/application_controller.rb +6 -1
- data/app/controllers/stockpot/database_cleaner_controller.rb +0 -2
- data/app/controllers/stockpot/healthz_controller.rb +0 -3
- data/app/controllers/stockpot/helper/errors.rb +26 -0
- data/app/controllers/stockpot/records_controller.rb +43 -36
- data/app/controllers/stockpot/redis_controller.rb +1 -2
- data/lib/stockpot.rb +1 -3
- data/lib/stockpot/version.rb +1 -1
- metadata +14 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7a4bf31bbaa54851f7afefe9140678b0290c3dee2fe80bfe577d23d9871f2e3
|
4
|
+
data.tar.gz: df5053bd8857e38ff7b17fafd70d3a5641b45193289a7e645f00ac7de499bd9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c2921f3892e44849c6b437d44a9fc071146e490a8577a3e04430efc1d313152814c435e8f41a051dc8cbc87d2346eb6d698a328442fbadaf78191a90a4b4b20
|
7
|
+
data.tar.gz: 9087bfcde46a56ac1e733ac045b1199afc08023c27bec695d99dcc5b7a29f6a5448221025aa2a00b9b08561c2ce5d2c1adb8809a996d7b6c1ad5e539be8c35ae
|
@@ -1,7 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
3
2
|
module Stockpot
|
4
3
|
class ApplicationController < ActionController::API
|
4
|
+
include ActiveSupport::Rescuable
|
5
|
+
include Helper::Errors
|
6
|
+
|
7
|
+
rescue_from StandardError do |exception|
|
8
|
+
render rescue_error(exception)
|
9
|
+
end
|
5
10
|
# protect_from_forgery with: :exception
|
6
11
|
end
|
7
12
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Stockpot
|
3
|
+
module Helper
|
4
|
+
module Errors
|
5
|
+
def rescue_error(error)
|
6
|
+
logger = Logger.new(STDERR)
|
7
|
+
logger.warn(error)
|
8
|
+
|
9
|
+
case error
|
10
|
+
when NameError
|
11
|
+
return_error(error.to_s, error.backtrace.first(5), :bad_request)
|
12
|
+
when PG::Error
|
13
|
+
return_error("Postgres error: #{error}", error.backtrace.first(5), :server_error)
|
14
|
+
when ActiveRecord::RecordInvalid, ActiveRecord::Validations
|
15
|
+
return_error("ActiveRecord error: #{error}", error.backtrace.first(5), :server_error)
|
16
|
+
else
|
17
|
+
return_error(error.to_s, error.backtrace.first(5),:server_error)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def return_error(message, backtrace, status)
|
22
|
+
{ json: { error: { status: status, backtrace: backtrace, message: message }}, status: status }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -1,78 +1,81 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_dependency "stockpot/application_controller"
|
4
|
-
|
5
2
|
require "factory_bot_rails"
|
6
3
|
|
7
4
|
module Stockpot
|
8
5
|
class RecordsController < ApplicationController
|
9
6
|
include ActiveSupport::Inflector
|
7
|
+
include ActiveRecord::Transactions
|
10
8
|
|
11
|
-
|
12
|
-
return_error("You need to provide at least one model name as an argument", 400)
|
9
|
+
before_action only: %i[index destroy update] do
|
10
|
+
return_error("You need to provide at least one model name as an argument", 400) if params.dig(:models).blank?
|
11
|
+
end
|
12
|
+
before_action only: %i[create] do
|
13
|
+
return_error("You need to provide at least one factory name as an argument", 400) if params.dig(:factory).blank?
|
14
|
+
end
|
13
15
|
|
16
|
+
def index
|
14
17
|
obj = {}
|
15
18
|
models.each_with_index do |element, i|
|
16
19
|
model = element[:model].to_s
|
17
|
-
|
20
|
+
class_name = find_correct_class_name(model)
|
21
|
+
|
22
|
+
obj[pluralize(model).camelize(:lower)] = class_name.constantize.where(models[i].except(:model))
|
18
23
|
end
|
19
24
|
|
20
25
|
render json: obj, status: :ok
|
21
26
|
end
|
22
27
|
|
23
28
|
def create
|
24
|
-
return_error("You need to provide at least one factory name as an argument", 400) && return if params.dig(:factory).blank?
|
25
|
-
|
26
29
|
list = params[:list] || 1
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
# rubocop:disable Rails/SaveBang
|
32
|
-
FactoryBot.create(factory)
|
33
|
-
# rubocop:enable Rails/SaveBang
|
34
|
-
elsif params[:attributes].blank?
|
35
|
-
FactoryBot.create(factory, *traits)
|
36
|
-
elsif params[:traits].blank?
|
37
|
-
FactoryBot.create(factory, attributes[n])
|
30
|
+
ActiveRecord::Base.transaction do
|
31
|
+
list.times do |n|
|
32
|
+
all_parameters = [ factory, *traits, attributes(n) ].compact
|
33
|
+
@factory = FactoryBot.create(*all_parameters)
|
38
34
|
end
|
39
35
|
end
|
40
|
-
obj = factory.
|
36
|
+
obj = @factory.class.name.constantize.last(list)
|
37
|
+
|
41
38
|
render json: obj, status: :created
|
42
39
|
end
|
43
40
|
|
44
41
|
def destroy
|
45
|
-
return_error("You need to provide at least one model name as an argument", 400) && return if params.dig(:models).blank?
|
46
|
-
|
47
42
|
obj = {}
|
48
|
-
|
49
|
-
|
50
|
-
|
43
|
+
ActiveRecord::Base.transaction do
|
44
|
+
models.each_with_index do |element, i|
|
45
|
+
model = element[:model].to_s
|
46
|
+
class_name = find_correct_class_name(model)
|
47
|
+
obj[pluralize(model).camelize(:lower)] = class_name.constantize.where(models[i].except(:model)).destroy_all
|
48
|
+
end
|
51
49
|
end
|
52
50
|
|
53
51
|
render json: obj, status: :accepted
|
54
52
|
end
|
55
53
|
|
56
54
|
def update
|
57
|
-
return_error("You need to provide at least one model name as an argument", 400) && return if params.dig(:models).blank?
|
58
|
-
|
59
55
|
obj = {}
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
56
|
+
ActiveRecord::Base.transaction do
|
57
|
+
models.each_with_index do |element, i|
|
58
|
+
model = element[:model].to_s
|
59
|
+
class_name = find_correct_class_name(model)
|
60
|
+
update_params = params.permit![:models][i][:update].to_h
|
61
|
+
obj[pluralize(model).camelize(:lower)] = class_name.constantize.where(models[i].except(:model, :update)).update(update_params)
|
62
|
+
end
|
64
63
|
end
|
65
|
-
|
66
64
|
render json: obj, status: :accepted
|
67
65
|
end
|
68
66
|
|
69
67
|
private
|
70
68
|
|
71
|
-
def
|
72
|
-
|
69
|
+
def find_correct_class_name(model)
|
70
|
+
# We are getting the class name from the factory or we default to whatever we send in.
|
71
|
+
# Something to keep in mind "module/class_name".camelize will translate into "Module::ClassName"
|
72
|
+
# which is perfect for namespaces in case there is no factory associated with a specific model
|
73
|
+
FactoryBot.factories.registered?(model) ? FactoryBot.build_stubbed(model).class.name : model.camelize
|
73
74
|
end
|
74
75
|
|
75
76
|
def traits
|
77
|
+
return if params[:traits].blank?
|
78
|
+
|
76
79
|
params[:traits].map(&:to_sym)
|
77
80
|
end
|
78
81
|
|
@@ -80,8 +83,12 @@ module Stockpot
|
|
80
83
|
params[:factory].to_sym
|
81
84
|
end
|
82
85
|
|
83
|
-
|
84
|
-
|
86
|
+
# rubocop:disable Naming/UncommunicativeMethodParamName
|
87
|
+
def attributes(n)
|
88
|
+
# rubocop:enable Naming/UncommunicativeMethodParamName
|
89
|
+
return if params[:attributes].blank?
|
90
|
+
|
91
|
+
params.permit![:attributes][n].to_h
|
85
92
|
end
|
86
93
|
|
87
94
|
def models
|
data/lib/stockpot.rb
CHANGED
data/lib/stockpot/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stockpot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jayson Smith
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-02-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -100,61 +100,55 @@ dependencies:
|
|
100
100
|
requirements:
|
101
101
|
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: '
|
103
|
+
version: '13.0'
|
104
104
|
- - ">="
|
105
105
|
- !ruby/object:Gem::Version
|
106
|
-
version:
|
106
|
+
version: 13.0.1
|
107
107
|
type: :development
|
108
108
|
prerelease: false
|
109
109
|
version_requirements: !ruby/object:Gem::Requirement
|
110
110
|
requirements:
|
111
111
|
- - "~>"
|
112
112
|
- !ruby/object:Gem::Version
|
113
|
-
version: '
|
113
|
+
version: '13.0'
|
114
114
|
- - ">="
|
115
115
|
- !ruby/object:Gem::Version
|
116
|
-
version:
|
116
|
+
version: 13.0.1
|
117
117
|
- !ruby/object:Gem::Dependency
|
118
118
|
name: rspec
|
119
119
|
requirement: !ruby/object:Gem::Requirement
|
120
120
|
requirements:
|
121
121
|
- - "~>"
|
122
122
|
- !ruby/object:Gem::Version
|
123
|
-
version: '3.
|
123
|
+
version: '3.9'
|
124
124
|
type: :development
|
125
125
|
prerelease: false
|
126
126
|
version_requirements: !ruby/object:Gem::Requirement
|
127
127
|
requirements:
|
128
128
|
- - "~>"
|
129
129
|
- !ruby/object:Gem::Version
|
130
|
-
version: '3.
|
130
|
+
version: '3.9'
|
131
131
|
- !ruby/object:Gem::Dependency
|
132
132
|
name: rspec-rails
|
133
133
|
requirement: !ruby/object:Gem::Requirement
|
134
134
|
requirements:
|
135
135
|
- - "~>"
|
136
136
|
- !ruby/object:Gem::Version
|
137
|
-
version: '3.
|
138
|
-
- - ">="
|
139
|
-
- !ruby/object:Gem::Version
|
140
|
-
version: 3.8.2
|
137
|
+
version: '3.9'
|
141
138
|
type: :development
|
142
139
|
prerelease: false
|
143
140
|
version_requirements: !ruby/object:Gem::Requirement
|
144
141
|
requirements:
|
145
142
|
- - "~>"
|
146
143
|
- !ruby/object:Gem::Version
|
147
|
-
version: '3.
|
148
|
-
- - ">="
|
149
|
-
- !ruby/object:Gem::Version
|
150
|
-
version: 3.8.2
|
144
|
+
version: '3.9'
|
151
145
|
- !ruby/object:Gem::Dependency
|
152
146
|
name: spicerack-styleguide
|
153
147
|
requirement: !ruby/object:Gem::Requirement
|
154
148
|
requirements:
|
155
149
|
- - ">="
|
156
150
|
- !ruby/object:Gem::Version
|
157
|
-
version: 0.
|
151
|
+
version: 0.21.0
|
158
152
|
- - "<"
|
159
153
|
- !ruby/object:Gem::Version
|
160
154
|
version: '1.0'
|
@@ -164,7 +158,7 @@ dependencies:
|
|
164
158
|
requirements:
|
165
159
|
- - ">="
|
166
160
|
- !ruby/object:Gem::Version
|
167
|
-
version: 0.
|
161
|
+
version: 0.21.0
|
168
162
|
- - "<"
|
169
163
|
- !ruby/object:Gem::Version
|
170
164
|
version: '1.0'
|
@@ -216,6 +210,7 @@ files:
|
|
216
210
|
- app/controllers/stockpot/application_controller.rb
|
217
211
|
- app/controllers/stockpot/database_cleaner_controller.rb
|
218
212
|
- app/controllers/stockpot/healthz_controller.rb
|
213
|
+
- app/controllers/stockpot/helper/errors.rb
|
219
214
|
- app/controllers/stockpot/records_controller.rb
|
220
215
|
- app/controllers/stockpot/redis_controller.rb
|
221
216
|
- config/routes.rb
|
@@ -245,7 +240,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
245
240
|
- !ruby/object:Gem::Version
|
246
241
|
version: '0'
|
247
242
|
requirements: []
|
248
|
-
rubygems_version: 3.
|
243
|
+
rubygems_version: 3.1.2
|
249
244
|
signing_key:
|
250
245
|
specification_version: 4
|
251
246
|
summary: Makes setting up test data in your Rails database from an external resource
|