Files
openfoodnetwork/app/controllers/admin/enterprise_fees_controller.rb
Matt-Yorkley 787f29105c Fix ForbiddenAttribute errors for bulk update of enterprise fees and order cycles
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='
2020-11-17 18:51:48 +00:00

93 lines
3.1 KiB
Ruby

module Admin
class EnterpriseFeesController < ResourceController
before_action :load_enterprise_fee_set, only: :index
before_action :load_data
def index
@include_calculators = params[:include_calculators].present?
@enterprise = current_enterprise
@enterprises = Enterprise.managed_by(spree_current_user).by_name
blank_enterprise_fee = EnterpriseFee.new
blank_enterprise_fee.enterprise = current_enterprise
3.times { @collection << blank_enterprise_fee }
respond_to do |format|
format.html
format.json { render_as_json @collection, controller: self, include_calculators: @include_calculators }
# format.json { @presented_collection = @collection.each_with_index.map { |ef, i| EnterpriseFeePresenter.new(self, ef, i) } }
end
end
def for_order_cycle
respond_to do |format|
format.html
format.json { render_as_json @collection, controller: self }
end
end
def bulk_update
@enterprise_fee_set = EnterpriseFeeSet.new(enterprise_fee_bulk_params)
if @enterprise_fee_set.save
redirect_to redirect_path, notice: I18n.t(:enterprise_fees_update_notice)
else
redirect_to redirect_path,
flash: { error: @enterprise_fee_set.errors.full_messages.to_sentence }
end
end
private
def load_enterprise_fee_set
@enterprise_fee_set = EnterpriseFeeSet.new collection: collection
end
def load_data
@calculators = EnterpriseFee.calculators.sort_by(&:name)
@tax_categories = Spree::TaxCategory.order('is_default DESC, name ASC')
end
def collection
case action
when :for_order_cycle
order_cycle = OrderCycle.find_by(id: params[:order_cycle_id]) if params[:order_cycle_id]
coordinator = Enterprise.find_by(id: params[:coordinator_id]) if params[:coordinator_id]
order_cycle = OrderCycle.new(coordinator: coordinator) if order_cycle.nil? && coordinator.present?
enterprises = OpenFoodNetwork::OrderCyclePermissions.new(spree_current_user, order_cycle).visible_enterprises
EnterpriseFee.for_enterprises(enterprises).order('enterprise_id', 'fee_type', 'name')
else
collection = EnterpriseFee.managed_by(spree_current_user).order('enterprise_id', 'fee_type', 'name')
collection = collection.for_enterprise(current_enterprise) if current_enterprise
collection
end
end
def collection_actions
[:index, :for_order_cycle, :bulk_update]
end
def current_enterprise
Enterprise.find params[:enterprise_id] if params.key? :enterprise_id
end
def redirect_path
if params.key? :enterprise_id
return main_app.admin_enterprise_fees_path(enterprise_id: params[:enterprise_id])
end
main_app.admin_enterprise_fees_path
end
def enterprise_fee_bulk_params
params.require(:enterprise_fee_set).permit(
collection_attributes: [
:id, :enterprise_id, :fee_type, :name, :tax_category_id,
:inherits_tax_category, :calculator_type,
{ calculator_attributes: [:id, :preferred_flat_percent] }
]
)
end
end
end