Data integrity

This commit is contained in:
Rohan Mitchell
2013-09-19 11:22:55 +10:00
parent f06f4702ed
commit 08941ae22b
4 changed files with 24 additions and 2 deletions

View File

@@ -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'

View File

@@ -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

View File

@@ -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"

View File

@@ -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