effective_datatables 2.1.3 → 2.1.4

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: 8936f9d6cfd2f9ca5a02c280c4f388526429dc15
4
- data.tar.gz: 35401b2a9625a6f6160401d9c6efb311f29d2321
3
+ metadata.gz: 6595ee6f3ff2e56ffd6066b8d649619e919b4837
4
+ data.tar.gz: 7d0b22801aaf7098bced2efd34a353ecb63fc41e
5
5
  SHA512:
6
- metadata.gz: e10681215022737bd36e39088b505365873a0ea63f3a419771579f7832a6b81fde1edc81f8601db1af7dce940c01a6b12b196f90eac880fbc730e4d6d1d5fbdb
7
- data.tar.gz: d243ff06c93761d84146fe39263db11ac0bd3665664da926d676e723541b7d153365ba59106c6ea1feafcf56d271b00bddb971c7e0c4b9a81c1f1a4a9be8495a
6
+ metadata.gz: a37dd4357ac17b28bf4fb5d605db9a5cd9c37318f35ddfbcce467775568750a066b7fba9aec123ec9674ebd6c4a0ad597cbcab96c751e659b13e71bb28ce12a3
7
+ data.tar.gz: e2f105f6c9dbfa8bb6acca5dc53c9e3220fb763ac290c8e8c1c9dd6e1aef79b907b386e1f289e4d999da2607cc37a409d65c051111fd5268060618f95fb5c084
data/README.md CHANGED
@@ -340,6 +340,10 @@ table_column :created_at, :filter => {...} # Enable filtering with these opti
340
340
  :filter => {:type => :select, :values => [*2010..(Time.zone.now.year+6)]}
341
341
  :filter => {:type => :select, :values => Proc.new { PostCategory.all } }
342
342
  :filter => {:type => :select, :values => Proc.new { User.all.order(:email).map { |obj| [obj.id, obj.email] } } }
