Replace references to 'standing line item' with 'subscription line item'

This commit is contained in:
Rob Harrington
2018-02-05 12:28:11 +11:00
parent ae4420d9ba
commit 7017c8540c
35 changed files with 159 additions and 159 deletions

View File

@@ -1,7 +1,7 @@
angular.module("admin.subscriptions").controller "ProductsController", ($scope, StatusMessage) ->
$scope.registerNextCallback 'products', ->
$scope.subscription_form.$submitted = true
if $scope.subscription.standing_line_items.length > 0
if $scope.subscription.subscription_line_items.length > 0
$scope.subscription_form.$setPristine()
StatusMessage.clear()
$scope.setView('review')

View File

@@ -4,8 +4,8 @@ angular.module("admin.subscriptions").controller "ProductsPanelController", ($sc
$scope.saving = false
$scope.saved = ->
pristine = Subscriptions.pristineByID[$scope.subscription.id].standing_line_items
return false unless angular.equals($scope.subscription.standing_line_items, pristine)
pristine = Subscriptions.pristineByID[$scope.subscription.id].subscription_line_items
return false unless angular.equals($scope.subscription.subscription_line_items, pristine)
true
$scope.save = ->

View File

@@ -1,7 +1,7 @@
angular.module("admin.subscriptions").controller "StandingLineItemsController", ($scope, InfoDialog) ->
angular.module("admin.subscriptions").controller "SubscriptionLineItemsController", ($scope, InfoDialog) ->
$scope.newItem = { variant_id: 0, quantity: 1 }
$scope.addStandingLineItem = ->
$scope.addSubscriptionLineItem = ->
match = $scope.match()
if match
if match._destroy
@@ -13,18 +13,18 @@ angular.module("admin.subscriptions").controller "StandingLineItemsController",
$scope.subscription_form.$setDirty()
$scope.subscription.buildItem($scope.newItem)
$scope.removeStandingLineItem = (item) ->
$scope.removeSubscriptionLineItem = (item) ->
$scope.subscription_form.$setDirty()
$scope.subscription.removeItem(item)
$scope.match = ->
matching = $scope.subscription.standing_line_items.filter (sli) ->
matching = $scope.subscription.subscription_line_items.filter (sli) ->
sli.variant_id == $scope.newItem.variant_id
return matching[0] if matching.length > 0
null
$scope.estimatedSubtotal = ->
$scope.subscription.standing_line_items.reduce (subtotal, item) ->
$scope.subscription.subscription_line_items.reduce (subtotal, item) ->
return subtotal if item._destroy
subtotal += item.price_estimate * item.quantity
, 0

View File

@@ -12,7 +12,7 @@ angular.module("admin.subscriptions").controller "SubscriptionsController", ($sc
$scope.subscriptions = Subscriptions.index("q[shop_id_eq]": $scope.shop_id, "q[canceled_at_null]": true)
$scope.itemCount = (subscription) ->
subscription.standing_line_items.reduce (sum, sli) ->
subscription.subscription_line_items.reduce (sum, sli) ->
return sum + sli.quantity
, 0

View File

@@ -3,8 +3,8 @@ angular.module("admin.subscriptions").factory 'SubscriptionPrototype', ($http, $
return false unless item.variant_id > 0
return false unless item.quantity > 0
data = angular.extend({}, item, { shop_id: @shop_id, schedule_id: @schedule_id })
$http.post("/admin/standing_line_items/build", data).then (response) =>
@standing_line_items.push response.data
$http.post("/admin/subscription_line_items/build", data).then (response) =>
@subscription_line_items.push response.data
, (response) =>
InfoDialog.open 'error', response.data.errors[0]

View File

@@ -23,5 +23,5 @@ angular.module("admin.subscriptions").factory 'Subscriptions', ($q, Subscription
afterRemoveItem: (id, deletedItemID) ->
return unless @pristineByID[id]?
for item, i in @pristineByID[id].standing_line_items when item.id == deletedItemID
@pristineByID[id].standing_line_items.splice(i, 1)
for item, i in @pristineByID[id].subscription_line_items when item.id == deletedItemID
@pristineByID[id].subscription_line_items.splice(i, 1)

View File

@@ -2,7 +2,7 @@ require 'open_food_network/permissions'
require 'open_food_network/order_cycle_permissions'
module Admin
class StandingLineItemsController < ResourceController
class SubscriptionLineItemsController < ResourceController
before_filter :load_build_context, only: [:build]
before_filter :ensure_shop, only: [:build]
before_filter :ensure_variant, only: [:build]
@@ -10,10 +10,10 @@ module Admin
respond_to :json
def build
@standing_line_item.assign_attributes(params[:standing_line_item])
@subscription_line_item.assign_attributes(params[:subscription_line_item])
fee_calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new(@shop, @order_cycle) if @order_cycle
OpenFoodNetwork::ScopeVariantToHub.new(@shop).scope(@variant)
render json: @standing_line_item, serializer: Api::Admin::StandingLineItemSerializer, fee_calculator: fee_calculator
render json: @subscription_line_item, serializer: Api::Admin::SubscriptionLineItemSerializer, fee_calculator: fee_calculator
end
private
@@ -26,7 +26,7 @@ module Admin
@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[:standing_line_item][:variant_id])
@variant = Spree::Variant.stockable_by(@shop).find_by_id(params[:subscription_line_item][:variant_id])
end
def new_actions

View File

@@ -80,7 +80,7 @@ module Admin
def collection
if request.format.json?
permissions.editable_subscriptions.ransack(params[:q]).result
.preload([:shop, :customer, :schedule, :standing_line_items, :ship_address, :bill_address, proxy_orders: { order: :order_cycle }])
.preload([:shop, :customer, :schedule, :subscription_line_items, :ship_address, :bill_address, proxy_orders: { order: :order_cycle }])
else
Subscription.where("1=0")
end
@@ -105,13 +105,13 @@ module Admin
OpenFoodNetwork::EnterpriseFeeCalculator.new(shop, next_oc)
end
# Wrap :standing_line_items_attributes in :subscription root
# Wrap :subscription_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"])
if params[:subscription_line_items].is_a? Array
attributes = params[:subscription_line_items].map do |sli|
sli.slice(*SubscriptionLineItem.attribute_names + ["_destroy"])
end
params[:subscription][:standing_line_items_attributes] = attributes
params[:subscription][:subscription_line_items_attributes] = attributes
end
wrap_bill_address_attrs if params[:bill_address]
wrap_ship_address_attrs if params[:ship_address]

View File

@@ -50,7 +50,7 @@ class ProxyOrder < ActiveRecord::Base
shipping_method_id: subscription.shipping_method_id
)
order.update_attribute(:user, subscription.customer.user)
subscription.standing_line_items.each do |sli|
subscription.subscription_line_items.each do |sli|
order.line_items.build(variant_id: sli.variant_id, quantity: sli.quantity, skip_stock_check: true)
end
order.update_attributes(bill_address: subscription.bill_address.dup, ship_address: subscription.ship_address.dup)

View File

@@ -256,9 +256,9 @@ class AbilityDecorator
can [:create, :edit, :update, :cancel, :pause, :unpause], Subscription do |subscription|
user.enterprises.include?(subscription.shop)
end
can [:admin, :build], StandingLineItem
can [:destroy], StandingLineItem do |standing_line_item|
user.enterprises.include?(standing_line_item.subscription.shop)
can [:admin, :build], SubscriptionLineItem
can [:destroy], SubscriptionLineItem do |subscription_line_item|
user.enterprises.include?(subscription_line_item.subscription.shop)
end
can [:admin, :edit, :cancel, :resume], ProxyOrder do |proxy_order|
user.enterprises.include?(proxy_order.subscription.shop)

View File

@@ -9,7 +9,7 @@ class Subscription < ActiveRecord::Base
belongs_to :bill_address, foreign_key: :bill_address_id, class_name: Spree::Address
belongs_to :ship_address, foreign_key: :ship_address_id, class_name: Spree::Address
belongs_to :credit_card, foreign_key: :credit_card_id, class_name: 'Spree::CreditCard'
has_many :standing_line_items, inverse_of: :subscription
has_many :subscription_line_items, inverse_of: :subscription
has_many :order_cycles, through: :schedule
has_many :proxy_orders
has_many :orders, through: :proxy_orders
@@ -17,7 +17,7 @@ class Subscription < ActiveRecord::Base
alias_attribute :billing_address, :bill_address
alias_attribute :shipping_address, :ship_address
accepts_nested_attributes_for :standing_line_items, allow_destroy: true
accepts_nested_attributes_for :subscription_line_items, allow_destroy: true
accepts_nested_attributes_for :bill_address, :ship_address
scope :not_ended, -> { where('subscriptions.ends_at > (?) OR subscriptions.ends_at IS NULL', Time.zone.now) }

View File

@@ -1,5 +1,5 @@
class StandingLineItem < ActiveRecord::Base
belongs_to :subscription, inverse_of: :standing_line_items
class SubscriptionLineItem < ActiveRecord::Base
belongs_to :subscription, inverse_of: :subscription_line_items
belongs_to :variant, class_name: 'Spree::Variant'
validates :subscription, presence: true

View File

@@ -8,7 +8,7 @@ module Api
if object.total.present?
object.total.to_money.to_s
else
object.subscription.standing_line_items.sum(&:total_estimate)
object.subscription.subscription_line_items.sum(&:total_estimate)
end
end

View File

@@ -1,6 +1,6 @@
module Api
module Admin
class StandingLineItemSerializer < ActiveModel::Serializer
class SubscriptionLineItemSerializer < ActiveModel::Serializer
attributes :id, :variant_id, :quantity, :description, :price_estimate
def description

View File

@@ -4,7 +4,7 @@ module Api
attributes :id, :shop_id, :customer_id, :schedule_id, :payment_method_id, :shipping_method_id, :begins_at, :ends_at
attributes :customer_email, :schedule_name, :edit_path, :canceled_at, :paused_at, :state, :credit_card_id
has_many :standing_line_items, serializer: Api::Admin::StandingLineItemSerializer
has_many :subscription_line_items, serializer: Api::Admin::SubscriptionLineItemSerializer
has_many :closed_proxy_orders, serializer: Api::Admin::ProxyOrderSerializer
has_many :not_closed_proxy_orders, serializer: Api::Admin::ProxyOrderSerializer
has_one :bill_address, serializer: Api::AddressSerializer

View File

@@ -1,5 +1,5 @@
# Responsible for keeping line items on initialised orders for a subscription in sync with
# the standing line items on that subscription.
# the subscription line items on that subscription.
class LineItemSyncer
def initialize(subscription, order_update_issues)
@@ -15,12 +15,12 @@ class LineItemSyncer
private
delegate :standing_line_items, to: :subscription
delegate :subscription_line_items, to: :subscription
attr_reader :subscription, :order_update_issues
def update_item_quantities(order)
changed_standing_line_items.each do |sli|
changed_subscription_line_items.each do |sli|
line_item = order.line_items.find_by_variant_id(sli.variant_id)
next if update_quantity(line_item, sli)
product_name = "#{line_item.product.name} - #{line_item.full_name}"
@@ -29,21 +29,21 @@ class LineItemSyncer
end
def create_new_items(order)
new_standing_line_items.each do |sli|
new_subscription_line_items.each do |sli|
order.line_items.create(variant_id: sli.variant_id, quantity: sli.quantity, skip_stock_check: true)
end
end
def destroy_obsolete_items(order)
order.line_items.where(variant_id: standing_line_items.select(&:marked_for_destruction?).map(&:variant_id)).destroy_all
order.line_items.where(variant_id: subscription_line_items.select(&:marked_for_destruction?).map(&:variant_id)).destroy_all
end
def changed_standing_line_items
standing_line_items.select{ |sli| sli.changed? && sli.persisted? }
def changed_subscription_line_items
subscription_line_items.select{ |sli| sli.changed? && sli.persisted? }
end
def new_standing_line_items
standing_line_items.select(&:new_record?)
def new_subscription_line_items
subscription_line_items.select(&:new_record?)
end
def update_quantity(line_item, sli)

View File

@@ -23,7 +23,7 @@ class OrderSyncer
attr_reader :subscription, :line_item_syncer
delegate :orders, :bill_address, :ship_address, :standing_line_items, to: :subscription
delegate :orders, :bill_address, :ship_address, :subscription_line_items, to: :subscription
delegate :shop_id, :customer, :customer_id, to: :subscription
delegate :shipping_method, :shipping_method_id, :payment_method, :payment_method_id, to: :subscription
delegate :shipping_method_id_changed?, :shipping_method_id_was, to: :subscription

View File

@@ -32,19 +32,19 @@ class SubscriptionForm
end
def validate_price_estimates
return unless params[:standing_line_items_attributes]
return unless params[:subscription_line_items_attributes]
return clear_price_estimates unless fee_calculator
calculate_prices_from_variant_ids
end
def clear_price_estimates
params[:standing_line_items_attributes].each do |item_attrs|
params[:subscription_line_items_attributes].each do |item_attrs|
item_attrs.delete(:price_estimate)
end
end
def calculate_prices_from_variant_ids
params[:standing_line_items_attributes].each do |item_attrs|
params[:subscription_line_items_attributes].each do |item_attrs|
variant = Spree::Variant.find_by_id(item_attrs[:variant_id])
next item_attrs.delete(:price_estimate) unless variant
item_attrs[:price_estimate] = price_estimate_for(variant)

View File

@@ -18,13 +18,13 @@ class SubscriptionValidator
validate :customer_allowed?
validate :schedule_allowed?
validate :credit_card_ok?
validate :standing_line_items_present?
validate :subscription_line_items_present?
validate :requested_variants_available?
delegate :shop, :customer, :schedule, :shipping_method, :payment_method, to: :subscription
delegate :bill_address, :ship_address, :begins_at, :ends_at, to: :subscription
delegate :credit_card, :credit_card_id, to: :subscription
delegate :standing_line_items, to: :subscription
delegate :subscription_line_items, to: :subscription
def initialize(subscription)
@subscription = subscription
@@ -82,26 +82,26 @@ class SubscriptionValidator
errors.add(:credit_card, :not_available)
end
def standing_line_items_present?
return if standing_line_items.reject(&:marked_for_destruction?).any?
errors.add(:standing_line_items, :at_least_one_product)
def subscription_line_items_present?
return if subscription_line_items.reject(&:marked_for_destruction?).any?
errors.add(:subscription_line_items, :at_least_one_product)
end
def requested_variants_available?
standing_line_items.each { |sli| verify_availability_of(sli.variant) }
subscription_line_items.each { |sli| verify_availability_of(sli.variant) }
end
def verify_availability_of(variant)
return if available_variant_ids.include? variant.id
name = "#{variant.product.name} - #{variant.full_name}"
errors.add(:standing_line_items, :not_available, name: name)
errors.add(:subscription_line_items, :not_available, name: name)
end
# TODO: Extract this into a separate class
def available_variant_ids
@available_variant_ids ||=
Spree::Variant.joins(exchanges: { order_cycle: :schedules })
.where(id: standing_line_items.map(&:variant_id))
.where(id: subscription_line_items.map(&:variant_id))
.where(schedules: { id: schedule }, exchanges: { incoming: false, receiver_id: shop })
.merge(OrderCycle.not_closed)
.select('DISTINCT spree_variants.id')

View File

@@ -16,5 +16,5 @@
%input#add_quantity.fullwidth{ type: 'number', min: 1, ng: { model: 'newItem.quantity' } }
%td{ style: "vertical-align:top" }
.actions
%a.icon-plus.button.fullwidth{ href: 'javascript:void(0)', method: :post, ng: { click: 'addStandingLineItem()' } }
%a.icon-plus.button.fullwidth{ href: 'javascript:void(0)', method: :post, ng: { click: 'addSubscriptionLineItem()' } }
= t(:add)

View File

@@ -1,3 +1,3 @@
%div{ ng: { controller: 'StandingLineItemsController' } }
%div{ ng: { controller: 'SubscriptionLineItemsController' } }
= render 'autocomplete'
= render 'standing_line_items'
= render 'subscription_line_items'

View File

@@ -1,4 +1,4 @@
%table#standing-line-items
%table#subscription-line-items
%colgroup
%col{:style => "width: 49%;"}/
%col{:style => "width: 14%;"}/
@@ -14,14 +14,14 @@
%span= t(:total)
%th.orders-actions.actions
%tbody
%tr.item{ id: "sli_{{$index}}", ng: { repeat: "item in subscription.standing_line_items | filter:{ _destroy: '!true' }", class: { even: 'even', odd: 'odd' } } }
%tr.item{ id: "sli_{{$index}}", ng: { repeat: "item in subscription.subscription_line_items | filter:{ _destroy: '!true' }", class: { even: 'even', odd: 'odd' } } }
%td.description {{ item.description }}
%td.price.align-center {{ item.price_estimate | currency }}
%td.quantity
%input{ name: 'quantity', type: 'number', min: 0, ng: { model: 'item.quantity' } }
%td.total.align-center {{ (item.price_estimate * item.quantity) | currency }}
%td.actions
%a.delete-item.icon-trash.no-text{ ng: { click: 'removeStandingLineItem(item)'}, :href => "javascript:void(0)" }
%a.delete-item.icon-trash.no-text{ ng: { click: 'removeSubscriptionLineItem(item)'}, :href => "javascript:void(0)" }
%tbody#subtotal.no-border-top{"data-hook" => "admin_order_form_subtotal"}
%tr#subtotal-row
%td{:colspan => "3"}

View File

@@ -77,7 +77,7 @@ en:
models:
subscription_validator:
attributes:
standing_line_items:
subscription_line_items:
at_least_one_product: "^Please add at least one product"
not_available: "^%{name} is not available from the selected schedule"
ends_at:

View File

@@ -186,7 +186,7 @@ Openfoodnetwork::Application.routes.draw do
put :unpause, on: :member
end
resources :standing_line_items, only: [], format: :json do
resources :subscription_line_items, only: [], format: :json do
post :build, on: :collection
end

View File

@@ -1,6 +1,6 @@
require 'spec_helper'
describe Admin::StandingLineItemsController, type: :controller do
describe Admin::SubscriptionLineItemsController, type: :controller do
include AuthenticationWorkflow
@@ -17,7 +17,7 @@ describe Admin::StandingLineItemsController, type: :controller do
let(:unmanaged_schedule) { create(:schedule, order_cycles: [create(:simple_order_cycle, coordinator: unmanaged_shop)]) }
context "json" do
let(:params) { { format: :json, standing_line_item: { quantity: 2, variant_id: variant.id } } }
let(:params) { { format: :json, subscription_line_item: { quantity: 2, variant_id: variant.id } } }
context 'as an enterprise user' do
before { allow(controller).to receive(:spree_current_user) { user } }
@@ -55,7 +55,7 @@ describe Admin::StandingLineItemsController, type: :controller do
end
context "but no schedule_id is provided" do
it "returns a serialized standing line item without a price estimate" do
it "returns a serialized subscription line item without a price estimate" do
spree_post :build, params
json_response = JSON.parse(response.body)
@@ -68,7 +68,7 @@ describe Admin::StandingLineItemsController, type: :controller do
context "but an unmanaged schedule_id is provided" do
before { params.merge!(schedule_id: unmanaged_schedule.id) }
it "returns a serialized standing line item without a price estimate" do
it "returns a serialized subscription line item without a price estimate" do
spree_post :build, params
json_response = JSON.parse(response.body)
@@ -81,7 +81,7 @@ describe Admin::StandingLineItemsController, type: :controller do
context "and a managed schedule_id is provided" do
before { params.merge!(schedule_id: schedule.id) }
it "returns a serialized standing line item with a price estimate" do
it "returns a serialized subscription line item with a price estimate" do
spree_post :build, params
json_response = JSON.parse(response.body)

View File

@@ -178,7 +178,7 @@ describe Admin::SubscriptionsController, type: :controller do
params.merge!(
bill_address: address.attributes.except('id'),
ship_address: address.attributes.except('id'),
standing_line_items: [{ quantity: 2, variant_id: variant.id}]
subscription_line_items: [{ quantity: 2, variant_id: variant.id}]
)
end
@@ -186,14 +186,14 @@ describe Admin::SubscriptionsController, type: :controller do
it 'returns an error' do
expect{ spree_post :create, params }.to_not change{ Subscription.count }
json_response = JSON.parse(response.body)
expect(json_response['errors']['standing_line_items']).to eq ["#{variant.product.name} - #{variant.full_name} is not available from the selected schedule"]
expect(json_response['errors']['subscription_line_items']).to eq ["#{variant.product.name} - #{variant.full_name} is not available from the selected schedule"]
end
end
context 'where the specified variants are available from the shop' do
let!(:exchange) { create(:exchange, order_cycle: order_cycle, incoming: false, receiver: shop, variants: [variant]) }
it 'creates standing line items for the subscription' do
it 'creates subscription line items for the subscription' do
expect{ spree_post :create, params }.to change{ Subscription.count }.by(1)
subscription = Subscription.last
expect(subscription.schedule).to eq schedule
@@ -202,10 +202,10 @@ describe Admin::SubscriptionsController, type: :controller do
expect(subscription.shipping_method).to eq shipping_method
expect(subscription.bill_address.firstname).to eq address.firstname
expect(subscription.ship_address.firstname).to eq address.firstname
expect(subscription.standing_line_items.count).to be 1
standing_line_item = subscription.standing_line_items.first
expect(standing_line_item.quantity).to be 2
expect(standing_line_item.variant).to eq variant
expect(subscription.subscription_line_items.count).to be 1
subscription_line_item = subscription.subscription_line_items.first
expect(subscription_line_item.quantity).to be 2
expect(subscription_line_item.variant).to eq variant
end
end
end
@@ -259,9 +259,9 @@ describe Admin::SubscriptionsController, type: :controller do
schedule: schedule,
payment_method: payment_method,
shipping_method: shipping_method,
standing_line_items: [create(:standing_line_item, variant: variant1, quantity: 2)])
subscription_line_items: [create(:subscription_line_item, variant: variant1, quantity: 2)])
}
let(:standing_line_item1) { subscription.standing_line_items.first }
let(:subscription_line_item1) { subscription.subscription_line_items.first }
let(:params) { { format: :json, id: subscription.id, subscription: {} } }
context 'as an non-manager of the subscription shop' do
@@ -339,32 +339,32 @@ describe Admin::SubscriptionsController, type: :controller do
expect(subscription.shipping_method).to eq new_shipping_method
end
context 'with standing_line_items params' do
context 'with subscription_line_items params' do
let!(:product2) { create(:product, supplier: shop) }
let!(:variant2) { create(:variant, product: product2, unit_value: '1000', price: 6.00, option_values: []) }
before do
params[:standing_line_items] = [{id: standing_line_item1.id, quantity: 1, variant_id: variant1.id}, { quantity: 2, variant_id: variant2.id}]
params[:subscription_line_items] = [{id: subscription_line_item1.id, quantity: 1, variant_id: variant1.id}, { quantity: 2, variant_id: variant2.id}]
end
context 'where the specified variants are not available from the shop' do
it 'returns an error' do
expect{ spree_post :update, params }.to_not change{ subscription.standing_line_items.count }
expect{ spree_post :update, params }.to_not change{ subscription.subscription_line_items.count }
json_response = JSON.parse(response.body)
expect(json_response['errors']['standing_line_items']).to eq ["#{product2.name} - #{variant2.full_name} is not available from the selected schedule"]
expect(json_response['errors']['subscription_line_items']).to eq ["#{product2.name} - #{variant2.full_name} is not available from the selected schedule"]
end
end
context 'where the specified variants are available from the shop' do
before { outgoing_exchange.update_attributes(variants: [variant1, variant2]) }
it 'creates standing line items for the subscription' do
expect{ spree_post :update, params }.to change{ subscription.standing_line_items.count }.by(1)
it 'creates subscription line items for the subscription' do
expect{ spree_post :update, params }.to change{ subscription.subscription_line_items.count }.by(1)
subscription.reload
expect(subscription.standing_line_items.count).to be 2
standing_line_item = subscription.standing_line_items.last
expect(standing_line_item.quantity).to be 2
expect(standing_line_item.variant).to eq variant2
expect(subscription.subscription_line_items.count).to be 2
subscription_line_item = subscription.subscription_line_items.last
expect(subscription_line_item.quantity).to be 2
expect(subscription_line_item.variant).to eq variant2
end
end
end

