From e4de7e262dadc92bc08c66bac66d78e8c424170d Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Wed, 24 Aug 2016 11:56:41 +1000 Subject: [PATCH] SO: Adding StandingLineItem model --- app/models/standing_line_item.rb | 8 ++++++++ ...20160824013751_create_standing_line_items.rb | 16 ++++++++++++++++ db/schema.rb | 14 ++++++++++++++ spec/models/standing_line_item_spec.rb | 17 +++++++++++++++++ spec/models/standing_order_spec.rb | 14 ++++++-------- 5 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 app/models/standing_line_item.rb create mode 100644 db/migrate/20160824013751_create_standing_line_items.rb create mode 100644 spec/models/standing_line_item_spec.rb diff --git a/app/models/standing_line_item.rb b/app/models/standing_line_item.rb new file mode 100644 index 0000000000..dd937d9348 --- /dev/null +++ b/app/models/standing_line_item.rb @@ -0,0 +1,8 @@ +class StandingLineItem < ActiveRecord::Base + belongs_to :standing_order + belongs_to :variant, class_name: 'Spree::Variant' + + validates :standing_order, presence: true + validates :variant, presence: true + validates :quantity, { presence: true, numericality: { only_integer: true } } +end diff --git a/db/migrate/20160824013751_create_standing_line_items.rb b/db/migrate/20160824013751_create_standing_line_items.rb new file mode 100644 index 0000000000..90a0d46af3 --- /dev/null +++ b/db/migrate/20160824013751_create_standing_line_items.rb @@ -0,0 +1,16 @@ +class CreateStandingLineItems < ActiveRecord::Migration + def change + create_table :standing_line_items do |t| + t.references :standing_order, null: false + t.references :variant, null: false + t.integer :quantity, null: false + t.timestamps + end + + add_index :standing_line_items, :standing_order_id + add_index :standing_line_items, :variant_id + + add_foreign_key :standing_line_items, :standing_orders, name: 'oc_standing_line_items_standing_order_id_fk' + add_foreign_key :standing_line_items, :spree_variants, name: 'oc_standing_line_items_variant_id_fk', column: :variant_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 98847e78ce..16db24b810 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1063,6 +1063,17 @@ ActiveRecord::Schema.define(:version => 20170921065259) do t.integer "zone_members_count", :default => 0 end + create_table "standing_line_items", :force => true do |t| + t.integer "standing_order_id", :null => false + t.integer "variant_id", :null => false + t.integer "quantity", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + + add_index "standing_line_items", ["standing_order_id"], :name => "index_standing_line_items_on_standing_order_id" + add_index "standing_line_items", ["variant_id"], :name => "index_standing_line_items_on_variant_id" + create_table "standing_orders", :force => true do |t| t.integer "shop_id", :null => false t.integer "customer_id", :null => false @@ -1309,6 +1320,9 @@ ActiveRecord::Schema.define(:version => 20170921065259) do add_foreign_key "spree_zone_members", "spree_zones", name: "spree_zone_members_zone_id_fk", column: "zone_id" + add_foreign_key "standing_line_items", "spree_variants", name: "oc_standing_line_items_variant_id_fk", column: "variant_id" + add_foreign_key "standing_line_items", "standing_orders", name: "oc_standing_line_items_standing_order_id_fk" + 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" diff --git a/spec/models/standing_line_item_spec.rb b/spec/models/standing_line_item_spec.rb new file mode 100644 index 0000000000..767a7c6a5f --- /dev/null +++ b/spec/models/standing_line_item_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe StandingLineItem, model: true do + describe "validations" do + it "requires a standing_order" do + expect(subject).to validate_presence_of :standing_order + end + + it "requires a variant" do + expect(subject).to validate_presence_of :variant + end + + it "requires a integer for quantity" do + expect(subject).to validate_numericality_of(:quantity).only_integer + end + end +end diff --git a/spec/models/standing_order_spec.rb b/spec/models/standing_order_spec.rb index 2f77044c21..62cf365b9a 100644 --- a/spec/models/standing_order_spec.rb +++ b/spec/models/standing_order_spec.rb @@ -1,31 +1,29 @@ require 'spec_helper' describe StandingOrder, type: :model do - let!(:standing_order) { create(:standing_order) } - describe "validations" do it "requires a shop" do - expect(standing_order).to validate_presence_of :shop + expect(subject).to validate_presence_of :shop end it "requires a customer" do - expect(standing_order).to validate_presence_of :customer + expect(subject).to validate_presence_of :customer end it "requires a schedule" do - expect(standing_order).to validate_presence_of :schedule + expect(subject).to validate_presence_of :schedule end it "requires a payment_method" do - expect(standing_order).to validate_presence_of :payment_method + expect(subject).to validate_presence_of :payment_method end it "requires a shipping_method" do - expect(standing_order).to validate_presence_of :shipping_method + expect(subject).to validate_presence_of :shipping_method end it "requires a begins_at date" do - expect(standing_order).to validate_presence_of :begins_at + expect(subject).to validate_presence_of :begins_at end end end