343
+
344
+ :filter => {:type => :grouped_select, :values => {'Active' => Events.active, 'Past' => Events.past }}
345
+ :filter => {:type => :grouped_select, :values => {'Active' => [['Event A', 1], ['Event B', 2]], 'Past' => [['Event C', 3], ['Event D', 4]]} }
346
+
343
347
  ```
344
348
 
345
349
  Some additional, lesser used options include:
@@ -40,7 +40,7 @@ module EffectiveDatatablesHelper
40
40
  opts[:filter][:values] = opts[:filter][:values].call()
41
41
 
42
42
  if opts[:filter][:values].kind_of?(ActiveRecord::Relation) || (opts[:filter][:values].kind_of?(Array) && opts[:filter][:values].first.kind_of?(ActiveRecord::Base))
43
- opts[:filter][:values] = opts[:filter][:values].map { |obj| [obj.to_s, obj.id] }
43
+ opts[:filter][:values] = opts[:filter][:values].map { |obj| [obj.to_s, obj.to_param] }
44
44
  end
45
45
  end
46
46
 
@@ -51,6 +51,29 @@ module EffectiveDatatablesHelper
51
51
  include_blank: (opts[:label] || name.titleize),
52
52
  input_html: { name: nil, autocomplete: 'off', data: {'column-name' => opts[:name], 'column-index' => opts[:index]} },
53
53
  input_js: { placeholder: (opts[:label] || name.titleize) }
54
+ when :grouped_select
55
+ raise "Expected :group_select filter to define its values as a Hash {'Posts' => Post.all, 'Events' => Event.all} or a Hash {'Posts' => [['Post A', 1], ['Post B', 2]], 'Events' => [['Event A', 1], ['Event B', 2]]}" unless opts[:filter][:values].kind_of?(Hash)
56
+
57
+ opts[:filter][:values].each do |group, options|
58
+ if options.kind_of?(ActiveRecord::Relation)
59
+ if opts[:type] == :belongs_to_polymorphic
60
+ opts[:filter][:values][group] = options.map { |obj| [obj.to_s, "#{options.model_name}_#{obj.to_param}"] }
61
+ else
62
+ opts[:filter][:values][group] = options.map { |obj| [obj.to_s, obj.to_param] }
63
+ end
64
+ end
65
+ end
66
+
67
+ form.input name, label: false, required: false,
68
+ as: :grouped_select,
69
+ collection: opts[:filter][:values],
70
+ multiple: opts[:filter][:multiple] == true,
71
+ include_blank: (opts[:label] || name.titleize),
72
+ grouped: true,
73
+ group_label_method: :first,
74
+ group_method: :last,
75
+ input_html: { name: nil, autocomplete: 'off', data: {'column-name' => opts[:name], 'column-index' => opts[:index]} },
76
+ input_js: { placeholder: (opts[:label] || name.titleize) }
54
77
  else
55
78
  content_tag(:p, opts[:label] || name)
56
79
  end
@@ -47,6 +47,15 @@ module Effective
47
47
  else
48
48
  collection.public_send(sql_op, "#{column} ILIKE :term", term: "%#{term}%")
49
49
  end
50
+ when :belongs_to_polymorphic
51
+ # our key will be something like Post_15, or Event_1
52
+ (type, id) = term.split('_')
53
+
54
+ if type.present? && id.present?
55
+ collection.public_send(sql_op, "#{column} = :id AND #{column.sub('_id"', '_type"')} = :type", id: id, type: type)
56
+ else
57
+ collection
58
+ end
50
59
  when :has_many
51
60
  inverse_ids = term.split(',').map { |term| (term = term.to_i) == 0 ? nil : term }.compact
52
61
  return collection unless inverse_ids.present?
@@ -16,10 +16,12 @@ module Effective
16
16
  # Here we identify all belongs_to associations and build up a Hash like:
17
17
  # {user: {foreign_key: 'user_id', klass: User}, order: {foreign_key: 'order_id', klass: Effective::Order}}
18
18
  belong_tos = (collection.klass.reflect_on_all_associations(:belongs_to) rescue []).inject({}) do |retval, bt|
19
- next if bt.options[:polymorphic]
20
-
21
- klass = bt.klass || (bt.foreign_type.sub('_type', '').classify.constantize rescue nil)
22
- retval[bt.name.to_s] = {foreign_key: bt.foreign_key, klass: klass} if bt.foreign_key.present? && klass.present?
19
+ if bt.options[:polymorphic]
20
+ retval[bt.name.to_s] = {foreign_key: bt.foreign_key, klass: bt.name, polymorphic: true}
21
+ else
22
+ klass = bt.klass || (bt.foreign_type.sub('_type', '').classify.constantize rescue nil)
23
+ retval[bt.name.to_s] = {foreign_key: bt.foreign_key, klass: klass} if bt.foreign_key.present? && klass.present?
24
+ end
23
25
 
24
26
  retval
25
27
  end
@@ -55,7 +57,11 @@ module Effective
55
57
 
56
58
  cols[name][:type] ||= (
57
59
  if belong_tos.key?(name)
58
- :belongs_to
60
+ if belong_tos[name][:polymorphic]
61
+ :belongs_to_polymorphic
62
+ else
63
+ :belongs_to
64
+ end
59
65
  elsif has_manys.key?(name)
60
66
  :has_many
61
67
  elsif sql_column.try(:type).present?
@@ -67,8 +73,8 @@ module Effective
67
73
 
68
74
  cols[name][:class] = "col-#{cols[name][:type]} col-#{name} #{cols[name][:class]}".strip
69
75
 
70
- # HasMany
71
- if cols[name][:type] == :has_many
76
+ # We can't easily sort these fields
77
+ if cols[name][:type] == :has_many || cols[name][:type] == :belongs_to_polymorphic
72
78
  cols[name][:sortable] = false
73
79
  end
74
80
 
@@ -118,7 +124,7 @@ module Effective
118
124
  filter[:selected] = filter[:selected].to_s unless filter[:selected].nil?
119
125
 
120
126
  # If you pass values, just assume it's a select
121
- filter[:type] ||= :select if filter.key?(:values)
127
+ filter[:type] ||= :select if filter.key?(:values) && col_type != :belongs_to_polymorphic
122
128
 
123
129
  # Check if this is an aggregate column
124
130
  if ['SUM(', 'COUNT(', 'MAX(', 'MIN(', 'AVG('].any? { |str| sql_column.include?(str) }
@@ -137,6 +143,8 @@ module Effective
137
143
  end
138
144
  )
139
145
  }
146
+ when :belongs_to_polymorphic
147
+ {type: :grouped_select, values: {}}
140
148
  when :has_many
141
149
  {
142
150
  type: :select,
@@ -113,6 +113,8 @@ module Effective
113
113
  view.instance_exec(obj, collection, self, &opts[:proc])
114
114
  elsif opts[:type] == :belongs_to
115
115
  (obj.send(name) rescue nil).to_s
116
+ elsif opts[:type] == :belongs_to_polymorphic
117
+ (obj.send(name) rescue nil).to_s
116
118
  elsif opts[:type] == :has_many
117
119
  objs = (obj.send(name).map { |x| x.to_s }.sort rescue [])
118
120
  objs.length == 1 ? objs.first : (opts[:sentence] ? objs.to_sentence : objs.join('<br>'))
@@ -1,3 +1,3 @@
1
1
  module EffectiveDatatables
2
- VERSION = '2.1.3'.freeze
2
+ VERSION = '2.1.4'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_datatables
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.3
4
+ version: 2.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-03 00:00:00.000000000 Z
11
+ date: 2015-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails