mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-26 20:56:48 +00:00
Example error:
As an administrator
I want to manage simple order cycles
updating many order cycle opening/closing times at once
Failure/Error: raise ActiveModel::ForbiddenAttributesError, params.to_s
ActiveModel::ForbiddenAttributesError:
{"order_cycle_set"=>{"collection_attributes"=>{"0"=>{"id"=>62, "name"=>"Updated Order Cycle 1", "orders_open_at"=>"2040-12-01 12:00:00", "orders_close_at"=>"2040-12-01 12:00:01"}, "1"=>{"id"=>63, "name"=>"Updated Order Cycle 2", "orders_open_at"=>"2040-12-01 12:00:02", "orders_close_at"=>"2040-12-01 12:00:03"}, "2"=>{"id"=>64, "name"=>"Updated Order Cycle 3", "orders_open_at"=>"2040-12-01 12:00:04", "orders_close_at"=>"2040-12-01 12:00:05"}}}, "controller"=>"admin/order_cycles", "action"=>"bulk_update", "format"=>"json", "order_cycle"=>{}}
# ./app/controllers/application_controller.rb:20:in `print_params'
# ./lib/open_food_network/rack_request_blocker.rb:36:in `call'
# ------------------
# --- Caused by: ---
# ActiveModel::ForbiddenAttributesError:
# ActiveModel::ForbiddenAttributesError
# ./app/models/model_set.rb:29:in `block in collection_attributes='
58 lines
1.7 KiB
Ruby
58 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module PermittedAttributes
|
|
class OrderCycle
|
|
def initialize(params)
|
|
@params = params
|
|
end
|
|
|
|
def call
|
|
return @params[:order_cycle] 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
|