lib/pricehubble/entity/concern/associations.rb in pricehubble-0.4.2 vs lib/pricehubble/entity/concern/associations.rb in pricehubble-1.0.0
- old
+ new
@@ -43,12 +43,12 @@
def map_has_one_association(attribute, opts, struct, hash)
# Early exit when the source key is missing on the given data
key = opts[:from]
# Initialize an object on the association, even without data
- if opts[:initialize] && send(attribute).nil?
- hash[key] = {} unless hash.key? key
+ if opts[:initialize] && send(attribute).nil? && !(hash.key? key)
+ hash[key] = {}
end
return [struct, hash] unless hash.key? key
# Instantiate a new entity from the association
@@ -90,12 +90,12 @@
# When a singular appender was configured, we allow to use it as
# attribute source if the regular source is not available
key = opts[:fallback_from] if opts[:fallback_from] && !hash.key?(key)
# Initialize an empty array on the association
- if opts[:initialize] && send(attribute).nil?
- hash[key] = [] unless hash.key? key
+ if opts[:initialize] && send(attribute).nil? && !(hash.key? key)
+ hash[key] = []
end
return [struct, hash] unless hash.key? key
# Instantiate a new entity from each association element
@@ -154,10 +154,11 @@
end
# Register the association
associations[entity] = opts
# Generate getters and setters
attr_accessor entity
+
# Add the entity to the tracked attributes if it should be persisted
tracked_attr entity if opts[:persist]
end
# Define a simple +has_many+ association.
@@ -185,9 +186,10 @@
end
# Register the association
associations[entity] = opts
# Generate getters and setters
attr_accessor entity
+
# Add the entity to the tracked attributes if it should be persisted
tracked_attr entity if opts[:persist]
end
end
# rubocop:enable Naming/PredicateName