View File

@@ -155,11 +155,11 @@ FactoryGirl.define do
after(:create) do |subscription, proxy|
if proxy.with_items
subscription.standing_line_items = build_list(:standing_line_item, 3, subscription: subscription)
subscription.subscription_line_items = build_list(:subscription_line_item, 3, subscription: subscription)
subscription.order_cycles.each do |oc|
ex = oc.exchanges.outgoing.find_by_sender_id_and_receiver_id(subscription.shop_id, subscription.shop_id) ||
create(:exchange, :order_cycle => oc, :sender => subscription.shop, :receiver => subscription.shop, :incoming => false, :pickup_time => 'time', :pickup_instructions => 'instructions')
subscription.standing_line_items.each { |sli| ex.variants << sli.variant }
subscription.subscription_line_items.each { |sli| ex.variants << sli.variant }
end
end
@@ -171,7 +171,7 @@ FactoryGirl.define do
end
end
factory :standing_line_item, :class => StandingLineItem do
factory :subscription_line_item, :class => SubscriptionLineItem do
subscription
variant
quantity 1

View File

@@ -204,7 +204,7 @@ feature 'Subscriptions' do
targetted_select2_search product1.name, from: '#add_variant_id', dropdown_css: '.select2-drop'
fill_in 'add_quantity', with: 2
click_link 'Add'
within 'table#standing-line-items tr.item', match: :first do
within 'table#subscription-line-items tr.item', match: :first do
expect(page).to have_selector 'td.description', text: "#{product1.name} - #{variant1.full_name}"
expect(page).to have_selector 'td.price', text: "$13.75"
expect(page).to have_input 'quantity', with: "2"
@@ -214,7 +214,7 @@ feature 'Subscriptions' do
click_button('Next')
# Deleting the existing product
within 'table#standing-line-items tr.item', match: :first do
within 'table#subscription-line-items tr.item', match: :first do
find("a.delete-item").click
end
@@ -228,7 +228,7 @@ feature 'Subscriptions' do
targetted_select2_search product2.name, from: '#add_variant_id', dropdown_css: '.select2-drop'
fill_in 'add_quantity', with: 3
click_link 'Add'
within 'table#standing-line-items tr.item', match: :first do
within 'table#subscription-line-items tr.item', match: :first do
expect(page).to have_selector 'td.description', text: "#{product2.name} - #{variant2.full_name}"
expect(page).to have_selector 'td.price', text: "$7.75"
expect(page).to have_input 'quantity', with: "3"
@@ -241,7 +241,7 @@ feature 'Subscriptions' do
}.to change(Subscription, :count).by(1)
# Prices are shown
within 'table#standing-line-items tr.item', match: :first do
within 'table#subscription-line-items tr.item', match: :first do
expect(page).to have_selector 'td.description', text: "#{product2.name} - #{variant2.full_name}"
expect(page).to have_selector 'td.price', text: "$7.75"
expect(page).to have_input 'quantity', with: "3"
@@ -259,10 +259,10 @@ feature 'Subscriptions' do
expect(subscription.credit_card_id).to eq credit_card2.id
# Standing Line Items are created
expect(subscription.standing_line_items.count).to eq 1
standing_line_item = subscription.standing_line_items.first
expect(standing_line_item.variant).to eq variant2
expect(standing_line_item.quantity).to eq 3
expect(subscription.subscription_line_items.count).to eq 1
subscription_line_item = subscription.subscription_line_items.first
expect(subscription_line_item.variant).to eq variant2
expect(subscription_line_item.quantity).to eq 3
end
context 'editing an existing subscription' do
@@ -288,7 +288,7 @@ feature 'Subscriptions' do
schedule: schedule,
payment_method: payment_method,
shipping_method: shipping_method,
standing_line_items: [create(:standing_line_item, variant: variant1, quantity: 2)],
subscription_line_items: [create(:subscription_line_item, variant: variant1, quantity: 2)],
with_proxy_orders: true)
}
@@ -356,8 +356,8 @@ feature 'Subscriptions' do
# Total should be $7.75
expect(page).to have_selector '#order_form_total', text: "$7.75"
expect(page).to have_selector 'tr.item', count: 1
expect(subscription.reload.standing_line_items.length).to eq 1
expect(subscription.standing_line_items.first.variant).to eq variant2
expect(subscription.reload.subscription_line_items.length).to eq 1
expect(subscription.subscription_line_items.first.variant).to eq variant2
end
context "with initialised order that has been changed" do

View File

@@ -1,8 +1,8 @@
describe "StandingLineItemsCtrl", ->
describe "SubscriptionLineItemsCtrl", ->
scope = null
http = null
subscription = { id: 1 }
standingLineItem = { id: 2, variant_id: 44 }
subscriptionLineItem = { id: 2, variant_id: 44 }
beforeEach ->
module('admin.subscriptions')
@@ -11,8 +11,8 @@ describe "StandingLineItemsCtrl", ->
scope = $rootScope
http = $httpBackend
scope.subscription = subscription
subscription.standing_line_items = [standingLineItem]
$controller 'StandingLineItemsController', {$scope: scope}
subscription.subscription_line_items = [subscriptionLineItem]
$controller 'SubscriptionLineItemsController', {$scope: scope}
describe "match", ->
describe "when newItem.variant_id matches an existing sli", ->
@@ -20,7 +20,7 @@ describe "StandingLineItemsCtrl", ->
scope.newItem.variant_id = 44
it "returns the matching sli ", ->
expect(scope.match()).toEqual standingLineItem
expect(scope.match()).toEqual subscriptionLineItem
describe "when newItem.variant_id dosn't match an existing sli", ->
beforeEach ->
@@ -29,7 +29,7 @@ describe "StandingLineItemsCtrl", ->
it "returns null", ->
expect(scope.match()).toEqual null
describe "addStandingLineItem", ->
describe "addSubscriptionLineItem", ->
beforeEach ->
scope.subscription_form = jasmine.createSpyObj('subscription_form', ['$setDirty'])
subscription.buildItem = jasmine.createSpy('buildItem')
@@ -50,14 +50,14 @@ describe "StandingLineItemsCtrl", ->
spyOn(InfoDialog, "open")
it "shows a message to the user", ->
scope.addStandingLineItem()
scope.addSubscriptionLineItem()
expect(InfoDialog.open).toHaveBeenCalled()
describe "when the matching item is marked for destruction", ->
beforeEach -> match._destroy = true
it "remove the delete flag from the match and merges properties from scope.newItem", ->
scope.addStandingLineItem()
scope.addSubscriptionLineItem()
expect(match._destroy).toBeUndefined()
expect(match.someProperty).toEqual "lalala"
@@ -66,6 +66,6 @@ describe "StandingLineItemsCtrl", ->
spyOn(scope, "match").and.returnValue(null)
it "sets the form to $dirty and called buildItem on scope.subscription", ->
scope.addStandingLineItem()
scope.addSubscriptionLineItem()
expect(scope.subscription_form.$setDirty).toHaveBeenCalled()
expect(subscription.buildItem).toHaveBeenCalledWith(scope.newItem)

View File

@@ -166,7 +166,7 @@ describe ProxyOrder, type: :model do
expect{ proxy_order.initialise_order! }.to change{ Spree::Order.count }.by(1)
expect(proxy_order.reload.order).to be_a Spree::Order
order = proxy_order.order
expect(order.line_items.count).to eq subscription.standing_line_items.count
expect(order.line_items.count).to eq subscription.subscription_line_items.count
expect(order.customer).to eq subscription.customer
expect(order.user).to eq subscription.customer.user
expect(order.distributor).to eq subscription.shop
@@ -180,7 +180,7 @@ describe ProxyOrder, type: :model do
end
context "when a requested quantity is greater than available stock" do
let(:sli) { subscription.standing_line_items.first }
let(:sli) { subscription.subscription_line_items.first }
let(:variant) { sli.variant }
before do

View File

@@ -1,6 +1,6 @@
require 'spec_helper'
describe StandingLineItem, model: true do
describe SubscriptionLineItem, model: true do
describe "validations" do
it "requires a subscription" do
expect(subject).to validate_presence_of :subscription

View File

@@ -10,7 +10,7 @@ describe Subscription, type: :model do
it { expect(subject).to belong_to(:ship_address) }
it { expect(subject).to belong_to(:bill_address) }
it { expect(subject).to belong_to(:credit_card) }
it { expect(subject).to have_many(:standing_line_items) }
it { expect(subject).to have_many(:subscription_line_items) }
it { expect(subject).to have_many(:order_cycles) }
it { expect(subject).to have_many(:proxy_orders) }
it { expect(subject).to have_many(:orders) }

View File

@@ -287,13 +287,13 @@ describe OrderSyncer do
describe "changing the quantity of a line item" do
let(:subscription) { create(:subscription, with_items: true, with_proxy_orders: true) }
let(:order) { subscription.proxy_orders.first.initialise_order! }
let(:sli) { subscription.standing_line_items.first }
let(:sli) { subscription.subscription_line_items.first }
let(:variant) { sli.variant }
before { variant.update_attribute(:count_on_hand, 2) }
context "when quantity is within available stock" do
let(:params) { { standing_line_items_attributes: [{ id: sli.id, quantity: 2}] } }
let(:params) { { subscription_line_items_attributes: [{ id: sli.id, quantity: 2}] } }
let(:syncer) { OrderSyncer.new(subscription) }
it "updates the line_item quantities and totals on all orders" do
@@ -307,7 +307,7 @@ describe OrderSyncer do
end
context "when quantity is greater than available stock" do
let(:params) { { standing_line_items_attributes: [{ id: sli.id, quantity: 3}] } }
let(:params) { { subscription_line_items_attributes: [{ id: sli.id, quantity: 3}] } }
let(:syncer) { OrderSyncer.new(subscription) }
it "updates the line_item quantities and totals on all orders" do
@@ -321,13 +321,13 @@ describe OrderSyncer do
end
context "where the quantity of the item on an initialised order has already been changed" do
let(:params) { { standing_line_items_attributes: [{ id: sli.id, quantity: 3}] } }
let(:params) { { subscription_line_items_attributes: [{ id: sli.id, quantity: 3}] } }
let(:syncer) { OrderSyncer.new(subscription) }
let(:changed_line_item) { order.line_items.find_by_variant_id(sli.variant_id) }
before { variant.update_attribute(:count_on_hand, 3) }
context "when the changed line_item quantity matches the new quantity on the standing line item" do
context "when the changed line_item quantity matches the new quantity on the subscription line item" do
before { changed_line_item.update_attributes(quantity: 3) }
it "does not change the quantity, and doesn't add the order to order_update_issues" do
@@ -340,7 +340,7 @@ describe OrderSyncer do
end
end
context "when the changed line_item quantity doesn't match the new quantity on the standing line item" do
context "when the changed line_item quantity doesn't match the new quantity on the subscription line item" do
before { changed_line_item.update_attributes(quantity: 2) }
it "does not change the quantity, and adds the order to order_update_issues" do
@@ -359,7 +359,7 @@ describe OrderSyncer do
let(:subscription) { create(:subscription, with_items: true, with_proxy_orders: true) }
let(:order) { subscription.proxy_orders.first.initialise_order! }
let(:variant) { create(:variant) }
let(:params) { { standing_line_items_attributes: [{ id: nil, variant_id: variant.id, quantity: 1}] } }
let(:params) { { subscription_line_items_attributes: [{ id: nil, variant_id: variant.id, quantity: 1}] } }
let(:syncer) { OrderSyncer.new(subscription) }
it "adds the line item and updates the total on all orders" do
@@ -375,9 +375,9 @@ describe OrderSyncer do
describe "removing an existing line item" do
let(:subscription) { create(:subscription, with_items: true, with_proxy_orders: true) }
let(:order) { subscription.proxy_orders.first.initialise_order! }
let(:sli) { subscription.standing_line_items.first }
let(:sli) { subscription.subscription_line_items.first }
let(:variant) { sli.variant }
let(:params) { { standing_line_items_attributes: [{ id: sli.id, _destroy: true }] } }
let(:params) { { subscription_line_items_attributes: [{ id: sli.id, _destroy: true }] } }
let(:syncer) { OrderSyncer.new(subscription) }
it "removes the line item and updates totals on all orders" do

View File

@@ -34,7 +34,7 @@ describe SubscriptionForm do
shipping_method_id: shipping_method.id,
begins_at: 4.days.ago,
ends_at: 14.days.from_now,
standing_line_items_attributes: [
subscription_line_items_attributes: [
{variant_id: variant1.id, quantity: 1},
{variant_id: variant2.id, quantity: 2},
{variant_id: variant3.id, quantity: 3}
@@ -89,7 +89,7 @@ describe SubscriptionForm do
end
end
describe "validating price_estimates on standing line items" do
describe "validating price_estimates on subscription line items" do
let(:params) { { } }
let(:form) { SubscriptionForm.new(nil, params) }
@@ -98,21 +98,21 @@ describe SubscriptionForm do
it "does nothing" do
form.send(:validate_price_estimates)
expect(form.params[:standing_line_items_attributes]).to be nil
expect(form.params[:subscription_line_items_attributes]).to be nil
end
end
context "when line_item params are present" do
before do
params[:standing_line_items_attributes] = [{ id: 1, price_estimate: 2.50 }, { id: 2, price_estimate: 3.50 }]
params[:subscription_line_items_attributes] = [{ id: 1, price_estimate: 2.50 }, { id: 2, price_estimate: 3.50 }]
end
context "when no fee calculator is present" do
before { allow(form).to receive(:price_estimate_for) }
it "clears price estimates on all standing line item attributes" do
it "clears price estimates on all subscription line item attributes" do
form.send(:validate_price_estimates)
attrs = form.params[:standing_line_items_attributes]
attrs = form.params[:subscription_line_items_attributes]
expect(attrs.first.keys).to_not include :price_estimate
expect(attrs.last.keys).to_not include :price_estimate
expect(form).to_not have_received(:price_estimate_for)
@@ -126,12 +126,12 @@ describe SubscriptionForm do
before do
allow(form).to receive(:fee_calculator) { fee_calculator }
allow(form).to receive(:price_estimate_for) { 5.30 }
params[:standing_line_items_attributes].first[:variant_id] = variant.id
params[:subscription_line_items_attributes].first[:variant_id] = variant.id
end
it "clears price estimates on standing line item attributes without variant ids" do
it "clears price estimates on subscription line item attributes without variant ids" do
form.send(:validate_price_estimates)
attrs = form.params[:standing_line_items_attributes]
attrs = form.params[:subscription_line_items_attributes]
expect(attrs.first.keys).to include :price_estimate
expect(attrs.last.keys).to_not include :price_estimate
expect(attrs.first[:price_estimate]).to eq 5.30

View File

@@ -43,7 +43,7 @@ describe SubscriptionValidator do
customer_allowed?: true,
schedule_allowed?: true,
credit_card_ok?: true,
standing_line_items_present?: true,
subscription_line_items_present?: true,
requested_variants_available?: true
}
end
@@ -326,7 +326,7 @@ describe SubscriptionValidator do
it "returns true" do
expect(validator.valid?).to be true
expect(validator.errors[:standing_line_items]).to be_empty
expect(validator.errors[:subscription_line_items]).to be_empty
end
end
@@ -381,38 +381,38 @@ describe SubscriptionValidator do
end
end
describe "standing line items" do
describe "subscription line items" do
let(:subscription) { instance_double(Subscription, subscription_stubs) }
before { stub_validations(validator, validation_stubs.except(:standing_line_items_present?)) }
before { expect(subscription).to receive(:standing_line_items).at_least(:once) { standing_line_items } }
before { stub_validations(validator, validation_stubs.except(:subscription_line_items_present?)) }
before { expect(subscription).to receive(:subscription_line_items).at_least(:once) { subscription_line_items } }
context "when no standing line items are present" do
let(:standing_line_items) { [] }
context "when no subscription line items are present" do
let(:subscription_line_items) { [] }
it "adds an error and returns false" do
expect(validator.valid?).to be false
expect(validator.errors[:standing_line_items]).to_not be_empty
expect(validator.errors[:subscription_line_items]).to_not be_empty
end
end
context "when standing line items are present but they are all marked for destruction" do
let(:standing_line_item1) { instance_double(StandingLineItem, marked_for_destruction?: true) }
let(:standing_line_items) { [standing_line_item1] }
context "when subscription line items are present but they are all marked for destruction" do
let(:subscription_line_item1) { instance_double(SubscriptionLineItem, marked_for_destruction?: true) }
let(:subscription_line_items) { [subscription_line_item1] }
it "adds an error and returns false" do
expect(validator.valid?).to be false
expect(validator.errors[:standing_line_items]).to_not be_empty
expect(validator.errors[:subscription_line_items]).to_not be_empty
end
end
context "when standing line items are present and some and not marked for destruction" do
let(:standing_line_item1) { instance_double(StandingLineItem, marked_for_destruction?: true) }
let(:standing_line_item2) { instance_double(StandingLineItem, marked_for_destruction?: false) }
let(:standing_line_items) { [standing_line_item1, standing_line_item2] }
context "when subscription line items are present and some and not marked for destruction" do
let(:subscription_line_item1) { instance_double(SubscriptionLineItem, marked_for_destruction?: true) }
let(:subscription_line_item2) { instance_double(SubscriptionLineItem, marked_for_destruction?: false) }
let(:subscription_line_items) { [subscription_line_item1, subscription_line_item2] }
it "returns true" do
expect(validator.valid?).to be true
expect(validator.errors[:standing_line_items]).to be_empty
expect(validator.errors[:subscription_line_items]).to be_empty
end
end
end
@@ -420,23 +420,23 @@ describe SubscriptionValidator do
describe "variant availability" do
let(:subscription) { instance_double(Subscription, subscription_stubs) }
before { stub_validations(validator, validation_stubs.except(:requested_variants_available?)) }
before { expect(subscription).to receive(:standing_line_items).at_least(:once) { standing_line_items } }
before { expect(subscription).to receive(:subscription_line_items).at_least(:once) { subscription_line_items } }
context "when no standing line items are present" do
let(:standing_line_items) { [] }
context "when no subscription line items are present" do
let(:subscription_line_items) { [] }
it "returns true" do
expect(validator.valid?).to be true
expect(validator.errors[:standing_line_items]).to be_empty
expect(validator.errors[:subscription_line_items]).to be_empty
end
end
context "when standing line items are present" do
context "when subscription line items are present" do
let(:variant1) { instance_double(Spree::Variant, id: 1) }
let(:variant2) { instance_double(Spree::Variant, id: 2) }
let(:standing_line_item1) { instance_double(StandingLineItem, variant: variant1) }
let(:standing_line_item2) { instance_double(StandingLineItem, variant: variant2) }
let(:standing_line_items) { [standing_line_item1] }
let(:subscription_line_item1) { instance_double(SubscriptionLineItem, variant: variant1) }
let(:subscription_line_item2) { instance_double(SubscriptionLineItem, variant: variant2) }
let(:subscription_line_items) { [subscription_line_item1] }
context "but some variants are unavailable" do
let(:product) { instance_double(Spree::Product, name: "some_name") }
@@ -448,7 +448,7 @@ describe SubscriptionValidator do
it "adds an error and returns false" do
expect(validator.valid?).to be false
expect(validator.errors[:standing_line_items]).to_not be_empty
expect(validator.errors[:subscription_line_items]).to_not be_empty
end
end
@@ -457,7 +457,7 @@ describe SubscriptionValidator do
it "returns true" do
expect(validator.valid?).to be true
expect(validator.errors[:standing_line_items]).to be_empty
expect(validator.errors[:subscription_line_items]).to be_empty
end
end
end