effective_datatables 2.3.1 → 2.3.2

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: 008015d3e0eee31f97cee6a004c806fb52fbb2d5
4
- data.tar.gz: 3cd65ca8bd24f4dff5fcda583fddce94a3e64544
3
+ metadata.gz: f1a04998ee9c940102f9df4a951d72bce9e4df41
4
+ data.tar.gz: 8344057c6f04c15bdf157b1e712634a3ffcaf73d
5
5
  SHA512:
6
- metadata.gz: 424ebcf833f830200a85057ca79e83b1ee6f43892cb91a1fcc688e9877ea01b231499097a05083183ee9677adf18697da0ac3e851982999c2e6166396de21471
7
- data.tar.gz: 6a1c85b2c91d434a5ec3fb83b3c9f39ca976d5684612b01be1981c58919ae03aed7acd7fbee05b312095d6724a70efa505901aff25e2bb95549c5615afea2888
6
+ metadata.gz: 49182f2222e7299401a32bbc36ed197fe3e4e3ed08d3811f4b0136f3e99216172ae6240bde97b255ed9aec62b1702c785494da8f69a610f834da6699a432d951
7
+ data.tar.gz: 827c9fdd9c2ed56210216634e480e1a857283a67d59005dda710ac59fb3db237ca9e4a718dbfa1553583cc80d3fbb83fded0aaaae24611796ade1b1f62e6af84
@@ -100,6 +100,38 @@ module Effective
100
100
  klass.where(conditions.join(' OR '), term: "%#{term}%", num: term.to_i).joins(inverse.name).pluck(inverse.foreign_key)
101
101
  end
102
102
 
103
+ collection.public_send(sql_op, id: ids)
104
+
105
+ when :has_and_belongs_to_many
106
+ reflection = collection.klass.reflect_on_association(table_column[:name].to_sym)
107
+ raise "unable to find #{collection.klass.name} :has_and_belongs_to_many :#{table_column[:name]} association" unless reflection
108
+
109
+ obj = reflection.build_association({})
110
+ klass = obj.class
111
+
112
+ inverse = reflection.inverse_of || klass.reflect_on_association(collection.table_name) || obj.class.reflect_on_association(collection.table_name.singularize)
113
+ raise "unable to find #{klass.name} has_and_belongs_to_many :#{collection.table_name} or belongs_to :#{collection.table_name.singularize} associations" unless inverse
114
+
115
+ ids = if [:select, :grouped_select].include?(table_column[:filter][:type])
116
+ # Treat the search term as one or more IDs
117
+ inverse_ids = term.split(',').map { |term| (term = term.to_i) == 0 ? nil : term }.compact
118
+ return collection unless inverse_ids.present?
119
+
120
+ klass.where(id: inverse_ids).flat_map { |klass| (klass.send(inverse.name).pluck(:id) rescue []) }
121
+ else
122
+ # Treat the search term as a string.
123
+
124
+ klass_columns = if (table_column[:column] == klass.table_name) # No custom column has been defined
125
+ klass.columns.map { |col| col.name if col.text? }.compact # Search all database text? columns
126
+ else
127
+ [table_column[:column].gsub("#{klass.table_name}.", '')] # table_column :order_items, column: 'order_items.title'
128
+ end
129
+
130
+ conditions = klass_columns.map { |col_name| "#{klass.table_name}.#{col_name} #{ilike} :term" }
131
+
132
+ klass.where(conditions.join(' OR '), term: "%#{term}%", num: term.to_i).flat_map { |klass| (klass.send(inverse.name).pluck(:id) rescue []) }
133
+ end
134
+
103
135
  collection.public_send(sql_op, id: ids)
104
136
  when :obfuscated_id
105
137
  if (deobfuscated_id = collection.deobfuscate(term)) == term # We weren't able to deobfuscate it, so this is an Invalid ID
@@ -30,11 +30,18 @@ module Effective
30
30
  retval
31
31
  end
32
32
 
33
- # has_manys
34
- has_manys = (collection.klass.reflect_on_all_associations(:has_many) rescue []).inject({}) do |retval, hm|
35
- klass = hm.klass || (hm.build_association({}).class)
36
- retval[hm.name.to_s] = {klass: klass}
37
- retval
33
+ # Figure out has_manys and has_many_belongs_to_many's
34
+ has_manys = {}
35
+ has_and_belongs_to_manys = {}
36
+
37
+ (collection.klass.reflect_on_all_associations() rescue []).each do |reflect|
38
+ if reflect.macro == :has_many
39
+ klass = reflect.klass || (reflect.build_association({}).class)
40
+ has_manys[reflect.name.to_s] = { klass: klass }
41
+ elsif reflect.macro == :has_and_belongs_to_many
42
+ klass = reflect.klass || (reflect.build_association({}).class)
43
+ has_and_belongs_to_manys[reflect.name.to_s] = { klass: klass }
44
+ end
38
45
  end
39
46
 
40
47
  table_columns = cols.each_with_index do |(name, _), index|
@@ -68,6 +75,8 @@ module Effective
68
75
  end
69
76
  elsif has_manys.key?(name)
70
77
  :has_many
78
+ elsif has_and_belongs_to_manys.key?(name)
79
+ :has_and_belongs_to_many
71
80
  elsif cols[name][:bulk_actions_column]
72
81
  :bulk_actions_column
73
82
  elsif name.include?('_address') && (collection_class.new rescue nil).respond_to?(:effective_addresses)
@@ -105,7 +114,7 @@ module Effective
105
114
  cols[name][:sql_as_column] = true
106
115
  end
107
116
 
108
- cols[name][:filter] = initialize_table_column_filter(cols[name], belong_tos[name], has_manys[name])
117
+ cols[name][:filter] = initialize_table_column_filter(cols[name], belong_tos[name], has_manys[name], has_and_belongs_to_manys[name])
109
118
 
110
119
  if cols[name][:partial]
111
120
  cols[name][:partial_local] ||= (sql_table.try(:name) || cols[name][:partial].split('/').last(2).first.presence || 'obj').singularize.to_sym
@@ -127,7 +136,7 @@ module Effective
127
136
  end
128
137
  end
129
138
 
130
- def initialize_table_column_filter(column, belongs_to, has_many)
139
+ def initialize_table_column_filter(column, belongs_to, has_many, has_and_belongs_to_manys)
131
140
  filter = column[:filter]
132
141
  col_type = column[:type]
133
142
  sql_column = column[:column].to_s.upcase
@@ -181,6 +190,18 @@ module Effective
181
190
  end
182
191
  )
183
192
  }
193
+ when :has_and_belongs_to_many
194
+ {
195
+ type: :select,
196
+ multiple: true,
197
+ values: (
198
+ if has_and_belongs_to_manys[:klass].respond_to?(:datatables_filter)
199
+ Proc.new { has_and_belongs_to_manys[:klass].datatables_filter }
200
+ else
201
+ Proc.new { has_and_belongs_to_manys[:klass].all.map { |obj| [obj.to_s, obj.id] }.sort { |x, y| x[0] <=> y[0] } }
202
+ end
203
+ )
204
+ }
184
205
  when :effective_address
185
206
  {type: :string}
186
207
  when :effective_roles
@@ -119,7 +119,9 @@ module Effective
119
119
  elsif opts[:type] == :belongs_to_polymorphic
120
120
  (obj.send(name) rescue nil)
121
121
  elsif opts[:type] == :has_many
122
- (obj.send(name).to_a rescue [])
122
+ (obj.send(name).map { |obj| obj.to_s }.join('<br>') rescue BLANK)
123
+ elsif opts[:type] == :has_and_belongs_to_many
124
+ (obj.send(name).map { |obj| obj.to_s }.join('<br>') rescue BLANK)
123
125
  elsif opts[:type] == :bulk_actions_column
124
126
  BLANK
125
127
  elsif opts[:type] == :obfuscated_id
@@ -1,3 +1,3 @@
1
1
  module EffectiveDatatables
2
- VERSION = '2.3.1'.freeze
2
+ VERSION = '2.3.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_datatables
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.1
4
+ version: 2.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect