Skip to content

Instantly share code, notes, and snippets.

@Aerlinger
Last active February 3, 2017 14:35
Show Gist options
  • Save Aerlinger/ba8fe98fb58ec6be4d6f to your computer and use it in GitHub Desktop.
Save Aerlinger/ba8fe98fb58ec6be4d6f to your computer and use it in GitHub Desktop.

Revisions

  1. Aerlinger renamed this gist Oct 21, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. 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
  3. 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