ar_to_html_table 0.1.3 → 0.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.
data/README.textile
CHANGED
@@ -109,15 +109,14 @@ bc. class Product < ActiveRecord::Base
|
|
109
109
|
column_format :name, :order => 1
|
110
110
|
column_format :orders, :total => :sum
|
111
111
|
column_format :revenue, :total => :sum, :order => 5, :class => 'right'
|
112
|
-
column_format :age,
|
112
|
+
column_format :age, :total => :avg, :order => 20, :class => 'right', :formatter => :number_with_delimiter
|
113
113
|
end
|
114
114
|
|
115
|
-
|
115
|
+
bc. p = Product.first
|
116
116
|
=> #<Product id: 55986, age: 4346, .........
|
117
|
-
|
117
|
+
p.age
|
118
118
|
=> 4346
|
119
|
-
|
120
|
-
# p.format_column(:age)
|
119
|
+
p.format_column(:age)
|
121
120
|
=> "4,346"
|
122
121
|
|
123
122
|
h1. License
|
data/ar_to_html_table.gemspec
CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |s|
|
|
22
22
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
23
|
s.require_paths = ["lib"]
|
24
24
|
|
25
|
-
s.add_dependency 'builder'
|
26
|
-
s.add_dependency 'activerecord', '~> 2.3.5'
|
27
|
-
s.add_dependency 'actionpack', '~> 2.3.5'
|
25
|
+
#s.add_dependency 'builder'
|
26
|
+
#s.add_dependency 'activerecord', '~> 2.3.5'
|
27
|
+
#s.add_dependency 'actionpack', '~> 2.3.5'
|
28
28
|
end
|
@@ -38,6 +38,8 @@ module ArToHtmlTable
|
|
38
38
|
end
|
39
39
|
|
40
40
|
module ClassMethods
|
41
|
+
include ArToHtmlTable::ColumnFormatter
|
42
|
+
include ::ActionView::Helpers::NumberHelper
|
41
43
|
# Define a column format.
|
42
44
|
#
|
43
45
|
# ====Options
|
@@ -60,10 +62,11 @@ module ArToHtmlTable
|
|
60
62
|
# column_format :age, :total => :avg, :order => 20, :class => 'right', :formatter => :number_with_delimiter
|
61
63
|
# end
|
62
64
|
def column_format(method, options)
|
65
|
+
options[:formatter] = procify(options[:formatter]) if options[:formatter] && options[:formatter].is_a?(Symbol)
|
63
66
|
@attr_formats = (@attr_formats || default_formats).deep_merge({method.to_s => options})
|
64
67
|
end
|
65
68
|
alias :table_format :column_format
|
66
|
-
|
69
|
+
|
67
70
|
# Retrieve a column format.
|
68
71
|
#
|
69
72
|
# ====Examples
|
@@ -111,7 +114,11 @@ module ArToHtmlTable
|
|
111
114
|
end
|
112
115
|
alias :format_attribute :format_column
|
113
116
|
|
114
|
-
private
|
117
|
+
private
|
118
|
+
def procify(symbol)
|
119
|
+
proc { |*args| send(symbol, *args) }
|
120
|
+
end
|
121
|
+
|
115
122
|
# Default column formats used in to_table for active_record
|
116
123
|
# result arrays
|
117
124
|
#
|
@@ -130,7 +137,7 @@ module ArToHtmlTable
|
|
130
137
|
when :text, :string
|
131
138
|
{ :formatter => lambda {|*args| args[0]} }
|
132
139
|
when :date, :datetime
|
133
|
-
{ :formatter => lambda {|*args| args[0].to_s(:db)} }
|
140
|
+
{ :formatter => lambda {|*args| (args[0].is_a?(Date) || args[0].is_a?(DateTime)) ? args[0].to_s(:db) : args[0].to_s} }
|
134
141
|
else
|
135
142
|
{ :formatter => lambda {|*args| args[0].to_s} }
|
136
143
|
end
|
@@ -39,7 +39,7 @@ module ArToHtmlTable
|
|
39
39
|
if options[:cell_type] == :th
|
40
40
|
val
|
41
41
|
else
|
42
|
-
val.blank? ? I18n.t(options[:not_set_key]) : val
|
42
|
+
val.blank? ? I18n.t(options[:not_set_key] || 'tables.not_set') : val
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
@@ -63,7 +63,7 @@ module ArToHtmlTable
|
|
63
63
|
if options[:cell_type] == :th
|
64
64
|
val
|
65
65
|
else
|
66
|
-
val.blank? ? I18n.t(options[:unknown_key]) : val
|
66
|
+
val.blank? ? I18n.t(options[:unknown_key] || 'tables.unknown') : val
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
@@ -73,7 +73,7 @@ module ArToHtmlTable
|
|
73
73
|
# ====Examples
|
74
74
|
#
|
75
75
|
# # Given a value of 3600, the formatter will output
|
76
|
-
# 00:05:00
|
76
|
+
# => 00:05:00
|
77
77
|
#
|
78
78
|
# val: the value to be formatted
|
79
79
|
# options: formatter options
|
@@ -92,21 +92,113 @@ module ArToHtmlTable
|
|
92
92
|
# ====Examples
|
93
93
|
#
|
94
94
|
# # Given a value of 11, the formatter will output
|
95
|
-
# 11:00
|
95
|
+
# => 11:00
|
96
96
|
#
|
97
97
|
# val: the value to be formatted
|
98
98
|
# options: formatter options
|
99
99
|
def hours_to_time(val, options)
|
100
100
|
"#{"%02d" % val}:00"
|
101
101
|
end
|
102
|
+
|
103
|
+
# Ordinalize a number (ie 1st, 2nd, 3rd, ....). Localization is
|
104
|
+
# handled externally to this method
|
105
|
+
#
|
106
|
+
# ====Examples
|
107
|
+
#
|
108
|
+
# # Given a value of 14, the formatter will output
|
109
|
+
# => 14th
|
110
|
+
#
|
111
|
+
# val: the value to be formatted
|
112
|
+
# options: formatter options
|
113
|
+
def ordinalize(val, options)
|
114
|
+
val ? val.to_i.ordinalize : val
|
115
|
+
end
|
116
|
+
|
117
|
+
# Display as a short date (localized)
|
118
|
+
#
|
119
|
+
# ====Examples
|
120
|
+
#
|
121
|
+
# # Given a value of 2010/10/1, the formatter will output
|
122
|
+
# => 1 Oct
|
123
|
+
#
|
124
|
+
# val: the value to be formatted
|
125
|
+
# options: formatter options
|
126
|
+
def short_date(val, options)
|
127
|
+
val ? val.to_date.to_s(:short) : val
|
128
|
+
end
|
102
129
|
|
130
|
+
# Display as a long date (localized)
|
131
|
+
#
|
132
|
+
# ====Examples
|
133
|
+
#
|
134
|
+
# # Given a value of 2010/10/1, the formatter will output
|
135
|
+
# => October 1, 2010
|
136
|
+
#
|
137
|
+
# val: the value to be formatted
|
138
|
+
# options: formatter options
|
139
|
+
def long_date(val, options)
|
140
|
+
val ? val.to_date.to_s(:long) : val
|
141
|
+
end
|
142
|
+
|
143
|
+
# Display as a long month name (localized)
|
144
|
+
#
|
145
|
+
# ====Examples
|
146
|
+
#
|
147
|
+
# # Given a value of 9, the formatter will output
|
148
|
+
# => 'September'
|
149
|
+
#
|
150
|
+
# val: the value to be formatted
|
151
|
+
# options: formatter options
|
152
|
+
def long_month_name(val, options)
|
153
|
+
val ? I18n.t('date.month_names')[val.to_i] : val
|
154
|
+
end
|
155
|
+
|
156
|
+
# Display as a short month name (localized)
|
157
|
+
#
|
158
|
+
# ====Examples
|
159
|
+
#
|
160
|
+
# # Given a value of 9, the formatter will output
|
161
|
+
# => 'Sep'
|
162
|
+
#
|
163
|
+
# val: the value to be formatted
|
164
|
+
# options: formatter options
|
165
|
+
def short_month_name(val, options)
|
166
|
+
val ? I18n.t('date.abbr_month_names')[val.to_i] : val
|
167
|
+
end
|
168
|
+
|
169
|
+
# Display as a long day name (localized)
|
170
|
+
#
|
171
|
+
# ====Examples
|
172
|
+
#
|
173
|
+
# # Given a value of 1, the formatter will output
|
174
|
+
# => 'Monday'
|
175
|
+
#
|
176
|
+
# val: the value to be formatted
|
177
|
+
# options: formatter options
|
178
|
+
def long_day_name(val, options)
|
179
|
+
val ? I18n.t('date.day_names')[val.to_i] : val
|
180
|
+
end
|
181
|
+
|
182
|
+
# Display as a short day name (localized)
|
183
|
+
#
|
184
|
+
# ====Examples
|
185
|
+
#
|
186
|
+
# # Given a value of 1, the formatter will output
|
187
|
+
# => 'Mon'
|
188
|
+
#
|
189
|
+
# val: the value to be formatted
|
190
|
+
# options: formatter options
|
191
|
+
def short_day_name(val, options)
|
192
|
+
val ? I18n.t('date.abbr_day_names')[val.to_i] : val
|
193
|
+
end
|
194
|
+
|
103
195
|
# Interprets an integer as a percentage with a single
|
104
196
|
# digit of precision. Shim for <tt>#number_to_percentage</tt>
|
105
197
|
#
|
106
198
|
# ====Examples
|
107
199
|
#
|
108
200
|
# # Given a value of 48, the formatter will output
|
109
|
-
# 48.00%
|
201
|
+
# => 48.00%
|
110
202
|
#
|
111
203
|
# val: the value to be formatted
|
112
204
|
# options: formatter options
|
@@ -1,8 +1,5 @@
|
|
1
1
|
module ArToHtmlTable
|
2
2
|
class TableFormatter
|
3
|
-
include ArToHtmlTable::ColumnFormatter
|
4
|
-
include ::ActionView::Helpers::NumberHelper
|
5
|
-
|
6
3
|
attr_accessor :html, :table_columns, :klass, :merged_options, :rows, :totals
|
7
4
|
attr_accessor :column_cache
|
8
5
|
|
@@ -142,24 +139,23 @@ module ArToHtmlTable
|
|
142
139
|
|
143
140
|
# Outputs one cells value after invoking its formatter
|
144
141
|
def output_cell_value(cell_type, value, column, options = {})
|
142
|
+
# raise ArgumentError, "Column has no formatter: #{column[:name]}." unless column[:formatter]
|
145
143
|
column_name = column[:name].to_sym
|
146
144
|
column_cache[column_name] = {} unless column_cache.has_key?(column_name)
|
147
145
|
|
148
146
|
if column_cache[column_name].has_key?(value)
|
149
147
|
result = column_cache[column_name][value]
|
148
|
+
elsif column[:formatter]
|
149
|
+
result = column[:formatter].call(value, options.reverse_merge({:cell_type => cell_type, :column => column})) || ''
|
150
|
+
column_cache[column_name][value] = result
|
150
151
|
else
|
151
|
-
result =
|
152
|
-
result ||= ''
|
152
|
+
result = value.to_s || ''
|
153
153
|
column_cache[column_name][value] = result
|
154
154
|
end
|
155
155
|
html.__send__(cell_type, (column[:class] ? {:class => column[:class]} : {})) do
|
156
156
|
html << result
|
157
157
|
end
|
158
158
|
end
|
159
|
-
|
160
|
-
def self.procify(symbol)
|
161
|
-
proc { |*args| send(symbol, *args) }
|
162
|
-
end
|
163
159
|
|
164
160
|
private
|
165
161
|
# Craft a CSS id
|
@@ -167,15 +163,6 @@ module ArToHtmlTable
|
|
167
163
|
"#{klass.name.underscore}_#{row[klass.primary_key]}"
|
168
164
|
end
|
169
165
|
|
170
|
-
def default_formatter(data, options)
|
171
|
-
case data
|
172
|
-
when Fixnum
|
173
|
-
integer_with_delimiter(data, options)
|
174
|
-
else
|
175
|
-
data.to_s
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
166
|
def table_has_totals?
|
180
167
|
!totals.empty?
|
181
168
|
end
|
@@ -218,47 +205,20 @@ module ArToHtmlTable
|
|
218
205
|
|
219
206
|
def column_definition(column)
|
220
207
|
@column_order += 1
|
221
|
-
|
222
|
-
|
223
|
-
css_class, formatter = get_column_formatter(column.to_s)
|
224
|
-
column_order = klass.format_of(column)[:order] || @column_order
|
225
|
-
totals = klass.format_of(column)[:total]
|
208
|
+
format_options = klass.format_of(column)
|
226
209
|
return {
|
227
210
|
:name => column,
|
228
211
|
:label => klass.human_attribute_name(column),
|
229
|
-
:formatter => formatter
|
230
|
-
:class =>
|
231
|
-
:order => column_order,
|
232
|
-
:total =>
|
212
|
+
:formatter => format_options[:formatter],
|
213
|
+
:class => format_options[:class],
|
214
|
+
:order => format_options[:order] || @column_order,
|
215
|
+
:total => format_options[:total]
|
233
216
|
}
|
234
217
|
end
|
235
218
|
|
236
219
|
def columns_from_row(row)
|
237
220
|
row.attributes.inject([]) {|columns, (k, v)| columns << k.to_s }
|
238
221
|
end
|
239
|
-
|
240
|
-
def get_column_formatter(column)
|
241
|
-
format = klass.format_of(column)
|
242
|
-
case format
|
243
|
-
when Symbol
|
244
|
-
formatter = procify(format)
|
245
|
-
when Proc
|
246
|
-
formatter = format
|
247
|
-
when Hash
|
248
|
-
css_class = format[:class] if format[:class]
|
249
|
-
formatter = format[:formatter] if format[:formatter]
|
250
|
-
formatter = procify(formatter) if formatter && formatter.is_a?(Symbol)
|
251
|
-
end
|
252
|
-
return css_class, formatter
|
253
|
-
end
|
254
|
-
|
255
|
-
# A data formatter can be a symbol or a proc
|
256
|
-
# If its a symbol then we 'procify' it so that
|
257
|
-
# we have on calling interface in the output_cell method
|
258
|
-
# - partially for clarity and partially for performance
|
259
|
-
def procify(sym)
|
260
|
-
self.procify(sym)
|
261
|
-
end
|
262
222
|
|
263
223
|
# Decide if the given column is to be displayed in the table
|
264
224
|
def include_column?(column, options)
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar_to_html_table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 4
|
10
|
+
version: 0.1.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kip Cole
|
@@ -15,55 +15,10 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-10-
|
18
|
+
date: 2010-10-21 00:00:00 +08:00
|
19
19
|
default_executable:
|
20
|
-
dependencies:
|
21
|
-
|
22
|
-
name: builder
|
23
|
-
prerelease: false
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ">="
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
hash: 3
|
30
|
-
segments:
|
31
|
-
- 0
|
32
|
-
version: "0"
|
33
|
-
type: :runtime
|
34
|
-
version_requirements: *id001
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: activerecord
|
37
|
-
prerelease: false
|
38
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
-
none: false
|
40
|
-
requirements:
|
41
|
-
- - ~>
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
hash: 9
|
44
|
-
segments:
|
45
|
-
- 2
|
46
|
-
- 3
|
47
|
-
- 5
|
48
|
-
version: 2.3.5
|
49
|
-
type: :runtime
|
50
|
-
version_requirements: *id002
|
51
|
-
- !ruby/object:Gem::Dependency
|
52
|
-
name: actionpack
|
53
|
-
prerelease: false
|
54
|
-
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
-
none: false
|
56
|
-
requirements:
|
57
|
-
- - ~>
|
58
|
-
- !ruby/object:Gem::Version
|
59
|
-
hash: 9
|
60
|
-
segments:
|
61
|
-
- 2
|
62
|
-
- 3
|
63
|
-
- 5
|
64
|
-
version: 2.3.5
|
65
|
-
type: :runtime
|
66
|
-
version_requirements: *id003
|
20
|
+
dependencies: []
|
21
|
+
|
67
22
|
description: " Defines Array#to_table that will render an ActiveRecord result set\n as an HTML table.\n"
|
68
23
|
email:
|
69
24
|
- kipcole9@gmail.com
|