Feature Request: Allow creation of factory from existing model #63

Closed
asanghi opened this Issue Jun 17, 2012 · 10 comments

Comments

Projects
None yet
6 participants

asanghi commented Jun 17, 2012

It would be nice to allow

rails g factory_girl:model Post --from-schema

This would read the existing Post model's attributes and generate a fixture from there instead of having to specify the attributes explicitly on the command line. The use case allows for reducing the effort a bit when moving a largish project a lot of models (with lots of attributes) to factory_girl.

Ideas? Thoughts?

Shouldn't --from-schema be the default if no options are provided, after all what's the point of a generator that only generates an empty factory? If anything --without-schema would be a better option if needed at all.

Other than that I like you idea although I had assumed there was a good reason this wasn't implemented to start with.

Owner

mike-burns commented Jul 9, 2012

Our default factories are minimal: they contain only what is needed to build a valid object, and no more.

We then add additional factories that go above and beyond.

class User < ActiveRecord::Base
  validates_presence_of :name
end

FactoryGirl.define do
  factory :user do
    name "Franklin Bluth"

    factory :admin do
      role 'admin'
    end
  end
end

asanghi commented Jul 10, 2012

@mike-burns understood and accepted that it should not be the default as per your philosophy on how this gem should behave.

Do you still think there is value in having an option (--from-schema) when the user knows that minimal generated is not enough for the model to be saved and they'de rather remove the ones they know are non-essential manually? Just that sometimes attributes essential are more than attributes non-essential.

If not, I'm happy for you to close the ticket.

Owner

mike-burns commented Jul 10, 2012

What would the values be? I guess it could do some simple mapping: int -> 1, varchar -> "hello", datetime -> { Time.now }.

Looking for feedback from @joshuaclayton and @jferris on what they are willing to support. Here's an idea: split the generators off into a separate gem, look for someone else to maintain it?

Owner

joshuaclayton commented Jul 10, 2012

If we were able to move the generators out to a separate gem, this gem would effectively go away I think; last I checked, the only other piece of functionality the factory_girl_rails gem added was using railties to wire everything up and set up the location of the factories

asanghi commented Jul 10, 2012

@joshuaclayton thanks for that. So are you saying there is scope for this gem to do a bit more? :)

Owner

joshuaclayton commented Jul 10, 2012

@asanghi not sure exactly; I'll have to dig through a bit more to fully understand everything it's doing and what functionality we want it to provide.

gregors commented Feb 8, 2013

+1 on this

Owner

joshuaclayton commented Feb 15, 2013

I've thought about this and I don't typically set a lot of defaults on my factories. When I do, it's often times implicit attributes for email/username/title (which need to be unique) and wouldn't benefit from the auto-generation of attributes anyway. We'd also have to probably be intelligent about STI, polymorphism, associations, etc. which are oftentimes ORM-specific, meaning we'd need to have different code paths per ORM. I don't think that it's worth introducing that amount of complexity for a minimal benefit, so I'm going to close the issue. If someone can argue against this and provide some great examples of factories vs schema definitions and how they're using FG, I'd definitely sit down and think about it more but I think this is the right decision for now.

Thanks for the discussion everyone; it's definitely an interesting/difficult problem to solve but I don't think it's one FGR wants to handle right now.

This would be very helpful in generating some demo data!

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