Created
November 18, 2009 22:39
-
-
Save mrinterweb/238342 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
class Override | |
# This assoc method is designed as a helpful tool to work around a shortcoming I found with Factory Girl's inability | |
# to associate records created by other factories. Instead of sharing previously created factories, | |
# it was creating duplicate records when associating records. | |
def self.assoc(record, model_name, factory_name = nil) | |
factory_name = model_name if factory_name.nil? | |
existing_factory = eval("@#{model_name}") | |
#puts "existing_factory: #{existing_factory}, model_name: #{model_name}, factory_name: #{factory_name}, model: #{model_name.to_s.camelize.constantize.name}" | |
#puts "#{model_name.to_s.camelize.constantize.find_by_oid(Factory.stub(factory_name).oid)}" | |
last_resort = Proc.new do | |
if defined?(@@stub) && @@stub == true | |
#puts "stubbing #{factory_name}" | |
last_resort = Factory.stub(factory_name) | |
else | |
last_resort = record.association(factory_name) | |
end | |
end | |
existing_factory || model_name.to_s.camelize.constantize.find_by_oid(Factory.stub(factory_name).oid) || last_resort.call | |
end | |
module String | |
# Yes this method was taken directly from of Rails | |
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) | |
if first_letter_in_uppercase | |
lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } | |
else | |
lower_case_and_underscored_word.first.downcase + camelize(lower_case_and_underscored_word)[1..-1] | |
end | |
end | |
end | |
end | |
Factory.define :event do |f| | |
f.oid 'mainEvent1' | |
f.name "Main Event" | |
f.short_name 'mainevent' | |
end | |
Factory.define :notification do |f| | |
f.event {|r| Override.assoc(r, :event)} | |
f.oid { Oid::create } | |
f.name "Penut Butter Jelly Time" | |
f.add_attribute :type, "Alert" | |
f.priority 10 | |
f.last_displayed Time.now - 2.days | |
f.content "IT'S PENUT BUTTER JELLY TIME. WHERE YOU AT? WHERE YOU AT? NOW THERE YOU GO. THERE YOU GO..." | |
#f.image | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment