minimalizer 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 638219c0b09d1604e575443a92adff59ab42bf91
4
- data.tar.gz: 7fc17e2bcd5e66ea82d2494e1b36a49cbe17119f
3
+ metadata.gz: 457231f04e30961579f27aad2237e0a1205ba9db
4
+ data.tar.gz: 821b0b7aa398798d7d1936bd85d20aedb4675632
5
5
  SHA512:
6
- metadata.gz: 08dc267a2969d516b50f1e8c119b802969463ce0ad837e65134e6e091eb1cbab4eaad6798bd4d89481ff270cc3ec2f8f3c95582f56168825cc04fcc783cfcd61
7
- data.tar.gz: 6ad803728c6fb8ae2804dffb18046307d7507919458bf411a811221c02dc44f56ff69076f33678c75070f50d764a43dc7afc53fd703e54b53142051cf54cfcaa
6
+ metadata.gz: bb39b5465d3dc8deb8bfa028ce7e3700480be4e0fde973b7b34ac971df60854f2e9f9e6ae0d2878ab682171edb32e261667c86dc78f34422d9c6f90d1768460b
7
+ data.tar.gz: e2fb7005eb5509673440a0cdef83579f1eaffb78931aaf0862a3c9f1b8182ce495509aa0109046fdd1d8e558bea5ff48e6e74849e525e5594189601e4fb851a1
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Write Ruby on Rails applications more easily with Minimalizer
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/minimalizer.svg)](http://badge.fury.io/rb/minimalizer)
4
+
3
5
  Minimalizer is a lightweight Ruby on Rails engine that enables you to write more
4
6
  minimal Ruby on Rails applications. Minimalizer convenience methods help you
5
7
  write simpler model and controller tests and declare basic controller behaviors
@@ -27,352 +27,141 @@ module Minimalizer
27
27
  end
28
28
  end
29
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.
30
+ # Respond to a boolean condition.
34
31
  #
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 :context argument will be passed to the record’s #save method
47
- # to set the validation context.
48
- #
49
- # def create
50
- # create_resource @record, record_params, context: :scenario
51
- # end
52
- #
53
- # An optional :location argument will override the redirect location.
54
- #
55
- # def create
56
- # create_resource @record, record_params, location: :records
57
- # end
32
+ # If the condition is truthful, set the notice flash, if present, and
33
+ # redirect to the :location option, if present.
58
34
  #
59
- # An optional :template argument will override the default :new template.
35
+ # If the condition is not truthful, set the alert flash, if present, and
36
+ # render the :template option or the default action. If the :redirect option
37
+ # is provided, redirect there instead. If the :redirect option equals true,
38
+ # then redirect to the :location option instead.
60
39
  #
61
40
  # def create
62
- # create_resource @record, record_params, template: :alternate
41
+ # respond_to_boolean(value, location: '/home')
63
42
  # end
64
43
  #
65
- # Passing a block will yield true if the model saves successfully, false
66
- # otherwise.
44
+ # Provide callbacks to :on_succeed or :on_fail and all methods will be
45
+ # called on the containing controller. Callbacks may be provided as a symbol
46
+ # or array of symbols.
67
47
  #
68
48
  # def create
69
- # create_resource @record, record_params do |success|
70
- # if sucess
71
- # # something
72
- # else
73
- # # something
74
- # end
75
- # end
76
- # end
77
- def create_resource(resource, attributes, context: nil, location: nil, template: nil)
78
- model = resource.is_a?(Array) ? resource.last : resource
79
- model.assign_attributes attributes
80
-
81
- if model.save(context: context)
82
- flash.notice = t('.notice')
83
- yield true if block_given?
84
- redirect_to location || resource
49
+ # respond_to_boolean(value, location: '/home', on_succeed: :do_something)
50
+ # end
51
+ #
52
+ # By default the notice and alert values will be set to the I18n
53
+ # translations of “.notice” and “.alert”, respectively. Pass a string to
54
+ # render that value directly, a hash to use that value as the locals during
55
+ # the translation, or a false-like value to skip setting that flash value.
56
+ def respond_to_boolean(condition, location: nil, template: nil, redirect: false, on_succeed: [], on_fail: [], notice: true, alert: true)
57
+ if condition
58
+ flash.notice = translate_key(:notice, notice)
59
+ redirect_to location if location
60
+ Array(on_succeed).each { |method| send(method) }
61
+ elsif redirect
62
+ flash.alert = translate_key(:alert, alert)
63
+ location = redirect unless redirect == true
64
+ redirect_to location if location
65
+ Array(on_fail).each { |method| send(method) }
85
66
  else
86
- flash.now.alert = t('.alert')
87
- response.status = 422
88
- yield false if block_given?
89
- render template || :new
67
+ flash.now.alert = translate_key(:alert, alert)
68
+ render template || { create: :new, update: :edit, destroy: :delete }[action_name.to_sym], status: 422
69
+ Array(on_fail).each { |method| send(method) }
90
70
  end
91
71
  end
92
72
 
93
-
94
- # Update an existing resource with the given attributes. If successful,
95
- # set the ".notice" flash and redirect to the resource; otherwise, set the
96
- # ".alert" flash and render the edit template with a 422 HTTP status
97
- # response.
98
- #
99
- # def update
100
- # update_resource @record, record_params
101
- # end
102
- #
103
- # A resource array can be provided to affect the redirect location. Only
104
- # the last resource will be updated.
105
- #
106
- # def update
107
- # update_resource [@parent, @record], record_params
108
- # end
109
- #
110
- # An optional :context argument will be passed to the record’s #save method
111
- # to set the validation context.
112
- #
113
- # def update
114
- # update_resource @record, record_params, context: :scenario
115
- # end
116
- #
117
- # An optional :location argument will override the redirect location.
118
- #
119
- # def update
120
- # update_resource @record, record_params, location: :records
121
- # end
122
- #
123
- # An optional :template argument will override the default :edit template.
124
- #
125
- # def update
126
- # update_resource @record, record_params, template: :alternate
127
- # end
128
- #
129
- # Passing a block will yield true if the model updates successfully, false
130
- # otherwise.
73
+ # Convenience method for responding to the boolean result of a model’s
74
+ # method. The model is extracted from the provided resource chain, and
75
+ # unless a :location option is provided the resource_chain will be used as
76
+ # the redirect location. See #respond_to_boolean for more information.
131
77
  #
132
- # def update
133
- # update_resource @record, record_params do |success|
134
- # if sucess
135
- # # something
136
- # else
137
- # # something
138
- # end
139
- # end
78
+ # def create
79
+ # respond_to_boolean([:namespace, @resource], :save, { attribute: 1 })
140
80
  # end
141
- def update_resource(resource, attributes, context: nil, location: nil, template: nil)
142
- model = resource.is_a?(Array) ? resource.last : resource
143
- model.assign_attributes(attributes)
144
-
145
- if model.save(context: context)
146
- flash.notice = t('.notice')
147
- yield true if block_given?
148
- redirect_to location || resource
81
+ def respond_to_resource(resource_chain, method, arguments = nil, options = {})
82
+ if resource_chain.is_a?(Array)
83
+ model = Array(resource_chain).reject { |o| [String, Symbol].include?(o.class) }.last
149
84
  else
150
- flash.now.alert = t('.alert')
151
- response.status = 422
152
- yield false if block_given?
153
- render template || :edit
85
+ model = resource_chain
154
86
  end
155
- end
156
87
 
157
- # Delete the given model.
158
- #
159
- # If the operation succeeds, provide a successful flash notice and
160
- # redirect to the provided location (if given) or to the pluralized path
161
- # of the original resource.
162
- #
163
- # If the operation fails, provide a failed flash alert. Then, if the
164
- # :delete action exists, render the edit action with an
165
- # :unprocessable_entity HTTP status; if the action does not exist,
166
- # redirect to the original resource.
167
- #
168
- #
169
- #
170
- # Destroy an existing resource. If successful, set the ".notice" flash and
171
- # redirect to the symbolized, plural name of the resource; otherwise, set
172
- # the ".alert" flash and render the delete template with a 422 HTTP status
173
- # response; if the delete action is not defined, instead redirect the
174
- # resource.
175
- #
176
- # def destroy
177
- # destroy_resource @record
178
- # end
179
- #
180
- # A resource array can be provided to affect the redirect location. Only
181
- # the last resource will be destroyed.
182
- #
183
- # def destroy
184
- # destroy_resource [@parent, @record]
185
- # end
186
- #
187
- # An optional :location argument will override the redirect location.
188
- #
189
- # def destroy
190
- # destroy_resource @record, location: :root
191
- # end
192
- #
193
- # Passing a block will yield true if the model destroys successfully, false
194
- # otherwise.
195
- #
196
- # def destroy
197
- # destroy_resource @record do |success|
198
- # if sucess
199
- # # something
200
- # else
201
- # # something
202
- # end
203
- # end
204
- # end
205
- def destroy_resource(resource, location: nil)
206
- model = resource.is_a?(Array) ? resource.last : resource
88
+ yield(model, options) if block_given?
207
89
 
208
- if model.destroy
209
- if !location
210
- location = Array(resource)[0..-2] + [model.model_name.plural.to_sym]
211
- end
90
+ unless options.key?(:location)
91
+ options[:location] = resource_chain
92
+ end
212
93
 
213
- flash.notice = t('.notice')
214
- yield true if block_given?
215
- redirect_to location
94
+ if arguments.is_a?(Hash)
95
+ respond_to_boolean(model.send(method, arguments), options)
216
96
  else
217
- if respond_to?(:delete)
218
- flash.now.alert = t('.alert')
219
- response.status = 422
220
- yield false if block_given?
221
- render :delete
222
- else
223
- flash.alert = t('.alert')
224
- yield false if block_given?
225
- redirect_to resource
226
- end
97
+ respond_to_boolean(model.send(method, *arguments), options)
227
98
  end
228
99
  end
229
100
 
230
- # Reorder the given models on the order attribute by the given attributes.
231
- #
232
- # If all operations succeed, provide a successful flash notice and
233
- # redirect to the provided location (if given) or to the pluralized path
234
- # of the first original resource.
235
- #
236
- # If any operation fails, provide a failed flash alert and render the
237
- # :edit action with an :unprocessable_entity HTTP status.
238
- #
239
- # def update
240
- # reorder_resources @records, record_params
241
- # end
242
- #
243
- # An optional :attribute argument will override the default reording
244
- # attribute (:position).
245
- #
246
- # def update
247
- # reorder_resources @records, record_params, attribute: :ranking
248
- # end
249
- #
250
- # An optional :location argument will override the redirect location.
251
- #
252
- # def update
253
- # reorder_resources @records, record_params, location: :root
254
- # end
255
- #
256
- # Passing a block will yield true if all models are reordered successfully,
257
- # false otherwise.
258
- #
259
- # def update
260
- # reorder_resources @records, record_params do |success|
261
- # if sucess
262
- # # something
263
- # else
264
- # # something
265
- # end
266
- # end
267
- # end
268
- def reorder_resources(resources, attributes, attribute: :position, location: nil)
269
- models = resources.is_a?(Array) && (resources.last.is_a?(Array) || resources.last.is_a?(ActiveRecord::Relation)) ? resources.last : resources
270
-
271
- models.each do |model|
272
- model.update(attribute => attributes[model.id.to_s].to_i)
101
+ # Convenience method for creating a new ActiveRecord-like resource.
102
+ # Attributes will be assigned to the model prior to saving. See
103
+ # #respond_to_resource for more information.
104
+ def create_resource(resource_chain, attributes, options = {})
105
+ context = options.slice!(:context) if options.key?(:context)
106
+ respond_to_resource(resource_chain, :save, context, options) do |model|
107
+ model.assign_attributes(attributes)
273
108
  end
109
+ end
274
110
 
275
- if models.all? { |model| model.errors.empty? }
276
- if !location
277
- if models.any?
278
- location = Array(resources)[0..-2] + [models.first.model_name.plural.to_sym]
279
- else
280
- raise ArgumentError, 'Must provide one or more resources or the :location argument'
281
- end
282
- end
111
+ # Convenience method for updating an existing ActiveRecord-like resource.
112
+ # See #respond_to_resource for more information.
113
+ def update_resource(resource_chain, attributes, options = {})
114
+ respond_to_resource(resource_chain, :update, attributes, options)
115
+ end
283
116
 
284
- flash.notice = t('.notice')
285
- yield true if block_given?
286
- redirect_to location
287
- else
288
- flash.now.alert = t('.alert')
289
- response.status = 422
290
- yield false if block_given?
291
- render :edit
117
+ # Convenience method for destroying an existing ActiveRecord-like resource.
118
+ # See #respond_to_resource for more information.
119
+ def destroy_resource(resource_chain, options = {})
120
+ respond_to_resource(resource_chain, :destroy, nil, options) do |model, options|
121
+ options[:location] ||= Array(resource_chain)[0..-2] + [model.model_name.plural.to_sym]
292
122
  end
293
123
  end
294
124
 
295
- # Toggle the given model attribute on.
296
- #
297
- # If the operation succeeds, provide a successful flash notice; otherwise,
298
- # provide a failed flash alert. Redirect to the provided location (if
299
- # given) or to the initial resource.
300
- #
301
- # def update
302
- # toggle_resource_boolen_on @record, :active
303
- # end
304
- #
305
- # An optional :location argument will override the redirect location.
306
- #
307
- # def update
308
- # toggle_resource_boolen_on @record, :active, location: :records
309
- # end
310
- #
311
- # Passing a block will yield true if the model is updated successfully,
312
- # false otherwise.
313
- #
314
- # def update
315
- # toggle_resource_boolen_on @record, :active do |success|
316
- # if success
317
- # # something
318
- # else
319
- # # something
320
- # end
321
- # end
322
- # end
323
- def toggle_resource_boolean_on(resource, attribute, location: nil)
324
- model = resource.is_a?(Array) ? resource.last : resource
125
+ # Convenience method for updating an existing ActiveRecord-like resource’s
126
+ # attribute to true. See #respond_to_resource for more information.
127
+ def enable_resource(resource_chain, attribute, options = {})
128
+ respond_to_resource(resource_chain, :update, { attribute => true }, options)
129
+ end
325
130
 
326
- if model.update(attribute => true)
327
- flash.notice = t('.notice')
328
- yield true if block_given?
329
- redirect_to location || resource
330
- else
331
- flash.alert = t('.alert')
332
- yield false if block_given?
333
- redirect_to location || resource
334
- end
131
+ # Convenience method for updating an existing ActiveRecord-like resource’s
132
+ # attribute to false. See #respond_to_resource for more information.
133
+ def disable_resource(resource_chain, attribute, options = {})
134
+ respond_to_resource(resource_chain, :update, { attribute => false }, options)
335
135
  end
336
136
 
337
- # Toggle the given model attribute off.
338
- #
339
- # If the operation succeeds, provide a successful flash notice; otherwise,
340
- # provide a failed flash alert. Redirect to the provided location (if
341
- # given) or to the initial resource.
342
- #
343
- # def update
344
- # toggle_resource_boolen_off @record, :active
345
- # end
346
- #
347
- # An optional :location argument will override the redirect location.
348
- #
349
- # def update
350
- # toggle_resource_boolen_off @record, :active, location: :records
351
- # end
352
- #
353
- # Passing a block will yield true if the model is updated successfully,
354
- # false otherwise.
355
- #
356
- # def update
357
- # toggle_resource_boolen_off @record, :active do |success|
358
- # if success
359
- # # something
360
- # else
361
- # # something
362
- # end
363
- # end
364
- # end
365
- def toggle_resource_boolean_off(resource, attribute, location: nil)
366
- model = resource.is_a?(Array) ? resource.last : resource
137
+ # Convenience method for updating an ActiveRecord::Relation-like
138
+ # collections’ attributes. Use the :permit option to limit the allowed
139
+ # attributes. See #respond_to_resource for more information.
140
+ def mass_update_resources(resources_chain, attributes, options = {})
141
+ attributes_values = attributes.values
367
142
 
368
- if model.update(attribute => false)
369
- flash.notice = t('.notice')
370
- yield true if block_given?
371
- redirect_to location || resource
372
- else
373
- flash.alert = t('.alert')
374
- yield false if block_given?
375
- redirect_to location || resource
143
+ if permit = options.delete(:permit)
144
+ attributes_values.map! { |attr| ActionController::Parameters.new(attr).permit(permit) }
145
+ end
146
+
147
+ respond_to_resource(resources_chain, :update, [attributes.keys, attributes_values], options) do |models, options|
148
+ options[:location] ||= Array(resources_chain)[0..-2] + [models.first.model_name.plural.to_sym]
149
+ end
150
+ end
151
+
152
+ private
153
+
154
+ # Returns a string for the value. If the value is a string it will be
155
+ # returned. If the value is a hash or is truthful the I18n translation for
156
+ # that key will be rendered with the value as its locals. Include a local of
157
+ # :_html to render the html version of the translation.
158
+ def translate_key(key, value)
159
+ if value.is_a?(String)
160
+ value
161
+ elsif value.kind_of?(Hash)
162
+ t(".#{key}#{'_html' if value.delete(:_html)}", value)
163
+ elsif value
164
+ t(".#{key}")
376
165
  end
377
166
  end
378
167
  end
@@ -1,3 +1,3 @@
1
1
  module Minimalizer
2
- VERSION = '0.0.2'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minimalizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Theodore Kimble
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-19 00:00:00.000000000 Z
11
+ date: 2016-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack-test
@@ -94,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  version: '0'
95
95
  requirements: []
96
96
  rubyforge_project:
97
- rubygems_version: 2.4.5
97
+ rubygems_version: 2.5.1
98
98
  signing_key:
99
99
  specification_version: 4
100
100
  summary: Write Ruby on Rails applications more easily with Minimalizer