mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-17 04:34:24 +00:00
Deal with Schedule creation in its own controller
This way it can assign the order cycles to the schedule when this is persisted. An OrderCycleSchedule (the join table) can't be created until both schedule and order_cycle got an id. Also, we do not call `#adapt_params` when creating the Schedule as that assigns `order_cycle_ids` to `@object.attributes` thus, attempting to create the OrderCycleSchedule without a schedule_id.
This commit is contained in:
@@ -3,7 +3,7 @@ require 'order_management/subscriptions/proxy_order_syncer'
|
||||
|
||||
module Admin
|
||||
class SchedulesController < ResourceController
|
||||
before_filter :adapt_params, only: [:create, :update]
|
||||
before_filter :adapt_params, only: [:update]
|
||||
before_filter :check_editable_order_cycle_ids, only: [:create, :update]
|
||||
before_filter :check_dependent_subscriptions, only: [:destroy]
|
||||
create.after :sync_subscriptions
|
||||
@@ -28,6 +28,25 @@ module Admin
|
||||
end
|
||||
end
|
||||
|
||||
def create
|
||||
invoke_callbacks(:create, :before)
|
||||
@object.attributes = permitted_resource_params
|
||||
@object.save!
|
||||
|
||||
@object.order_cycle_ids = params[:order_cycle_ids]
|
||||
if @object.save
|
||||
invoke_callbacks(:create, :after)
|
||||
flash[:success] = flash_message_for(@object, :successfully_created)
|
||||
respond_with(@object) do |format|
|
||||
format.html { redirect_to location_after_save }
|
||||
format.js { render layout: false }
|
||||
end
|
||||
else
|
||||
invoke_callbacks(:create, :fails)
|
||||
respond_with(@object)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def collection
|
||||
@@ -95,11 +114,7 @@ module Admin
|
||||
end
|
||||
|
||||
def permitted_resource_params
|
||||
params.require(:schedule).permit(
|
||||
:id,
|
||||
:name,
|
||||
order_cycle_ids: []
|
||||
)
|
||||
params.require(:schedule).permit(:id, :name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user