Module: Minimalizer::ControllerHelpers
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/minimalizer/controller_helpers.rb
Instance Method Summary collapse
-
#create_resource(resource, attributes, context: nil, location: nil, template: nil) ⇒ Object
Create a new resource with the given attributes.
-
#destroy_resource(resource, location: nil) ⇒ Object
Delete the given model.
-
#reorder_resources(resources, attributes, attribute: :position, location: nil) ⇒ Object
Reorder the given models on the order attribute by the given attributes.
-
#toggle_resource_boolean_off(resource, attribute, location: nil) ⇒ Object
Toggle the given model attribute off.
-
#toggle_resource_boolean_on(resource, attribute, location: nil) ⇒ Object
Toggle the given model attribute on.
-
#update_resource(resource, attributes, context: nil, location: nil, template: nil) ⇒ Object
Update an existing resource with the given attributes.
Instance Method Details
#create_resource(resource, attributes, context: nil, location: nil, template: nil) ⇒ Object
Create a new resource with the given attributes. If successful, set the “.notice” flash and redirect to the newly created resource; otherwise, set the “.alert” flash and render the new template with a 422 HTTP status reponse.
def create
create_resource @record, record_params
end
A resource array can be provided to affect the redirect location. Only the last resource will be saved.
def create
create_resource [@parent, @record], record_params
end
An optional :context argument will be passed to the record’s #save method to set the validation context.
def create
create_resource @record, record_params, context: :scenario
end
An optional :location argument will override the redirect location.
def create
create_resource @record, record_params, location: :records
end
An optional :template argument will override the default :new template.
def create
create_resource @record, record_params, template: :alternate
end
Passing a block will yield true if the model saves successfully, false otherwise.
def create
create_resource @record, record_params do |success|
if sucess
# something
else
# something
end
end
end
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/minimalizer/controller_helpers.rb', line 77 def create_resource(resource, attributes, context: nil, location: nil, template: nil) model = resource.is_a?(Array) ? resource.last : resource model.assign_attributes attributes if model.save(context: context) flash.notice = t('.notice') yield true if block_given? redirect_to location || resource else flash.now.alert = t('.alert') response.status = 422 yield false if block_given? render template || :new end end |
#destroy_resource(resource, location: nil) ⇒ Object
Delete the given model.
If the operation succeeds, provide a successful flash notice and redirect to the provided location (if given) or to the pluralized path of the original resource.
If the operation fails, provide a failed flash alert. Then, if the :delete action exists, render the edit action with an :unprocessable_entity HTTP status; if the action does not exist, redirect to the original resource.
Destroy an existing resource. If successful, set the “.notice” flash and redirect to the symbolized, plural name of the resource; otherwise, set the “.alert” flash and render the delete template with a 422 HTTP status response; if the delete action is not defined, instead redirect the resource.
def destroy
destroy_resource @record
end
A resource array can be provided to affect the redirect location. Only the last resource will be destroyed.
def destroy
destroy_resource [@parent, @record]
end
An optional :location argument will override the redirect location.
def destroy
destroy_resource @record, location: :root
end
Passing a block will yield true if the model destroys successfully, false otherwise.
def destroy
destroy_resource @record do |success|
if sucess
# something
else
# something
end
end
end
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/minimalizer/controller_helpers.rb', line 205 def destroy_resource(resource, location: nil) model = resource.is_a?(Array) ? resource.last : resource if model.destroy if !location location = Array(resource)[0..-2] + [model.model_name.plural.to_sym] end flash.notice = t('.notice') yield true if block_given? redirect_to location else if respond_to?(:delete) flash.now.alert = t('.alert') response.status = 422 yield false if block_given? render :delete else flash.alert = t('.alert') yield false if block_given? redirect_to resource end end end |
#reorder_resources(resources, attributes, attribute: :position, location: nil) ⇒ Object
Reorder the given models on the order attribute by the given attributes.
If all operations succeed, provide a successful flash notice and redirect to the provided location (if given) or to the pluralized path of the first original resource.
If any operation fails, provide a failed flash alert and render the :edit action with an :unprocessable_entity HTTP status.
def update
reorder_resources @records, record_params
end
An optional :attribute argument will override the default reording attribute (:position).
def update
reorder_resources @records, record_params, attribute: :ranking
end
An optional :location argument will override the redirect location.
def update
reorder_resources @records, record_params, location: :root
end
Passing a block will yield true if all models are reordered successfully, false otherwise.
def update
reorder_resources @records, record_params do |success|
if sucess
# something
else
# something
end
end
end
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/minimalizer/controller_helpers.rb', line 268 def reorder_resources(resources, attributes, attribute: :position, location: nil) models = resources.is_a?(Array) && (resources.last.is_a?(Array) || resources.last.is_a?(ActiveRecord::Relation)) ? resources.last : resources models.each do |model| model.update(attribute => attributes[model.id.to_s].to_i) end if models.all? { |model| model.errors.empty? } if !location if models.any? location = Array(resources)[0..-2] + [models.first.model_name.plural.to_sym] else raise ArgumentError, 'Must provide one or more resources or the :location argument' end end flash.notice = t('.notice') yield true if block_given? redirect_to location else flash.now.alert = t('.alert') response.status = 422 yield false if block_given? render :edit end end |
#toggle_resource_boolean_off(resource, attribute, location: nil) ⇒ Object
Toggle the given model attribute off.
If the operation succeeds, provide a successful flash notice; otherwise, provide a failed flash alert. Redirect to the provided location (if given) or to the initial resource.
def update
toggle_resource_boolen_off @record, :active
end
An optional :location argument will override the redirect location.
def update
toggle_resource_boolen_off @record, :active, location: :records
end
Passing a block will yield true if the model is updated successfully, false otherwise.
def update
toggle_resource_boolen_off @record, :active do |success|
if success
# something
else
# something
end
end
end
365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/minimalizer/controller_helpers.rb', line 365 def toggle_resource_boolean_off(resource, attribute, location: nil) model = resource.is_a?(Array) ? resource.last : resource if model.update(attribute => false) flash.notice = t('.notice') yield true if block_given? redirect_to location || resource else flash.alert = t('.alert') yield false if block_given? redirect_to location || resource end end |
#toggle_resource_boolean_on(resource, attribute, location: nil) ⇒ Object
Toggle the given model attribute on.
If the operation succeeds, provide a successful flash notice; otherwise, provide a failed flash alert. Redirect to the provided location (if given) or to the initial resource.
def update
toggle_resource_boolen_on @record, :active
end
An optional :location argument will override the redirect location.
def update
toggle_resource_boolen_on @record, :active, location: :records
end
Passing a block will yield true if the model is updated successfully, false otherwise.
def update
toggle_resource_boolen_on @record, :active do |success|
if success
# something
else
# something
end
end
end
323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/minimalizer/controller_helpers.rb', line 323 def toggle_resource_boolean_on(resource, attribute, location: nil) model = resource.is_a?(Array) ? resource.last : resource if model.update(attribute => true) flash.notice = t('.notice') yield true if block_given? redirect_to location || resource else flash.alert = t('.alert') yield false if block_given? redirect_to location || resource end end |
#update_resource(resource, attributes, context: nil, location: nil, template: nil) ⇒ Object
Update an existing resource with the given attributes. If successful, set the “.notice” flash and redirect to the resource; otherwise, set the “.alert” flash and render the edit template with a 422 HTTP status response.
def update
update_resource @record, record_params
end
A resource array can be provided to affect the redirect location. Only the last resource will be updated.
def update
update_resource [@parent, @record], record_params
end
An optional :context argument will be passed to the record’s #save method to set the validation context.
def update
update_resource @record, record_params, context: :scenario
end
An optional :location argument will override the redirect location.
def update
update_resource @record, record_params, location: :records
end
An optional :template argument will override the default :edit template.
def update
update_resource @record, record_params, template: :alternate
end
Passing a block will yield true if the model updates successfully, false otherwise.
def update
update_resource @record, record_params do |success|
if sucess
# something
else
# something
end
end
end
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/minimalizer/controller_helpers.rb', line 141 def update_resource(resource, attributes, context: nil, location: nil, template: nil) model = resource.is_a?(Array) ? resource.last : resource model.assign_attributes(attributes) if model.save(context: context) flash.notice = t('.notice') yield true if block_given? redirect_to location || resource else flash.now.alert = t('.alert') response.status = 422 yield false if block_given? render template || :edit end end |