Module: Minimalizer::ControllerHelpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/minimalizer/controller_helpers.rb

Instance Method Summary collapse

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, options = {})
  context = options.slice!(:context) if options.key?(:context)
  respond_to_resource(resource_chain, :save, context, options) 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, options = {})
  respond_to_resource(resource_chain, :destroy, nil, options) do |model, options|
  options[: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, options = {})
  respond_to_resource(resource_chain, :update, { attribute => false }, options)
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, options = {})
  respond_to_resource(resource_chain, :update, { attribute => true }, options)
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, options = {})
  attributes_values = attributes.values

  if permit = options.delete(:permit)
  attributes_values.map! { |attr| ActionController::Parameters.new(attr).permit(permit) }
  end

  respond_to_resource(resources_chain, :update, [attributes.keys, attributes_values], options) do |models, options|
  options[: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

Yields:

  • (model, options)


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, options = {})
  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, options) if block_given?

  unless options[:location].present? || options[:location] == false
  options[:location] = resource_chain
  end

  if arguments.is_a?(Hash)
  respond_to_boolean(model.send(method, arguments), options)
  else
  respond_to_boolean(model.send(method, *arguments), options)
  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, options = {})
  respond_to_resource(resource_chain, :update, attributes, options)
end