From 56b590a6f8d8a331827de50f905048ac01cbeeec Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 29 May 2020 10:41:17 +0200 Subject: [PATCH] Extract `#sync_subscription` This makes it far easier to spot what's the difference between create and update regarding subs syncing without having to mess with `#update` and its callbacks (for now). --- app/controllers/admin/schedules_controller.rb | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/app/controllers/admin/schedules_controller.rb b/app/controllers/admin/schedules_controller.rb index 35df2c4190..4debb5f91f 100644 --- a/app/controllers/admin/schedules_controller.rb +++ b/app/controllers/admin/schedules_controller.rb @@ -7,7 +7,7 @@ module Admin before_filter :check_editable_order_cycle_ids_create, only: [:create] before_filter :check_editable_order_cycle_ids_update, only: [:update] before_filter :check_dependent_subscriptions, only: [:destroy] - update.after :sync_subscriptions + update.after :sync_subscriptions_update respond_to :json @@ -125,21 +125,19 @@ module Admin @permissions = OpenFoodNetwork::Permissions.new(spree_current_user) end - def sync_subscriptions + def sync_subscriptions_update return unless params[:schedule][:order_cycle_ids] - removed_ids = @existing_order_cycle_ids - @schedule.order_cycle_ids - new_ids = @schedule.order_cycle_ids - @existing_order_cycle_ids - return unless removed_ids.any? || new_ids.any? - - subscriptions = Subscription.where(schedule_id: @schedule) - syncer = OrderManagement::Subscriptions::ProxyOrderSyncer.new(subscriptions) - syncer.sync! + sync_subscriptions end def sync_subscriptions_create return unless params[:order_cycle_ids] + sync_subscriptions + end + + def sync_subscriptions removed_ids = @existing_order_cycle_ids - @schedule.order_cycle_ids new_ids = @schedule.order_cycle_ids - @existing_order_cycle_ids return unless removed_ids.any? || new_ids.any?