ar_to_html_table 0.1.9 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +17 -0
- data/README.textile +1 -1
- data/ar_to_html_table.gemspec +2 -3
- data/lib/ar_to_html_table/column_formats.rb +31 -7
- data/lib/ar_to_html_table/model.rb +2 -2
- data/lib/ar_to_html_table/table_formatter.rb +10 -21
- data/lib/ar_to_html_table/version.rb +1 -1
- metadata +20 -7
data/CHANGELOG
CHANGED
@@ -1,3 +1,20 @@
|
|
1
|
+
ar_to_html_table (0.2.0) October 30, 2010
|
2
|
+
|
3
|
+
* Table formatter now calls row instance method (DRY's up formatting)
|
4
|
+
and making column formatting stand alone from table formatting. Next
|
5
|
+
step separate column_formatting into a separate gem.
|
6
|
+
|
7
|
+
* Options can now be defined on the column_format call (:options = {})
|
8
|
+
which are passed to the formatter at invocation
|
9
|
+
|
10
|
+
* :trend is now a valid totaling method. Requires ar_results_calculations
|
11
|
+
|
12
|
+
* Add dependency for ar_result_calculations
|
13
|
+
|
14
|
+
ar_to_html_table (0.1.10) October 24, 2010
|
15
|
+
|
16
|
+
* Options can be passed in #format_column
|
17
|
+
|
1
18
|
ar_to_html_table (0.1.9) October 24, 2010
|
2
19
|
|
3
20
|
* Bar and percentage fixes for when % > 100
|
data/README.textile
CHANGED
@@ -45,7 +45,7 @@ h2. Column options
|
|
45
45
|
|
46
46
|
|_. Option|_. Description|
|
47
47
|
|:order|Positions a column order relative to other columns. The number isn't important, just its relative value compared to other columns. Columns are sorted by order and rendered in that order. The default order is the order in which the columns are defined.|
|
48
|
-
|:total|Renders a table footer with a calculation of all the values in the column. The available totaling methods are **:sum**, **:count
|
48
|
+
|:total|Renders a table footer with a calculation of all the values in the column. The available totaling methods are **:sum**, **:count**, **:average** (or :avg or :mean) and **trend** which is a calculated slope from a linear regression of the column|
|
49
49
|
|:class|The CSS class for this column. Note that a **colgroup** is defined for each column and each **colgroup** has as CSS class that is the column name|
|
50
50
|
|:formatter|A :symbol representing a method to format the value of each table cell. There are several predefined formatters but any method with a signature of **method(value, options = {})** will work, including the methods in ActionView::Helpers::NumberHelper. Lastly a lambda can be provided for arbitrary formatting.|
|
51
51
|
|
data/ar_to_html_table.gemspec
CHANGED
@@ -22,7 +22,6 @@ 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
|
-
|
26
|
-
|
27
|
-
#s.add_dependency 'actionpack', '~> 2.3.5'
|
25
|
+
s.add_dependency 'ar_result_calculations'
|
26
|
+
|
28
27
|
end
|
@@ -31,8 +31,26 @@ module ArToHtmlTable
|
|
31
31
|
# ====Parameters
|
32
32
|
#
|
33
33
|
# column_name: A column (attribute) on the the model instance
|
34
|
-
|
35
|
-
|
34
|
+
# options: Formatter options (passed to the formatter)
|
35
|
+
#
|
36
|
+
# ====Options and where they come from
|
37
|
+
#
|
38
|
+
# Options provided to a formatter are merged from several sources
|
39
|
+
# Depending on who is calling the formatter.
|
40
|
+
#
|
41
|
+
# table_formatter calls with options :cell_type and :column. :cell_type
|
42
|
+
# is either :td or :th depending on the type of cell the table formatter i
|
43
|
+
# populating. :column is the internal column definition used by the table_formatter
|
44
|
+
# which includes some attributes it uses.
|
45
|
+
#
|
46
|
+
# instance#format_column (this method) merges in the :row options which is the
|
47
|
+
# model instance.
|
48
|
+
#
|
49
|
+
# class#format_column (the class method) merges in the formatter options (defined
|
50
|
+
# as :options on the column_format definition)
|
51
|
+
def format_column(column_name, options = {})
|
52
|
+
formatter_options = options.merge(:row => self)
|
53
|
+
self.class.format_column(column_name, self[column_name], formatter_options)
|
36
54
|
end
|
37
55
|
alias :format_attribute :format_column
|
38
56
|
end
|
@@ -62,7 +80,7 @@ module ArToHtmlTable
|
|
62
80
|
# column_format :revenue, :total => :sum, :order => 5, :class => 'right'
|
63
81
|
# column_format :age, :total => :avg, :order => 20, :class => 'right', :formatter => :number_with_delimiter
|
64
82
|
# end
|
65
|
-
def column_format(method, options)
|
83
|
+
def column_format(method, options = {})
|
66
84
|
options[:formatter] = procify(options[:formatter]) if options[:formatter] && options[:formatter].is_a?(Symbol)
|
67
85
|
@attr_formats = (@attr_formats || default_formats).deep_merge({method.to_s => options})
|
68
86
|
end
|
@@ -108,10 +126,16 @@ module ArToHtmlTable
|
|
108
126
|
#
|
109
127
|
# column_name: A column (attribute) on the the model instance
|
110
128
|
# value: The value to be formatted
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
formatter
|
129
|
+
# options Formatter options
|
130
|
+
def format_column(column_name, value, options = {})
|
131
|
+
format = format_of(column_name)
|
132
|
+
if format && (formatter = format[:formatter])
|
133
|
+
formatter_options = options.merge(:options => format[:options])
|
134
|
+
formatter.call(value, formatter_options)
|
135
|
+
else
|
136
|
+
Rails.logger.debug "[table_formatter] Column #{column_name} has no configured formatter"
|
137
|
+
value.to_s
|
138
|
+
end
|
115
139
|
end
|
116
140
|
alias :format_attribute :format_column
|
117
141
|
|
@@ -19,8 +19,8 @@ module ArToHtmlTable
|
|
19
19
|
#
|
20
20
|
# See ArToHtmlTable::TableFormatter for options.
|
21
21
|
def to_table(options = {})
|
22
|
-
@
|
23
|
-
@
|
22
|
+
@table_formatter = ArToHtmlTable::TableFormatter.new(self, options)
|
23
|
+
@table_formatter.to_html
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -108,7 +108,7 @@ module ArToHtmlTable
|
|
108
108
|
html_options[:class] = (count.even? ? options[:even_row] : options[:odd_row])
|
109
109
|
html_options[:id] = row_id(row) if row[klass.primary_key]
|
110
110
|
html.tr html_options do
|
111
|
-
table_columns.each {|column| output_cell(row, column, options) }
|
111
|
+
table_columns.each {|column| output_cell(:td, row, column, options) }
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
@@ -125,32 +125,20 @@ module ArToHtmlTable
|
|
125
125
|
first_column = true
|
126
126
|
table_columns.each do |column|
|
127
127
|
value = first_column ? first_column_total(options) : totals[column[:name].to_s]
|
128
|
-
|
128
|
+
output_cell(:th, value, column, options)
|
129
129
|
first_column = false
|
130
130
|
end
|
131
131
|
end
|
132
132
|
end
|
133
133
|
end
|
134
134
|
|
135
|
-
# Outputs one cell
|
136
|
-
def output_cell(
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
# Outputs one cells value after invoking its formatter
|
141
|
-
def output_cell_value(cell_type, value, column, options = {})
|
142
|
-
# raise ArgumentError, "Column has no formatter: #{column[:name]}." unless column[:formatter]
|
143
|
-
column_name = column[:name].to_sym
|
144
|
-
column_cache[column_name] = {} unless column_cache.has_key?(column_name)
|
145
|
-
|
146
|
-
if column_cache[column_name].has_key?(value)
|
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
|
135
|
+
# Outputs one formatted cell
|
136
|
+
def output_cell(cell_type, row_or_value, column, options = {})
|
137
|
+
formatter_options = options.reverse_merge({:cell_type => cell_type, :column => column})
|
138
|
+
if row_or_value.class.respond_to? :descends_from_active_record?
|
139
|
+
result = row_or_value.format_column(column[:name], formatter_options)
|
151
140
|
else
|
152
|
-
result =
|
153
|
-
column_cache[column_name][value] = result
|
141
|
+
result = klass.format_column(column[:name], row_or_value, formatter_options)
|
154
142
|
end
|
155
143
|
html.__send__(cell_type, (column[:class] ? {:class => column[:class]} : {})) do
|
156
144
|
html << result.to_s
|
@@ -190,6 +178,8 @@ module ArToHtmlTable
|
|
190
178
|
totals[column[:name]] = rows.make_numeric(column[:name]).mean(column[:name])
|
191
179
|
when :count
|
192
180
|
totals[column[:name]] = rows.make_numeric(column[:name]).count(column[:name])
|
181
|
+
when :trend
|
182
|
+
totals[column[:name]] = rows.make_numeric(column[:name]).trend(column[:name])
|
193
183
|
end
|
194
184
|
totals
|
195
185
|
end
|
@@ -209,7 +199,6 @@ module ArToHtmlTable
|
|
209
199
|
return {
|
210
200
|
:name => column,
|
211
201
|
:label => klass.human_attribute_name(column),
|
212
|
-
:formatter => format_options[:formatter],
|
213
202
|
:class => format_options[:class],
|
214
203
|
:order => format_options[:order] || @column_order,
|
215
204
|
:total => format_options[:total]
|
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: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kip Cole
|
@@ -15,10 +15,23 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-10-
|
18
|
+
date: 2010-10-30 00:00:00 +08:00
|
19
19
|
default_executable:
|
20
|
-
dependencies:
|
21
|
-
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: ar_result_calculations
|
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
|
22
35
|
description: " Defines Array#to_table that will render an ActiveRecord result set\n as an HTML table.\n"
|
23
36
|
email:
|
24
37
|
- kipcole9@gmail.com
|