effective_datatables 3.0.8 → 3.0.9

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: 96df2e08f32b87a62ce043e3760697cb7439d134
4
- data.tar.gz: e053c8993e6f151086fe31e1daeb4a24b984f17a
3
+ metadata.gz: 58e4a1ee916c1766e2d3d2be61ffe2607d92c0d4
4
+ data.tar.gz: b796116f649e7ca0711d7436f50b587d12c47576
5
5
  SHA512:
6
- metadata.gz: 1b3b944e79218ff0a5ae54d6080be055e4b15c14da25337b2d6400efc8a1703839b41c25a4dafb8759a645ea8f5dc9a015b4fed5545c416f76802c5711436efe
7
- data.tar.gz: dfb15a2aceb2d3615c7cae86453fd5ed8e2e10eae999ed9672aa5923dde2798d1ae4a1e04a7451b4ba67383418d0da662238c0c23c9582b079dfd644595c0a14
6
+ metadata.gz: 1bcd2435e64b3576f7568f126791a77e6c5da1de4a26244b8f290ce4f9d03f43037d23075b7f932e21a3c288a8049c4a0fd9b3b4ec314f1c0498f37edd7f4734
7
+ data.tar.gz: 5286e5530da18eaea928e8fae7311be484778b6be2694e1d0855c69c61cda7ba74efaa4b62e3fd79e8c7f44d2dbdf50d1b5bd6278ba202052403b3526dc6b518
data/README.md CHANGED
@@ -593,6 +593,14 @@ aggregate :average_as_percentage do |values, column|
593
593
  end
594
594
  ```
595
595
 
596
+ You can also override an individual columns aggregate calculation as follows:
597
+
598
+ ```ruby
599
+ col :created_at, label: 'Created' do |post|
600
+ time_ago_in_words(post.created_at)
601
+ end.aggregate { |values, column| distance_of_time_in_words(values.min, values.max) }
602
+ ```
603
+
596
604
  In the above example, `values` is an Array containing all row's values for one column at a time.
597
605
 
598
606
  ## filters
@@ -147,7 +147,7 @@ table.dataTable.dtr-inline.collapsed > tbody > tr:not(.child) > th:first-child {
147
147
  }
148
148
 
149
149
  // Column specific adjustments
150
- table.dataTable tbody {
150
+ table.dataTable {
151
151
  td.col-price { text-align: right; }
152
152
  td.col-right { text-align: right; }
153
153
 
@@ -162,5 +162,4 @@ table.dataTable tbody {
162
162
  td {
163
163
  p { margin-bottom: 0px; }
164
164
  }
165
-
166
165
  }
@@ -69,6 +69,7 @@ module EffectiveDatatablesPrivateHelper
69
69
  wrapper_html: wrapper_html,
70
70
  input_group: false,
71
71
  input_html: input_html,
72
+ date_linked: false,
72
73
  input_js: { useStrict: true, keepInvalid: true }
73
74
  # Keep invalid format like "2015-11" so we can still search by year, month or day
74
75
  when :select, :boolean
@@ -12,6 +12,10 @@ module Effective
12
12
  self[:name]
13
13
  end
14
14
 
15
+ def aggregate(&block)
16
+ @attributes[:aggregate] = block; self
17
+ end
18
+
15
19
  def format(&block)
16
20
  @attributes[:format] = block; self
17
21
  end
@@ -40,7 +40,7 @@ module Effective
40
40
  end
41
41
 
42
42
  def order_column(collection, direction, column, sql_column)
43
- Rails.logger.info "COLUMN TOOL: order_column #{column.to_s} #{direction} #{sql_column}"
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
46
  collection.order("#{sql_column} #{datatable.resource.sql_direction(direction)}")
@@ -73,7 +73,7 @@ module Effective
73
73
  end
74
74
 
75
75
  def search_column(collection, value, column, sql_column)
76
- Rails.logger.info "COLUMN TOOL: search_column #{column.to_s} #{value} #{sql_column}"
76
+ Rails.logger.info "COLUMN TOOL: search_column #{column.to_s} #{value} #{sql_column}" if EffectiveDatatables.debug
77
77
 
78
78
  Effective::Resource.new(collection)
79
79
  .search(column[:name], value, as: column[:as], fuzzy: column[:search][:fuzzy], sql_column: sql_column)
@@ -37,7 +37,7 @@ module Effective
37
37
  end
38
38
 
39
39
  def order_column(collection, direction, column, index)
40
- Rails.logger.info "VALUE TOOL: order_column :#{column.to_s} :#{direction} #{index}"
40
+ Rails.logger.info "VALUE TOOL: order_column :#{column.to_s} :#{direction} #{index}" if EffectiveDatatables.debug
41
41
 
42
42
  if direction == :asc
43
43
  collection.sort! do |x, y|
@@ -69,7 +69,7 @@ module Effective
69
69
  end
70
70
 
71
71
  def search_column(collection, value, column, index)
72
- Rails.logger.info "VALUE TOOL: search_column #{column.to_s} #{value} #{index}"
72
+ Rails.logger.info "VALUE TOOL: search_column #{column.to_s} #{value} #{index}" if EffectiveDatatables.debug
73
73
 
74
74
  fuzzy = column[:search][:fuzzy]
75
75
  term = Effective::Attribute.new(column[:as]).parse(value, name: column[:name])
@@ -81,6 +81,8 @@ module Effective
81
81
  cols = collection.transpose
82
82
 
83
83
  _aggregates.map do |_, aggregate|
84
+ aggregate[:labeled] = false
85
+
84
86
  columns.map do |name, opts|
85
87
  next if state[:visible][name] == false && datatables_ajax_request?
86
88
 
@@ -88,44 +90,43 @@ module Effective
88
90
 
89
91
  if state[:visible][name] == false
90
92
  BLANK
93
+ elsif [:bulk_actions, :actions].include?(opts[:as])
94
+ BLANK
95
+ elsif values.length == 0
96
+ BLANK
97
+ elsif opts[:aggregate]
98
+ dsl_tool.instance_exec(values, columns[name], &opts[:aggregate])
91
99
  elsif aggregate[:compute]
92
100
  dsl_tool.instance_exec(values, columns[name], &aggregate[:compute])
93
101
  else
94
102
  format_column(aggregate_column(values, opts, aggregate), opts)
95
- end
103
+ end || BLANK
96
104
  end.compact
97
105
  end
98
106
  end
99
107
 
100
108
  def aggregate_column(values, column, aggregate)
101
- labeled = false
102
109
  length = values.length
103
110
  values = values.reject { |value| value.nil? }
104
111
 
105
- if [:bulk_actions, :actions].include?(column[:as]) || length == 0
106
- return BLANK
107
- end
108
-
109
112
  case aggregate[:name]
110
113
  when :total
111
- if values.all? { |value| value.kind_of?(Numeric) }
114
+ if [:percentage].include?(column[:as])
115
+ BLANK
116
+ elsif values.all? { |value| value.kind_of?(Numeric) }
112
117
  values.sum
113
118
  elsif values.all? { |value| value == true || value == false }
114
- "#{values.count { |val| val == true }} / #{values.count { |val| val == false}}"
115
- elsif !labeled
116
- labeled = aggregate[:label]
117
- elsif values.any? { |value| value.kind_of?(String) == false }
118
- "#{values.flatten.count} total"
119
+ "#{values.count { |val| val == true }} • #{values.count { |val| val == false}}"
120
+ elsif aggregate[:labeled] == false
121
+ aggregate[:labeled] = aggregate[:label]
119
122
  end
120
123
  when :average
121
124
  if values.all? { |value| value.kind_of?(Numeric) }
122
125
  values.sum / [length, 1].max
123
126
  elsif values.all? { |value| value == true || value == false }
124
127
  values.count { |val| val == true } >= (length / 2) ? true : false
125
- elsif !labeled
126
- labeled = aggregate[:label]
127
- elsif values.any? { |value| value.kind_of?(String) == false }
128
- '-'
128
+ elsif aggregate[:labeled] == false
129
+ aggregate[:labeled] = aggregate[:label]
129
130
  end
130
131
  else
131
132
  raise 'not implemented'
@@ -54,12 +54,15 @@ module Effective
54
54
  def format_column(value, column)
55
55
  return if value.nil?
56
56
 
57
+ unless column[:as] == :email
58
+ return value if value.kind_of?(String)
59
+ end
60
+
57
61
  case column[:as]
58
62
  when :boolean
59
63
  case value
60
64
  when true ; 'Yes'
61
65
  when false ; 'No'
62
- when String ; value
63
66
  end
64
67
  when :currency
65
68
  view.number_to_currency(value)
@@ -85,13 +88,11 @@ module Effective
85
88
  case value
86
89
  when Integer ; "#{value}%"
87
90
  when Numeric ; view.number_to_percentage(value * 100, precision: 2)
88
- when String ; value
89
91
  end
90
92
  when :price
91
93
  case value
92
94
  when Integer ; view.number_to_currency(value / 100.0) # an Integer representing the number of cents
93
95
  when Numeric ; view.number_to_currency(value)
94
- when String ; value
95
96
  end
96
97
  else
97
98
  value.to_s
@@ -38,4 +38,7 @@ EffectiveDatatables.setup do |config|
38
38
  destroy: true,
39
39
  }
40
40
 
41
+ # Log search/sort information to the console
42
+ config.debug = true
43
+
41
44
  end
@@ -8,6 +8,7 @@ module EffectiveDatatables
8
8
 
9
9
  mattr_accessor :default_length
10
10
  mattr_accessor :actions_column # A Hash
11
+ mattr_accessor :debug
11
12
 
12
13
  def self.setup
13
14
  yield self
@@ -1,3 +1,3 @@
1
1
  module EffectiveDatatables
2
- VERSION = '3.0.8'.freeze
2
+ VERSION = '3.0.9'.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: 3.0.8
4
+ version: 3.0.9
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: 2017-05-31 00:00:00.000000000 Z
11
+ date: 2017-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails