Change product-distributor relation to *..*

This commit is contained in:
Rohan Mitchell
2012-06-18 19:43:30 +10:00
parent 3d0f6a43b4
commit 3b075a7c14
5 changed files with 26 additions and 8 deletions

View File

@@ -3,6 +3,7 @@ module Spree
self.table_name = 'distributors'
belongs_to :country
belongs_to :state
has_and_belongs_to_many :products
validates :name, :pickup_address, :country_id, :state_id, :city, :post_code, :presence => true

View File

@@ -1,5 +1,6 @@
Spree::Product.class_eval do
belongs_to :supplier
has_and_belongs_to_many :distributors
attr_accessible :supplier_id
end
end

View File

@@ -1,12 +1,23 @@
class MoveDistributorFromOrderToProduct < ActiveRecord::Migration
def change
def up
remove_column :spree_orders, :distributor_id
add_column :spree_products, :distributor_id, :integer
Spree::Order.reset_column_information
Spree::Product.reset_column_information
# Associate all products with the first distributor so they'll be valid
create_table :distributors_products, :id => false do |t|
t.references :product
t.references :distributor
end
# Associate all products with the first distributor
distributor = Spree::Distributor.first
Spree::Product.update_all("distributor_id = #{distributor.id}") if distributor
if distributor
Spree::Product.all.each do |product|
product.distributors << distributor
end
end
end
def down
drop_table :distributors_products
add_column :spree_orders, :distributor_id, :integer
end
end

View File

@@ -32,6 +32,11 @@ ActiveRecord::Schema.define(:version => 20120618061537) do
t.integer "state_id"
end
create_table "distributors_products", :id => false, :force => true do |t|
t.integer "product_id"
t.integer "distributor_id"
end
create_table "spree_activators", :force => true do |t|
t.string "description"
t.datetime "expires_at"
@@ -353,7 +358,6 @@ ActiveRecord::Schema.define(:version => 20120618061537) do
t.datetime "updated_at"
t.integer "count_on_hand", :default => 0, :null => false
t.integer "supplier_id"
t.integer "distributor_id"
end
add_index "spree_products", ["available_on"], :name => "index_products_on_available_on"

View File

@@ -4,6 +4,7 @@ describe Spree::Product do
describe "associations" do
it { should belong_to(:supplier) }
it { should have_and_belong_to_many(:distributors) }
end
end