mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-17 00:07:24 +00:00
Replace references to 'standing order' with 'subscription'
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
require 'open_food_network/permissions'
|
||||
|
||||
module Admin
|
||||
class StandingOrdersController < ResourceController
|
||||
class SubscriptionsController < ResourceController
|
||||
before_filter :load_shops, only: [:index]
|
||||
before_filter :load_form_data, only: [:new, :edit]
|
||||
before_filter :strip_banned_attrs, only: [:update]
|
||||
@@ -12,7 +12,7 @@ module Admin
|
||||
def index
|
||||
respond_to do |format|
|
||||
format.html do
|
||||
if view_context.standing_orders_setup_complete?(@shops)
|
||||
if view_context.subscriptions_setup_complete?(@shops)
|
||||
@order_cycles = OrderCycle.joins(:schedules).managed_by(spree_current_user)
|
||||
@payment_methods = Spree::PaymentMethod.managed_by(spree_current_user)
|
||||
@shipping_methods = Spree::ShippingMethod.managed_by(spree_current_user)
|
||||
@@ -26,48 +26,48 @@ module Admin
|
||||
end
|
||||
|
||||
def new
|
||||
@standing_order.bill_address = Spree::Address.new
|
||||
@standing_order.ship_address = Spree::Address.new
|
||||
@subscription.bill_address = Spree::Address.new
|
||||
@subscription.ship_address = Spree::Address.new
|
||||
end
|
||||
|
||||
def create
|
||||
form = StandingOrderForm.new(@standing_order, params[:standing_order], fee_calculator)
|
||||
form = SubscriptionForm.new(@subscription, params[:subscription], fee_calculator)
|
||||
if form.save
|
||||
render_as_json @standing_order, fee_calculator: fee_calculator
|
||||
render_as_json @subscription, fee_calculator: fee_calculator
|
||||
else
|
||||
render json: { errors: form.json_errors }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
form = StandingOrderForm.new(@standing_order, params[:standing_order], fee_calculator)
|
||||
form = SubscriptionForm.new(@subscription, params[:subscription], fee_calculator)
|
||||
if form.save
|
||||
render_as_json @standing_order, fee_calculator: fee_calculator, order_update_issues: form.order_update_issues
|
||||
render_as_json @subscription, fee_calculator: fee_calculator, order_update_issues: form.order_update_issues
|
||||
else
|
||||
render json: { errors: form.json_errors }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def cancel
|
||||
@standing_order.cancel(@open_orders_to_keep || [])
|
||||
@subscription.cancel(@open_orders_to_keep || [])
|
||||
|
||||
respond_with(@standing_order) do |format|
|
||||
format.json { render_as_json @standing_order, fee_calculator: fee_calculator }
|
||||
respond_with(@subscription) do |format|
|
||||
format.json { render_as_json @subscription, fee_calculator: fee_calculator }
|
||||
end
|
||||
end
|
||||
|
||||
def pause
|
||||
unless params[:open_orders] == 'keep'
|
||||
@standing_order.proxy_orders.placed_and_open.each(&:cancel)
|
||||
@subscription.proxy_orders.placed_and_open.each(&:cancel)
|
||||
end
|
||||
|
||||
@standing_order.update_attributes(paused_at: Time.zone.now)
|
||||
render_as_json @standing_order, fee_calculator: fee_calculator
|
||||
@subscription.update_attributes(paused_at: Time.zone.now)
|
||||
render_as_json @subscription, fee_calculator: fee_calculator
|
||||
end
|
||||
|
||||
def unpause
|
||||
@standing_order.update_attributes(paused_at: nil)
|
||||
render_as_json @standing_order, fee_calculator: fee_calculator
|
||||
@subscription.update_attributes(paused_at: nil)
|
||||
render_as_json @subscription, fee_calculator: fee_calculator
|
||||
end
|
||||
|
||||
private
|
||||
@@ -79,68 +79,68 @@ module Admin
|
||||
|
||||
def collection
|
||||
if request.format.json?
|
||||
permissions.editable_standing_orders.ransack(params[:q]).result
|
||||
permissions.editable_subscriptions.ransack(params[:q]).result
|
||||
.preload([:shop, :customer, :schedule, :standing_line_items, :ship_address, :bill_address, proxy_orders: { order: :order_cycle }])
|
||||
else
|
||||
StandingOrder.where("1=0")
|
||||
Subscription.where("1=0")
|
||||
end
|
||||
end
|
||||
|
||||
def load_shops
|
||||
@shops = Enterprise.managed_by(spree_current_user).is_distributor.where(enable_standing_orders: true)
|
||||
@shops = Enterprise.managed_by(spree_current_user).is_distributor.where(enable_subscriptions: true)
|
||||
end
|
||||
|
||||
def load_form_data
|
||||
@customers = Customer.of(@standing_order.shop)
|
||||
@schedules = Schedule.with_coordinator(@standing_order.shop)
|
||||
@payment_methods = Spree::PaymentMethod.for_distributor(@standing_order.shop).for_standing_orders
|
||||
@shipping_methods = Spree::ShippingMethod.for_distributor(@standing_order.shop)
|
||||
@customers = Customer.of(@subscription.shop)
|
||||
@schedules = Schedule.with_coordinator(@subscription.shop)
|
||||
@payment_methods = Spree::PaymentMethod.for_distributor(@subscription.shop).for_subscriptions
|
||||
@shipping_methods = Spree::ShippingMethod.for_distributor(@subscription.shop)
|
||||
@order_cycles = OrderCycle.joins(:schedules).managed_by(spree_current_user)
|
||||
@fee_calculator = fee_calculator
|
||||
end
|
||||
|
||||
def fee_calculator
|
||||
shop, next_oc = @standing_order.shop, @standing_order.schedule.andand.current_or_next_order_cycle
|
||||
shop, next_oc = @subscription.shop, @subscription.schedule.andand.current_or_next_order_cycle
|
||||
return nil unless shop && next_oc
|
||||
OpenFoodNetwork::EnterpriseFeeCalculator.new(shop, next_oc)
|
||||
end
|
||||
|
||||
# Wrap :standing_line_items_attributes in :standing_order root
|
||||
# Wrap :standing_line_items_attributes in :subscription root
|
||||
def wrap_nested_attrs
|
||||
if params[:standing_line_items].is_a? Array
|
||||
attributes = params[:standing_line_items].map do |sli|
|
||||
sli.slice(*StandingLineItem.attribute_names + ["_destroy"])
|
||||
end
|
||||
params[:standing_order][:standing_line_items_attributes] = attributes
|
||||
params[:subscription][:standing_line_items_attributes] = attributes
|
||||
end
|
||||
wrap_bill_address_attrs if params[:bill_address]
|
||||
wrap_ship_address_attrs if params[:ship_address]
|
||||
end
|
||||
|
||||
def wrap_bill_address_attrs
|
||||
params[:standing_order][:bill_address_attributes] = params[:bill_address].slice(*Spree::Address.attribute_names)
|
||||
params[:subscription][:bill_address_attributes] = params[:bill_address].slice(*Spree::Address.attribute_names)
|
||||
end
|
||||
|
||||
def wrap_ship_address_attrs
|
||||
params[:standing_order][:ship_address_attributes] = params[:ship_address].slice(*Spree::Address.attribute_names)
|
||||
params[:subscription][:ship_address_attributes] = params[:ship_address].slice(*Spree::Address.attribute_names)
|
||||
end
|
||||
|
||||
def check_for_open_orders
|
||||
return if params[:open_orders] == 'cancel'
|
||||
@open_orders_to_keep = @standing_order.proxy_orders.placed_and_open.pluck(:id)
|
||||
@open_orders_to_keep = @subscription.proxy_orders.placed_and_open.pluck(:id)
|
||||
return if @open_orders_to_keep.empty? || params[:open_orders] == 'keep'
|
||||
render json: { errors: { open_orders: t('admin.standing_orders.confirm_cancel_open_orders_msg') } }, status: :conflict
|
||||
render json: { errors: { open_orders: t('admin.subscriptions.confirm_cancel_open_orders_msg') } }, status: :conflict
|
||||
end
|
||||
|
||||
def strip_banned_attrs
|
||||
params[:standing_order].delete :schedule_id
|
||||
params[:standing_order].delete :customer_id
|
||||
params[:subscription].delete :schedule_id
|
||||
params[:subscription].delete :customer_id
|
||||
end
|
||||
|
||||
# Overriding Spree method to load data from params here so that
|
||||
# we can authorise #create using an object with required attributes
|
||||
def build_resource
|
||||
StandingOrder.new(params[:standing_order])
|
||||
Subscription.new(params[:subscription])
|
||||
end
|
||||
|
||||
def ams_prefix_whitelist
|
||||
|
||||
Reference in New Issue
Block a user