minimalizer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3cd12461b740324f36bb3072f0eeb571bd365622
4
+ data.tar.gz: 41f079b1df0c15dcdb95df53e3152ae68d504233
5
+ SHA512:
6
+ metadata.gz: 971c722baef967057363bee256938a458924e83fa36e6fcf3774e8ae291feb2f8f5222585c15580b89a807908ac4626ad70469c021fc3a89fdd085a0d437e7e8
7
+ data.tar.gz: 6e877f226ad28106c14295a8d98a6dc828c88ecf11b8d7ad6c36011c61b6957e95fb1a482e15a650b522ddd07dfd02abd5672b31b28a77b58719590924c98a60
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Theodore Kimble
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ # Write Ruby on Rails applications more easily with Minimalizer
2
+
3
+ Minimalizer is a lightweight Ruby on Rails engine that enables you to write more
4
+ minimal Ruby on Rails applications. Minimalizer convenience methods help you
5
+ write simpler model and controller tests and declare basic controller behaviors
6
+ with ease.
7
+
8
+ ## Model Test Helpers
9
+
10
+ Added to `ActiveSupport::TestCase`:
11
+
12
+ * `assert_errors`
13
+ * `refute_errors`
14
+ * `validation_context`
15
+
16
+ See `lib/minimalizer/model_test_helpers.rb` for more detailed documentation.
17
+
18
+ ## Controller Test Helpers
19
+
20
+ Added to `ActionController::TestCase`:
21
+
22
+ * `assert_redirect`
23
+ * `assert_render`
24
+ * `assert_flash`
25
+
26
+ See `lib/minimalizer/controller_test_helpers.rb` for more detailed
27
+ documentation.
28
+
29
+ ## Controller Helpers
30
+
31
+ Added to `ActionController::Base`:
32
+
33
+ * `self.new_actions`
34
+ * `self.member_actions`
35
+ * `create_resource`
36
+ * `update_resource`
37
+ * `destroy_resource`
38
+ * `reorder_resources`
39
+ * `toggle_resource_boolean_on`
40
+ * `toggle_resource_boolean_off`
41
+
42
+ See `lib/minimalizer/controller_helpers.rb` for more detailed documentation.
43
+
44
+ ## License
45
+
46
+ Minimalizer is released under the MIT license. Copyright 2015 Theodore Kimble.
@@ -0,0 +1,9 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.test_files = Dir.glob('test/**/*_test.rb')
7
+ end
8
+
9
+ task(default: :test)
@@ -0,0 +1,6 @@
1
+ require 'rails'
2
+ require 'minimalizer/controller_helpers'
3
+ require 'minimalizer/controller_test_helpers'
4
+ require 'minimalizer/model_test_helpers'
5
+ require 'minimalizer/engine'
6
+ require 'minimalizer/version'
@@ -0,0 +1,354 @@
1
+ require 'action_controller'
2
+ require 'active_support/concern'
3
+
4
+ module Minimalizer
5
+ module ControllerHelpers
6
+ extend ActiveSupport::Concern
7
+
8
+ class_methods do
9
+ # Convenience method for specifying/excluding new actions in a before
10
+ # filter.
11
+ #
12
+ # before_action only: new_actions do
13
+ # @record = Record.new
14
+ # end
15
+ def new_actions
16
+ %i[new create]
17
+ end
18
+
19
+ # Convenience method for specifying/excluding member actions in a before
20
+ # filter.
21
+ #
22
+ # before_action only: member_actions do
23
+ # @record = Record.find(params[:id])
24
+ # end
25
+ def member_actions
26
+ %i[show edit update delete destroy]
27
+ end
28
+ end
29
+
30
+ # Create a new resource with the given attributes. If successful, set the
31
+ # ".notice" flash and redirect to the newly created resource; otherwise,
32
+ # set the ".alert" flash and render the new template with a 422 HTTP status
33
+ # reponse.
34
+ #
35
+ # def create
36
+ # create_resource @record, record_params
37
+ # end
38
+ #
39
+ # A resource array can be provided to affect the redirect location. Only
40
+ # the last resource will be saved.
41
+ #
42
+ # def create
43
+ # create_resource [@parent, @record], record_params
44
+ # end
45
+ #
46
+ # An optional :location argument will override the redirect location.
47
+ #
48
+ # def create
49
+ # create_resource @record, record_params, location: :records
50
+ # end
51
+ #
52
+ # Passing a block will yield true if the model saves successfully, false
53
+ # otherwise.
54
+ #
55
+ # def create
56
+ # create_resource @record, record_params do |success|
57
+ # if sucess
58
+ # # something
59
+ # else
60
+ # # something
61
+ # end
62
+ # end
63
+ # end
64
+ def create_resource(resource, attributes, location: nil)
65
+ model = resource.is_a?(Array) ? resource.last : resource
66
+ model.assign_attributes attributes
67
+
68
+ if model.save
69
+ flash.notice = t('.notice')
70
+ yield true if block_given?
71
+ redirect_to location || resource
72
+ else
73
+ flash.now.alert = t('.alert')
74
+ response.status = 422
75
+ yield false if block_given?
76
+ render :new
77
+ end
78
+ end
79
+
80
+
81
+ # Update an existing resource with the given attributes. If successful,
82
+ # set the ".notice" flash and redirect to the resource; otherwise, set the
83
+ # ".alert" flash and render the edit template with a 422 HTTP status
84
+ # response.
85
+ #
86
+ # def update
87
+ # update_resource @record, record_params
88
+ # end
89
+ #
90
+ # A resource array can be provided to affect the redirect location. Only
91
+ # the last resource will be updated.
92
+ #
93
+ # def update
94
+ # update_resource [@parent, @record], record_params
95
+ # end
96
+ #
97
+ # An optional :location argument will override the redirect location.
98
+ #
99
+ # def update
100
+ # update_resource @record, record_params, location: :records
101
+ # end
102
+ #
103
+ # Passing a block will yield true if the model updates successfully, false
104
+ # otherwise.
105
+ #
106
+ # def update
107
+ # update_resource @record, record_params do |success|
108
+ # if sucess
109
+ # # something
110
+ # else
111
+ # # something
112
+ # end
113
+ # end
114
+ # end
115
+ def update_resource(resource, attributes, location: nil)
116
+ model = resource.is_a?(Array) ? resource.last : resource
117
+
118
+ if model.update(attributes)
119
+ flash.notice = t('.notice')
120
+ yield true if block_given?
121
+ redirect_to location || resource
122
+ else
123
+ flash.now.alert = t('.alert')
124
+ response.status = 422
125
+ yield false if block_given?
126
+ render :edit
127
+ end
128
+ end
129
+
130
+ # Delete the given model.
131
+ #
132
+ # If the operation succeeds, provide a successful flash notice and
133
+ # redirect to the provided location (if given) or to the pluralized path
134
+ # of the original resource.
135
+ #
136
+ # If the operation fails, provide a failed flash alert. Then, if the
137
+ # :delete action exists, render the edit action with an
138
+ # :unprocessable_entity HTTP status; if the action does not exist,
139
+ # redirect to the original resource.
140
+ #
141
+ #
142
+ #
143
+ # Destroy an existing resource. If successful, set the ".notice" flash and
144
+ # redirect to the symbolized, plural name of the resource; otherwise, set
145
+ # the ".alert" flash and render the delete template with a 422 HTTP status
146
+ # response; if the delete action is not defined, instead redirect the
147
+ # resource.
148
+ #
149
+ # def destroy
150
+ # destroy_resource @record
151
+ # end
152
+ #
153
+ # A resource array can be provided to affect the redirect location. Only
154
+ # the last resource will be destroyed.
155
+ #
156
+ # def destroy
157
+ # destroy_resource [@parent, @record]
158
+ # end
159
+ #
160
+ # An optional :location argument will override the redirect location.
161
+ #
162
+ # def destroy
163
+ # destroy_resource @record, location: :root
164
+ # end
165
+ #
166
+ # Passing a block will yield true if the model destroys successfully, false
167
+ # otherwise.
168
+ #
169
+ # def destroy
170
+ # destroy_resource @record do |success|
171
+ # if sucess
172
+ # # something
173
+ # else
174
+ # # something
175
+ # end
176
+ # end
177
+ # end
178
+ def destroy_resource(resource, location: nil)
179
+ model = resource.is_a?(Array) ? resource.last : resource
180
+
181
+ if model.destroy
182
+ if !location
183
+ location = Array(resource)[0..-2] + [model.model_name.plural.to_sym]
184
+ end
185
+
186
+ flash.notice = t('.notice')
187
+ yield true if block_given?
188
+ redirect_to location
189
+ else
190
+ if respond_to?(:delete)
191
+ flash.now.alert = t('.alert')
192
+ response.status = 422
193
+ yield false if block_given?
194
+ render :delete
195
+ else
196
+ flash.alert = t('.alert')
197
+ yield false if block_given?
198
+ redirect_to resource
199
+ end
200
+ end
201
+ end
202
+
203
+ # Reorder the given models on the order attribute by the given attributes.
204
+ #
205
+ # If all operations succeed, provide a successful flash notice and
206
+ # redirect to the provided location (if given) or to the pluralized path
207
+ # of the first original resource.
208
+ #
209
+ # If any operation fails, provide a failed flash alert and render the
210
+ # :edit action with an :unprocessable_entity HTTP status.
211
+ #
212
+ # def update
213
+ # reorder_resources @records, record_params
214
+ # end
215
+ #
216
+ # An optional :attribute argument will override the default reording
217
+ # attribute (:position).
218
+ #
219
+ # def update
220
+ # reorder_resources @records, record_params, attribute: :ranking
221
+ # end
222
+ #
223
+ # An optional :location argument will override the redirect location.
224
+ #
225
+ # def update
226
+ # reorder_resources @records, record_params, location: :root
227
+ # end
228
+ #
229
+ # Passing a block will yield true if all models are reordered successfully,
230
+ # false otherwise.
231
+ #
232
+ # def update
233
+ # reorder_resources @records, record_params do |success|
234
+ # if sucess
235
+ # # something
236
+ # else
237
+ # # something
238
+ # end
239
+ # end
240
+ # end
241
+ def reorder_resources(resources, attributes, attribute: :position, location: nil)
242
+ models = resources.is_a?(Array) && (resources.last.is_a?(Array) || resources.last.is_a?(ActiveRecord::Relation)) ? resources.last : resources
243
+
244
+ models.each do |model|
245
+ model.update(attribute => attributes[model.id.to_s].to_i)
246
+ end
247
+
248
+ if models.all? { |model| model.errors.empty? }
249
+ if !location
250
+ if models.any?
251
+ location = Array(resources)[0..-2] + [models.first.model_name.plural.to_sym]
252
+ else
253
+ raise ArgumentError, 'Must provide one or more resources or the :location argument'
254
+ end
255
+ end
256
+
257
+ flash.notice = t('.notice')
258
+ yield true if block_given?
259
+ redirect_to location
260
+ else
261
+ flash.now.alert = t('.alert')
262
+ response.status = 422
263
+ yield false if block_given?
264
+ render :edit
265
+ end
266
+ end
267
+
268
+ # Toggle the given model attribute on.
269
+ #
270
+ # If the operation succeeds, provide a successful flash notice; otherwise,
271
+ # provide a failed flash alert. Redirect to the provided location (if
272
+ # given) or to the initial resource.
273
+ #
274
+ # def update
275
+ # toggle_resource_boolen_on @record, :active
276
+ # end
277
+ #
278
+ # An optional :location argument will override the redirect location.
279
+ #
280
+ # def update
281
+ # toggle_resource_boolen_on @record, :active, location: :records
282
+ # end
283
+ #
284
+ # Passing a block will yield true if the model is updated successfully,
285
+ # false otherwise.
286
+ #
287
+ # def update
288
+ # toggle_resource_boolen_on @record, :active do |success|
289
+ # if success
290
+ # # something
291
+ # else
292
+ # # something
293
+ # end
294
+ # end
295
+ # end
296
+ def toggle_resource_boolean_on(resource, attribute, location: nil)
297
+ model = resource.is_a?(Array) ? resource.last : resource
298
+
299
+ if model.update(attribute => true)
300
+ flash.notice = t('.notice')
301
+ yield true if block_given?
302
+ redirect_to location || resource
303
+ else
304
+ flash.alert = t('.alert')
305
+ yield false if block_given?
306
+ redirect_to location || resource
307
+ end
308
+ end
309
+
310
+ # Toggle the given model attribute off.
311
+ #
312
+ # If the operation succeeds, provide a successful flash notice; otherwise,
313
+ # provide a failed flash alert. Redirect to the provided location (if
314
+ # given) or to the initial resource.
315
+ #
316
+ # def update
317
+ # toggle_resource_boolen_off @record, :active
318
+ # end
319
+ #
320
+ # An optional :location argument will override the redirect location.
321
+ #
322
+ # def update
323
+ # toggle_resource_boolen_off @record, :active, location: :records
324
+ # end
325
+ #
326
+ # Passing a block will yield true if the model is updated successfully,
327
+ # false otherwise.
328
+ #
329
+ # def update
330
+ # toggle_resource_boolen_off @record, :active do |success|
331
+ # if success
332
+ # # something
333
+ # else
334
+ # # something
335
+ # end
336
+ # end
337
+ # end
338
+ def toggle_resource_boolean_off(resource, attribute, location: nil)
339
+ model = resource.is_a?(Array) ? resource.last : resource
340
+
341
+ if model.update(attribute => false)
342
+ flash.notice = t('.notice')
343
+ yield true if block_given?
344
+ redirect_to location || resource
345
+ else
346
+ flash.alert = t('.alert')
347
+ yield false if block_given?
348
+ redirect_to location || resource
349
+ end
350
+ end
351
+ end
352
+ end
353
+
354
+ ActionController::Base.send :include, Minimalizer::ControllerHelpers
@@ -0,0 +1,41 @@
1
+ require 'action_controller'
2
+ require 'active_support/concern'
3
+
4
+ module Minimalizer
5
+ module ControllerTestHelpers
6
+ extend ActiveSupport::Concern
7
+
8
+ private
9
+
10
+ # Assert the correct redirect location is given; optionally test the
11
+ # response status and flash messages.
12
+ def assert_redirect(location, status: 302, alert: nil, notice: nil)
13
+ assert_redirected_to location
14
+ assert_response status
15
+ assert_flash :alert, alert if alert
16
+ assert_flash :notice, notice if notice
17
+ end
18
+
19
+ # Assert the correct template is rendered; optionally test the response
20
+ # status and flash messages.
21
+ def assert_render(template, status: 200, alert: nil, notice: nil)
22
+ assert_template template
23
+ assert_response status
24
+ assert_flash :alert, alert if alert
25
+ assert_flash :notice, notice if notice
26
+ end
27
+
28
+ # Assert that the flash variant (e.g. :notice) is equal to the local
29
+ # translation for the given key.
30
+ def assert_flash(variant, key)
31
+ assert_equal I18n.t(key, scope: local_translation_scope), flash[variant]
32
+ end
33
+
34
+ # Local translation scope for the current controller and action.
35
+ def local_translation_scope
36
+ [@controller.controller_path.split('/'), @controller.action_name].flatten.compact.join('.')
37
+ end
38
+ end
39
+ end
40
+
41
+ ActionController::TestCase.send :include, Minimalizer::ControllerTestHelpers
@@ -0,0 +1,12 @@
1
+ module Minimalizer
2
+ class Engine < ::Rails::Engine
3
+ initializer 'minimalizer.add_controller_helpers' do
4
+ end
5
+
6
+ initializer 'minimalizer.add_controller_test_helpers' do
7
+ end
8
+
9
+ initializer 'minimalizer.add_model_test_helpers' do
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,47 @@
1
+ require 'active_model'
2
+ require 'active_model/errors_details'
3
+ require 'active_support/concern'
4
+
5
+ module Minimalizer
6
+ module ModelTestHelpers
7
+ extend ActiveSupport::Concern
8
+
9
+ private
10
+
11
+ # Assert that the given model‘s attribute contains errors. If error details
12
+ # are provided, assert that each provided set of details is found on the
13
+ # model’s attribute.
14
+ def assert_errors(attribute, model, *error_details)
15
+ model.valid?(@_validation_context)
16
+ assert_not_empty model.errors[attribute]
17
+
18
+ if error_details.any?
19
+ model_error_details = model.errors.details[attribute]
20
+ model_errors = model_error_details.map { |d| d[:error] }
21
+
22
+ error_details.each do |details|
23
+ if details.keys == [:error]
24
+ assert_includes model_errors, details[:error], "Expected error details to include error #{details[:error]}; found #{model_errors}."
25
+ else
26
+ assert_includes model_error_details, details, "Expected error details to include #{details}: Found #{model_error_details}."
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ # Refute that the given model‘s attribute contains any errors.
33
+ def refute_errors(attribute, model)
34
+ model.valid?(@_validation_context)
35
+ assert_empty model.errors[attribute]
36
+ end
37
+
38
+ # Set the validation context used during model error assertions.
39
+ def validation_context(context)
40
+ @_validation_context = context
41
+ yield
42
+ @_validation_context = nil
43
+ end
44
+ end
45
+ end
46
+
47
+ ActiveSupport::TestCase.send :include, Minimalizer::ModelTestHelpers
@@ -0,0 +1,3 @@
1
+ module Minimalizer
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ class Minimalizer::ControllerHelpersTest < Minitest::Test
4
+ def test_new_actions
5
+ assert_equal %i[new create], ActionController::Base.new_actions
6
+ end
7
+
8
+ def test_member_actions
9
+ assert_equal %i[show edit update delete destroy], ActionController::Base.member_actions
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ require 'rack/test'
2
+ require 'minimalizer'
3
+ require 'minitest/autorun'
4
+ require 'minitest/pride'
5
+
6
+ ActiveSupport::TestCase.test_order = :random
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minimalizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Theodore Kimble
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack-test
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: active_model-errors_details
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.1'
55
+ description: |2
56
+ Minimalizer is a lightweight Ruby on Rails engine that enables you to write
57
+ more minimal Ruby on Rails applications. Minimalizer convenience methods
58
+ help you write simpler model and controller tests and declare basic
59
+ controller behaviors with ease.
60
+ email:
61
+ - mail@theodorekimble.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - MIT-LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - lib/minimalizer.rb
70
+ - lib/minimalizer/controller_helpers.rb
71
+ - lib/minimalizer/controller_test_helpers.rb
72
+ - lib/minimalizer/engine.rb
73
+ - lib/minimalizer/model_test_helpers.rb
74
+ - lib/minimalizer/version.rb
75
+ - test/minimalizer/controller_helpers_test.rb
76
+ - test/test_helper.rb
77
+ homepage: https://github.com/theodorekimble/minimalizer
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.4.5
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Write Ruby on Rails applications more easily with Minimalizer
101
+ test_files:
102
+ - test/minimalizer/controller_helpers_test.rb
103
+ - test/test_helper.rb