diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index b91b2436cb..97a298b766 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -9,7 +9,7 @@ class Enterprise < ActiveRecord::Base has_many :product_distributions, :foreign_key => 'distributor_id', :dependent => :destroy has_many :distributed_products, :through => :product_distributions, :source => :product has_many :enterprise_fees - has_many :enterprise_roles + has_many :enterprise_roles, :dependent => :destroy has_many :users, through: :enterprise_roles has_and_belongs_to_many :payment_methods, join_table: 'distributors_payment_methods', class_name: 'Spree::PaymentMethod', foreign_key: 'distributor_id' diff --git a/db/migrate/20130919010513_ensure_shipping_methods_have_distributors.rb b/db/migrate/20130919010513_ensure_shipping_methods_have_distributors.rb new file mode 100644 index 0000000000..7e7167b00c --- /dev/null +++ b/db/migrate/20130919010513_ensure_shipping_methods_have_distributors.rb @@ -0,0 +1,12 @@ +class EnsureShippingMethodsHaveDistributors < ActiveRecord::Migration + def up + d = Enterprise.is_distributor.first + sms = Spree::ShippingMethod.where('distributor_id IS NULL') + say "Assigning an arbitrary distributor (#{d.name}) to all shipping methods without one (#{sms.count} total)" + + sms.update_all(distributor_id: d) + end + + def down + end +end diff --git a/db/schema.rb b/db/schema.rb index 792ed59fee..517b5eb6ca 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130912021938) do +ActiveRecord::Schema.define(:version => 20130919010513) do create_table "adjustment_metadata", :force => true do |t| t.integer "adjustment_id" diff --git a/spec/models/enterprises_spec.rb b/spec/models/enterprises_spec.rb index 35aa2db0df..f47d1fb336 100644 --- a/spec/models/enterprises_spec.rb +++ b/spec/models/enterprises_spec.rb @@ -7,6 +7,16 @@ describe Enterprise do it { should have_many(:distributed_orders) } it { should belong_to(:address) } it { should have_many(:product_distributions) } + + it "should destroy enterprise roles upon its own demise" do + e = create(:enterprise) + u = create(:user) + u.enterprise_roles.build(enterprise: e).save! + + role = e.enterprise_roles.first + e.destroy + EnterpriseRole.where(id: role.id).should be_empty + end end describe "validations" do