Module: UpdateOrCreate::ClassMethods

Defined in:
lib/update_or_create.rb

Overview

add your static(class) methods here

Instance Method Summary collapse

Instance Method Details

#method_missing_with_update_or_create(method_id, *args, &block) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/update_or_create.rb', line 8

def method_missing_with_update_or_create(method_id, *args, &block)
  method_name = method_id.to_s
  if method_name =~ /^update_or_create_by_(.+)$/
    update_or_create($1, *args, &block)
  else
    method_missing_without_update_or_create(method_id, *args, &block)
  end
end

#update_or_create(search, *args) {|obj| ... } ⇒ Object

Yields:

  • (obj)


19
20
21
22
23
24
25
26
# File 'lib/update_or_create.rb', line 19

def update_or_create(search, *args, &block)
  parameters = search.split("_and_")
  params = Hash[ parameters.zip(args) ]
  obj = where(params).first || self.new(params)
  yield obj
  obj.save
  obj
end