graphql-activerecord 0.5.4 → 0.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bcecb2063effc41338f80d629d054c2c0e6c6881
4
- data.tar.gz: 8d7d98648bbf801ef9d9ed4a88fde65c0728e22f
3
+ metadata.gz: 8e580775093b61f44e8c119e6cdba9c551877841
4
+ data.tar.gz: 6d8dfe863f25e8345e1724fcdf636c8d3237712b
5
5
  SHA512:
6
- metadata.gz: e0f38f3e7fe6c173fbc2980b3117f9344232ca68bdd4f48758d06c337d482232a5019934ce75ae2eb38c7671ca275776c902ac34f22d78857a30db4119dde8b3
7
- data.tar.gz: 86731a8685dc5a008cfbe03f7a898f99561cda12dc31614222c87cf823749e688dfe0c83c24351e1da42ca8fde92ce59f78dcd4a0e0f29b5fe3572b7553b9c66
6
+ metadata.gz: 20f0318b73f6a2ac455cbe6e00af3560920b2cbc68886d93ae4cf370dc10d6709388399cd63ec64858082b59f26aaae5011a0b9322c94c4d198b5d14f3e361ef
7
+ data.tar.gz: 49d36558ff541fdf34b060e627d67bd9af2173c8e8597d4784477801819e70b0e8b5a1c492a3850da6e3d9e5bc36f518746661679cbe61235729ac05a4d82e0b
@@ -85,14 +85,14 @@ module GraphQL::Models
85
85
  find_by = Array.wrap(child_map.find_by).map(&:to_s)
86
86
 
87
87
  if find_by.empty?
88
- return match_inputs_by_position(model, child_map, next_inputs, changes, associated_models, find_by)
88
+ return match_inputs_by_position(model, child_map, next_inputs, changes, associated_models)
89
89
  else
90
90
  return match_inputs_by_fields(model, child_map, next_inputs, changes, associated_models, find_by)
91
91
  end
92
92
  end
93
93
  end
94
94
 
95
- def self.match_inputs_by_position(model, child_map, next_inputs, changes, associated_models, find_by)
95
+ def self.match_inputs_by_position(model, child_map, next_inputs, changes, associated_models)
96
96
  count = [associated_models.length, next_inputs.length].max
97
97
 
98
98
  matches = []
@@ -118,20 +118,30 @@ module GraphQL::Models
118
118
  end
119
119
 
120
120
  def self.match_inputs_by_fields(model, child_map, next_inputs, changes, associated_models, find_by)
121
- grouped_models = associated_models.group_by { |m| m.attributes.slice(*find_by) }
122
- grouped_inputs = next_inputs.group_by { |ni| ni.to_h.slice(*find_by) }
121
+ # Convert the find_by into the field definitions, so that we properly unmap aliased fields
122
+ find_by_defs = find_by.map { |name| child_map.fields.detect { |f| f[:attribute].to_s == name.to_s } }
123
+ name_to_attr = find_by_defs.map { |f| [f[:name], f[:attribute].to_s] }.to_h
124
+
125
+ indexed_models = associated_models.index_by { |m| m.attributes.slice(*find_by) }
126
+
127
+ # Inputs are a little nasty, the keys have to be converted from camelCase back to snake_case
128
+ indexed_inputs = next_inputs.index_by { |ni| ni.to_h.slice(*name_to_attr.keys) }
129
+
130
+ indexed_inputs = indexed_inputs.map do |key, inputs|
131
+ key = key.map { |name, val| [name_to_attr[name], val] }.to_h
132
+ [key, inputs]
133
+ end
134
+
135
+ indexed_inputs = indexed_inputs.to_h
123
136
 
124
137
  # Match each model to its input. If there is no input for it, mark that the model should be destroyed.
125
138
  matches = []
126
139
 
127
140
  # TODO: Support for finding by an ID field, that needs to be untranslated from a Relay ID into a model ID
128
141
 
129
- grouped_models.each do |key_attrs, vals|
130
- child_model = vals[0]
142
+ indexed_models.each do |key_attrs, child_model|
143
+ inputs = indexed_inputs[key_attrs]
131
144
 
132
- inputs = grouped_inputs[key_attrs]
133
- inputs = inputs[0] if inputs
134
-
135
145
  if inputs.nil?
136
146
  child_model.mark_for_destruction
137
147
  changes.push({ model_instance: child_model, action: :destroy })
@@ -141,10 +151,9 @@ module GraphQL::Models
141
151
  end
142
152
 
143
153
  # Build a new model for each input that doesn't have a model
144
- grouped_inputs.each do |key_attrs, vals|
145
- inputs = vals[0]
154
+ indexed_inputs.each do |key_attrs, inputs|
155
+ next if indexed_models.include?(key_attrs)
146
156
 
147
- next if grouped_models.include?(key_attrs)
148
157
  child_model = associated_models.build
149
158
  changes.push({ model_instance: child_model, action: :create })
150
159
  matches.push({ child_model: child_model, child_inputs: inputs, input_path: next_inputs.index(inputs) })
@@ -1,5 +1,5 @@
1
1
  module GraphQL
2
2
  module Models
3
- VERSION = "0.5.4"
3
+ VERSION = "0.5.5"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Foster
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-18 00:00:00.000000000 Z
11
+ date: 2016-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql