stockpot 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -5
- data/app/controllers/stockpot/helper/errors.rb +6 -6
- data/app/controllers/stockpot/main_controller.rb +1 -2
- data/app/controllers/stockpot/records_controller.rb +58 -41
- data/app/controllers/stockpot/redis_controller.rb +6 -0
- data/config/routes.rb +2 -1
- data/lib/stockpot/engine.rb +0 -6
- data/lib/stockpot/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d3e810f46e006579bd1532c1f2d2fc291110cec1f20fc432b1f073efb7f104f2
|
4
|
+
data.tar.gz: 8373caf574a7f15a48ae2139606d85541f91c3ce26b05b7295ef293ddb592974
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 486f9a8a802130c36c0528a9dffb4d7d7dc90686b170058ecc590c04d182a70fabacbebfc73d9f0feb445cbfa55e98c5ea7bb49cc4412748b6c84548a1a71e4d
|
7
|
+
data.tar.gz: b18a2ac9341c86fae9d69b91c9e6b008352bc4a5ec989bb14b6d31ed8d8982c01acf28da797f9afd8542667c8b5265891b8d032147343be7f64b82cd86563b82
|
data/README.md
CHANGED
@@ -79,7 +79,7 @@ Query for data. Accepts a array of objects that require at least a model name, b
|
|
79
79
|
|
80
80
|
#### POST
|
81
81
|
|
82
|
-
Create new data. Accepts an
|
82
|
+
Create new data. Accepts an an array of objects specifying a single model with additional qualifiers.
|
83
83
|
|
84
84
|
* factory (required) - Specify which factory to create a record with.
|
85
85
|
* list (optional) - Specify a count of items to create, default: 1
|
@@ -87,7 +87,7 @@ Create new data. Accepts an object specifying a single model with additional qua
|
|
87
87
|
* attributes (optional) - An array of objects allowing for the specification of data to be used when creating records. Array position matches with list order if multiple records are desired.
|
88
88
|
|
89
89
|
```javascript
|
90
|
-
{
|
90
|
+
[{
|
91
91
|
factory: "user",
|
92
92
|
traits: ["having_address"],
|
93
93
|
attributes: [{
|
@@ -96,7 +96,7 @@ Create new data. Accepts an object specifying a single model with additional qua
|
|
96
96
|
first_name: "Foo",
|
97
97
|
last_name: "Bar"
|
98
98
|
}]
|
99
|
-
}
|
99
|
+
}]
|
100
100
|
```
|
101
101
|
|
102
102
|
#### DELETE
|
@@ -129,7 +129,7 @@ Clears Rails & Redis caches and truncates Active Records databases. No body requ
|
|
129
129
|
### `/stockpot/redis`
|
130
130
|
|
131
131
|
#### GET
|
132
|
-
|
132
|
+
|
133
133
|
Query for data. Accepts key or field to use to search cache for record.
|
134
134
|
|
135
135
|
```javascript
|
@@ -139,6 +139,10 @@ Query for data. Accepts key or field to use to search cache for record.
|
|
139
139
|
}
|
140
140
|
```
|
141
141
|
|
142
|
+
#### GET - Keys
|
143
|
+
|
144
|
+
Query for all keys within data. No body or argument required.
|
145
|
+
|
142
146
|
#### POST - Create new data
|
143
147
|
|
144
148
|
Accepts an object specifying a key, field, and value to be inserted into Redis cache.
|
@@ -168,7 +172,7 @@ Cypress.Commands.add("getRecords", args => {
|
|
168
172
|
```
|
169
173
|
|
170
174
|
Our tests can then call this command like this
|
171
|
-
|
175
|
+
|
172
176
|
```javascript
|
173
177
|
cy.getRecords([{ model: "user", id: user.id }])
|
174
178
|
.then(res => {
|
@@ -8,18 +8,18 @@ module Stockpot
|
|
8
8
|
|
9
9
|
case error
|
10
10
|
when NameError
|
11
|
-
return_error(error.
|
11
|
+
return_error(error.message, error.backtrace.first(5), :bad_request)
|
12
12
|
when PG::Error
|
13
|
-
return_error("Postgres error: #{error}", error.backtrace.first(5), :
|
14
|
-
when ActiveRecord::RecordInvalid, ActiveRecord::Validations
|
15
|
-
return_error("
|
13
|
+
return_error("Postgres error: #{error.message}", error.backtrace.first(5), :internal_server_error)
|
14
|
+
when ActiveRecord::RecordInvalid, ActiveRecord::Validations, ActiveRecord::RecordNotDestroyed
|
15
|
+
return_error("In #{error.record.class} class, #{error.message}", error.backtrace.first(5), :expectation_failed)
|
16
16
|
else
|
17
|
-
return_error(error.
|
17
|
+
return_error(error.message, error.backtrace.first(5),:internal_server_error)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
def return_error(message, backtrace, status)
|
22
|
-
|
22
|
+
render json: { error: { status: status, backtrace: backtrace, message: message }}, status: status
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
require "factory_bot_rails"
|
3
|
+
require "active_record/persistence"
|
3
4
|
|
4
5
|
module Stockpot
|
5
6
|
class RecordsController < MainController
|
@@ -10,58 +11,84 @@ module Stockpot
|
|
10
11
|
return_error("You need to provide at least one model name as an argument", 400) if params.dig(:models).blank?
|
11
12
|
end
|
12
13
|
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(:
|
14
|
+
return_error("You need to provide at least one factory name as an argument", 400) if params.dig(:factories).blank?
|
15
|
+
end
|
16
|
+
before_action do
|
17
|
+
@response_data = {}
|
14
18
|
end
|
15
19
|
|
16
20
|
def index
|
17
|
-
obj = {}
|
18
21
|
models.each_with_index do |element, i|
|
19
|
-
model = element[:model]
|
22
|
+
model = element[:model]
|
20
23
|
class_name = find_correct_class_name(model)
|
24
|
+
formatted_model = pluralize(model).camelize(:lower).gsub("::", "")
|
21
25
|
|
22
|
-
|
26
|
+
@response_data[formatted_model] = [] unless @response_data.key?(formatted_model)
|
27
|
+
@response_data[formatted_model].concat(class_name.constantize.where(models[i].except(:model)))
|
28
|
+
@response_data[formatted_model].reverse!.uniq! { |obj| obj["id"] }
|
23
29
|
end
|
24
|
-
|
25
|
-
render json: obj, status: :ok
|
30
|
+
render json: @response_data, status: :ok
|
26
31
|
end
|
27
32
|
|
28
33
|
def create
|
29
|
-
list = params[:list] || 1
|
30
34
|
ActiveRecord::Base.transaction do
|
31
|
-
|
32
|
-
|
33
|
-
|
35
|
+
factories.each do |element|
|
36
|
+
ids = []
|
37
|
+
list = element[:list] || 1
|
38
|
+
factory = element[:factory]
|
39
|
+
traits = element[:traits].map(&:to_sym) if element[:traits].present?
|
40
|
+
attributes = element[:attributes].to_h if element[:attributes].present?
|
41
|
+
|
42
|
+
list.times do
|
43
|
+
factory_arguments = [ factory, *traits, attributes ].compact
|
44
|
+
@factory_instance = FactoryBot.create(*factory_arguments)
|
45
|
+
ids << @factory_instance.id
|
46
|
+
end
|
47
|
+
|
48
|
+
factory_name = pluralize(factory).camelize(:lower)
|
49
|
+
|
50
|
+
@response_data[factory_name] = [] unless @response_data.key?(factory_name)
|
51
|
+
@response_data[factory_name].concat(@factory_instance.class.name.constantize.where(id: ids))
|
34
52
|
end
|
35
53
|
end
|
36
|
-
|
37
|
-
|
38
|
-
render json: obj, status: :created
|
54
|
+
render json: @response_data, status: :accepted
|
39
55
|
end
|
40
56
|
|
41
57
|
def destroy
|
42
|
-
obj = {}
|
43
58
|
ActiveRecord::Base.transaction do
|
44
59
|
models.each_with_index do |element, i|
|
45
|
-
model = element[:model]
|
60
|
+
model = element[:model]
|
46
61
|
class_name = find_correct_class_name(model)
|
47
|
-
|
62
|
+
formatted_model = pluralize(model).camelize(:lower).gsub("::", "")
|
63
|
+
|
64
|
+
class_name.constantize.where(models[i].except(:model)).each do |record|
|
65
|
+
record.destroy!
|
66
|
+
@response_data[formatted_model] = [] unless @response_data.key?(formatted_model)
|
67
|
+
@response_data[formatted_model] << record
|
68
|
+
end
|
48
69
|
end
|
49
70
|
end
|
50
|
-
|
51
|
-
render json: obj, status: :accepted
|
71
|
+
render json: @response_data, status: :accepted
|
52
72
|
end
|
53
73
|
|
54
74
|
def update
|
55
|
-
obj = {}
|
56
75
|
ActiveRecord::Base.transaction do
|
57
76
|
models.each_with_index do |element, i|
|
58
|
-
model = element[:model]
|
77
|
+
model = element[:model]
|
59
78
|
class_name = find_correct_class_name(model)
|
60
79
|
update_params = params.permit![:models][i][:update].to_h
|
61
|
-
|
80
|
+
attributes_to_search = models[i].except(:model, :update)
|
81
|
+
formatted_model = pluralize(model).camelize(:lower).gsub("::", "")
|
82
|
+
|
83
|
+
class_name.constantize.where(attributes_to_search).each do |record|
|
84
|
+
record.update!(update_params)
|
85
|
+
@response_data[formatted_model] = [] unless @response_data.key?(formatted_model)
|
86
|
+
@response_data[formatted_model] << class_name.constantize.find(record.id)
|
87
|
+
@response_data[formatted_model].reverse!.uniq! { |obj| obj["id"] }
|
88
|
+
end
|
62
89
|
end
|
63
90
|
end
|
64
|
-
render json:
|
91
|
+
render json: @response_data, status: :accepted
|
65
92
|
end
|
66
93
|
|
67
94
|
private
|
@@ -70,29 +97,19 @@ module Stockpot
|
|
70
97
|
# We are getting the class name from the factory or we default to whatever we send in.
|
71
98
|
# Something to keep in mind "module/class_name".camelize will translate into "Module::ClassName"
|
72
99
|
# which is perfect for namespaces in case there is no factory associated with a specific model
|
73
|
-
FactoryBot.factories.registered?(model)
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
params[:traits].map(&:to_sym)
|
80
|
-
end
|
81
|
-
|
82
|
-
def factory
|
83
|
-
params[:factory].to_sym
|
84
|
-
end
|
85
|
-
|
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
|
100
|
+
if FactoryBot.factories.registered?(model)
|
101
|
+
FactoryBot.factories.find(model).build_class.to_s
|
102
|
+
else
|
103
|
+
model.camelize
|
104
|
+
end
|
92
105
|
end
|
93
106
|
|
94
107
|
def models
|
95
108
|
params.permit![:models].map(&:to_h)
|
96
109
|
end
|
110
|
+
|
111
|
+
def factories
|
112
|
+
params.permit![:factories].map(&:to_h)
|
113
|
+
end
|
97
114
|
end
|
98
115
|
end
|
data/config/routes.rb
CHANGED
@@ -8,8 +8,9 @@ Stockpot::Engine.routes.draw do
|
|
8
8
|
|
9
9
|
delete "/clean_database", to: "database_cleaner#index"
|
10
10
|
|
11
|
-
get "/redis", to: "redis#index"
|
12
11
|
post "/redis", to: "redis#create"
|
12
|
+
get "/redis", to: "redis#index"
|
13
|
+
get "/redis/keys", to: "redis#keys"
|
13
14
|
|
14
15
|
get "/healthz", to: "healthz#index"
|
15
16
|
end
|
data/lib/stockpot/engine.rb
CHANGED
@@ -4,11 +4,5 @@ module Stockpot
|
|
4
4
|
class Engine < ::Rails::Engine
|
5
5
|
isolate_namespace Stockpot
|
6
6
|
config.generators.api_only = true
|
7
|
-
|
8
|
-
config.generators do |generators|
|
9
|
-
generators.test_framework :rspec
|
10
|
-
generators.fixture_replacement :factory_bot
|
11
|
-
generators.factory_bot dir: 'spec/factories'
|
12
|
-
end
|
13
7
|
end
|
14
8
|
end
|
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.4.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: 2020-
|
11
|
+
date: 2020-06-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|