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 +4 -4
- data/README.md +2 -0
- data/lib/minimalizer/controller_helpers.rb +102 -313
- data/lib/minimalizer/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 457231f04e30961579f27aad2237e0a1205ba9db
|
4
|
+
data.tar.gz: 821b0b7aa398798d7d1936bd85d20aedb4675632
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
+
[](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
|
-
#
|
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
|
-
#
|
36
|
-
#
|
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
|
-
#
|
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
|
-
#
|
41
|
+
# respond_to_boolean(value, location: '/home')
|
63
42
|
# end
|
64
43
|
#
|
65
|
-
#
|
66
|
-
#
|
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
|
-
#
|
70
|
-
#
|
71
|
-
#
|
72
|
-
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
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
|
-
|
87
|
-
|
88
|
-
|
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
|
-
#
|
95
|
-
#
|
96
|
-
#
|
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
|
133
|
-
#
|
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
|
142
|
-
|
143
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
209
|
-
|
210
|
-
|
211
|
-
end
|
90
|
+
unless options.key?(:location)
|
91
|
+
options[:location] = resource_chain
|
92
|
+
end
|
212
93
|
|
213
|
-
|
214
|
-
|
215
|
-
redirect_to location
|
94
|
+
if arguments.is_a?(Hash)
|
95
|
+
respond_to_boolean(model.send(method, arguments), options)
|
216
96
|
else
|
217
|
-
|
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
|
-
#
|
231
|
-
#
|
232
|
-
#
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
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
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
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
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
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
|
-
#
|
296
|
-
#
|
297
|
-
|
298
|
-
|
299
|
-
|
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
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
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
|
-
#
|
338
|
-
#
|
339
|
-
#
|
340
|
-
|
341
|
-
|
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
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
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
|
data/lib/minimalizer/version.rb
CHANGED
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
|
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:
|
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.
|
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
|