activerecord-filter 6.0.0.3 → 6.0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d5c6eebcaf0ce67328ce60b589d3db745379c3e1f9e167a72b6d529c12dd242
4
- data.tar.gz: 12e0734ef2c953958cdbef7725e71273d7f6173abeb9e2155c81eb5096e93f87
3
+ metadata.gz: 86c7f7cf36ec41fc4dae21d1460151fcf2a32a9813f13a8b0acec56ebdccfbdd
4
+ data.tar.gz: 9993953d4a401d46e1bfd60102162794fd6f13d496d2fcd3352534fa0601ef90
5
5
  SHA512:
6
- metadata.gz: 4faa92743587408661292c04b9100929519354d194a583a0ba9fadb51e208d895ee0d79c2d188563479ce8dc51892d6678f039644ae41cccdafc53eae28c226d
7
- data.tar.gz: 15acb3d28304602f7c24b2ac966f37cd809a31bd36ba4d0fed0f2a8c8d3cdfebe16eb2ea536309d05a458146b31cde62b8fd9e6c6ee1ff7b82e924c44351ff41
6
+ metadata.gz: 69ad9b34f9b0511a0e34d76ef819b147a5705b9d0731fdca7fdcd3b876a90d1685321386033354653c39d30d3fb96283a4e4443e5a330bc405736e3342c24e06
7
+ data.tar.gz: f466d0b394b040109ba460a24a1811c8f4550bdeb9ca17904a4906ee1c2f011e59bf38481e074c29e08b4f6af4ea81360352bebb65fa45e49a285377d798f291
@@ -77,13 +77,13 @@ module ActiveRecord
77
77
  relations
78
78
  end
79
79
 
80
- def build_from_filter_hash(attributes)
80
+ def build_from_filter_hash(attributes, relation_trail, alias_tracker)
81
81
  if attributes.is_a?(Array)
82
- node = build_from_filter_hash(attributes.shift)
82
+ node = build_from_filter_hash(attributes.shift, relation_trail, alias_tracker)
83
83
 
84
84
  n = attributes.shift(2)
85
85
  while !n.empty?
86
- n[1] = build_from_filter_hash(n[1])
86
+ n[1] = build_from_filter_hash(n[1], relation_trail, alias_tracker)
87
87
  if n[0] == 'AND'
88
88
  if node.is_a?(Arel::Nodes::And)
89
89
  node.children.push(n[1])
@@ -100,26 +100,26 @@ module ActiveRecord
100
100
 
101
101
  node
102
102
  elsif attributes.is_a?(Hash)
103
- expand_from_filter_hash(attributes)
103
+ expand_from_filter_hash(attributes, relation_trail, alias_tracker)
104
104
  else
105
- expand_from_filter_hash({id: attributes})
105
+ expand_from_filter_hash({id: attributes}, relation_trail, alias_tracker)
106
106
  end
107
107
  end
108
108
 
109
- def expand_from_filter_hash(attributes)
109
+ def expand_from_filter_hash(attributes, relation_trail, alias_tracker)
110
110
  klass = table.send(:klass)
111
111
 
112
112
  children = attributes.flat_map do |key, value|
113
113
  if custom_filter = klass.filters[key]
114
- self.instance_exec(klass, table, key, value, &custom_filter[:block])
114
+ self.instance_exec(klass, table, key, value, relation_trail, alias_tracker, &custom_filter[:block])
115
115
  elsif column = klass.columns_hash[key.to_s] || klass.columns_hash[key.to_s.split('.').first]
116
- expand_filter_for_column(key, column, value)
116
+ expand_filter_for_column(key, column, value, relation_trail)
117
117
  elsif relation = klass.reflect_on_association(key)
118
- expand_filter_for_relationship(relation, value)
118
+ expand_filter_for_relationship(relation, value, relation_trail, alias_tracker)
119
119
  elsif key.to_s.ends_with?('_ids') && relation = klass.reflect_on_association(key.to_s.gsub(/_ids$/, 's'))
120
- expand_filter_for_relationship(relation, {id: value})
120
+ expand_filter_for_relationship(relation, {id: value}, relation_trail, alias_tracker)
121
121
  elsif relation = klass.reflect_on_all_associations(:has_and_belongs_to_many).find {|r| r.join_table == key.to_s && value.keys.first.to_s == r.association_foreign_key.to_s }
122
- expand_filter_for_join_table(relation, value)
122
+ expand_filter_for_join_table(relation, value, relation_trail, alias_tracker)
123
123
  else
124
124
  raise ActiveRecord::UnkownFilterError.new("Unkown filter \"#{key}\" for #{klass}.")
125
125
  end
@@ -133,21 +133,12 @@ module ActiveRecord
133
133
  end
134
134
  end
135
135
 
136
- def expand_filter_for_column(key, column, value)
137
- # Not sure why
138
- # activerecord/lib/active_record/table_metadata.rb#arel_attribute
139
- # doesn't work here, something's not working with a
140
- # Arel::Nodes::TableAlias, would like to go back to it one day
141
- attribute = if klass = table.send(:klass)
142
- if Arel::Nodes::TableAlias === table.send(:arel_table)
143
- klass.arel_attribute(column.name, table.send(:arel_table).left)
144
- else
145
- klass.arel_attribute(column.name, table.send(:arel_table))
146
- end
147
- else
148
- table.send(:arel_table)[column.name]
136
+ def expand_filter_for_column(key, column, value, relation_trail)
137
+ attribute = table.arel_attribute(column.name)
138
+ relation_trail.each do |rt|
139
+ attribute = Arel::Attributes::Relation.new(attribute, rt)
149
140
  end
150
-
141
+
151
142
  if column.type == :json || column.type == :jsonb
152
143
  names = key.to_s.split('.')
153
144
  names.shift
@@ -250,11 +241,11 @@ module ActiveRecord
250
241
  raise "Not Supported value for within: #{value.inspect}"
251
242
  end
252
243
  else
253
- raise "Not Supported: #{key.to_sym}"
244
+ raise "Not Supported: #{key.to_sym} on column \"#{column.name}\" of type #{column.type}"
254
245
  end
255
246
  end
256
247
 
257
- def expand_filter_for_relationship(relation, value)
248
+ def expand_filter_for_relationship(relation, value, relation_trail, alias_tracker)
258
249
  case relation.macro
259
250
  when :has_many
260
251
  if value == true || value == 'true'
@@ -279,16 +270,37 @@ module ActiveRecord
279
270
  return table.arel_attribute(relation.foreign_key).eq(nil)
280
271
  end
281
272
  end
282
-
283
- builder = associated_predicate_builder(relation.name.to_sym)
284
- builder.build_from_filter_hash(value)
273
+
274
+ builder = self.class.new(TableMetadata.new(
275
+ relation.klass,
276
+ alias_tracker.aliased_table_for(
277
+ relation.table_name,
278
+ relation.alias_candidate(table.send(:arel_table).name),
279
+ relation.klass.type_caster
280
+ ),
281
+ relation
282
+ ))
283
+ builder.build_from_filter_hash(value, relation_trail + [relation.name], alias_tracker)
285
284
  end
286
285
 
287
- def expand_filter_for_join_table(relation, value)
286
+
287
+ def expand_filter_for_join_table(relation, value, relation_trail, alias_tracker)
288
288
  relation = relation.active_record._reflections[relation.active_record._reflections[relation.name.to_s].send(:delegate_reflection).options[:through].to_s]
289
-
290
- builder = associated_predicate_builder(relation.name.to_sym)
291
- builder.build_from_filter_hash(value)
289
+ STDOUT.puts [
290
+ relation.table_name,
291
+ relation.alias_candidate(table.send(:arel_table).name)
292
+
293
+ ].inspect
294
+ builder = self.class.new(TableMetadata.new(
295
+ relation.klass,
296
+ alias_tracker.aliased_table_for(
297
+ relation.table_name,
298
+ relation.alias_candidate(table.send(:arel_table).name),
299
+ relation.klass.type_caster
300
+ ),
301
+ relation
302
+ ))
303
+ builder.build_from_filter_hash(value, relation_trail + [relation.name], alias_tracker)
292
304
  end
293
305
 
294
306
  end
@@ -303,14 +315,9 @@ module ActiveRecord
303
315
  @predicate_builder = predicate_builder
304
316
  end
305
317
 
306
- def build(filters)
318
+ def build(filters, alias_tracker)
307
319
  if filters.is_a?(Hash) || filters.is_a?(Array)
308
- # attributes = predicate_builder.resolve_column_aliases(filters)
309
- # attributes = klass.send(:expand_hash_conditions_for_aggregates, attributes)
310
- # attributes.stringify_keys!
311
- #
312
- # attributes, binds = predicate_builder.create_binds(attributes)
313
- parts = [predicate_builder.build_from_filter_hash(filters)]
320
+ parts = [predicate_builder.build_from_filter_hash(filters, [], alias_tracker)]
314
321
  else
315
322
  raise ArgumentError, "Unsupported argument type: #{filters.inspect} (#{filters.class})"
316
323
  end
@@ -371,13 +378,14 @@ class ActiveRecord::Relation
371
378
 
372
379
  def build_arel(aliases)
373
380
  arel = super
374
- build_filters(arel)
381
+ my_alias_tracker = ActiveRecord::Associations::AliasTracker.create(connection, table.name, [])
382
+ build_filters(arel, my_alias_tracker)
375
383
  arel
376
384
  end
377
385
 
378
- def build_filters(manager)
386
+ def build_filters(manager, aliases)
379
387
  @filters.each do |filters|
380
- manager.where(filter_clause_factory.build(filters).ast)
388
+ manager.where(filter_clause_factory.build(filters, alias_tracker).ast)
381
389
  end
382
390
  end
383
391
 
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Filter
3
- VERSION = '6.0.0.3'
3
+ VERSION = '6.0.0.4'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-filter
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.0.3
4
+ version: 6.0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Bracy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-16 00:00:00.000000000 Z
11
+ date: 2019-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 6.0.0
33
+ version: 6.0.0.5
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 6.0.0
40
+ version: 6.0.0.5
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: pg
43
43
  requirement: !ruby/object:Gem::Requirement