Sha256: 051bef5813a8f154e82770a9f875ddb35dc334731818feb833711ead16852ef7
Contents?: true
Size: 1.46 KB
Versions: 1
Compression:
Stored size: 1.46 KB
Contents
module Spyke module Associations class HasMany < Association def initialize(*args) super @options.reverse_merge!(uri: "/#{parent.class.model_name.plural}/:#{foreign_key}/#{@name}/(:id)") @params[foreign_key] = parent.id end def load self end def assign_nested_attributes(incoming) incoming = incoming.values if incoming.is_a?(Hash) combined_attributes = combine_with_existing(incoming) clear_existing! combined_attributes.each do |attributes| build(attributes) end end private def combine_with_existing(incoming) return incoming unless primary_keys_present_in_existing? combined = embedded_params + incoming group_by_primary_key(combined).flat_map do |primary_key, hashes| if primary_key.present? hashes.reduce(:merge) else hashes end end end def group_by_primary_key(array) array.group_by { |h| h.with_indifferent_access[:id].to_s } end def primary_keys_present_in_existing? embedded_params && embedded_params.any? { |attr| attr.has_key?('id') } end def clear_existing! update_parent [] end def add_to_parent(record) parent.attributes[name] ||= [] parent.attributes[name] << record record end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
spyke-1.8.8 | lib/spyke/associations/has_many.rb |