Skip to content

Instantly share code, notes, and snippets.

@renius
Forked from thewatts/fg_find_or_create.rb
Last active October 31, 2017 15:18

Revisions

  1. renius revised this gist May 29, 2015. 1 changed file with 12 additions and 12 deletions.
    24 changes: 12 additions & 12 deletions fg_find_or_create.rb
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,17 @@
    module FactoryGirl::Syntax::Methods
    def find_or_create(name, attributes = {}, &block)
    factory = FactoryGirl.factory_by_name(name)
    klass = factory.build_class
    module FactoryGirl
    module Syntax
    module Methods
    def find_or_create(name, attributes = {}, &block)
    attributes = FactoryGirl.attributes_for(name).merge(attributes)

    factory_attributes = FactoryGirl.attributes_for(name)
    attributes = factory_attributes.merge(attributes)
    result =
    FactoryGirl.
    factory_by_name(name).
    build_class.
    find_by(attributes, &block)

    result = klass.find_by(attributes, &block)

    if result
    result
    else
    FactoryGirl.create(name, attributes, &block)
    result || FactoryGirl.create(name, attributes, &block)
    end
    end
    end
    end
  2. @thewatts thewatts revised this gist Dec 23, 2014. 1 changed file with 9 additions and 3 deletions.
    12 changes: 9 additions & 3 deletions fg_find_or_create.rb
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,17 @@
    module FactoryGirl::Syntax::Methods
    def find_or_create(name, attributes = {}, &block)
    factory = FactoryGirl.factory_by_name(name)
    clazz = factory.build_class
    klass = factory.build_class

    factory_attributes = FactoryGirl.attributes_for(name)
    attributes = factory_attributes.merge(attributes)

    clazz.find_or_create_by(attributes, &block)
    result = klass.find_by(attributes, &block)

    if result
    result
    else
    FactoryGirl.create(name, attributes, &block)
    end
    end
    end
    end
  3. @abinoam abinoam revised this gist Dec 5, 2014. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions fg_find_or_create.rb
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    module FactoryGirl::Syntax::Methods
    def find_or_create(resource_name, attributes = {}, &block)
    clazz = resource_name.to_s.classify.constantize
    def find_or_create(name, attributes = {}, &block)
    factory = FactoryGirl.factory_by_name(name)
    clazz = factory.build_class

    model = clazz.find_by(attributes) || FactoryGirl.build(resource_name, attributes)
    factory_attributes = FactoryGirl.attributes_for(name)
    attributes = factory_attributes.merge(attributes)

    yield(model) if block_given? && model.new_record?

    model.tap(&:save)
    clazz.find_or_create_by(attributes, &block)
    end
    end
  4. @abinoam abinoam revised this gist Dec 5, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions fg_find_or_create.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    module FactoryGirl
    def self.find_or_create(resource_name, attributes = {}, &block)
    module FactoryGirl::Syntax::Methods
    def find_or_create(resource_name, attributes = {}, &block)
    clazz = resource_name.to_s.classify.constantize

    model = clazz.find_by(attributes) || FactoryGirl.build(resource_name, attributes)
  5. @Aerlinger Aerlinger renamed this gist Oct 21, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. @Aerlinger Aerlinger revised this gist Oct 21, 2014. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions find_or_create factory girl
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,11 @@
    def find_or_create(resource_name, attributes = {}, &block)
    clazz = resource_name.to_s.classify.constantize
    module FactoryGirl
    def self.find_or_create(resource_name, attributes = {}, &block)
    clazz = resource_name.to_s.classify.constantize

    model = clazz.find_by(attributes) || FactoryGirl.build(resource_name, attributes)
    model = clazz.find_by(attributes) || FactoryGirl.build(resource_name, attributes)

    yield(model) if block_given? && model.new_record?
    yield(model) if block_given? && model.new_record?

    model.tap(&:save)
    model.tap(&:save)
    end
    end
  7. @Aerlinger Aerlinger created this gist Oct 21, 2014.
    9 changes: 9 additions & 0 deletions find_or_create factory girl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    def find_or_create(resource_name, attributes = {}, &block)
    clazz = resource_name.to_s.classify.constantize

    model = clazz.find_by(attributes) || FactoryGirl.build(resource_name, attributes)

    yield(model) if block_given? && model.new_record?

    model.tap(&:save)
    end