From 0b9061df286e5c4e9d94e56f7b060a3c80e70453 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Sat, 11 Aug 2018 22:17:12 +0100 Subject: [PATCH 1/2] removed Cart route, controller, model and specs: dead code --- .../open_food_network/cart_controller.rb | 39 ------ app/models/cart.rb | 28 ---- app/views/open_food_network/cart/show.v1.rabl | 6 - .../line_items/index.v1.rabl | 3 - .../open_food_network/line_items/show.v1.rabl | 4 - .../open_food_network/orders/index.v1.rabl | 3 - .../open_food_network/orders/show.v1.rabl | 7 - config/routes.rb | 6 - spec/controllers/cart_controller_spec.rb | 86 ------------ spec/models/cart_spec.rb | 131 ------------------ 10 files changed, 313 deletions(-) delete mode 100644 app/controllers/open_food_network/cart_controller.rb delete mode 100644 app/models/cart.rb delete mode 100644 app/views/open_food_network/cart/show.v1.rabl delete mode 100644 app/views/open_food_network/line_items/index.v1.rabl delete mode 100644 app/views/open_food_network/line_items/show.v1.rabl delete mode 100644 app/views/open_food_network/orders/index.v1.rabl delete mode 100644 app/views/open_food_network/orders/show.v1.rabl delete mode 100644 spec/controllers/cart_controller_spec.rb delete mode 100644 spec/models/cart_spec.rb diff --git a/app/controllers/open_food_network/cart_controller.rb b/app/controllers/open_food_network/cart_controller.rb deleted file mode 100644 index a4bc1c7b76b..00000000000 --- a/app/controllers/open_food_network/cart_controller.rb +++ /dev/null @@ -1,39 +0,0 @@ -module OpenFoodNetwork - class CartController < ApplicationController - respond_to :json - - # before_filter :authorize_read!, :except => [:index, :search, :create] - - def new - @cart = Cart.new(current_api_user) - if @cart.save - respond_with(@cart, :status => 201) - else - invalid_resource!(@cart) - end - end - - def show - @cart = Cart.find(params[:id]) - respond_with(@cart) - end - - def add_variant - @cart = Cart.find(params[:cart_id]) - distributor = Enterprise.find_by_permalink(params[:distributor_id]) - order_cycle = OrderCycle.find(params[:order_cycle_id]) if params[:order_cycle_id] - - if @cart.add_variant params[:variant_id], params[:quantity], distributor, order_cycle, current_currency - respond_with(@cart) - else - respond_with(@cart.populate_errors) - end - end - - private - - def current_currency - Spree::Config[:currency] - end - end -end diff --git a/app/models/cart.rb b/app/models/cart.rb deleted file mode 100644 index ecd1cfb94cf..00000000000 --- a/app/models/cart.rb +++ /dev/null @@ -1,28 +0,0 @@ -class Cart < ActiveRecord::Base - has_many :orders, :class_name => 'Spree::Order' - belongs_to :user, :class_name => Spree.user_class - - def add_variant variant_id, quantity, distributor, order_cycle, currency - order = create_or_find_order_for_distributor distributor, order_cycle, currency - - @populator = Spree::OrderPopulator.new(order, currency) - @populator.populate({ :variants => { variant_id => quantity } }) - end - - def create_or_find_order_for_distributor distributor, order_cycle, currency - order_for_distributor = orders.find { |order| order.distributor == distributor && order.order_cycle == order_cycle } - unless order_for_distributor - order_for_distributor = Spree::Order.create(:currency => currency, :distributor => distributor) - order_for_distributor.distributor = distributor - order_for_distributor.order_cycle = order_cycle - order_for_distributor.save! - orders << order_for_distributor - end - - order_for_distributor - end - - def populate_errors - @populator.errors - end -end diff --git a/app/views/open_food_network/cart/show.v1.rabl b/app/views/open_food_network/cart/show.v1.rabl deleted file mode 100644 index 056b439cd5a..00000000000 --- a/app/views/open_food_network/cart/show.v1.rabl +++ /dev/null @@ -1,6 +0,0 @@ -object @cart -attributes :id - -node( :orders ) do |p| - partial '/open_food_network/orders/index', object: p.orders -end diff --git a/app/views/open_food_network/line_items/index.v1.rabl b/app/views/open_food_network/line_items/index.v1.rabl deleted file mode 100644 index 3bc817063f5..00000000000 --- a/app/views/open_food_network/line_items/index.v1.rabl +++ /dev/null @@ -1,3 +0,0 @@ -collection @line_items - -extends "open_food_network/line_items/show" diff --git a/app/views/open_food_network/line_items/show.v1.rabl b/app/views/open_food_network/line_items/show.v1.rabl deleted file mode 100644 index cf790085fe7..00000000000 --- a/app/views/open_food_network/line_items/show.v1.rabl +++ /dev/null @@ -1,4 +0,0 @@ -object @line_item -attributes :id, :quantity - -node(:name) { |p| p.variant.name } diff --git a/app/views/open_food_network/orders/index.v1.rabl b/app/views/open_food_network/orders/index.v1.rabl deleted file mode 100644 index 157b3c61323..00000000000 --- a/app/views/open_food_network/orders/index.v1.rabl +++ /dev/null @@ -1,3 +0,0 @@ -collection @orders - -extends "open_food_network/orders/show" diff --git a/app/views/open_food_network/orders/show.v1.rabl b/app/views/open_food_network/orders/show.v1.rabl deleted file mode 100644 index 6b49a458316..00000000000 --- a/app/views/open_food_network/orders/show.v1.rabl +++ /dev/null @@ -1,7 +0,0 @@ -object @order -attributes :id - -node( :distributor ) { |p| p.distributor.blank? ? "" : p.distributor.name } -node( :line_items ) do |p| - partial '/open_food_network/line_items/index', object: p.line_items -end diff --git a/config/routes.rb b/config/routes.rb index 42488bf3367..e6c446b6290 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -100,12 +100,6 @@ post '/product_images/:product_id', to: 'product_images#update_product_image' end - namespace :open_food_network do - resources :cart do - post :add_variant - end - end - get 'sitemap.xml', to: 'sitemap#index', defaults: { format: 'xml' } # Mount Spree's routes diff --git a/spec/controllers/cart_controller_spec.rb b/spec/controllers/cart_controller_spec.rb deleted file mode 100644 index 5d939b62542..00000000000 --- a/spec/controllers/cart_controller_spec.rb +++ /dev/null @@ -1,86 +0,0 @@ -require 'spec_helper' -require 'spree/api/testing_support/helpers' - -module OpenFoodNetwork - describe CartController, type: :controller do - render_views - - let(:user) { FactoryBot.create(:user) } - let(:product1) { FactoryBot.create(:product) } - let(:cart) { Cart.create(user: user) } - let(:distributor) { FactoryBot.create(:distributor_enterprise) } - - before do - end - - context "as a normal user" do - - context 'with an existing cart' do - - it "retrieves an empty cart" do - get :show, {id: cart, :format => :json } - json_response = JSON.parse(response.body) - - json_response['id'].should == cart.id - json_response['orders'].size.should == 0 - end - - context 'with an empty order' do - let(:order) { FactoryBot.create(:order, distributor: distributor) } - - before(:each) do - cart.orders << order - cart.save! - end - - it "retrieves a cart with a single order and line item" do - get :show, {id: cart, :format => :json } - json_response = JSON.parse(response.body) - - json_response['orders'].size.should == 1 - json_response['orders'].first['distributor'].should == order.distributor.name - json_response['orders'].first['line_items'].size.should == 0 - end - end - - context 'an order with line items' do - let(:product) { FactoryBot.create(:product, distributors: [ distributor ]) } - let(:order) { FactoryBot.create(:order, { distributor: distributor } ) } - let(:line_item) { FactoryBot.create(:line_item, { variant: product.master }) } - - before(:each) do - order.line_items << line_item - order.save - cart.orders << order - cart.save! - end - - it "retrieves a cart with a single order and line item" do - get :show, {id: cart, :format => :json } - json_response = JSON.parse(response.body) - - json_response['orders'].size.should == 1 - json_response['orders'].first['distributor'].should == order.distributor.name - json_response['orders'].first['line_items'].first["name"].should == product.name - json_response['orders'].first['line_items'].first["quantity"].should == line_item.quantity - end - end - - context 'adding a variant' do - - it 'should add variant to new order and return the order' do - product1.distributors << distributor - product1.save - variant = product1.variants.first - - put :add_variant, { cart_id: cart, variant_id: variant.id, quantity: (variant.on_hand-1), distributor_id: distributor, order_cycle_id: nil, max_quantity: nil } - - cart.orders.size.should == 1 - cart.orders.first.line_items.size.should == 1 - cart.orders.first.line_items.first.product.should == product1 - end - end - end - end - end -end diff --git a/spec/models/cart_spec.rb b/spec/models/cart_spec.rb deleted file mode 100644 index 08bc4ff3a89..00000000000 --- a/spec/models/cart_spec.rb +++ /dev/null @@ -1,131 +0,0 @@ -require 'spec_helper' - -# TODO this seems to be redundant -describe Cart do - - describe "associations" do - it { should have_many(:orders) } - end - - describe 'when adding a product' do - - let(:cart) { Cart.create(user: user) } - let(:distributor) { FactoryBot.create(:distributor_enterprise) } - let(:other_distributor) { FactoryBot.create(:distributor_enterprise) } - let(:currency) { "AUD" } - - let(:product) { FactoryBot.create(:product, :distributors => [distributor]) } - - let(:product_with_order_cycle) { create(:product) } - let(:order_cycle) { create(:simple_order_cycle, distributors: [distributor, other_distributor], variants: [product_with_order_cycle.master]) } - - describe 'to an empty cart' do - it 'should create an order for the product being added, and associate the product to the selected distribution' do - subject.add_variant product.master.id, 3, distributor, nil, currency - - subject.orders.size.should == 1 - order = subject.orders.first.reload - order.currency.should == currency - order.distributor.should == product.distributors.first - order.order_cycle.should be_nil - order.line_items.first.product.should == product - end - - it 'should create an order for the product being added, and associate the order with an order cycle and distributor' do - subject.add_variant product_with_order_cycle.master.id, 3, distributor, order_cycle, currency - - subject.orders.size.should == 1 - order = subject.orders.first.reload - order.currency.should == currency - order.distributor.should == distributor - order.order_cycle.should == order_cycle - order.line_items.first.product.should == product_with_order_cycle - end - end - - describe 'to a cart with an order for a distributor' do - let(:product_from_other_distributor) { FactoryBot.create(:product, :distributors => [other_distributor]) } - let(:order) { FactoryBot.create(:order, :distributor => distributor) } - - before do - FactoryBot.create(:line_item, :order => order, :product => product) - order.reload - subject.orders << order - subject.save! - end - - it 'should create a new order and add a line item to it when product added for different distributor' do - subject.add_variant product_from_other_distributor.master.id, 3, other_distributor, nil, currency - - subject.reload - subject.orders.size.should == 2 - new_order_for_other_distributor = subject.orders.find { |order| order.distributor == other_distributor } - new_order_for_other_distributor.order_cycle.should be_nil - order.line_items.size.should == 1 - new_order_for_other_distributor.line_items.size.should == 1 - new_order_for_other_distributor.line_items.first.product.should == product_from_other_distributor - end - - it 'should group line item in existing order, when product added for the same distributor' do - subject.add_variant product.master.id, 3, distributor, nil, currency - - subject.orders.size.should == 1 - order = subject.orders.first.reload - order.line_items.size.should == 2 - order.line_items.first.product.should == product - end - - it 'should create a new order for product in an order cycle' do - subject.add_variant product_with_order_cycle.master.id, 3, distributor, order_cycle, currency - - subject.orders.size.should == 2 - new_order_for_distributor = subject.orders.find { |order| order.order_cycle == order_cycle } - new_order_for_distributor.reload - new_order_for_distributor.line_items.first.product.should == product_with_order_cycle - end - end - - describe 'existing order for distributor and order cycle' do - let(:order) { FactoryBot.create(:order, :distributor => distributor, :order_cycle => order_cycle) } - - before do - subject.orders << order - subject.save! - end - - it 'should group line items in existing order when added for the same distributor and order cycle' do - subject.add_variant product_with_order_cycle.master.id, 3, distributor, order_cycle, currency - - subject.orders.size.should == 1 - order = subject.orders.first.reload - order.line_items.size.should == 1 - order.line_items.find{ |line_item| line_item.product == product_with_order_cycle }.should_not be_nil - end - - it 'should create line item in new order when product added is for a different order cycle' do - order_cycle2 = create(:simple_order_cycle, distributors: [distributor], variants: [product_with_order_cycle.master]) - - subject.add_variant product_with_order_cycle.master.id, 3, distributor, order_cycle2, currency - - subject.orders.size.should == 2 - new_order_for_second_order_cycle = subject.orders.find { |order| order.order_cycle == order_cycle2 } - new_order_for_second_order_cycle.reload - new_order_for_second_order_cycle.line_items.size.should == 1 - new_order_for_second_order_cycle.line_items.first.product.should == product_with_order_cycle - new_order_for_second_order_cycle.distributor.should == distributor - new_order_for_second_order_cycle.order_cycle.should == order_cycle2 - end - - it 'should create line_items in new order when added with different distributor, but same order_cycle' do - subject.add_variant product_with_order_cycle.master.id, 3, other_distributor, order_cycle, currency - - subject.orders.size.should == 2 - new_order_for_second_order_cycle = subject.orders.find { |order| order.distributor == other_distributor } - new_order_for_second_order_cycle.reload - new_order_for_second_order_cycle.line_items.size.should == 1 - new_order_for_second_order_cycle.line_items.find{ |line_item| line_item.product == product_with_order_cycle }.should_not be_nil - new_order_for_second_order_cycle.order_cycle.should == order_cycle - end - end - end -end From 91e57cb893c454fa8f6c9bd18c87cdc8cd418a40 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Wed, 15 Aug 2018 23:29:28 +0100 Subject: [PATCH 2/2] Removed Cart table, its dependency on spree orders table and removed some more dead code related to this --- .../spree/orders_controller_decorator.rb | 14 -------------- app/models/spree/order_decorator.rb | 1 - app/models/spree/user_decorator.rb | 1 - db/migrate/20180812214434_drop_carts.rb | 7 +++++++ db/schema.rb | 12 +----------- 5 files changed, 8 insertions(+), 27 deletions(-) create mode 100644 db/migrate/20180812214434_drop_carts.rb diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index 47425bcbf2c..e435b645227 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -202,20 +202,6 @@ def populate_variant_attributes end end - def populate_cart hash - if spree_current_user - unless spree_current_user.cart - spree_current_user.build_cart - cart = Cart.create(user: spree_current_user) - spree_current_user.cart = cart - spree_current_user.save - end - distributor = Enterprise.find(hash[:distributor_id]) - order_cycle = OrderCycle.find(hash[:order_cycle_id]) if hash[:order_cycle_id] - spree_current_user.cart.add_variant hash[:variants].keys.first, hash[:variants].values.first, distributor, order_cycle, current_currency - end - end - # Rails to_json encodes Float::INFINITY as Infinity, which is not valid JSON # Return it as a large integer (max 32 bit signed int) def wrap_json_infinity(n) diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index c6def73d680..bc8142694bc 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -10,7 +10,6 @@ Spree::Order.class_eval do belongs_to :order_cycle belongs_to :distributor, class_name: 'Enterprise' - belongs_to :cart belongs_to :customer has_one :proxy_order has_one :subscription, through: :proxy_order diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index 8ae76a11b44..cf375e7e65d 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -9,7 +9,6 @@ has_many :owned_groups, class_name: 'EnterpriseGroup', foreign_key: :owner_id, inverse_of: :owner has_many :account_invoices has_many :billable_periods, foreign_key: :owner_id, inverse_of: :owner - has_one :cart has_many :customers has_many :credit_cards diff --git a/db/migrate/20180812214434_drop_carts.rb b/db/migrate/20180812214434_drop_carts.rb new file mode 100644 index 00000000000..f3dfb36ee77 --- /dev/null +++ b/db/migrate/20180812214434_drop_carts.rb @@ -0,0 +1,7 @@ +class DropCarts < ActiveRecord::Migration + def change + remove_foreign_key :spree_orders, column: :cart_id + remove_column :spree_orders, :cart_id + drop_table :carts + end +end diff --git a/db/schema.rb b/db/schema.rb index d3121a1f40d..13c0c125b85 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20180510083800) do +ActiveRecord::Schema.define(:version => 20180812214434) do create_table "account_invoices", :force => true do |t| t.integer "user_id", :null => false @@ -53,12 +53,6 @@ add_index "billable_periods", ["account_invoice_id"], :name => "index_billable_periods_on_account_invoice_id" - create_table "carts", :force => true do |t| - t.integer "user_id" - end - - add_index "carts", ["user_id"], :name => "index_carts_on_user_id" - create_table "column_preferences", :force => true do |t| t.integer "user_id", :null => false t.string "action_name", :null => false @@ -621,7 +615,6 @@ t.integer "order_cycle_id" t.string "currency" t.string "last_ip_address" - t.integer "cart_id" t.integer "customer_id" end @@ -1203,8 +1196,6 @@ add_foreign_key "billable_periods", "enterprises", name: "bill_items_enterprise_id_fk" add_foreign_key "billable_periods", "spree_users", name: "bill_items_owner_id_fk", column: "owner_id" - add_foreign_key "carts", "spree_users", name: "carts_user_id_fk", column: "user_id" - add_foreign_key "coordinator_fees", "enterprise_fees", name: "coordinator_fees_enterprise_fee_id_fk" add_foreign_key "coordinator_fees", "order_cycles", name: "coordinator_fees_order_cycle_id_fk" @@ -1285,7 +1276,6 @@ add_foreign_key "spree_option_values_variants", "spree_option_values", name: "spree_option_values_variants_option_value_id_fk", column: "option_value_id" add_foreign_key "spree_option_values_variants", "spree_variants", name: "spree_option_values_variants_variant_id_fk", column: "variant_id" - add_foreign_key "spree_orders", "carts", name: "spree_orders_cart_id_fk" add_foreign_key "spree_orders", "customers", name: "spree_orders_customer_id_fk" add_foreign_key "spree_orders", "enterprises", name: "spree_orders_distributor_id_fk", column: "distributor_id" add_foreign_key "spree_orders", "order_cycles", name: "spree_orders_order_cycle_id_fk"