mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Convert distributors_products join table into ProductDistribution explicit join model
This commit is contained in:
@@ -2,9 +2,11 @@ module Spree
|
||||
class Distributor < ActiveRecord::Base
|
||||
self.table_name = 'distributors'
|
||||
belongs_to :pickup_address, :foreign_key => 'pickup_address_id', :class_name => 'Spree::Address'
|
||||
has_and_belongs_to_many :products
|
||||
has_many :orders
|
||||
|
||||
has_many :product_distributions
|
||||
has_many :products, :through => :product_distributions
|
||||
|
||||
accepts_nested_attributes_for :pickup_address
|
||||
|
||||
validates_presence_of :name, :pickup_address
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
Spree::Product.class_eval do
|
||||
belongs_to :supplier
|
||||
has_and_belongs_to_many :distributors
|
||||
|
||||
has_many :product_distributions
|
||||
has_many :distributors, :through => :product_distributions
|
||||
|
||||
attr_accessible :supplier_id, :distributor_ids
|
||||
|
||||
validates_presence_of :supplier, :distributors
|
||||
validates_presence_of :supplier
|
||||
|
||||
scope :in_supplier, lambda { |supplier| where(:supplier_id => supplier) }
|
||||
scope :in_distributor, lambda { |distributor| joins(:distributors).where('distributors.id = ?', (distributor.respond_to?(:id) ? distributor.id : distributor.to_i)) }
|
||||
scope :in_distributor, lambda { |distributor| joins(:product_distributions).where('product_distributions.distributor_id = ?', (distributor.respond_to?(:id) ? distributor.id : distributor.to_i)) }
|
||||
end
|
||||
|
||||
9
app/models/spree/product_distribution.rb
Normal file
9
app/models/spree/product_distribution.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
module Spree
|
||||
class ProductDistribution < ActiveRecord::Base
|
||||
self.table_name = 'product_distributions'
|
||||
|
||||
belongs_to :product
|
||||
belongs_to :distributor
|
||||
belongs_to :shipping_method
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,24 @@
|
||||
class RenameDistributorsProductsToProductDistributions < ActiveRecord::Migration
|
||||
def up
|
||||
# Convert m2m join table into explicit join model, and add a shipping method relation and timestamps
|
||||
rename_table :distributors_products, :product_distributions
|
||||
add_column :product_distributions, :id, :primary_key
|
||||
change_table :product_distributions do |t|
|
||||
t.references :shipping_method
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
# Set default shipping method on all product distributions
|
||||
sm = Spree::ShippingMethod.first
|
||||
Spree::ProductDistribution.update_all(:shipping_method_id => sm.id) if sm
|
||||
end
|
||||
|
||||
def down
|
||||
change_table :product_distributions do |t|
|
||||
t.remove :id
|
||||
t.remove :shipping_method_id
|
||||
t.remove :created_at, :updated_at
|
||||
end
|
||||
rename_table :product_distributions, :distributors_products
|
||||
end
|
||||
end
|
||||
11
db/schema.rb
11
db/schema.rb
@@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended to check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(:version => 20120626013846) do
|
||||
ActiveRecord::Schema.define(:version => 20120626233350) do
|
||||
|
||||
create_table "distributors", :force => true do |t|
|
||||
t.string "name"
|
||||
@@ -28,9 +28,12 @@ ActiveRecord::Schema.define(:version => 20120626013846) do
|
||||
t.integer "pickup_address_id"
|
||||
end
|
||||
|
||||
create_table "distributors_products", :id => false, :force => true do |t|
|
||||
t.integer "product_id"
|
||||
t.integer "distributor_id"
|
||||
create_table "product_distributions", :force => true do |t|
|
||||
t.integer "product_id"
|
||||
t.integer "distributor_id"
|
||||
t.integer "shipping_method_id"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
end
|
||||
|
||||
create_table "spree_activators", :force => true do |t|
|
||||
|
||||
@@ -19,12 +19,21 @@ FactoryGirl.define do
|
||||
pickup_times "Whenever you're free"
|
||||
pickup_address { Spree::Address.first || FactoryGirl.create(:address) }
|
||||
end
|
||||
|
||||
factory :product_distribution, :class => Spree::ProductDistribution do
|
||||
product { |pd| Spree::Product.first || FactoryGirl.create(:product) }
|
||||
distributor { |pd| Spree::Distributor.first || FactoryGirl.create(:distributor) }
|
||||
shipping_method { |pd| Spree::ShippingMethod.first || FactoryGirl.create(:shipping_method) }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
FactoryGirl.modify do
|
||||
factory :simple_product do
|
||||
supplier { Spree::Supplier.first || FactoryGirl.create(:supplier) }
|
||||
distributors { [Spree::Distributor.first || FactoryGirl.create(:distributor)] }
|
||||
|
||||
# before(:create) do |product, evaluator|
|
||||
# product.product_distributions = [FactoryGirl.create(:product_distribution, :product => product)]
|
||||
# end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,7 +5,7 @@ module Spree
|
||||
|
||||
describe "associations" do
|
||||
it { should belong_to(:pickup_address) }
|
||||
it { should have_and_belong_to_many(:products) }
|
||||
it { should have_many(:product_distributions) }
|
||||
it { should have_many(:orders) }
|
||||
end
|
||||
|
||||
|
||||
@@ -4,25 +4,19 @@ describe Spree::Product do
|
||||
|
||||
describe "associations" do
|
||||
it { should belong_to(:supplier) }
|
||||
it { should have_and_belong_to_many(:distributors) }
|
||||
it { should have_many(:product_distributions) }
|
||||
end
|
||||
|
||||
describe "validations" do
|
||||
it "is valid when created from factory" do
|
||||
build(:product).should be_valid
|
||||
create(:product).should be_valid
|
||||
end
|
||||
|
||||
it "requires a supplier" do
|
||||
product = build(:product)
|
||||
product = create(:product)
|
||||
product.supplier = nil
|
||||
product.should_not be_valid
|
||||
end
|
||||
|
||||
it "requires at least one distributor" do
|
||||
product = build(:product)
|
||||
product.distributors.clear
|
||||
product.should_not be_valid
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user