Skip to content

Instantly share code, notes, and snippets.

@mikz
Created September 5, 2011 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikz/1195984 to your computer and use it in GitHub Desktop.
Save mikz/1195984 to your computer and use it in GitHub Desktop.
factory_girl singleton helper
require 'factory_girl/step_definitions'
def FactorySingleton(*args)
FactorySingleton.find_or_self *args
end
module FactorySingleton
def find_or_self object, attribute = nil
# FIXME: instead of this probably could check validation reflection for unique attributes
attribute ||= case object
when User
:login
end
raise 'Unknown class. Please supply attribute' unless attribute
object.class.where(attribute => object.send(attribute)).first || object
end
extend self
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment