Files
openfoodnetwork/app/controllers/admin/subscription_line_items_controller.rb
Maikel Linke a447fe4f40 Require lib file where needed
We can't always rely on other parts of the code been loaded first. We
need to declare dependencies so that they are always present.

I just ran into this problem in my dev environment.
2018-09-13 12:01:16 +10:00

55 lines
1.8 KiB
Ruby

require 'open_food_network/permissions'
require 'open_food_network/order_cycle_permissions'
require 'open_food_network/scope_variant_to_hub'
module Admin
class SubscriptionLineItemsController < ResourceController
before_filter :load_build_context, only: [:build]
before_filter :ensure_shop, only: [:build]
before_filter :ensure_variant, only: [:build]
respond_to :json
def build
@subscription_line_item.assign_attributes(params[:subscription_line_item])
@subscription_line_item.price_estimate = price_estimate
render json: @subscription_line_item, serializer: Api::Admin::SubscriptionLineItemSerializer
end
private
def permissions
OpenFoodNetwork::Permissions.new(spree_current_user)
end
def load_build_context
@shop = Enterprise.managed_by(spree_current_user).find_by_id(params[:shop_id])
@schedule = permissions.editable_schedules.find_by_id(params[:schedule_id])
@order_cycle = @schedule.andand.current_or_next_order_cycle
@variant = Spree::Variant.stockable_by(@shop).find_by_id(params[:subscription_line_item][:variant_id])
end
def new_actions
[:new, :create, :build] # Added build
end
def ensure_shop
return if @shop
render json: { errors: ['Unauthorised'] }, status: :unauthorized
end
def ensure_variant
return if @variant
error = "#{@shop.name} is not permitted to sell the selected product"
render json: { errors: [error] }, status: :unprocessable_entity
end
def price_estimate
return unless @order_cycle
fee_calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new(@shop, @order_cycle)
OpenFoodNetwork::ScopeVariantToHub.new(@shop).scope(@variant)
@variant.price + fee_calculator.indexed_fees_for(@variant)
end
end
end