Files
openfoodnetwork/app/services/permitted_attributes/order_cycle.rb
Matt-Yorkley 72c6935ff8 Fix handling of empty order cycle params
This guard clause was returning an instance of an unpermitted Params object (containing a blank hash) here, which was causing unexpected results in various places. Returning a blank hash explicitly resolved the issue.

Fixes:

4) Admin::OrderCyclesController create as a manager of a shop when creation is successful returns success: true and a valid edit path
     Failure/Error:
       @order_cycle_params ||= PermittedAttributes::OrderCycle.new(params).call.
         to_h.with_indifferent_access

     ActionController::UnfilteredParameters:
       unable to convert unpermitted parameters to hash
     # ./app/controllers/admin/order_cycles_controller.rb:245:in `order_cycle_params'
     # ./app/controllers/admin/order_cycles_controller.rb:44:in `create'
     # ./spec/support/controller_requests_helper.rb:49:in `process_action_with_route'
     # ./spec/support/controller_requests_helper.rb:27:in `spree_post'
     # ./spec/controllers/admin/order_cycles_controller_spec.rb:124:in `block (5 levels) in <module:Admin>'
2021-04-14 09:16:27 -07:00

58 lines
1.7 KiB
Ruby

# frozen_string_literal: true
module PermittedAttributes
class OrderCycle
def initialize(params)
@params = params
end
def call
return {} if @params[:order_cycle].blank?
@params.require(:order_cycle).permit(attributes)
end
def self.basic_attributes
[
:name, :orders_open_at, :orders_close_at, :coordinator_id,
:preferred_product_selection_from_coordinator_inventory_only,
schedule_ids: [], coordinator_fee_ids: []
]
end
private
def attributes
self.class.basic_attributes + [incoming_exchanges: permitted_exchange_attributes,
outgoing_exchanges: permitted_exchange_attributes]
end
def permitted_exchange_attributes
[
:id, :sender_id, :receiver_id, :enterprise_id, :incoming, :active,
:select_all_variants, :receival_instructions,
:pickup_time, :pickup_instructions,
:tag_list,
tags: [:text],
enterprise_fee_ids: [],
variants: permitted_variant_ids
]
end
# In rails 5 we will be able to permit random hash keys simply with :variants => {}
# See https://github.com/rails/rails/commit/e86524c0c5a26ceec92895c830d1355ae47a7034
#
# Until then, we need to create an array of variant IDs in order to permit them
def permitted_variant_ids
variant_ids(@params[:order_cycle][:incoming_exchanges]) +
variant_ids(@params[:order_cycle][:outgoing_exchanges])
end
def variant_ids(exchange_params)
return [] unless exchange_params
exchange_params.map { |exchange| exchange[:variants].map { |key, _value| key } }.flatten
end
end
end