Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linting with Dependent Association #696

Closed
summera opened this issue Sep 18, 2014 · 2 comments
Closed

Linting with Dependent Association #696

summera opened this issue Sep 18, 2014 · 2 comments

Comments

@summera
Copy link

summera commented Sep 18, 2014

I have a case where two models depend on each other, i.e. one cannot exist without the other and the relationship can never be nil. As you can see below, this is also a polymorphic relationship.

Models

class Destination < ActiveRecord::Base
  belongs_to :destinationable, polymorphic: true

  validates :destinationable_id, uniqueness: { scope: :destinationable_type }
end

class Lander < ActiveRecord::Base
  has_one :destination, as: :destinationable, dependent: :destroy

  accepts_nested_attributes_for :destination

  validates :destination, presence: true
  validates_associated :destination
end

class Offer < ActiveRecord::Base
  has_one :destination, as: :destinationable, dependent: :destroy

  accepts_nested_attributes_for :destination

  validates :destination, presence: true
  validates_associated :destination
end

In my Lander and Offer factories I am creating the Destination since Destination will never be directly created in the application. When linting factories, FactoryGirl's implementation calls build on each factory. This causes an issue since when building Lander and Offer factories, Destinations are created with a nil destinationable association. This is because both Lander and Offer are built, not saved to the database, and do not have an id when linted

This causes two issues

  1. Destination should never exist in the database with a nil destinationable association
  2. Gives a unique validation error since it tries to create multiple destinationables with nil

My factories are linted without errors when I override calculate_invalid_factories to call create instead of build: https://github.com/thoughtbot/factory_girl/blob/master/lib/factory_girl/linter.rb#L24

    def calculate_invalid_factories
      factories_to_lint.select do |factory|
        built_factory = FactoryGirl.create(factory.name) #<< this line

        if built_factory.respond_to?(:valid?)
          !built_factory.valid?
        end
      end
    end

Any ideas for how to handle this issue? Do you think it would be a good idea to allow the option tocreate instead of build on linting? I think this would be an issue whenever you try and build two dependent models that must be created together to exist.

@drapergeek
Copy link
Contributor

Looks like this was solved by #707, if that's not correct, please let me know and I'm happy to re-open.

@summera
Copy link
Author

summera commented May 9, 2015

@drapergeek 👍 thanks for the update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants