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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2683178878bc312393087331178faa1770530da412e17fdfbf17df3a25b11d38
4
- data.tar.gz: de6f99f3fb93c4679d752ea699fd5be7110caf2a9d1de5963670435f9285bd49
3
+ metadata.gz: a7a4bf31bbaa54851f7afefe9140678b0290c3dee2fe80bfe577d23d9871f2e3
4
+ data.tar.gz: df5053bd8857e38ff7b17fafd70d3a5641b45193289a7e645f00ac7de499bd9a
5
5
  SHA512:
6
- metadata.gz: 71a3938561c28cf45fc99e7a90fdb452be47660978269677f41e369b4881c06b5a1c376845a7fce24b73bf8e0e77abe3be7dc250336f305552de5b192dc7e492
7
- data.tar.gz: e28cc6b970e452adf4b3a311ffa33b2a0cff9e5cf7f4183919d4b21f6620f169c73fbc85eb1d8104c7594a458290b261fb29922e9cc5b51d8860c24d914500aa
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
@@ -2,8 +2,6 @@
2
2
 
3
3
  require "database_cleaner"
4
4
 
5
- require_dependency "stockpot/application_controller"
6
-
7
5
  module Stockpot
8
6
  class DatabaseCleanerController < ApplicationController
9
7
  # Clean database before, between, and after tests by clearing Rails
@@ -1,7 +1,4 @@
1
1
  # frozen_string_literal: true
2
-
3
- require_dependency "stockpot/application_controller"
4
-
5
2
  module Stockpot
6
3
  class HealthzController < ApplicationController
7
4
  def index
@@ -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
- def index
12
- return_error("You need to provide at least one model name as an argument", 400) && return if params.dig(:models).blank?
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
- obj[pluralize(model).camelize(:lower)] = model.camelize.constantize.where(models[i].except(:model))
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
- list.times do |n|
28
- if params[:traits].present? && params[:attributes].present?
29
- FactoryBot.create(factory, *traits, attributes[n])
30
- elsif params[:traits].blank? && params[:attributes].blank?
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.to_s.camelize.constantize.last(list)
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
- models.each_with_index do |element, i|
49
- model = element[:model].to_s
50
- obj[pluralize(model).camelize(:lower)] = model.camelize.constantize.where(models[i].except(:model)).destroy_all
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
- models.each_with_index do |element, i|
61
- model = element[:model].to_s
62
- update_params = params.permit![:models][i][:update].to_h
63
- obj[pluralize(model).camelize(:lower)] = model.camelize.constantize.where(models[i].except(:model, :update)).update(update_params)
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 return_error(message, status)
72
- render json: { "error": { "status": status, "message": message }}.to_json, status: status
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
- def attributes
84
- params.permit![:attributes].map(&:to_h)
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
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
-
3
2
  module Stockpot
4
- class RedisController < ActionController::Base
3
+ class RedisController < ApplicationController
5
4
  def index
6
5
  if params[:field].present?
7
6
  # Returns the value associated with :field in the hash stored at :key
data/lib/stockpot.rb CHANGED
@@ -2,6 +2,4 @@
2
2
 
3
3
  require "stockpot/engine"
4
4
 
5
- module Stockpot
6
- # Your code goes here...
7
- end
5
+ module Stockpot; end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Stockpot
4
- VERSION = "0.2.1"
4
+ VERSION = "0.3.0"
5
5
  end
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.2.1
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: 2019-11-04 00:00:00.000000000 Z
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: '12.3'
103
+ version: '13.0'
104
104
  - - ">="
105
105
  - !ruby/object:Gem::Version
106
- version: 12.3.3
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: '12.3'
113
+ version: '13.0'
114
114
  - - ">="
115
115
  - !ruby/object:Gem::Version
116
- version: 12.3.3
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.8'
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.8'
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.8'
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.8'
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.16.2
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.16.2
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.0.6
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