New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added update_or_create method #4
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
require 'active_support' | ||
|
||
module Easy | ||
module ReferenceData | ||
def self.refresh(clazz, unique_attribute_symbol, unique_attribute_value, attributes) | ||
record = clazz.where(unique_attribute_symbol => unique_attribute_value).first | ||
self.update_or_create(clazz, attributes.merge(unique_attribute_symbol => unique_attribute_value), keys: [unique_attribute_symbol]) | ||
end | ||
|
||
if record.nil? | ||
record = clazz.new | ||
record.send "#{unique_attribute_symbol}=", unique_attribute_value | ||
end | ||
def self.update_or_create(clazz, attributes, options) | ||
unique_attribute_keys = options.fetch(:keys) | ||
|
||
attributes.each_pair do |key, value| | ||
record.send "#{key}=", value | ||
end | ||
record = clazz.where(attributes.slice(*unique_attribute_keys)).first_or_initialize | ||
|
||
if record.new_record? | ||
puts "..creating #{clazz}(#{unique_attribute_value})" | ||
elsif record.changed? | ||
puts "..updating #{clazz}(#{unique_attribute_value})" | ||
puts "..creating #{clazz}(#{attributes.slice(*unique_attribute_keys)})" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. at the moment, these |
||
else | ||
puts "..updating #{clazz}(#{attributes.slice(*unique_attribute_keys)})" | ||
end | ||
|
||
begin | ||
record.save! | ||
record.update_attributes!(attributes) | ||
rescue | ||
puts "Save failed for #{record.class}[#{unique_attribute_symbol}: #{unique_attribute_value}] with attributes #{attributes.inspect}" | ||
puts "Save failed for #{record.class} with attributes #{attributes.inspect}" | ||
raise | ||
end | ||
|
||
record | ||
end | ||
|
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module Easy | ||
module ReferenceData | ||
VERSION = "0.1.2" | ||
VERSION = "0.2.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we should just go straight to version |
||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these examples be adjusted to separate the
keys
option from theUser
attributes? Eg:At the moment, it looks as if
keys
is just another attribute ofUser
.