data_tables-responder 0.2.1 → 0.2.2

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
  SHA1:
3
- metadata.gz: 499aade7583b2148751f39d6004c1b3bea164620
4
- data.tar.gz: 502c2d8e2aa857390f3e55c8139204d375670032
3
+ metadata.gz: 0798fa16c1404198a97647a9bfc2f4514f3a21e4
4
+ data.tar.gz: 04e1541db4c718874ecbf131613569b34ebadf30
5
5
  SHA512:
6
- metadata.gz: 71c654d1f58279888be4541f919e4ce8b51f1c7da354b4d4b9a04ee5eacb6be6844cd87e6857711e7b8c9a7341ba94f0cc4910afdcd57e428f40897a8878117b
7
- data.tar.gz: d938cf9b87575b5d2a18dbf0dacb315e1fa7e286bf1f5cc37bbb228bbe1e0f7da89f59d653d730bd0f5d87ec3da49878b367dfeb506946558aacb449353a30df
6
+ metadata.gz: 030493b51f5b002e10b36561b9ec9dbd900c457ab3159f000c19091caccd757cc506e79901d61d321f546422e40eb160b24ea0c0ff00a28fc61e76538e049957
7
+ data.tar.gz: 08eb69d5a37e099868c9869f2a66e1959cf3fd02fa19f86b94a3e05bd5ca2dcba472272456a980d27c5459fcefa733bb4f70bb172d5983e493f0090fa9052652
@@ -26,35 +26,39 @@ module DataTables
26
26
 
27
27
  searches = DataTables.flat_keys_to_nested columns
28
28
 
29
- or_clause = nil
30
- search_by = searches.collect do |k, query|
31
- if query.is_a? Hash
32
- assoc = model.reflect_on_association(k)
33
- assoc_klass = assoc.klass
34
- assoc_arel_table = assoc_klass.arel_table
29
+ search_by = build_search(model, searches)
35
30
 
36
- if model.respond_to? :left_outer_join
37
- @collection = @collection.left_outer_join(k)
38
- else
39
- @collection = @collection.includes(k).references(k)
40
- end
31
+ @collection.where(search_by.reduce(:or))
32
+ end
41
33
 
42
- assoc_arel_table[query.first.first].matches(query.first.last)
34
+ def build_search(model, searches)
35
+ queries = []
36
+ searches.inject(queries) do |query, junk|
37
+ column, search = junk
38
+ case search
39
+ when Hash
40
+ assoc = model.reflect_on_association(column)
41
+ assoc_klass = assoc.klass
42
+
43
+ outer_join = Arel::Nodes::OuterJoin.new(assoc_klass.arel_table,
44
+ Arel::Nodes::On.new(
45
+ model.arel_table[assoc.active_record_primary_key].eq(assoc_klass.arel_table[assoc.foreign_key])
46
+ ))
47
+ @collection.joins(outer_join)
48
+ query << build_search(assoc_klass, search).reduce(:or)
43
49
  else
44
- if (column = model.columns.find { |c| c.name == k.to_s })
45
- case column.type
46
- when :string
47
- model.arel_table[k].matches("%#{query}%")
48
- when :integer
49
- model.arel_table[k].eq(query)
50
- else
51
- nil
52
- end
50
+ col_s = column.to_s
51
+ case (k = model.columns.find(nil) { |c| c.name == col_s })&.type
52
+ when :string
53
+ query << model.arel_table[k.name].matches("%#{search}%")
54
+ when :integer
55
+ query << model.arel_table[k.name].eq(search)
56
+ else
57
+ query
53
58
  end
54
59
  end
55
- end.compact
56
-
57
- @collection.where(search_by.reduce(:or))
60
+ end
61
+ queries
58
62
  end
59
63
 
60
64
  protected
@@ -1,5 +1,5 @@
1
1
  module DataTables
2
2
  module Responder
3
- VERSION = '0.2.1'
3
+ VERSION = '0.2.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_tables-responder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dale Stevens
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-06 00:00:00.000000000 Z
11
+ date: 2017-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers