Skip to content

Commit

Permalink
Renaming existing zone
Browse files Browse the repository at this point in the history
If previously created by user
  • Loading branch information
slemrmartin committed Jul 10, 2018
1 parent ea27a25 commit c8b53f3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
20 changes: 18 additions & 2 deletions db/migrate/20180626125654_make_maintenance_zone_record.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
class MakeMaintenanceZoneRecord < ActiveRecord::Migration[5.0]
class Zone < ActiveRecord::Base
validates_presence_of :name
validates_uniqueness_of :name

MAINTENANCE_ZONE_NAME = "__maintenance__".freeze
end

def up
say_with_time("Creating Maintenance Zone") do
Zone.create_with(:description => "Maintenance Zone", :visible => false).find_or_create_by!(:name => 'maintenance')
zone = Zone.where(:name => Zone::MAINTENANCE_ZONE_NAME).first
if zone.present?
zone.name = "#{zone.name}_0"
zone.save
end

Zone.create_with(:description => "Maintenance Zone", :visible => false).find_or_create_by!(:name => Zone::MAINTENANCE_ZONE_NAME)
end
end

def down
say_with_time("Deleting Maintenance Zone") do
Zone.where(:name => 'maintenance').where(:visible => false).destroy_all
Zone.where(:name => Zone::MAINTENANCE_ZONE_NAME).where(:visible => false).destroy_all

orig = Zone.where(:name => "#{Zone::MAINTENANCE_ZONE_NAME}_0").first
if orig.present?
orig.name = orig.name[0..orig.name.size - 3]
orig.save
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,48 @@
let(:zone_stub) { migration_stub(:Zone) }

migration_context :up do
before(:each) do
zone_stub.delete_all
end

it "adds MaintenanceZone" do
migrate

expect(zone_stub.where(:name => 'maintenance').where(:visible => false).count).to eq(1)
expect(zone_stub.where(:name => zone_stub::MAINTENANCE_ZONE_NAME).where(:visible => false).count).to eq(1)
end

it "renames original maintenance zone" do
orig = zone_stub.create!(:name => zone_stub::MAINTENANCE_ZONE_NAME)

migrate
orig.reload

expect(orig.name).to eq("#{zone_stub::MAINTENANCE_ZONE_NAME}_0")
end
end

migration_context :down do
before(:each) do
zone_stub.delete_all
end

it "removes MaintenanceZone" do
zone_stub.create!(:name => 'maintenance',
zone_stub.create!(:name => zone_stub::MAINTENANCE_ZONE_NAME,
:description => 'Maintenance Zone',
:visible => false)

migrate

expect(zone_stub.where(:name => 'maintenance').where(:visible => false).count).to eq(0)
expect(zone_stub.where(:name => zone_stub::MAINTENANCE_ZONE_NAME).where(:visible => false).count).to eq(0)
end

it "renames original maintenance zone back" do
orig = zone_stub.create!(:name => "#{zone_stub::MAINTENANCE_ZONE_NAME}_0")

migrate
orig.reload

expect(orig.name).to eq(zone_stub::MAINTENANCE_ZONE_NAME)
end
end
end

0 comments on commit c8b53f3

Please sign in to comment.