-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make find_or_create and find_or_initialize work mixing explicit param…
…eters and a hash. ht: Marc-André Lafortune [#4457 state:committed] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
- Loading branch information
1 parent
adcfb4e
commit f967b35
Showing
3 changed files
with
33 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
|
@@ -1942,23 +1942,28 @@ def self.#{method_id}(*args) | ||
# end | # end | ||
self.class_eval <<-EOS, __FILE__, __LINE__ + 1 | self.class_eval <<-EOS, __FILE__, __LINE__ + 1 | ||
def self.#{method_id}(*args) | def self.#{method_id}(*args) | ||
guard_protected_attributes = false | attributes = [:#{attribute_names.join(',:')}] | ||
protected_attributes_for_create = unprotected_attributes_for_create = {} | |||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
spastorino
Author
Contributor
|
|||
if args[0].is_a?(Hash) | args.each_with_index do |arg, i| | ||
guard_protected_attributes = true | if arg.is_a?(Hash) | ||
attributes = args[0].with_indifferent_access | protected_attributes_for_create = args[i].with_indifferent_access | ||
find_attributes = attributes.slice(*[:#{attribute_names.join(',:')}]) | else | ||
else | unprotected_attributes_for_create[attributes[i]] = args[i] | ||
find_attributes = attributes = construct_attributes_from_arguments([:#{attribute_names.join(',:')}], args) | end | ||
end | end | ||
find_attributes = (protected_attributes_for_create.merge(unprotected_attributes_for_create)).slice(*attributes) | |||
options = { :conditions => find_attributes } | options = { :conditions => find_attributes } | ||
set_readonly_option!(options) | set_readonly_option!(options) | ||
record = find(:first, options) | record = find(:first, options) | ||
if record.nil? | if record.nil? | ||
record = self.new { |r| r.send(:attributes=, attributes, guard_protected_attributes) } | record = self.new do |r| | ||
r.send(:attributes=, protected_attributes_for_create, true) unless protected_attributes_for_create.empty? | |||
r.send(:attributes=, unprotected_attributes_for_create, false) unless unprotected_attributes_for_create.empty? | |||
end | |||
#{'yield(record) if block_given?'} | #{'yield(record) if block_given?'} | ||
#{'record.save' if instantiator == :create} | #{'record.save' if instantiator == :create} | ||
record | record | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Do you mean to do this? You're assigning the same object to two separate variables, and then merging them later on 1955 (when they're going to be the same?) Perhaps you mean to define these variables both as empty hashes? Is there something I'm missing?