Module: FindOrCreateOnScopes

Defined in:
lib/find_or_create_on_scopes.rb

Overview

Adds ‘find_or_create`-type methods to named scopes.

Constant Summary collapse

ABORT_SAVE =

Return this in a block from ‘find_or_create` or `create_or_update` to abort the creation/updating of a record.

Object.new

Instance Method Summary collapse

Instance Method Details

#create_or_update(*args) {|record| ... } ⇒ ActiveRecord::Base

Locates a record according to the current scope. Updates the record with the provided attributes if found. If not found, creates a new record with the current scope’s attributes and the provided attributes.

Parameters:

  • args (Array)

    Arguments to pass to the ‘assign_attributes` method.

Yields:

  • (record)

    Yields the record before it is saved.

Yield Parameters:

  • record (ActiveRecord::Base)

    The found or created record before it is saved.

Yield Returns:

  • (ABORT_SAVE, nil)

    If ‘ABORT_SAVE` the record is not saved.

Returns:

  • (ActiveRecord::Base)

    The found or created record.



53
54
55
# File 'lib/find_or_create_on_scopes.rb', line 53

def create_or_update(*args, &block)
  create_or_update_and_do :save, *args, &block
end

#create_or_update!(*args, &block) ⇒ Object

Same as #create_or_update but calls ‘save!` instead of `save` on the record.

See Also:



62
63
64
# File 'lib/find_or_create_on_scopes.rb', line 62

def create_or_update!(*args, &block)
  create_or_update_and_do :save!, *args, &block
end

#find_or_create(*args) {|record| ... } ⇒ ActiveRecord::Base

Locates a record according to the current scope. Returns the record if found. If not found, creates a new record with the attributes of the current scope and the provided attributes.

Parameters:

  • args (Array)

    Arguments to pass to the ‘create` method.

Yields:

  • (record)

    Yields the record before it is saved.

Yield Parameters:

  • record (ActiveRecord::Base)

    The found or created record before it is saved.

Yield Returns:

  • (ABORT_SAVE, nil)

    If ‘ABORT_SAVE` the record is not saved.

Returns:

  • (ActiveRecord::Base)

    The found or created record.



20
21
22
# File 'lib/find_or_create_on_scopes.rb', line 20

def find_or_create(*args, &block)
  find_or_initialize_and_do :save, *args, &block
end

#find_or_create!(*args, &block) ⇒ Object

Same as #find_or_create but calls ‘save!` instead of `save` on the record.

See Also:



28
29
30
# File 'lib/find_or_create_on_scopes.rb', line 28

def find_or_create!(*args, &block)
  find_or_initialize_and_do :save!, *args, &block
end

#find_or_initialize(*args, &block) ⇒ Object

Same as #find_or_create but does not save the record. Please note that unless this method is called in a transaction, you might have a race condition when trying to save the record.

See Also:



38
39
40
# File 'lib/find_or_create_on_scopes.rb', line 38

def find_or_initialize(*args, &block)
  find_or_initialize_and_do nil, *args, &block
end

#initialize_or_update(*args, &block) ⇒ Object

Same as #create_or_update but does not save the record. Please note that unless this method is called in a transaction, you might have a race condition when trying to save the record.

See Also:



72
73
74
# File 'lib/find_or_create_on_scopes.rb', line 72

def initialize_or_update(*args, &block)
  create_or_update_and_do nil, *args, &block
end