Module: Minimalizer::ControllerHelpers
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/minimalizer/controller_helpers.rb
Instance Method Summary collapse
-
#create_resource(resource_chain, attributes, options = {}) ⇒ Object
Convenience method for creating a new ActiveRecord-like resource.
-
#destroy_resource(resource_chain, options = {}) ⇒ Object
Convenience method for destroying an existing ActiveRecord-like resource.
-
#disable_resource(resource_chain, attribute, options = {}) ⇒ Object
Convenience method for updating an existing ActiveRecord-like resource’s attribute to false.
-
#enable_resource(resource_chain, attribute, options = {}) ⇒ Object
Convenience method for updating an existing ActiveRecord-like resource’s attribute to true.
-
#mass_update_resources(resources_chain, attributes, options = {}) ⇒ Object
Convenience method for updating an ActiveRecord::Relation-like collections’ attributes.
-
#respond_to_boolean(condition, location: nil, template: nil, redirect: false, on_succeed: [], on_fail: [], notice: true, alert: true) ⇒ Object
Respond to a boolean condition.
-
#respond_to_resource(resource_chain, method, arguments = nil, options = {}) {|model, options| ... } ⇒ Object
Convenience method for responding to the boolean result of a model’s method.
-
#update_resource(resource_chain, attributes, options = {}) ⇒ Object
Convenience method for updating an existing ActiveRecord-like resource.
Instance Method Details
#create_resource(resource_chain, attributes, options = {}) ⇒ Object
Convenience method for creating a new ActiveRecord-like resource. Attributes will be assigned to the model prior to saving. See #respond_to_resource for more information.
104 105 106 107 108 109 |
# File 'lib/minimalizer/controller_helpers.rb', line 104 def create_resource(resource_chain, attributes, = {}) context = .slice!(:context) if .key?(:context) respond_to_resource(resource_chain, :save, context, ) do |model| model.assign_attributes(attributes) end end |
#destroy_resource(resource_chain, options = {}) ⇒ Object
Convenience method for destroying an existing ActiveRecord-like resource. See #respond_to_resource for more information.
119 120 121 122 123 |
# File 'lib/minimalizer/controller_helpers.rb', line 119 def destroy_resource(resource_chain, = {}) respond_to_resource(resource_chain, :destroy, nil, ) do |model, | [:location] ||= Array(resource_chain)[0..-2] + [model.model_name.plural.to_sym] end end |
#disable_resource(resource_chain, attribute, options = {}) ⇒ Object
Convenience method for updating an existing ActiveRecord-like resource’s attribute to false. See #respond_to_resource for more information.
133 134 135 |
# File 'lib/minimalizer/controller_helpers.rb', line 133 def disable_resource(resource_chain, attribute, = {}) respond_to_resource(resource_chain, :update, { attribute => false }, ) end |
#enable_resource(resource_chain, attribute, options = {}) ⇒ Object
Convenience method for updating an existing ActiveRecord-like resource’s attribute to true. See #respond_to_resource for more information.
127 128 129 |
# File 'lib/minimalizer/controller_helpers.rb', line 127 def enable_resource(resource_chain, attribute, = {}) respond_to_resource(resource_chain, :update, { attribute => true }, ) end |
#mass_update_resources(resources_chain, attributes, options = {}) ⇒ Object
Convenience method for updating an ActiveRecord::Relation-like collections’ attributes. Use the :permit option to limit the allowed attributes. See #respond_to_resource for more information.
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/minimalizer/controller_helpers.rb', line 140 def mass_update_resources(resources_chain, attributes, = {}) attributes_values = attributes.values if permit = .delete(:permit) attributes_values.map! { |attr| ActionController::Parameters.new(attr).permit(permit) } end respond_to_resource(resources_chain, :update, [attributes.keys, attributes_values], ) do |models, | [:location] ||= Array(resources_chain)[0..-2] + [models.first.model_name.plural.to_sym] end end |
#respond_to_boolean(condition, location: nil, template: nil, redirect: false, on_succeed: [], on_fail: [], notice: true, alert: true) ⇒ Object
Respond to a boolean condition.
If the condition is truthful, set the notice flash, if present, and redirect to the :location option, if present.
If the condition is not truthful, set the alert flash, if present, and render the :template option or the default action. If the :redirect option is provided, redirect there instead. If the :redirect option equals true, then redirect to the :location option instead.
def create
respond_to_boolean(value, location: '/home')
end
Provide callbacks to :on_succeed or :on_fail and all methods will be called on the containing controller. Callbacks may be provided as a symbol or array of symbols.
def create
respond_to_boolean(value, location: '/home', on_succeed: :do_something)
end
By default the notice and alert values will be set to the I18n translations of “.notice” and “.alert”, respectively. Pass a string to render that value directly, a hash to use that value as the locals during the translation, or a false-like value to skip setting that flash value.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/minimalizer/controller_helpers.rb', line 56 def respond_to_boolean(condition, location: nil, template: nil, redirect: false, on_succeed: [], on_fail: [], notice: true, alert: true) if condition flash.notice = translate_key(:notice, notice) redirect_to location if location Array(on_succeed).each { |method| send(method) } elsif redirect flash.alert = translate_key(:alert, alert) location = redirect unless redirect == true redirect_to location if location Array(on_fail).each { |method| send(method) } else flash.now.alert = translate_key(:alert, alert) render template || { create: :new, update: :edit, destroy: :delete }[action_name.to_sym], status: 422 Array(on_fail).each { |method| send(method) } end end |
#respond_to_resource(resource_chain, method, arguments = nil, options = {}) {|model, options| ... } ⇒ Object
Convenience method for responding to the boolean result of a model’s method. The model is extracted from the provided resource chain, and unless a :location option is provided the resource_chain will be used as the redirect location. See #respond_to_boolean for more information.
def create
respond_to_boolean([:namespace, @resource], :save, { attribute: 1 })
end
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/minimalizer/controller_helpers.rb', line 81 def respond_to_resource(resource_chain, method, arguments = nil, = {}) if resource_chain.is_a?(Array) model = Array(resource_chain).reject { |o| [String, Symbol].include?(o.class) }.last else model = resource_chain end yield(model, ) if block_given? unless [:location].present? || [:location] == false [:location] = resource_chain end if arguments.is_a?(Hash) respond_to_boolean(model.send(method, arguments), ) else respond_to_boolean(model.send(method, *arguments), ) end end |
#update_resource(resource_chain, attributes, options = {}) ⇒ Object
Convenience method for updating an existing ActiveRecord-like resource. See #respond_to_resource for more information.
113 114 115 |
# File 'lib/minimalizer/controller_helpers.rb', line 113 def update_resource(resource_chain, attributes, = {}) respond_to_resource(resource_chain, :update, attributes, ) end |