effective_datatables 3.7.7 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +5 -5
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +32 -32
  4. data/app/assets/images/dataTables/sort-down.svg +1 -0
  5. data/app/assets/images/dataTables/sort-up.svg +1 -0
  6. data/app/assets/images/dataTables/sort.svg +1 -0
  7. data/app/assets/javascripts/dataTables/buttons/{buttons.bootstrap.js → buttons.bootstrap4.js} +7 -15
  8. data/app/assets/javascripts/dataTables/dataTables.bootstrap4.js +184 -0
  9. data/app/assets/javascripts/dataTables/responsive/dataTables.responsive.js +30 -11
  10. data/app/assets/javascripts/dataTables/responsive/{responsive.bootstrap.js → responsive.bootstrap4.js} +6 -6
  11. data/app/assets/javascripts/effective_datatables.js +4 -4
  12. data/app/assets/javascripts/effective_datatables/bulk_actions.js.coffee +43 -43
  13. data/app/assets/javascripts/effective_datatables/events.js.coffee +7 -4
  14. data/app/assets/javascripts/effective_datatables/filters.js.coffee +0 -1
  15. data/app/assets/javascripts/effective_datatables/initialize.js.coffee +45 -49
  16. data/app/assets/javascripts/effective_datatables/overrides.js +12 -0
  17. data/app/assets/javascripts/effective_datatables/reset.js.coffee +1 -1
  18. data/app/assets/stylesheets/dataTables/buttons/{buttons.bootstrap.scss → buttons.bootstrap4.css} +68 -1
  19. data/app/assets/stylesheets/dataTables/{dataTables.bootstrap.scss → dataTables.bootstrap4.css} +44 -29
  20. data/app/assets/stylesheets/dataTables/responsive/{responsive.bootstrap.scss → responsive.bootstrap4.css} +3 -3
  21. data/app/assets/stylesheets/effective_datatables.scss +3 -4
  22. data/app/assets/stylesheets/effective_datatables/_overrides.scss +72 -152
  23. data/app/controllers/effective/datatables_controller.rb +6 -39
  24. data/app/helpers/effective_datatables_helper.rb +55 -50
  25. data/app/helpers/effective_datatables_private_helper.rb +47 -179
  26. data/app/models/effective/datatable.rb +16 -44
  27. data/app/models/effective/datatable_column.rb +0 -1
  28. data/app/models/effective/datatable_column_tool.rb +1 -1
  29. data/app/models/effective/datatable_dsl_tool.rb +3 -11
  30. data/app/models/effective/datatable_value_tool.rb +23 -23
  31. data/app/models/effective/effective_datatable/attributes.rb +13 -5
  32. data/app/models/effective/effective_datatable/collection.rb +3 -18
  33. data/app/models/effective/effective_datatable/compute.rb +6 -17
  34. data/app/models/effective/effective_datatable/cookie.rb +20 -19
  35. data/app/models/effective/effective_datatable/dsl.rb +3 -8
  36. data/app/models/effective/effective_datatable/dsl/bulk_actions.rb +25 -14
  37. data/app/models/effective/effective_datatable/dsl/datatable.rb +28 -70
  38. data/app/models/effective/effective_datatable/dsl/filters.rb +5 -5
  39. data/app/models/effective/effective_datatable/format.rb +50 -95
  40. data/app/models/effective/effective_datatable/params.rb +3 -8
  41. data/app/models/effective/effective_datatable/resource.rb +76 -137
  42. data/app/models/effective/effective_datatable/state.rb +15 -30
  43. data/app/views/effective/datatables/_actions_column.html.haml +8 -1
  44. data/app/views/effective/datatables/_bulk_actions_column.html.haml +1 -1
  45. data/app/views/effective/datatables/_filters.html.haml +11 -12
  46. data/app/views/effective/datatables/_resource_column.html.haml +8 -11
  47. data/config/effective_datatables.rb +14 -12
  48. data/config/routes.rb +0 -1
  49. data/lib/effective_datatables.rb +4 -57
  50. data/lib/effective_datatables/engine.rb +1 -1
  51. data/lib/effective_datatables/version.rb +1 -1
  52. metadata +20 -30
  53. data/app/assets/images/dataTables/sort_asc.png +0 -0
  54. data/app/assets/images/dataTables/sort_both.png +0 -0
  55. data/app/assets/images/dataTables/sort_desc.png +0 -0
  56. data/app/assets/javascripts/dataTables/dataTables.bootstrap.js +0 -182
  57. data/app/assets/javascripts/dataTables/locales/en.lang +0 -33
  58. data/app/assets/javascripts/dataTables/locales/es.lang +0 -36
  59. data/app/assets/javascripts/dataTables/locales/nl.lang +0 -30
  60. data/app/assets/javascripts/effective_datatables/flash.js.coffee +0 -31
  61. data/app/assets/javascripts/effective_datatables/inline_crud.js.coffee +0 -217
  62. data/app/assets/javascripts/effective_datatables/overrides.js.coffee +0 -7
  63. data/app/assets/javascripts/effective_datatables/reorder.js.coffee +0 -43
  64. data/app/assets/stylesheets/effective_datatables/_filters.scss +0 -7
  65. data/app/views/effective/datatables/_reorder_column.html.haml +0 -5
  66. data/config/locales/en.yml +0 -12
  67. data/config/locales/es.yml +0 -12
  68. data/config/locales/nl.yml +0 -12
@@ -1,4 +1,3 @@
1
- # In practice this is just a regular hash with the aggregate, format, search, sort do syntax that saves a block
2
1
  module Effective
3
2
  class DatatableColumn
4
3
  attr_accessor :attributes
@@ -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.effective_resource.sql_direction(direction)}")
46
+ collection.order("#{sql_column} #{datatable.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)
@@ -16,24 +16,16 @@ module Effective
16
16
  @view = datatable.view
17
17
  end
18
18
 
19
- def method_missing(method, *args, &block)
19
+ def method_missing(method, *args)
20
20
  # Catch a common error
21
21
  if [:bulk_actions, :charts, :collection, :filters].include?(method) && in_datatables_do_block
22
22
  raise "#{method} block must be declared outside the datatable do ... end block"
23
23
  end
24
24
 
25
25
  if datatable.respond_to?(method)
26
- if block_given?
27
- datatable.send(method, *args) { yield }
28
- else
29
- datatable.send(method, *args)
30
- end
26
+ datatable.send(method, *args)
31
27
  elsif view.respond_to?(method)
32
- if block_given?
33
- view.send(method, *args) { yield }
34
- else
35
- view.send(method, *args)
36
- end
28
+ view.send(method, *args)
37
29
  else
38
30
  super
39
31
  end
@@ -74,12 +74,13 @@ module Effective
74
74
  collection
75
75
  end
76
76
 
77
- def search_column(collection, original, column, index)
78
- Rails.logger.info "VALUE TOOL: search_column #{column.to_s} #{original} #{index}" if EffectiveDatatables.debug
77
+ def search_column(collection, value, column, index)
78
+ Rails.logger.info "VALUE TOOL: search_column #{column.to_s} #{value} #{index}" if EffectiveDatatables.debug
79
79
 
80
+ macros = Effective::Resource.new('').macros
80
81
  fuzzy = column[:search][:fuzzy]
81
82
 
82
- term = Effective::Attribute.new(column[:as]).parse(original, name: column[:name])
83
+ term = Effective::Attribute.new(column[:as]).parse(value, name: column[:name])
83
84
  term_downcased = term.to_s.downcase
84
85
 
85
86
  # term == 'nil' rescue false is a Rails 4.1 fix, where you can't compare a TimeWithZone to 'nil'
@@ -89,19 +90,18 @@ module Effective
89
90
 
90
91
  # See effective_resources gem search() method # relation.rb
91
92
  collection.select! do |row|
92
- obj = row[index]
93
- value = obj_to_value(row[index], column, row)
93
+ obj = obj_to_value(row[index], column, row)
94
94
 
95
95
  case column[:as]
96
96
  when :boolean
97
- if term
98
- ['Yes', 'yes', true, 'true', '1'].include?(value)
97
+ if fuzzy
98
+ term ? (obj == true) : (obj != true)
99
99
  else
100
- ['No', 'no', false, 'false', '0'].include?(value)
100
+ obj == term
101
101
  end
102
102
  when :datetime, :date
103
103
  end_at = (
104
- case (original.to_s.scan(/(\d+)/).flatten).length
104
+ case (value.to_s.scan(/(\d+)/).flatten).length
105
105
  when 1 ; term.end_of_year # Year
106
106
  when 2 ; term.end_of_month # Year-Month
107
107
  when 3 ; term.end_of_day # Year-Month-Day
@@ -111,35 +111,35 @@ module Effective
111
111
  else term
112
112
  end
113
113
  )
114
- value >= term && value <= end_at
114
+ obj >= term && obj <= end_at
115
115
  when :time
116
- (value.hour == term.hour) && (term.min == 0 ? true : (value.min == term.min))
116
+ (obj.hour == term.hour) && (term.min == 0 ? true : (obj.min == term.min))
117
117
  when :decimal, :currency
118
- if fuzzy && (term.round(0) == term) && original.to_s.include?('.') == false
118
+ if fuzzy && (term.round(0) == term) && value.to_s.include?('.') == false
119
119
  if term < 0
120
- value <= term && value > (term - 1.0)
120
+ obj <= term && obj > (term - 1.0)
121
121
  else
122
- value >= term && value < (term + 1.0)
122
+ obj >= term && obj < (term + 1.0)
123
123
  end
124
124
  else
125
- value == term
125
+ obj == term
126
126
  end
127
127
  when :duration
128
- if fuzzy && (term % 60 == 0) && original.to_s.include?('m') == false
128
+ if fuzzy && (term % 60 == 0) && value.to_s.include?('m') == false
129
129
  if term < 0
130
- value <= term && value > (term - 60)
130
+ obj <= term && obj > (term - 60)
131
131
  else
132
- value >= term && value < (term + 60)
132
+ obj >= term && obj < (term + 60)
133
133
  end
134
134
  else
135
- value == term
135
+ obj == term
136
136
  end
137
- when *datatable.association_macros, :resource
137
+ when *macros, :resource
138
138
  Array(obj).any? do |resource|
139
139
  Array(term).any? do |term|
140
140
  matched = false
141
141
 
142
- if term.kind_of?(Integer) && resource.respond_to?(:id) && resource.respond_to?(:to_param)
142
+ if term.kind_of?(Integer) && resource.respond_to?(:to_param)
143
143
  matched = (resource.id == term || resource.to_param == term)
144
144
  end
145
145
 
@@ -148,9 +148,9 @@ module Effective
148
148
  end
149
149
  else # :string, :text, :email
150
150
  if fuzzy
151
- value.to_s.downcase.include?(term_downcased)
151
+ obj.to_s.downcase.include?(term_downcased)
152
152
  else
153
- value == term || (value.to_s == term.to_s)
153
+ obj == term || (obj.to_s == term.to_s)
154
154
  end
155
155
  end
156
156
  end || collection
@@ -4,14 +4,22 @@ module Effective
4
4
 
5
5
  private
6
6
 
7
- def assert_attributes!
8
- if datatables_ajax_request? || datatables_inline_request?
9
- raise 'expected attributes to be present' unless attributes.present?
10
- end
7
+ def initial_attributes(args)
8
+ raise "#{self.class.name}.new() expected Hash like arguments" unless args.kind_of?(Hash)
9
+ args
11
10
  end
12
11
 
13
12
  def load_attributes!
14
- @attributes[:namespace] ||= view.controller_path.split('/')[0...-1].join('/')
13
+ if datatables_ajax_request?
14
+ raise 'expected cookie to be present' unless cookie
15
+ raise 'expected attributes cookie to be present' unless cookie[:attributes]
16
+
17
+ @attributes = cookie.delete(:attributes)
18
+ end
19
+
20
+ unless datatables_ajax_request?
21
+ @attributes[:_n] ||= view.controller_path.split('/')[0...-1].join('/').presence
22
+ end
15
23
  end
16
24
 
17
25
  end
@@ -7,23 +7,10 @@ module Effective
7
7
  @collection_class # Will be either User/Post/etc or Array
8
8
  end
9
9
 
10
- # User.all
11
10
  def active_record_collection?
12
11
  @active_record_collection == true
13
12
  end
14
13
 
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']]
27
14
  def array_collection?
28
15
  @array_collection == true
29
16
  end
@@ -34,13 +21,11 @@ module Effective
34
21
  raise 'No collection defined. Please add a collection with collection do ... end' if collection.nil?
35
22
 
36
23
  @collection_class = (collection.respond_to?(:klass) ? collection.klass : self.class)
37
-
38
24
  @active_record_collection = (collection.ancestors.include?(ActiveRecord::Base) rescue false)
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))
25
+ @array_collection = (collection.kind_of?(Array) && (collection.length == 0 || collection.first.kind_of?(Array)))
41
26
 
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']]"
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']]"
44
29
  end
45
30
 
46
31
  _scopes.each do |scope, _|
@@ -14,14 +14,11 @@ module Effective
14
14
  @total_records = (active_record_collection? ? column_tool.size(col) : value_tool.size(col))
15
15
 
16
16
  # Apply scope
17
- col = column_tool.scope(col) if @_collection_apply_scope
17
+ col = column_tool.scope(col)
18
18
 
19
19
  # Apply column searching
20
20
  col = column_tool.search(col)
21
-
22
- unless value_tool.searched.present? || (column_tool.scoped.blank? && column_tool.searched.blank?)
23
- @display_records = column_tool.size(col)
24
- end
21
+ @display_records = column_tool.size(col) unless value_tool.searched.present?
25
22
 
26
23
  # Apply column ordering
27
24
  col = column_tool.order(col)
@@ -64,15 +61,11 @@ module Effective
64
61
  if state[:visible][name] == false && (name != order_name) # Sort by invisible array column
65
62
  BLANK
66
63
  elsif 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
64
+ dsl_tool.instance_exec(obj, (active_record_collection? ? collection : obj[opts[:index]]), &opts[:compute])
72
65
  elsif (opts[:partial] || opts[:format])
73
- array_collection? ? obj[opts[:index]] : obj
66
+ active_record_collection? ? obj : obj[opts[:index]]
74
67
  elsif opts[:resource]
75
- resource = array_collection? ? obj[opts[:index]] : obj
68
+ resource = active_record_collection? ? obj : obj[opts[:index]]
76
69
 
77
70
  if opts[:resource_field]
78
71
  (associated, field) = name.to_s.split('.').first(2)
@@ -82,8 +75,6 @@ module Effective
82
75
  resource.send(name)
83
76
  end
84
77
 
85
- elsif opts[:as] == :actions
86
- obj
87
78
  elsif opts[:as] == :effective_obfuscation
88
79
  obj.to_param
89
80
  elsif array_collection?
@@ -135,11 +126,9 @@ module Effective
135
126
  length = values.length
136
127
  values = values.reject { |value| value.nil? }
137
128
 
138
- return BLANK if [:id, :year].include?(column[:name])
139
-
140
129
  case aggregate[:name]
141
130
  when :total
142
- if [:percent].include?(column[:as])
131
+ if [:percentage].include?(column[:as])
143
132
  BLANK
144
133
  elsif values.all? { |value| value.kind_of?(Numeric) }
145
134
  values.sum
@@ -1,20 +1,32 @@
1
1
  module Effective
2
2
  module EffectiveDatatable
3
3
  module Cookie
4
+ MAX_COOKIE_SIZE = 2500 # String size. Real byte size is about 1.5 times bigger.
5
+
4
6
  def cookie
5
7
  @cookie
6
8
  end
7
9
 
10
+ def cookie_key
11
+ @cookie_key ||= (datatables_ajax_request? ? view.params[:cookie] : cookie_param)
12
+ end
13
+
14
+ # All possible dt cookie keys. Used to make sure the datatable has a cookie set for this session.
15
+ def cookie_keys
16
+ @cookie_keys ||= Array(@dt_cookie).compact.map(&:first)
17
+ end
18
+
19
+ def cookie_param
20
+ [self.class, attributes].hash.to_s.last(12)
21
+ end
22
+
8
23
  private
9
24
 
10
25
  def load_cookie!
11
- return unless EffectiveDatatables.save_state
12
-
13
26
  @dt_cookie = view.cookies.signed['_effective_dt']
14
27
 
15
28
  # Load global datatables cookie
16
29
  if @dt_cookie.present?
17
-
18
30
  @dt_cookie = Marshal.load(Base64.decode64(@dt_cookie))
