Allow to use AR class names as arguments to create, build etc #386

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
3 participants

ineu commented May 22, 2012

According to discussion in c006dd1#commitcomment-1320334
As Object.make syntax is deprecated now, we need a way to pass classes to build/create methods. Currently you have to do

my_class = BookAuthor
create my_class.to_s.underscore

This commit allows to do

my_class = BookAuthor
create my_class

I never looked into FG internals, so please let me know if this patch should be modified or rewritten.

@@ -65,6 +65,7 @@ def self.register_factory(factory)
end
def self.factory_by_name(name)
+ name = name.to_s.underscore unless name.class.in? String, Symbol # looks like AR object
@mike-burns

mike-burns May 31, 2012

Owner

Thinking aloud here; @jferris and @joshuaclayton might have more ideas on this. How about if FactoryGirl::Registry's @items was a subclass of Hash that knew what to do with Class objects for keys.

class ClassyHash < Hash
  def [](key)
    super || super(key.to_s.underscore)
  end
end
@joshuaclayton

joshuaclayton Jun 8, 2012

Owner

There's actually a few decorators now - I think it'd make more sense there, actually, but I like that idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment