From b701ca43f4564897ad8f13375a4a71e47f2ea2b2 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Fri, 14 Oct 2016 11:54:50 +1100 Subject: [PATCH] WIP: adding shiping and billing address to StandingOrder model --- app/models/standing_order.rb | 10 ++++++++-- ...6_add_ship_and_bill_address_to_standing_orders.rb | 12 ++++++++++++ db/schema.rb | 6 ++++++ spec/factories.rb | 2 ++ spec/models/standing_order_spec.rb | 8 ++++++++ 5 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20161014000216_add_ship_and_bill_address_to_standing_orders.rb diff --git a/app/models/standing_order.rb b/app/models/standing_order.rb index 5d403f4471..0a02aaddb3 100644 --- a/app/models/standing_order.rb +++ b/app/models/standing_order.rb @@ -4,11 +4,17 @@ class StandingOrder < ActiveRecord::Base belongs_to :schedule belongs_to :shipping_method, class_name: 'Spree::ShippingMethod' belongs_to :payment_method, class_name: 'Spree::PaymentMethod' + 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 has_many :standing_line_items, inverse_of: :standing_order - accepts_nested_attributes_for :standing_line_items + alias_attribute :billing_address, :bill_address + alias_attribute :shipping_address, :ship_address - validates_presence_of :shop, :customer, :schedule, :payment_method, :shipping_method, :begins_at + accepts_nested_attributes_for :standing_line_items, :bill_address, :ship_address + + validates_presence_of :shop, :customer, :schedule, :payment_method, :shipping_method + validates_presence_of :billing_address, :shipping_address, :begins_at validate :ends_at_after_begins_at validate :standing_line_items_available validate :check_associations diff --git a/db/migrate/20161014000216_add_ship_and_bill_address_to_standing_orders.rb b/db/migrate/20161014000216_add_ship_and_bill_address_to_standing_orders.rb new file mode 100644 index 0000000000..259227be5f --- /dev/null +++ b/db/migrate/20161014000216_add_ship_and_bill_address_to_standing_orders.rb @@ -0,0 +1,12 @@ +class AddShipAndBillAddressToStandingOrders < ActiveRecord::Migration + def change + add_column :standing_orders, :bill_address_id, :integer, null: false + add_column :standing_orders, :ship_address_id, :integer, null: false + + add_index :standing_orders, :bill_address_id + add_index :standing_orders, :ship_address_id + + add_foreign_key :standing_orders, :spree_addresses, column: :bill_address_id + add_foreign_key :standing_orders, :spree_addresses, column: :ship_address_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 16db24b810..08db735f02 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1084,11 +1084,15 @@ ActiveRecord::Schema.define(:version => 20170921065259) do t.datetime "ends_at" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false + t.integer "bill_address_id", :null => false + t.integer "ship_address_id", :null => false end + add_index "standing_orders", ["bill_address_id"], :name => "index_standing_orders_on_bill_address_id" add_index "standing_orders", ["customer_id"], :name => "index_standing_orders_on_customer_id" add_index "standing_orders", ["payment_method_id"], :name => "index_standing_orders_on_payment_method_id" add_index "standing_orders", ["schedule_id"], :name => "index_standing_orders_on_schedule_id" + add_index "standing_orders", ["ship_address_id"], :name => "index_standing_orders_on_ship_address_id" add_index "standing_orders", ["shipping_method_id"], :name => "index_standing_orders_on_shipping_method_id" add_index "standing_orders", ["shop_id"], :name => "index_standing_orders_on_shop_id" @@ -1326,6 +1330,8 @@ ActiveRecord::Schema.define(:version => 20170921065259) do add_foreign_key "standing_orders", "customers", name: "oc_standing_orders_customer_id_fk" add_foreign_key "standing_orders", "enterprises", name: "oc_standing_orders_shop_id_fk", column: "shop_id" add_foreign_key "standing_orders", "schedules", name: "oc_standing_orders_schedule_id_fk" + add_foreign_key "standing_orders", "spree_addresses", name: "standing_orders_bill_address_id_fk", column: "bill_address_id" + add_foreign_key "standing_orders", "spree_addresses", name: "standing_orders_ship_address_id_fk", column: "ship_address_id" add_foreign_key "standing_orders", "spree_payment_methods", name: "oc_standing_orders_payment_method_id_fk", column: "payment_method_id" add_foreign_key "standing_orders", "spree_shipping_methods", name: "oc_standing_orders_shipping_method_id_fk", column: "shipping_method_id" diff --git a/spec/factories.rb b/spec/factories.rb index f38fa6655d..4a710bf01d 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -142,6 +142,8 @@ FactoryGirl.define do shop { FactoryGirl.create :enterprise } schedule { FactoryGirl.create(:schedule, order_cycles: [create(:simple_order_cycle, coordinator: shop)]) } customer { create(:customer, enterprise: shop) } + bill_address { create(:address) } + ship_address { create(:address) } payment_method { create(:payment_method, distributors: [shop]) } shipping_method { create(:shipping_method, distributors: [shop]) } begins_at { 1.month.ago } diff --git a/spec/models/standing_order_spec.rb b/spec/models/standing_order_spec.rb index a2ff33f04d..db2b69f17b 100644 --- a/spec/models/standing_order_spec.rb +++ b/spec/models/standing_order_spec.rb @@ -73,6 +73,14 @@ describe StandingOrder, type: :model do end end + it "requires a billing_address" do + expect(subject).to validate_presence_of :billing_address + end + + it "requires a shipping_address" do + expect(subject).to validate_presence_of :shipping_address + end + it "requires a begins_at date" do expect(subject).to validate_presence_of :begins_at end