19
31
  raise 'invalid datatables cookie' unless @dt_cookie.kind_of?(Array)
20
32
 
@@ -27,33 +39,21 @@ module Effective
27
39
  if @cookie.kind_of?(Array)
28
40
  @cookie = initial_state.keys.zip(@cookie.second).to_h
29
41
  end
30
-
31
42
  end
32
43
 
33
44
  def save_cookie!
34
- return unless EffectiveDatatables.save_state
35
-
36
45
  @dt_cookie ||= []
37
46
  @dt_cookie << [cookie_key, cookie_payload]
38
47
 
39
- while @dt_cookie.to_s.size > EffectiveDatatables.cookie_max_size.to_i
48
+ while @dt_cookie.to_s.size > MAX_COOKIE_SIZE
40
49
  @dt_cookie.shift((@dt_cookie.length / 3) + 1)
41
50
  end
42
51
 
43
- # Generate cookie
44
- domain = EffectiveDatatables.cookie_domain || :all
45
- tld_length = EffectiveDatatables.cookie_tld_length
46
- tld_length ||= (view.request.host == 'localhost' ? nil : view.request.host.to_s.split('.').count)
47
-
48
- view.cookies.signed['_effective_dt'] = { value: Base64.encode64(Marshal.dump(@dt_cookie)), domain: domain, tld_length: tld_length }.compact
49
- end
50
-
51
- def cookie_key
52
- @cookie_key ||= to_param
52
+ view.cookies.signed['_effective_dt'] = Base64.encode64(Marshal.dump(@dt_cookie))
53
53
  end
54
54
 
55
55
  def cookie_payload
56
- payload = state.except(:visible)
56
+ payload = state.except(:attributes, :visible)
57
57
 
58
58
  # Turn visible into a bitmask. This is undone in load_columns!
59
59
  payload[:vismask] = (
@@ -62,7 +62,8 @@ module Effective
62
62
  end
63
63
  )
64
64
 
65
- payload.values # Just store the values
65
+ # Just store the values
66
+ [attributes.delete_if { |k, v| v.nil? }] + payload.values
66
67
  end
67
68
 
68
69
  end
@@ -3,20 +3,15 @@ module Effective
3
3
  module Dsl
4
4
 
5
5
  def bulk_actions(&block)
6
- define_method('initialize_bulk_actions') { dsl_tool.instance_exec(&block); dsl_tool.bulk_actions_col }
6
+ define_method('initialize_bulk_actions') { dsl_tool.instance_exec(&block) }
7
7
  end
8
8
 
9
9
  def charts(&block)
10
10
  define_method('initialize_charts') { dsl_tool.instance_exec(&block) }
11
11
  end
12
12
 
13
- def collection(apply_belongs_to: true, apply_scope: true, &block)
14
- define_method('initialize_collection') {
15
- self._collection_apply_belongs_to = apply_belongs_to
16
- self._collection_apply_scope = apply_scope
17
-
18
- self._collection = dsl_tool.instance_exec(&block)
19
- }
13
+ def collection(&block)
14
+ define_method('initialize_collection') { self._collection = dsl_tool.instance_exec(&block) }
20
15
  end
21
16
 
22
17
  def datatable(&block)
@@ -3,12 +3,12 @@ module Effective
3
3
  module Dsl
4
4
  module BulkActions
5
5
 
6
- def bulk_action(title, url, opts = {})
7
- datatable._bulk_actions.push(link_to_bulk_action(title, url, opts))
6
+ def bulk_action(*args)
7
+ datatable._bulk_actions.push(link_to_bulk_action(*args))
8
8
  end
9
9
 
10
- def bulk_download(title, url, opts = {})
11
- datatable._bulk_actions.push(link_to_bulk_action(title, url, opts.merge('data-bulk-download': true)))
10
+ def bulk_download(*args)
11
+ datatable._bulk_actions.push(link_to_bulk_action(*args.merge('data-authenticity-token' => form_authenticity_token)))
12
12
  end
13
13
 
14
14
  def bulk_action_divider
@@ -22,19 +22,30 @@ module Effective
22
22
  private
23
23
 
24
24
  # We can't let any data-method be applied to the link, or jquery_ujs does the wrong thing with it
25
- def link_to_bulk_action(title, url, opts = {})
26
-
27
- # Transform data: { ... } hash into 'data-' keys
28
- if (data = opts.delete(:data))
29
- data.each { |k, v| opts["data-#{k}"] ||= v }
25
+ def link_to_bulk_action(*args)
26
+ args.map! do |arg|
27
+ if arg.kind_of?(Hash)
28
+ data_method = (
29
+ arg.delete(:'data-method') ||
30
+ arg.delete('data-method') ||
31
+ (arg[:data] || {}).delete('method') ||
32
+ (arg[:data] || {}).delete(:method)
33
+ )
34
+
35
+ # But if the data-method was :get, we add bulk-actions-get-link = true
36
+ if data_method.to_s == 'get'
37
+ arg[:data].present? ? arg[:data]['bulk-actions-get'] = true : arg['data-bulk-actions-get'] = true
38
+ end
39
+
40
+ arg[:class] = [arg[:class], 'dropdown-item'].compact.join(' ')
41
+ end
42
+
43
+ arg
30
44
  end
31
45
 
32
- verbs = {'DELETE' => 'DELETE', 'GET' => 'GET'}
33
- opts['data-ajax-method'] = verbs[opts.delete('data-method').to_s.upcase] || 'POST'
34
-
35
- opts[:class] = [opts[:class], 'dropdown-item'].compact.join(' ')
46
+ args << { class: 'dropdown-item' } if args.none? { |arg| arg.kind_of?(Hash) }
36
47
 
37
- content_tag(:li, link_to(title, url, opts))
48
+ link_to(*args)
38
49
  end
39
50
 
40
51
  end
@@ -3,11 +3,6 @@ module Effective
3
3
  module Dsl
4
4
  module Datatable
5
5
  # Instance Methods inside the datatable do .. end block
6
- def length(length)
7
- raise 'length must be 5, 10, 25, 50, 100, 250, 500, :all' unless [5, 10, 25, 50, 100, 250, 500, :all].include?(length)
8
- datatable.state[:length] ||= (length == :all ? 9999999 : length)
9
- end
10
-
11
6
  def order(name, dir = nil)
12
7
  raise 'order direction must be :asc or :desc' unless [nil, :asc, :desc].include?(dir)
13
8
 
@@ -15,13 +10,9 @@ module Effective
15
10
  datatable.state[:order_dir] ||= dir
16
11
  end
17
12
 
18
- def reorder(name, dir = nil)
19
- raise 'order direction must be :asc or :desc' unless [nil, :asc, :desc].include?(dir)
20
-
21
- datatable.state[:order_name] = :_reorder
22
- datatable.state[:order_dir] = dir
23
-
24
- reorder_col(name)
13
+ def length(length)
14
+ raise 'length must be 5, 10, 25, 50, 100, 250, 500, :all' unless [5, 10, 25, 50, 100, 250, 500, :all].include?(length)
15
+ datatable.state[:length] ||= (length == :all ? 9999999 : length)
25
16
  end
26
17
 
27
18
  # A col has its internal values sorted/searched before the block is run
@@ -34,7 +25,7 @@ module Effective
34
25
  name = name.to_sym unless name.to_s.include?('.')
35
26
 
36
27
  datatable._columns[name] = Effective::DatatableColumn.new(
37
- action: action,
28
+ action: action, # resource columns only
38
29
  as: as,
39
30
  compute: nil,
40
31
  col_class: col_class,
@@ -62,7 +53,7 @@ module Effective
62
53
  name = name.to_sym unless name.to_s.include?('.')
63
54
 
64
55
  datatable._columns[name] = Effective::DatatableColumn.new(
65
- action: action,
56
+ action: action, # Resource columns only
66
57
  as: as,
67
58
  compute: (compute if block_given?),
68
59
  col_class: col_class,
@@ -82,48 +73,8 @@ module Effective
82
73
  )
83
74
  end
84
75
 
85
- def actions_col(btn_class: nil, col_class: nil, inline: nil, partial: nil, partial_as: nil, actions_partial: nil, responsive: 5000, visible: true, **actions, &format)
86
- raise 'You can only have one actions column' if datatable.columns[:_actions].present?
87
-
88
- datatable._columns[:_actions] = Effective::DatatableColumn.new(
89
- action: false,
90
- as: :actions,
91
- compute: nil,
92
- btn_class: (btn_class || 'btn-sm btn-outline-primary'),
93
- col_class: col_class,
94
- format: (format if block_given?),
95
- index: nil,
96
- inline: (inline.nil? ? datatable.inline? : inline),
97
- label: false,
98
- name: :actions,
99
- partial: partial,
100
- partial_as: partial_as,
101
- actions_partial: (actions_partial || :glyphicons),
102
- responsive: responsive,
103
- search: false,
104
- sort: false,
105
- sql_column: nil,
106
- th: nil,
107
- th_append: nil,
108
- visible: visible,
109
-
110
- # { approve: false }. These args are passed to effective_resources render_resource_actions
111
- actions: actions
112
- )
113
- end
114
-
115
- def aggregate(name, label: nil, &compute)
116
- datatable._aggregates[name.to_sym] = {
117
- compute: (compute if block_given?),
118
- label: label || name.to_s.titleize,
119
- name: name.to_sym,
120
- }
121
- end
122
-
123
- # Called automatically after bulk_actions do ... end
124
- # Call again if you want to change the position of the bulk_actions_col
125
- def bulk_actions_col(col_class: nil, input_name: nil, partial: nil, partial_as: nil, responsive: 5000)
126
- datatable._columns.delete(:_bulk_actions) if datatable.columns[:_bulk_actions]
76
+ def bulk_actions_col(col_class: nil, partial: nil, partial_as: nil, responsive: 5000)
77
+ raise 'You can only have one bulk actions column' if datatable.columns[:_bulk_actions].present?
127
78
 
128
79
  datatable._columns[:_bulk_actions] = Effective::DatatableColumn.new(
129
80
  action: false,
@@ -132,7 +83,6 @@ module Effective
132
83
  col_class: col_class,
133
84
  format: nil,
134
85
  index: nil,
135
- input_name: (input_name || 'bulk_actions_resources'),
136
86
  label: false,
137
87
  name: :bulk_actions,
138
88
  partial: partial || '/effective/datatables/bulk_actions_column',
@@ -147,33 +97,41 @@ module Effective
147
97
  )
148
98
  end
149
99
 
150
- # Called automatically after reorder
151
- # Call again if you want to change the position of the reorder_col
152
- def reorder_col(name, col_class: nil, partial: nil, partial_as: nil, sql_column: nil, responsive: 5000)
153
- datatable._columns.delete(:_reorder) if datatable.columns[:_reorder]
100
+ def actions_col(show: true, edit: true, destroy: true, col_class: nil, partial: nil, partial_as: nil, responsive: 5000, visible: true, &format)
101
+ raise 'You can only have one actions column' if datatable.columns[:_actions].present?
154
102
 
155
- datatable._columns[:_reorder] = Effective::DatatableColumn.new(
103
+ datatable._columns[:_actions] = Effective::DatatableColumn.new(
156
104
  action: false,
157
- as: :reorder,
105
+ as: :actions,
158
106
  compute: nil,
159
107
  col_class: col_class,
160
- format: nil,
108
+ format: (format if block_given?),
161
109
  index: nil,
162
110
  label: false,
163
- name: :reorder,
164
- partial: partial || '/effective/datatables/reorder_column',
111
+ name: :actions,
112
+ partial: partial || '/effective/datatables/actions_column',
165
113
  partial_as: partial_as,
166
- reorder: name,
167
114
  responsive: responsive,
168
115
  search: false,
169
- sort: true,
170
- sql_column: (sql_column || name),
116
+ sort: false,
117
+ sql_column: nil,
171
118
  th: nil,
172
119
  th_append: nil,
173
- visible: false
120
+ visible: visible,
121
+
122
+ show: show,
123
+ edit: edit,
124
+ destroy: destroy
174
125
  )
175
126
  end
176
127
 
128
+ def aggregate(name, label: nil, &compute)
129
+ datatable._aggregates[name.to_sym] = {
130
+ compute: (compute if block_given?),
131
+ label: label || name.to_s.titleize,
132
+ name: name.to_sym,
133
+ }
134
+ end
177
135
  end
178
136
  end
179
137
  end