Skip to content

Commit

Permalink
Merge pull request #309 from kbrock/classification_parent
Browse files Browse the repository at this point in the history
classification#parent_id = nil when no parents
  • Loading branch information
bdunne authored Jun 13, 2019
2 parents 7541bce + 2f21d18 commit efbba46
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions db/migrate/20181130203334_classification_parent_null.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class ClassificationParentNull < ActiveRecord::Migration[5.0]
class Classification < ActiveRecord::Base
end

def up
change_column_default(:classifications, :parent_id, nil)
Classification.where(:parent_id => 0).update_all(:parent_id => nil)
end

def down
change_column_default(:classifications, :parent_id, 0)
Classification.where(:parent_id => nil).update_all(:parent_id => 0)
end
end
30 changes: 30 additions & 0 deletions spec/migrations/20181130203334_classification_parent_null_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require_migration

describe ClassificationParentNull do
let(:classification_stub) { migration_stub(:Classification) }

migration_context :up do
it 'changes parent to nil when parent is 0' do
c = classification_stub.create(:parent_id => 0)
migrate
c.reload
expect(c.parent_id).to be_nil
end

it 'leaves other parents alone' do
c = classification_stub.create(:parent_id => 1)
migrate
c.reload
expect(c.parent_id).to eq(1)
end
end

migration_context :down do
it 'changes parent to 0 when parent is null' do
c = classification_stub.create(:parent_id => nil)
migrate
c.reload
expect(c.parent_id).to eq(0)
end
end
end

0 comments on commit efbba46

Please sign in to comment.