Created
January 13, 2011 10:47
-
-
Save zoras/777694 to your computer and use it in GitHub Desktop.
cf document migration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# after deploy worker is references_one and human_worker << worker | |
# add references_one dummy_worker and copy dummy_worker.rb | |
# station.rb | |
class Station | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
# Fields | |
field :station_type | |
# Associations | |
embeds_one :instruction | |
# RobotWorker which inherits from worker is referenced_in Robot so we need reference worker instead of embedding it. | |
references_one :worker | |
# add referenced dummy_worker to copy embedded worker | |
references_one :dummy_worker | |
references_many :station_instances | |
referenced_in :line | |
# Validations | |
validates :station_type, :presence => true, :on => :create | |
end | |
#!ruby | |
# copy referenced dummy_worker-> referenced worker script | |
# Each station dummy_worker is copied to human_worker which is inherited from worker (later is referenced in station) | |
# below script deletes dummy_worker, removing child human_workers also | |
# remove dummy_worker.rb after running below script | |
Station.all.each do |station| | |
dummy_worker = station.dummy_worker | |
human_worker = HumanWorker.new(:number => dummy_worker.number, :reward => dummy_worker.reward, :worker_type => dummy_worker.worker_type, :station_id => station.id) if dummy_worker | |
station.worker = human_worker | |
human_worker.save! if human_worker | |
station.dummy_worker.delete if station.dummy_worker | |
station.save! | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# station.rb | |
# added referenced dummy_worker | |
class Station | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
# Fields | |
field :station_type | |
# Associations | |
embeds_one :instruction | |
# RobotWorker which inherits from worker is referenced_in Robot so we need reference worker instead of embedding it. | |
embeds_one :worker | |
# add referenced dummy_worker to copy embedded worker | |
references_one :dummy_worker | |
references_many :station_instances | |
referenced_in :line | |
# Validations | |
validates :station_type, :presence => true, :on => :create | |
end | |
# dummy_worker.rb | |
# same as worker.rb except class name | |
class DummyWorker | |
include Mongoid::Document | |
# Fields | |
field :worker_type | |
field :reward, :type => Float | |
field :number, :type => Integer, :default => 1 | |
# Associations | |
referenced_in :station | |
# validates :reward, :human_reward => true | |
end | |
# human_worker.rb | |
# human_woker inherits from dummy_worker which is referenced_in station | |
class HumanWorker < DummyWorker | |
include Mongoid::Document | |
validates :reward, :format => {:with => /^[0-9]*\.?[0-9]{1,2}$/, :message => "must be numeric with two digit at most after decimal"} | |
end | |
#!ruby | |
# w = Station.first.build_worker(:reward => 2.0, :number => 1) | |
# w.save! | |
# copy referenced dummy_worker-> referenced worker script | |
# Each station worker is copied to human_worker which is inherited from dummy_worker (later is referenced in station) | |
# below script deletes embedded workers from station | |
# add dummy_worker.rb before running below script | |
Station.all.each do |station| | |
worker = station.worker | |
human_worker = HumanWorker.new(:number => worker.number, :reward => worker.reward, :worker_type => worker.worker_type, :station_id => station.id) if worker | |
station.dummy_worker = human_worker | |
human_worker.save! if human_worker | |
station.worker.delete if station.worker | |
station.save! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment