WIP: adding shiping and billing address to StandingOrder model

This commit is contained in:
Rob Harrington
2016-10-14 11:54:50 +11:00
parent 88563431b8
commit b701ca43f4
5 changed files with 36 additions and 2 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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"

View File

@@ -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 }

View File

@@ -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