Class: UuidAssociations::ActiveRecord::NestedAttributes::UuidFinder
- Inherits:
-
Object
- Object
- UuidAssociations::ActiveRecord::NestedAttributes::UuidFinder
- Defined in:
- lib/uuid_associations/active_record/nested_attributes/uuid_finder.rb
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(association_klass, attribute_collection, options) ⇒ UuidFinder
constructor
A new instance of UuidFinder.
Constructor Details
#initialize(association_klass, attribute_collection, options) ⇒ UuidFinder
Returns a new instance of UuidFinder.
11 12 13 14 15 |
# File 'lib/uuid_associations/active_record/nested_attributes/uuid_finder.rb', line 11 def initialize(association_klass, attribute_collection, ) @association_klass = association_klass @attribute_collection = attribute_collection @options = end |
Class Method Details
.replaced_uuids_with_ids(association_klass, attribute_collection, options) ⇒ Object
7 8 9 |
# File 'lib/uuid_associations/active_record/nested_attributes/uuid_finder.rb', line 7 def self.replaced_uuids_with_ids(association_klass, attribute_collection, ) new(association_klass, attribute_collection, ).call end |
Instance Method Details
#call ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/uuid_associations/active_record/nested_attributes/uuid_finder.rb', line 17 def call to_replace, to_keep = attribute_collection.partition do |attributes| symbol_keys = attributes.keys.map(&:to_sym) (symbol_keys & [:uuid, :id]) == [:uuid] end uuids_to_find = to_replace.map { |element| element[:uuid] } found_records = association_klass.where(uuid: uuids_to_find) replaced = to_replace.each_with_object([]) do |attributes, collection| uuid = attributes.delete(:uuid) record = found_records.find { |found_record| found_record.uuid == uuid } if @options[:create_missing_uuids] collection << if record.blank? attributes.merge(uuid: uuid) else attributes.merge(id: record.id) end else raise_not_found_error(uuid) if record.blank? collection << attributes.merge(id: record.id) end end to_keep + replaced end |