Created
January 13, 2011 10:47
-
-
Save zoras/777691 to your computer and use it in GitHub Desktop.
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
#!ruby | |
# w = Station.first.build_worker(:reward => 2.0, :number => 1) | |
# w.save! | |
# Each station worker is copied to human_worker which is inherited from dummy_worker (later is referenced in station) | |
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) | |
station.dummy_worker = human_worker | |
human_worker.save! | |
station.worker.delete | |
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
# 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 |
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
# 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 |
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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment