effective_datatables 4.7.5 → 4.7.6

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
  SHA256:
3
- metadata.gz: 48cac2f10a4ebc72966c0dfffbb2cfa813743d2bcc37ca2571e86252241d66ec
4
- data.tar.gz: d7baf2ef61b1008ee0265c65bb3acb878c8da0d4031ea12cb4a27b000f54ef08
3
+ metadata.gz: 9a83b549ce682f713931db6fb31f1432b9ccfca4d8e24ba43c56e0dcf8ff117f
4
+ data.tar.gz: 53709d9a9d9e7dd7bf8f6b8025088976090a8a3571f264e08e03da534b28119f
5
5
  SHA512:
6
- metadata.gz: 77eeca0bd8b7908e0fa6a893cb4cd96ab84c16bd235f520829c862104540861381af977ea017fd926608ea06620f43e935fd7af53a565321a7e00f52b66db54b
7
- data.tar.gz: 42d1909524c35fc87fdd8495c5c28b84f93f8259b008a66d0a1037d1fc13f30e2efe86776f5fb5a35b2985b2bc32febdf14347e84abf9deed186810c19c16ea5
6
+ metadata.gz: 0ebb22d41947d8e8fe4a8deebc9472f442617e4ace66e8cce6312e7f1e6de714e169b05a2146d823a568644b73a44871a82c7ab3faea72ca42fb1fb46d7c0ffb
7
+ data.tar.gz: ac2d99e28b03a04f60cd583c2a0c657380d505f53490be7a9a0e39d05d7005c6a0c2cb3f6e92723a5f126a22454834453a7581f0fbb124e9a4be56ed2436c038
@@ -38,7 +38,6 @@ $(document).on 'ajax:success', '.dataTables_wrapper .col-actions', (event, data)
38
38
  EffectiveForm.remote_form_payload = ''
39
39
  EffectiveForm.remote_form_commit = ''
40
40
  EffectiveForm.remote_form_flash = ''
41
- EffectiveForm.remote_form_refresh_datatables = ''
42
41
 
43
42
  true
44
43
 
@@ -53,7 +52,6 @@ $(document).on 'ajax:error', '.dataTables_wrapper', (event) ->
53
52
  EffectiveForm.remote_form_payload = ''
54
53
  EffectiveForm.remote_form_commit = ''
55
54
  EffectiveForm.remote_form_flash = ''
56
- EffectiveForm.remote_form_refresh_datatables = ''
57
55
  true
58
56
 
59
57
  # Submitting an inline datatables form
@@ -84,7 +82,6 @@ $(document).on 'effective-form:success', '.dataTables_wrapper .col-inline-form',
84
82
  $tr.fadeOut('slow')
85
83
 
86
84
  $table.DataTable().draw()
87
- refreshDatatables($table)
88
85
 
89
86
  beforeNew = ($action) ->
90
87
  $table = $action.closest('table')
@@ -150,9 +147,7 @@ afterAction = ($action) ->
150
147
  else
151
148
  $table.DataTable().flash('Successfully ' + $action.attr('title'), 'success')
152
149
 
153
- unless redirectDatatables($table)
154
- $table.DataTable().draw()
155
- refreshDatatables($table)
150
+ $table.DataTable().draw()
156
151
 
157
152
  afterError = ($action) ->
158
153
  $table = $action.closest('table')
@@ -186,28 +181,6 @@ cancel = ($table) ->
186
181
  if $wrapper.find('.effective-datatables-inline-row').length == 0
187
182
  $wrapper.removeClass('effective-datatables-inline-expanded')
188
183
 
189
- redirectDatatables = ($source) ->
190
- return false unless EffectiveForm.remote_form_refresh_datatables.length > 0
191
-
192
- if EffectiveForm.remote_form_refresh_datatables.includes('refresh')
193
- if Turbolinks?
194
- Turbolinks.visit(window.location.href, { action: 'replace'})
195
- else
196
- window.location.reload()
197
-
198
- return true
199
-
200
- false
201
-
202
- refreshDatatables = ($source) ->
203
- return unless EffectiveForm.remote_form_refresh_datatables.length > 0
204
-
205
- $('table.dataTable.initialized').each ->
206
- $table = $(this)
207
-
208
- if EffectiveForm.remote_form_refresh_datatables.find((id) -> $table.attr('id').startsWith(id) || id == 'all')
209
- $table.DataTable().draw() if $table != $source
210
-
211
184
  # Cancel button clicked. Blow away new tr, or restore edit tr
212
185
  # No data will have changed at this point
213
186
  $(document).on 'click', ".dataTables_wrapper a[data-role='inline-form-cancel']", (event) ->
@@ -39,10 +39,24 @@ module EffectiveDatatablesPrivateHelper
39
39
  end
40
40
 
41
41
  def datatable_new_resource_button(datatable, name, column)
42
- return unless column[:inline] && (column[:actions][:new] != false) && (datatable.resource.actions.include?(:new) rescue false)
42
+ return unless column[:inline] && (column[:actions][:new] != false)
43
43
 
44
- actions = {t('effective_datatables.new') => { action: :new, class: ['btn', column[:btn_class].presence].compact.join(' '), 'data-remote': true } }
45
- render_resource_actions(datatable.resource.klass, actions: actions, effective_resource: datatable.resource) # Will only work if permitted
44
+ action = { action: :new, class: ['btn', column[:btn_class].presence].compact.join(' '), 'data-remote': true }
45
+
46
+ if column[:actions][:new].kind_of?(Hash) # This might be active_record_array_collection?
47
+ action = action.merge(column[:actions][:new])
48
+
49
+ effective_resource = (datatable.effective_resource || datatable.fallback_effective_resource)
50
+ klass = (column[:actions][:new][:klass] || effective_resource&.klass || datatable.collection_class)
51
+ elsif Array(datatable.effective_resource&.actions).include?(:new)
52
+ effective_resource = datatable.effective_resource
53
+ klass = effective_resource.klass
54
+ else
55
+ return
56
+ end
57
+
58
+ # Will only work if permitted
59
+ render_resource_actions(klass, actions: { t('effective_datatables.new') => action }, effective_resource: effective_resource)
46
60
  end
47
61
 
48
62
  def datatable_label_tag(datatable, name, opts)
@@ -1,7 +1,7 @@
1
1
  module Effective
2
2
  class Datatable
3
3
  attr_reader :attributes # Anything that we initialize our table with. That's it. Can't be changed by state.
4
- attr_reader :resource
4
+ attr_reader :effective_resource
5
5
  attr_reader :state
6
6
 
7
7
  # Hashes of DSL options
@@ -64,7 +64,6 @@ module Effective
64
64
  # This means filters do know about attributes but not about columns.
65
65
  initialize_filters if respond_to?(:initialize_filters)
66
66
  load_filters!
67
-
68
67
  load_state!
69
68
 
70
69
  # Bulk actions called first so it can add the bulk_actions_col first
@@ -83,8 +82,6 @@ module Effective
83
82
 
84
83
  # Figure out the class, and if it's activerecord, do all the resource discovery on it
85
84
  load_resource!
86
- apply_belongs_to_attributes!
87
- load_resource_search!
88
85
 
89
86
  # Check everything is okay
90
87
  validate_datatable!
@@ -165,6 +162,14 @@ module Effective
165
162
  @dsl_tool ||= DatatableDslTool.new(self)
166
163
  end
167
164
 
165
+ def resource
166
+ raise('depecated. Please use .effective_resource instead')
167
+ end
168
+
169
+ def fallback_effective_resource
170
+ @fallback_effective_resource ||= Effective::Resource.new('', namespace: controller_namespace)
171
+ end
172
+
168
173
  private
169
174
 
170
175
  def column_tool
@@ -177,8 +182,8 @@ module Effective
177
182
 
178
183
  def validate_datatable!
179
184
  if reorder?
180
- raise 'cannot use reorder with an Array collection' if array_collection?
181
- raise 'cannot use reorder with a non-Integer column' if resource.sql_type(columns[:_reorder][:reorder]) != :integer
185
+ raise 'cannot use reorder with an Array collection' unless active_record_collection?
186
+ raise 'cannot use reorder with a non-Integer column' if effective_resource.sql_type(columns[:_reorder][:reorder]) != :integer
182
187
  end
183
188
  end
184
189
 
@@ -43,7 +43,7 @@ module Effective
43
43
  Rails.logger.info "COLUMN TOOL: order_column #{column.to_s} #{direction} #{sql_column}" if EffectiveDatatables.debug
44
44
 
45
45
  if column[:sql_as_column]
46
- collection.order("#{sql_column} #{datatable.resource.sql_direction(direction)}")
46
+ collection.order("#{sql_column} #{datatable.effective_resource.sql_direction(direction)}")
47
47
  else
48
48
  Effective::Resource.new(collection)
49
49
  .order(column[:name], direction, as: column[:as], sort: column[:sort], sql_column: column[:sql_column], limit: datatable.limit)
@@ -77,7 +77,6 @@ module Effective
77
77
  def search_column(collection, value, column, index)
78
78
  Rails.logger.info "VALUE TOOL: search_column #{column.to_s} #{value} #{index}" if EffectiveDatatables.debug
79
79
 
80
- macros = Effective::Resource.new('').macros
81
80
  fuzzy = column[:search][:fuzzy]
82
81
 
83
82
  term = Effective::Attribute.new(column[:as]).parse(value, name: column[:name])
@@ -134,7 +133,7 @@ module Effective
134
133
  else
135
134
  obj == term
136
135
  end
137
- when *macros, :resource
136
+ when *datatable.association_macros, :resource
138
137
  Array(obj).any? do |resource|
139
138
  Array(term).any? do |term|
140
139
  matched = false
@@ -7,10 +7,23 @@ module Effective
7
7
  @collection_class # Will be either User/Post/etc or Array
8
8
  end
9
9
 
10
+ # User.all
10
11
  def active_record_collection?
11
12
  @active_record_collection == true
12
13
  end
13
14
 
15
+ # [User<1>, User<2>, Post<1>, Page<3>]
16
+ def active_record_array_collection?
17
+ @active_record_array_collection == true
18
+ end
19
+
20
+ def active_record_polymorphic_array_collection?
21
+ return false unless active_record_array_collection?
22
+ return @active_record_polymorphic_array_collection unless @active_record_polymorphic_array_collection.nil?
23
+ @active_record_polymorphic_array_collection = collection.map { |obj| obj.class }.uniq.length > 1
24
+ end
25
+
26
+ # [[1, 'foo'], [2, 'bar']]
14
27
  def array_collection?
15
28
  @array_collection == true
16
29
  end
@@ -21,11 +34,13 @@ module Effective
21
34
  raise 'No collection defined. Please add a collection with collection do ... end' if collection.nil?
22
35
 
23
36
  @collection_class = (collection.respond_to?(:klass) ? collection.klass : self.class)
37
+
24
38
  @active_record_collection = (collection.ancestors.include?(ActiveRecord::Base) rescue false)
25
- @array_collection = (collection.kind_of?(Array) && (collection.length == 0 || collection.first.kind_of?(Array)))
39
+ @active_record_array_collection = collection.kind_of?(Array) && collection.present? && collection.first.kind_of?(ActiveRecord::Base)
40
+ @array_collection = collection.kind_of?(Array) && (collection.blank? || collection.first.kind_of?(Array))
26
41
 
27
- unless active_record_collection? || array_collection?
28
- raise "Unsupported collection type. Expecting an ActiveRecord class, ActiveRecord relation, or an Array of Arrays [[1, 'foo'], [2, 'bar']]"
42
+ unless active_record_collection? || active_record_array_collection? || array_collection?
43
+ raise "Unsupported collection. Expecting an ActiveRecord relation, an Array of ActiveRecord objects, or an Array of Arrays [[1, 'foo'], [2, 'bar']]"
29
44
  end
30
45
 
31
46
  _scopes.each do |scope, _|
@@ -64,11 +64,15 @@ module Effective
64
64
  if state[:visible][name] == false && (name != order_name) # Sort by invisible array column
65
65
  BLANK
66
66
  elsif opts[:compute]
67
- dsl_tool.instance_exec(obj, (active_record_collection? ? collection : obj[opts[:index]]), &opts[:compute])
67
+ if array_collection?
68
+ dsl_tool.instance_exec(obj, obj[opts[:index]], &opts[:compute])
69
+ else
70
+ dsl_tool.instance_exec(obj, collection, &opts[:compute])
71
+ end
68
72
  elsif (opts[:partial] || opts[:format])
69
- active_record_collection? ? obj : obj[opts[:index]]
73
+ array_collection? ? obj[opts[:index]] : obj
70
74
  elsif opts[:resource]
71
- resource = active_record_collection? ? obj : obj[opts[:index]]
75
+ resource = array_collection? ? obj[opts[:index]] : obj
72
76
 
73
77
  if opts[:resource_field]
74
78
  (associated, field) = name.to_s.split('.').first(2)
@@ -16,7 +16,7 @@ module Effective
16
16
  next unless state[:visible][name]
17
17
 
18
18
  if opts[:partial]
19
- locals = { datatable: self, column: columns[name] }.merge(resource_col_locals(opts))
19
+ locals = { datatable: self, column: opts }.merge(resource_col_locals(opts))
20
20
 
21
21
  rendered[name] = (view.render(
22
22
  partial: opts[:partial],
@@ -29,11 +29,24 @@ module Effective
29
29
  elsif opts[:as] == :actions # This is actions_col and actions_col do .. end, but not actions_col partial: 'something'
30
30
  resources = collection.map { |row| row[opts[:index]] }
31
31
  locals = { datatable: self, column: opts, spacer_template: SPACER_TEMPLATE }
32
- atts = { actions: actions_col_actions(opts), effective_resource: resource, locals: locals, partial: opts[:actions_partial], btn_class: opts[:btn_class] }.merge(opts[:actions])
33
32
 
34
- rendered[name] = (view.render_resource_actions(resources, atts, &opts[:format]) || '').split(SPACER)
35
- end
33
+ atts = {
34
+ actions: actions_col_actions(opts),
35
+ btn_class: opts[:btn_class],
36
+ effective_resource: effective_resource,
37
+ locals: locals,
38
+ partial: opts[:actions_partial],
39
+ }.compact.merge(opts[:actions])
36
40
 
41
+ rendered[name] = if effective_resource.blank?
42
+ resources.map do |resource|
43
+ polymorphic_resource = Effective::Resource.new(resource, namespace: controller_namespace)
44
+ (view.render_resource_actions(resource, atts.merge(effective_resource: polymorphic_resource), &opts[:format]) || '')
45
+ end
46
+ else
47
+ (view.render_resource_actions(resources, atts, &opts[:format]) || '').split(SPACER)
48
+ end
49
+ end
37
50
  end
38
51
 
39
52
  collection.each_with_index do |row, row_index|
@@ -69,9 +82,7 @@ module Effective
69
82
 
70
83
  case column[:as]
71
84
  when :actions
72
- atts = { actions: actions_col_actions(column), effective_resource: resource, partial: column[:actions_partial] }.merge(column[:actions])
73
-
74
- (view.render_resource_actions(value, atts) || '')
85
+ raise("please use actions_col instead of col(#{name}, as: :actions)")
75
86
  when :boolean
76
87
  view.t("effective_datatables.boolean_#{value}")
77
88
  when :currency
@@ -115,10 +126,12 @@ module Effective
115
126
  # Applies data-remote to anything that's data-method post or delete
116
127
  # Merges in any extra attributes when passed as a Hash
117
128
  def actions_col_actions(column)
129
+ resource_actions = (effective_resource&.resource_actions || fallback_effective_resource.fallback_resource_actions)
130
+
118
131
  actions = if column[:inline]
119
- resource.resource_actions.transform_values { |opts| opts['data-remote'] = true; opts }
132
+ resource_actions.transform_values { |opts| opts['data-remote'] = true; opts }
120
133
  else
121
- resource.resource_actions.transform_values { |opts| opts['data-remote'] = true if opts['data-method']; opts }
134
+ resource_actions.transform_values { |opts| opts['data-remote'] = true if opts['data-method']; opts }
122
135
  end
123
136
 
124
137
  # Merge local options. Special behaviour for remote: false
@@ -140,26 +153,32 @@ module Effective
140
153
  end
141
154
 
142
155
  def resource_col_locals(opts)
143
- return {} unless (resource = opts[:resource]).present?
156
+ return {} unless (associated_resource = opts[:resource]).present?
144
157
 
158
+ associated = associated_resource.macros.include?(opts[:as])
145
159
  polymorphic = (opts[:as] == :belongs_to_polymorphic)
146
- associated = resource.macros.include?(opts[:as])
147
160
 
148
- resource_name = opts[:name] if associated
161
+ resource_name = opts[:name] if associated
149
162
  resource_to_s = opts[:name] unless associated || array_collection?
150
163
 
151
- locals = { resource_name: resource_name, resource_to_s: resource_to_s, effective_resource: resource, show_action: false, edit_action: false }
164
+ locals = {
165
+ resource_name: resource_name,
166
+ resource_to_s: resource_to_s,
167
+ effective_resource: associated_resource,
168
+ show_action: false,
169
+ edit_action: false
170
+ }
152
171
 
153
172
  case opts[:action]
154
173
  when :edit
155
- locals[:edit_action] = (resource.routes[:edit].present? || polymorphic)
174
+ locals[:edit_action] = (polymorphic || associated_resource.routes[:edit].present?)
156
175
  when :show
157
- locals[:show_action] = (resource.routes[:show].present? || polymorphic)
176
+ locals[:show_action] = (polymorphic || associated_resource.routes[:show].present?)
158
177
  when false
159
178
  # Nothing. Already false.
160
179
  else
161
- locals[:edit_action] = (resource.routes[:edit].present? || polymorphic)
162
- locals[:show_action] = (resource.routes[:show].present? || polymorphic)
180
+ locals[:edit_action] = (polymorphic || associated_resource.routes[:edit].present?)
181
+ locals[:show_action] = (polymorphic || associated_resource.routes[:show].present?)
163
182
  end
164
183
 
165
184
  locals
@@ -11,99 +11,126 @@ module Effective
11
11
  @attributes[:namespace]
12
12
  end
13
13
 
14
+ def association_macros
15
+ [:belongs_to, :belongs_to_polymorphic, :has_many, :has_and_belongs_to_many, :has_one]
16
+ end
17
+
14
18
  private
15
19
 
16
20
  # This looks at all the columns and figures out the as:
17
21
  def load_resource!
18
- @resource = Effective::Resource.new(collection_class, namespace: controller_namespace)
22
+ load_effective_resource!
19
23
 
20
- if active_record_collection?
21
- columns.each do |name, opts|
24
+ load_active_record_collection!
25
+ load_active_record_array_collection!
26
+ load_array_collection!
22
27
 
23
- # col 'comments.title'
24
- if name.kind_of?(String) && name.include?('.')
25
- raise "invalid datatables column '#{name}'. the joined syntax only supports one dot." if name.scan(/\./).count > 1
28
+ load_resource_columns!
29
+ load_resource_belongs_tos!
30
+ load_resource_search!
31
+ end
26
32
 
27
- (associated, field) = name.split('.').first(2)
33
+ def load_effective_resource!
34
+ @effective_resource = if active_record_collection?
35
+ Effective::Resource.new(collection_class, namespace: controller_namespace)
36
+ end
37
+ end
28
38
 
29
- unless resource.macros.include?(resource.sql_type(associated))
30
- raise "invalid datatables column '#{name}'. unable to find '#{name.split('.').first}' association on '#{resource}'."
31
- end
39
+ def load_active_record_collection!
40
+ return unless active_record_collection?
32
41
 
33
- joins_values = (collection.joins_values + collection.left_outer_joins_values)
42
+ columns.each do |name, opts|
43
+ # col 'comments.title'
44
+ if name.kind_of?(String) && name.include?('.')
45
+ raise "invalid datatables column '#{name}'. the joined syntax only supports one dot." if name.scan(/\./).count > 1
34
46
 
35
- unless joins_values.include?(associated.to_sym)
36
- raise "your datatables collection must .joins(:#{associated}) or .left_outer_joins(:#{associated}) to work with the joined syntax"
37
- end
47
+ (associated, field) = name.split('.').first(2)
38
48
 
39
- opts[:resource] = Effective::Resource.new(resource.associated(associated), namespace: controller_namespace)
49
+ unless association_macros.include?(effective_resource.sql_type(associated))
50
+ raise "invalid datatables column '#{name}'. unable to find '#{name.split('.').first}' association on '#{effective_resource}'."
51
+ end
40
52
 
41
- if opts[:resource].column(field)
42
- opts[:as] ||= opts[:resource].sql_type(field)
43
- opts[:as] = :integer if opts[:resource].sql_type(field) == :belongs_to && field.end_with?('_id')
44
- opts[:sql_column] = opts[:resource].sql_column(field) if opts[:sql_column].nil?
53
+ joins_values = (collection.joins_values + collection.left_outer_joins_values)
45
54
 
46
- opts[:resource].sort_column = field
47
- opts[:resource].search_columns = field
48
- end
55
+ unless joins_values.include?(associated.to_sym)
56
+ raise "your datatables collection must .joins(:#{associated}) or .left_outer_joins(:#{associated}) to work with the joined syntax"
57
+ end
49
58
 
50
- opts[:resource_field] = field
59
+ opts[:resource] = Effective::Resource.new(effective_resource.associated(associated), namespace: controller_namespace)
51
60
 
52
- next
61
+ if opts[:resource].column(field)
62
+ opts[:as] ||= opts[:resource].sql_type(field)
63
+ opts[:as] = :integer if opts[:resource].sql_type(field) == :belongs_to && field.end_with?('_id')
64
+ opts[:sql_column] = opts[:resource].sql_column(field) if opts[:sql_column].nil?
65
+
66
+ opts[:resource].sort_column = field
67
+ opts[:resource].search_columns = field
53
68
  end
54
69
 
55
- # Regular fields
56
- opts[:as] ||= resource.sql_type(name)
57
- opts[:sql_column] = resource.sql_column(name) if opts[:sql_column].nil?
70
+ opts[:resource_field] = field
71
+
72
+ next
73
+ end
74
+
75
+ # Regular fields
76
+ opts[:as] ||= effective_resource.sql_type(name)
77
+ opts[:sql_column] = effective_resource.sql_column(name) if opts[:sql_column].nil?
58
78
 
59
- case opts[:as]
60
- when *resource.macros
61
- opts[:resource] ||= Effective::Resource.new(resource.associated(name), namespace: controller_namespace)
79
+ case opts[:as]
80
+ when *association_macros
81
+ opts[:resource] ||= Effective::Resource.new(effective_resource.associated(name), namespace: controller_namespace)
82
+ opts[:sql_column] = name if opts[:sql_column].nil?
83
+ when Class
84
+ if opts[:as].ancestors.include?(ActiveRecord::Base)
85
+ opts[:resource] = Effective::Resource.new(opts[:as], namespace: controller_namespace)
86
+ opts[:as] = :resource
62
87
  opts[:sql_column] = name if opts[:sql_column].nil?
63
- when Class
64
- if opts[:as].ancestors.include?(ActiveRecord::Base)
65
- opts[:resource] = Effective::Resource.new(opts[:as], namespace: controller_namespace)
66
- opts[:as] = :resource
67
- opts[:sql_column] = name if opts[:sql_column].nil?
68
- end
69
- when :effective_addresses
70
- opts[:resource] = Effective::Resource.new(resource.associated(name), namespace: controller_namespace)
71
- opts[:sql_column] = :effective_addresses
72
- when :effective_roles
73
- opts[:sql_column] = :effective_roles
74
- when :string # This is the fallback
75
- # Anything that doesn't belong to the model or the sql table, we assume is a SELECT SUM|AVG|RANK() as fancy
76
- opts[:sql_as_column] = true if (resource.table && resource.column(name).blank?)
77
88
  end
89
+ when :effective_addresses
90
+ opts[:resource] = Effective::Resource.new(effective_resource.associated(name), namespace: controller_namespace)
91
+ opts[:sql_column] = :effective_addresses
92
+ when :effective_roles
93
+ opts[:sql_column] = :effective_roles
94
+ when :string # This is the fallback
95
+ # Anything that doesn't belong to the model or the sql table, we assume is a SELECT SUM|AVG|RANK() as fancy
96
+ opts[:sql_as_column] = true if (effective_resource.table && effective_resource.column(name).blank?)
97
+ end
78
98
 
79
- if opts[:sql_column].present? && AGGREGATE_SQL_FUNCTIONS.any? { |str| opts[:sql_column].to_s.start_with?(str) }
80
- opts[:sql_as_column] = true
81
- end
99
+ if opts[:sql_column].present? && AGGREGATE_SQL_FUNCTIONS.any? { |str| opts[:sql_column].to_s.start_with?(str) }
100
+ opts[:sql_as_column] = true
82
101
  end
83
102
  end
103
+ end
84
104
 
85
- if array_collection?
86
- row = collection.first
105
+ def load_active_record_array_collection!
106
+ return unless active_record_array_collection?
107
+ end
87
108
 
88
- columns.each do |name, opts|
89
- if opts[:as].kind_of?(Class) && opts[:as].ancestors.include?(ActiveRecord::Base)
90
- opts[:resource] = Effective::Resource.new(opts[:as], namespace: controller_namespace)
109
+ def load_array_collection!
110
+ return unless array_collection?
111
+
112
+ row = collection.first
113
+
114
+ columns.each do |name, opts|
115
+ if opts[:as].kind_of?(Class) && opts[:as].ancestors.include?(ActiveRecord::Base)
116
+ opts[:resource] = Effective::Resource.new(opts[:as], namespace: controller_namespace)
117
+ opts[:as] = :resource
118
+ elsif opts[:as] == nil && row.present?
119
+ if (value = Array(row[opts[:index]]).first).kind_of?(ActiveRecord::Base)
120
+ opts[:resource] = Effective::Resource.new(value, namespace: controller_namespace)
91
121
  opts[:as] = :resource
92
- elsif opts[:as] == nil && row.present?
93
- if (value = Array(row[opts[:index]]).first).kind_of?(ActiveRecord::Base)
94
- opts[:resource] = Effective::Resource.new(value, namespace: controller_namespace)
95
- opts[:as] = :resource
96
- end
97
122
  end
98
123
  end
99
124
  end
125
+ end
100
126
 
127
+ def load_resource_columns!
101
128
  columns.each do |name, opts|
102
129
  opts[:as] ||= :string
103
130
  opts[:as] = :email if (opts[:as] == :string && name.to_s.end_with?('email'))
104
131
 
105
132
  if opts[:action]
106
- opts[:resource] ||= resource
133
+ opts[:resource] ||= effective_resource
107
134
  end
108
135
 
109
136
  if opts[:resource] && !opts[:resource_field] && opts[:as] != :effective_addresses
@@ -113,7 +140,7 @@ module Effective
113
140
  opts[:col_class] = [
114
141
  "col-#{opts[:as]}",
115
142
  "col-#{name.to_s.parameterize}",
116
- ("colvis-default" if opts[:visible]),
143
+ ('colvis-default' if opts[:visible]),
117
144
  opts[:col_class].presence
118
145
  ].compact.join(' ')
119
146
  end
@@ -153,12 +180,13 @@ module Effective
153
180
 
154
181
  search[:fuzzy] = true unless search.key?(:fuzzy)
155
182
 
156
- if array_collection? && opts[:resource].present?
157
- search.reverse_merge!(resource.search_form_field(name, collection.first[opts[:index]]))
158
- elsif search[:as] == :select && search.key?(:collection)
183
+ if search[:as] == :select && search.key?(:collection)
159
184
  # No Action
185
+ elsif array_collection? && opts[:resource].present?
186
+ search.reverse_merge!(opts[:resource].search_form_field(name, collection.first[opts[:index]]))
160
187
  elsif search[:as] != :string
161
- search.reverse_merge!(resource.search_form_field(name, opts[:as]))
188
+ search_resource = (opts[:resource] || effective_resource || fallback_effective_resource)
189
+ search.reverse_merge!(search_resource.search_form_field(name, opts[:as]))
162
190
  end
163
191
 
164
192
  # Assign default include_null
@@ -168,7 +196,7 @@ module Effective
168
196
  end
169
197
  end
170
198
 
171
- def apply_belongs_to_attributes!
199
+ def load_resource_belongs_tos!
172
200
  return unless active_record_collection?
173
201
 
174
202
  changed = attributes.select do |attribute, value|
@@ -181,19 +209,19 @@ module Effective
181
209
 
182
210
  if columns[associated][:as] == :belongs_to
183
211
  if @_collection_apply_belongs_to && !@_collection.where_values_hash.include?(attribute)
184
- @_collection = @_collection.where(attribute => value)
212
+ @_collection = @_collection.where(attribute => value)
185
213
  end
186
214
 
187
215
  columns.delete(associated)
188
216
  elsif columns[associated][:as] == :belongs_to_polymorphic
189
217
  associated_type = attributes["#{associated}_type".to_sym] || raise("Expected #{associated}_type attribute to be present when #{associated}_id is present on a polymorphic belongs to")
190
218
 
191
- if @_collection_apply_belongs_to
219
+ if @_collection_apply_belongs_to
192
220
  if !@_collection.where_values_hash.include?(attribute) && !@_collection.where_values_hash.include?("#{associated}_type")
193
221
  @_collection = @_collection.where(attribute => value).where("#{associated}_type" => associated_type)
194
222
  end
195
223
  end
196
-
224
+
197
225
  columns.delete(associated)
198
226
  end
199
227
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveDatatables
2
- VERSION = '4.7.5'.freeze
2
+ VERSION = '4.7.6'.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: 4.7.5
4
+ version: 4.7.6
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: 2019-06-17 00:00:00.000000000 Z
11
+ date: 2019-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails