mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-17 00:07:24 +00:00
Add initial VariantOverride model with price lookup
This commit is contained in:
8
app/models/variant_override.rb
Normal file
8
app/models/variant_override.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
class VariantOverride < ActiveRecord::Base
|
||||
belongs_to :variant, class_name: 'Spree::Variant'
|
||||
belongs_to :hub, class_name: 'Enterprise'
|
||||
|
||||
def self.price_for(variant, hub)
|
||||
VariantOverride.where(variant_id: variant, hub_id: hub).first.andand.price
|
||||
end
|
||||
end
|
||||
15
db/migrate/20141113053004_create_variant_overrides.rb
Normal file
15
db/migrate/20141113053004_create_variant_overrides.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
class CreateVariantOverrides < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :variant_overrides do |t|
|
||||
t.references :variant
|
||||
t.references :hub
|
||||
t.decimal :price, precision: 8, scale: 2
|
||||
t.integer :count_on_hand
|
||||
end
|
||||
|
||||
add_foreign_key :variant_overrides, :spree_variants, column: :variant_id
|
||||
add_foreign_key :variant_overrides, :enterprises, column: :hub_id
|
||||
|
||||
add_index :variant_overrides, [:variant_id, :hub_id]
|
||||
end
|
||||
end
|
||||
14
db/schema.rb
14
db/schema.rb
@@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended to check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(:version => 20141023050324) do
|
||||
ActiveRecord::Schema.define(:version => 20141113053004) do
|
||||
|
||||
create_table "adjustment_metadata", :force => true do |t|
|
||||
t.integer "adjustment_id"
|
||||
@@ -1033,6 +1033,15 @@ ActiveRecord::Schema.define(:version => 20141023050324) do
|
||||
t.integer "state_id"
|
||||
end
|
||||
|
||||
create_table "variant_overrides", :force => true do |t|
|
||||
t.integer "variant_id"
|
||||
t.integer "hub_id"
|
||||
t.decimal "price", :precision => 8, :scale => 2
|
||||
t.integer "count_on_hand"
|
||||
end
|
||||
|
||||
add_index "variant_overrides", ["variant_id", "hub_id"], :name => "index_variant_overrides_on_variant_id_and_hub_id"
|
||||
|
||||
add_foreign_key "adjustment_metadata", "enterprises", name: "adjustment_metadata_enterprise_id_fk"
|
||||
add_foreign_key "adjustment_metadata", "spree_adjustments", name: "adjustment_metadata_adjustment_id_fk", column: "adjustment_id"
|
||||
|
||||
@@ -1190,4 +1199,7 @@ ActiveRecord::Schema.define(:version => 20141023050324) do
|
||||
|
||||
add_foreign_key "suburbs", "spree_states", name: "suburbs_state_id_fk", column: "state_id"
|
||||
|
||||
add_foreign_key "variant_overrides", "enterprises", name: "variant_overrides_hub_id_fk", column: "hub_id"
|
||||
add_foreign_key "variant_overrides", "spree_variants", name: "variant_overrides_variant_id_fk", column: "variant_id"
|
||||
|
||||
end
|
||||
|
||||
17
spec/models/variant_override_spec.rb
Normal file
17
spec/models/variant_override_spec.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe VariantOverride do
|
||||
describe "looking up prices" do
|
||||
let(:variant) { create(:variant) }
|
||||
let(:hub) { create(:distributor_enterprise) }
|
||||
|
||||
it "returns the numeric price when present" do
|
||||
VariantOverride.create!(variant: variant, hub: hub, price: 12.34)
|
||||
VariantOverride.price_for(variant, hub).should == 12.34
|
||||
end
|
||||
|
||||
it "returns nil otherwise" do
|
||||
VariantOverride.price_for(variant, hub).should be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user