spec/factories.rb
# has_many is a neat helper that can be used to eliminate quirky before/after books for
# creating associations.
#
# Credits: https://gist.github.com/ybart/8844969
#
# Usage: ctrl+f "has_many"
#
has_many is a neat helper that can be used to eliminate quirky before/after books for
creating associations.




has_many is a neat helper that can be used to eliminate quirky before / after books for creating associations .
def has_many(collection, count = 1)
    after (:build) do |instance, evaluator|
      if instance.send(collection).blank?
        count.times { instance.send(collection) << yield(instance, evaluator) } if instance.send(collection).empty?
      end
    end
    after(:create) do |instance|
      instance.send(collection).each { |i| i.save! }
    end
  end
def has_many ( collection , count = 1 ) after ( :build ) do | instance , evaluator | if instance . send ( collection ) . blank? count . times { instance . send ( collection ) << yield ( instance , evaluator ) } if instance . send ( collection ) . empty? end end after ( :create ) do | instance | instance . send ( collection ) . each { | i | i . save! } end end
has_many is a neat helper that can be used to eliminate quirky before/after books for
creating associations.
has_many is a neat helper that can be used to eliminate quirky before / after books for creating associations .
# after_build is where you add instances to the factory-built collection. # Typically you'll want to Factory.build() these instances. # after_create will be called after after_build if the build strategy is Factory.create()
collection
count