diff --git a/app/models/spree/distributor.rb b/app/models/spree/distributor.rb index 6024684207..f7f87617d5 100644 --- a/app/models/spree/distributor.rb +++ b/app/models/spree/distributor.rb @@ -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 diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 9e0a5feeda..3626d15630 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -1,5 +1,6 @@ Spree::Product.class_eval do belongs_to :supplier + has_and_belongs_to_many :distributors attr_accessible :supplier_id -end \ No newline at end of file +end diff --git a/db/migrate/20120618061537_move_distributor_from_order_to_product.rb b/db/migrate/20120618061537_move_distributor_from_order_to_product.rb index c7d3343292..5b5c102331 100644 --- a/db/migrate/20120618061537_move_distributor_from_order_to_product.rb +++ b/db/migrate/20120618061537_move_distributor_from_order_to_product.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index b881d00e3c..c98bb1ca6e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/spec/models/product_spec.rb b/spec/models/product_spec.rb index c7d9c776e0..712942a375 100644 --- a/spec/models/product_spec.rb +++ b/spec/models/product_spec.rb @@ -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