effective_datatables 2.1.7 → 2.1.8

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: e0cbbbfb201841f1a9e49d34525f20769fc53397
4
- data.tar.gz: b362424906465b500b750d761386aa82ad0bf530
3
+ metadata.gz: af9c292325c6b762e373141abc1423733b680e07
4
+ data.tar.gz: 90eae3479c8852b652b45a050b0cd25e002e2ab8
5
5
  SHA512:
6
- metadata.gz: bea6151ccc549cbb1a40dd86de6540461d265844dd7dc6621701ca573f13664ef5f3a0339b01f6f79df15b05acf8bbba60b45f3cafad8068da131af4e003fed9
7
- data.tar.gz: f5d8a60fddb6e2aa73f2e4ff7124aba97ae723a57c144a091fd61c21c6113fc295e5a1abdf469f310a2bec46e30d2a24b08a3baa374dc5d9d412fbbf1b5aaccd
6
+ metadata.gz: adcafc661510d837fe435defab4fce2078c07a203ad2edc815a3af94926e9798934f06979656fd5ff4e7b359dbc2c2a5c0941f29793e8e791d448a39ae0b65fd
7
+ data.tar.gz: 663e67d4613b20b1d925b0166d8feae2f27750ad3771941a64c893c4f2f85a4d8120ddab46b7d3fc3640b2807ab040c8b32500d981966a97c6120be283803595
@@ -3,6 +3,7 @@
3
3
  module Effective
4
4
  module EffectiveDatatable
5
5
  module Rendering
6
+ BLANK = ''.freeze
6
7
 
7
8
  def finalize(collection) # Override me if you like
8
9
  collection
@@ -50,6 +51,7 @@ module Effective
50
51
  col = self.arrayize(col)
51
52
  end
52
53
 
54
+ self.format(col)
53
55
  col = self.finalize(col)
54
56
  end
55
57
 
@@ -104,7 +106,7 @@ module Effective
104
106
  collection.each_with_index.map do |obj, index|
105
107
  (display_table_columns || table_columns).map do |name, opts|
106
108
  if opts[:visible] == false
107
- ''
109
+ BLANK
108
110
  elsif opts[:partial]
109
111
  rendered[name][index]
110
112
  elsif opts[:block]
@@ -112,60 +114,77 @@ module Effective
112
114
  elsif opts[:proc]
113
115
  view.instance_exec(obj, collection, self, &opts[:proc])
114
116
  elsif opts[:type] == :belongs_to
115
- (obj.send(name) rescue nil).to_s
117
+ (obj.send(name) rescue nil)
116
118
  elsif opts[:type] == :belongs_to_polymorphic
117
- (obj.send(name) rescue nil).to_s
119
+ (obj.send(name) rescue nil)
118
120
  elsif opts[:type] == :has_many
119
- objs = (obj.send(name).map { |x| x.to_s }.sort rescue [])
120
- objs.length == 1 ? objs.first : (opts[:sentence] ? objs.to_sentence : objs.join('<br>'))
121
+ (obj.send(name).to_a rescue [])
121
122
  elsif opts[:type] == :obfuscated_id
122
123
  (obj.send(:to_param) rescue nil).to_s
123
124
  elsif opts[:type] == :effective_roles
124
125
  (obj.send(:roles) rescue []).join(', ')
126
+ elsif obj.kind_of?(Array) # Array backed collection
127
+ obj[opts[:array_index]]
125
128
  else
126
- # Normal value, but we still may want to format it
127
- value = (obj.send(name) rescue (obj[name] rescue (obj[opts[:array_index]] rescue nil)))
128
-
129
- case opts[:type]
130
- when :datetime
131
- value.strftime(EffectiveDatatables.datetime_format) rescue nil
132
- when :date
133
- value.strftime(EffectiveDatatables.date_format) rescue nil
134
- when :price
135
- # This is an integer value, "number of cents"
136
- value ||= 0
137
- raise 'column type: price expects an Integer representing the number of cents' unless value.kind_of?(Integer)
138
- number_to_currency(value / 100.0)
139
- when :currency
140
- number_to_currency(value || 0)
141
- when :integer
142
- if EffectiveDatatables.integer_format.kind_of?(Symbol)
143
- view.instance_exec { public_send(EffectiveDatatables.integer_format, value) }
144
- elsif EffectiveDatatables.integer_format.respond_to?(:call)
145
- view.instance_exec { EffectiveDatatables.integer_format.call(value) }
146
- else
147
- value
148
- end
149
- when :boolean
150
- if EffectiveDatatables.boolean_format == :yes_no && value == true
151
- 'Yes'
152
- elsif EffectiveDatatables.boolean_format == :yes_no && value == false
153
- 'No'
154
- else
155
- value
156
- end
157
- when :string
158
- if name == 'email' && value.present?
159
- mail_to(value)
129
+ (obj.send(name) rescue (obj[name] rescue nil))
130
+ end
131
+ end
132
+ end
133
+ end
134
+
135
+ def format(collection)
136
+ collection.each do |row|
137
+ (display_table_columns || table_columns).each_with_index do |(name, opts), index|
138
+ value = row[index]
139
+ next if value == nil || value == BLANK || opts[:visible] == false
140
+ next if opts[:block] || opts[:partial] || opts[:proc]
141
+
142
+ case opts[:type]
143
+ when :belongs_to, :belongs_to_polymorphic
144
+ row[index] = value.to_s
145
+ when :has_many
146
+ if value.kind_of?(Array)
147
+ if value.length == 0
148
+ row[index] = BLANK
149
+ elsif value.length == 1
150
+ row[index] = value.first.to_s
151
+ elsif opts[:sentence]
152
+ row[index] = value.map { |v| v.to_s }.to_sentence
160
153
  else
161
- value
154
+ row[index] = value.map { |v| v.to_s }.join('<br>')
162
155
  end
163
- else # Other col_type
164
- value
165
156
  end
157
+ when :datetime
158
+ row[index] = value.strftime(EffectiveDatatables.datetime_format) rescue BLANK
159
+ when :date
160
+ row[index] = value.strftime(EffectiveDatatables.date_format) rescue BLANK
161
+ when :price
162
+ # This is an integer value, "number of cents"
163
+ raise 'column type: price expects an Integer representing the number of cents' unless value.kind_of?(Integer)
164
+ row[index] = number_to_currency(value / 100.0)
165
+ when :currency
166
+ row[index] = number_to_currency(value || 0)
167
+ when :integer
168
+ if EffectiveDatatables.integer_format.kind_of?(Symbol)
169
+ row[index] = view.instance_exec { public_send(EffectiveDatatables.integer_format, value) }
170
+ elsif EffectiveDatatables.integer_format.respond_to?(:call)
171
+ row[index] = view.instance_exec { EffectiveDatatables.integer_format.call(value) }
172
+ end
173
+ when :boolean
174
+ if EffectiveDatatables.boolean_format == :yes_no && value == true
175
+ row[index] = 'Yes'
176
+ elsif EffectiveDatatables.boolean_format == :yes_no && value == false
177
+ row[index] = 'No'
178
+ end
179
+ when :string
180
+ row[index] = mail_to(value) if name == 'email'
181
+ else
182
+ ; # Nothing
166
183
  end
167
184
  end
168
185
  end
186
+
187
+ collection
169
188
  end
170
189
 
171
190
  end # / Rendering
@@ -1,3 +1,3 @@
1
1
  module EffectiveDatatables
2
- VERSION = '2.1.7'.freeze
2
+ VERSION = '2.1.8'.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.7
4
+ version: 2.1.8
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-04 00:00:00.000000000 Z
11
+ date: 2015-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails