ar_to_html_table 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +13 -5
- data/Rakefile +7 -0
- data/ar_to_html_table.gemspec +5 -2
- data/lib/ar_to_html_table/column_formats.rb +6 -5
- data/lib/ar_to_html_table/table_formatter.rb +5 -1
- data/lib/ar_to_html_table/version.rb +1 -1
- data/lib/tasks/ar_to_html_tables_tasks.rake +0 -0
- data/test/boot.rb +21 -0
- data/test/database.yml +22 -0
- data/test/fixtures/schema.rb +11 -0
- data/test/helper.rb +26 -0
- data/test/lib/activerecord_test_case.rb +44 -0
- data/test/lib/activerecord_test_connector.rb +76 -0
- data/test/test_ar_to_html_table.rb +12 -0
- metadata +53 -8
- data/lib/tasks/html_tables_tasks.rake +0 -4
data/README.textile
CHANGED
@@ -47,7 +47,7 @@ h2. Column options
|
|
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
48
|
|:total|Renders a table footer with a calculation of all the values in the column. The available totaling methods are **:sum**, **:count** and **:average** (or :avg or :mean)|
|
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
|
-
|:formatter|
|
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
|
|
52
52
|
h2. Predefined formatters
|
53
53
|
|
@@ -62,11 +62,16 @@ h2. Predefined formatters
|
|
62
62
|
|:unknown_on_blank|Displays **(unknown)** when **column.blank?** is true. This is a localized value. The key **I18n.t('tables.unknown')** is used.|
|
63
63
|
|:not_set_on_blank|Displays **(not set)** when **column.blank?** is true. This is a localized value. The key **I18n.t('tables.not_set')** is used.|
|
64
64
|
|
65
|
-
p. If no formatter is specified then
|
65
|
+
p. If no formatter is specified then:
|
66
|
+
|
67
|
+
* string and text values are used unmodified
|
68
|
+
* date and datetime values are called on **to_s(:db)**
|
69
|
+
* integers and floats are called on **number_with_delimiter**
|
70
|
+
* **to_s** is called on all other column types.
|
66
71
|
|
67
72
|
h2. Table rendering
|
68
73
|
|
69
|
-
To render the html table, call
|
74
|
+
To render the html table, call **to_table(options)** on any ActiveRecord result set. The default options are:
|
70
75
|
|
71
76
|
bc. :exclude => EXCLUDE_COLUMNS,
|
72
77
|
:exclude_ids => true,
|
@@ -76,7 +81,10 @@ bc. :exclude => EXCLUDE_COLUMNS,
|
|
76
81
|
:total_one => 'tables.total_one',
|
77
82
|
:total_many => 'tables.total_many',
|
78
83
|
:unknown_key => 'tables.unknown',
|
79
|
-
:not_set_key => 'tables.not_set'
|
84
|
+
:not_set_key => 'tables.not_set',
|
85
|
+
:heading => nil,
|
86
|
+
:caption => nil,
|
87
|
+
:sort => nil
|
80
88
|
|
81
89
|
|_. Option|_. Description|
|
82
90
|
|:include|Array of columns that should be rendered|
|
@@ -84,7 +92,7 @@ bc. :exclude => EXCLUDE_COLUMNS,
|
|
84
92
|
|:exclude_ids|Don't render columns that end in **_id**|
|
85
93
|
|:sort|A **Proc** that is called to sort the rows. Called as **results.sort(options[:sort])**.|
|
86
94
|
|:heading|A table heading that is placed in the first row of a table|
|
87
|
-
|:caption|A table caption applied
|
95
|
+
|:caption|A table caption applied|
|
88
96
|
|:odd_row|CSS class name for odd rows|
|
89
97
|
|:even_row|CSS class name for even rows|
|
90
98
|
|:totals|Add a total row at the bottom of the table|
|
data/Rakefile
CHANGED
data/ar_to_html_table.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.authors = ["Kip Cole"]
|
10
10
|
s.email = ["kipcole9@gmail.com"]
|
11
11
|
s.homepage = "http://github.com/kipcole9/ar_to_html_table"
|
12
|
-
s.summary = %q{Render
|
12
|
+
s.summary = %q{Render an ActiveRecord result set as an HTML table}
|
13
13
|
s.description = <<-EOF
|
14
14
|
Defines Array#to_table that will render an ActiveRecord result set
|
15
15
|
as an HTML table.
|
@@ -21,5 +21,8 @@ Gem::Specification.new do |s|
|
|
21
21
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
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
28
|
end
|
@@ -59,7 +59,7 @@ module ArToHtmlTable
|
|
59
59
|
# => { :total => :sum, :order => 5, :class => 'right' }
|
60
60
|
def format_of(name)
|
61
61
|
@attr_formats ||= default_formats
|
62
|
-
@attr_formats[name] || {}
|
62
|
+
@attr_formats[name.to_s] || {}
|
63
63
|
end
|
64
64
|
|
65
65
|
private
|
@@ -76,13 +76,14 @@ module ArToHtmlTable
|
|
76
76
|
columns.each do |column|
|
77
77
|
attr_formats[column.name] = case column.type
|
78
78
|
when :integer, :float
|
79
|
-
{:class => :right,
|
79
|
+
{ :class => :right,
|
80
|
+
:formatter => lambda {|*args| number_with_delimiter(args[0])} }
|
80
81
|
when :text, :string
|
81
|
-
{}
|
82
|
+
{ :formatter => lambda {|*args| args[0]} }
|
82
83
|
when :date, :datetime
|
83
|
-
{}
|
84
|
+
{ :formatter => lambda {|*args| args[0].to_s(:db)} }
|
84
85
|
else
|
85
|
-
{}
|
86
|
+
{ :formatter => lambda {|*args| args[0].to_s} }
|
86
87
|
end
|
87
88
|
end
|
88
89
|
attr_formats
|
@@ -156,6 +156,10 @@ module ArToHtmlTable
|
|
156
156
|
html << result
|
157
157
|
end
|
158
158
|
end
|
159
|
+
|
160
|
+
def self.procify(symbol)
|
161
|
+
proc { |*args| send(symbol, *args) }
|
162
|
+
end
|
159
163
|
|
160
164
|
private
|
161
165
|
# Craft a CSS id
|
@@ -253,7 +257,7 @@ module ArToHtmlTable
|
|
253
257
|
# we have on calling interface in the output_cell method
|
254
258
|
# - partially for clarity and partially for performance
|
255
259
|
def procify(sym)
|
256
|
-
|
260
|
+
self.procify(sym)
|
257
261
|
end
|
258
262
|
|
259
263
|
# Decide if the given column is to be displayed in the table
|
File without changes
|
data/test/boot.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
plugin_root = File.join(File.dirname(__FILE__), '..')
|
2
|
+
version = ENV['RAILS_VERSION']
|
3
|
+
version = nil if version and version == ""
|
4
|
+
|
5
|
+
# first look for a symlink to a copy of the framework
|
6
|
+
if !version and framework_root = ["#{plugin_root}/rails", "#{plugin_root}/../../rails"].find { |p| File.directory? p }
|
7
|
+
puts "found framework root: #{framework_root}"
|
8
|
+
# this allows for a plugin to be tested outside of an app and without Rails gems
|
9
|
+
$:.unshift "#{framework_root}/activesupport/lib", "#{framework_root}/activerecord/lib", "#{framework_root}/actionpack/lib"
|
10
|
+
else
|
11
|
+
# simply use installed gems if available
|
12
|
+
puts "using Rails#{version ? ' ' + version : nil} gems"
|
13
|
+
require 'rubygems'
|
14
|
+
|
15
|
+
if version
|
16
|
+
gem 'rails', version
|
17
|
+
else
|
18
|
+
gem 'actionpack', '< 3.0.0.a'
|
19
|
+
gem 'activerecord', '< 3.0.0.a'
|
20
|
+
end
|
21
|
+
end
|
data/test/database.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
sqlite3:
|
2
|
+
database: ":memory:"
|
3
|
+
adapter: sqlite3
|
4
|
+
timeout: 500
|
5
|
+
|
6
|
+
sqlite2:
|
7
|
+
database: ":memory:"
|
8
|
+
adapter: sqlite2
|
9
|
+
|
10
|
+
mysql:
|
11
|
+
adapter: mysql
|
12
|
+
username: root
|
13
|
+
password:
|
14
|
+
encoding: utf8
|
15
|
+
database: will_paginate_unittest
|
16
|
+
|
17
|
+
postgres:
|
18
|
+
adapter: postgresql
|
19
|
+
username: mislav
|
20
|
+
password:
|
21
|
+
database: will_paginate_unittest
|
22
|
+
min_messages: warning
|
data/test/helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
|
4
|
+
# gem install redgreen for colored test output
|
5
|
+
begin require 'redgreen'; rescue LoadError; end
|
6
|
+
|
7
|
+
require 'boot' unless defined?(ActiveRecord)
|
8
|
+
|
9
|
+
class Test::Unit::TestCase
|
10
|
+
protected
|
11
|
+
def assert_respond_to_all object, methods
|
12
|
+
methods.each do |method|
|
13
|
+
[method.to_s, method.to_sym].each { |m| assert_respond_to object, m }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
# Wrap tests that use Mocha and skip if unavailable.
|
20
|
+
def uses_mocha(test_name)
|
21
|
+
require 'mocha'
|
22
|
+
rescue LoadError
|
23
|
+
$stderr.puts "Skipping #{test_name} tests. `gem install mocha` and try again."
|
24
|
+
else
|
25
|
+
yield
|
26
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'lib/activerecord_test_connector'
|
2
|
+
|
3
|
+
class ActiveRecordTestCase < Test::Unit::TestCase
|
4
|
+
if defined?(ActiveSupport::Testing::SetupAndTeardown)
|
5
|
+
include ActiveSupport::Testing::SetupAndTeardown
|
6
|
+
end
|
7
|
+
|
8
|
+
if defined?(ActiveRecord::TestFixtures)
|
9
|
+
include ActiveRecord::TestFixtures
|
10
|
+
end
|
11
|
+
|
12
|
+
# Set our fixture path
|
13
|
+
if ActiveRecordTestConnector.able_to_connect
|
14
|
+
self.fixture_path = File.join(File.dirname(__FILE__), '..', 'fixtures')
|
15
|
+
self.use_transactional_fixtures = true
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.fixtures(*args)
|
19
|
+
super if ActiveRecordTestConnector.connected
|
20
|
+
end
|
21
|
+
|
22
|
+
def run(*args)
|
23
|
+
super if ActiveRecordTestConnector.connected
|
24
|
+
end
|
25
|
+
|
26
|
+
# Default so Test::Unit::TestCase doesn't complain
|
27
|
+
def test_truth
|
28
|
+
end
|
29
|
+
|
30
|
+
protected
|
31
|
+
|
32
|
+
def assert_queries(num = 1)
|
33
|
+
$query_count = 0
|
34
|
+
yield
|
35
|
+
ensure
|
36
|
+
assert_equal num, $query_count, "#{$query_count} instead of #{num} queries were executed."
|
37
|
+
end
|
38
|
+
|
39
|
+
def assert_no_queries(&block)
|
40
|
+
assert_queries(0, &block)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
ActiveRecordTestConnector.setup
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_record/version'
|
3
|
+
require 'active_record/fixtures'
|
4
|
+
|
5
|
+
class ActiveRecordTestConnector
|
6
|
+
cattr_accessor :able_to_connect
|
7
|
+
cattr_accessor :connected
|
8
|
+
|
9
|
+
FIXTURES_PATH = File.join(File.dirname(__FILE__), '..', 'fixtures')
|
10
|
+
|
11
|
+
# Set our defaults
|
12
|
+
self.connected = false
|
13
|
+
self.able_to_connect = true
|
14
|
+
|
15
|
+
def self.setup
|
16
|
+
unless self.connected || !self.able_to_connect
|
17
|
+
setup_connection
|
18
|
+
load_schema
|
19
|
+
add_load_path FIXTURES_PATH
|
20
|
+
self.connected = true
|
21
|
+
end
|
22
|
+
rescue Exception => e # errors from ActiveRecord setup
|
23
|
+
$stderr.puts "\nSkipping ActiveRecord tests: #{e}\n\n"
|
24
|
+
self.able_to_connect = false
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def self.add_load_path(path)
|
30
|
+
dep = defined?(ActiveSupport::Dependencies) ? ActiveSupport::Dependencies : ::Dependencies
|
31
|
+
autoload_paths = dep.respond_to?(:autoload_paths) ? dep.autoload_paths : dep.load_paths
|
32
|
+
autoload_paths.unshift path
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.setup_connection
|
36
|
+
db = ENV['DB'].blank?? 'sqlite3' : ENV['DB']
|
37
|
+
|
38
|
+
configurations = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'database.yml'))
|
39
|
+
raise "no configuration for '#{db}'" unless configurations.key? db
|
40
|
+
configuration = configurations[db]
|
41
|
+
|
42
|
+
ActiveRecord::Base.logger = Logger.new(STDOUT) if $0 == 'irb'
|
43
|
+
puts "using #{configuration['adapter']} adapter" unless ENV['DB'].blank?
|
44
|
+
|
45
|
+
gem 'sqlite3-ruby' if 'sqlite3' == db
|
46
|
+
|
47
|
+
ActiveRecord::Base.establish_connection(configuration)
|
48
|
+
ActiveRecord::Base.configurations = { db => configuration }
|
49
|
+
prepare ActiveRecord::Base.connection
|
50
|
+
|
51
|
+
unless Object.const_defined?(:QUOTED_TYPE)
|
52
|
+
Object.send :const_set, :QUOTED_TYPE, ActiveRecord::Base.connection.quote_column_name('type')
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.load_schema
|
57
|
+
ActiveRecord::Base.silence do
|
58
|
+
ActiveRecord::Migration.verbose = false
|
59
|
+
load File.join(FIXTURES_PATH, 'schema.rb')
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.prepare(conn)
|
64
|
+
class << conn
|
65
|
+
IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SHOW FIELDS /]
|
66
|
+
|
67
|
+
def execute_with_counting(sql, name = nil, &block)
|
68
|
+
$query_count ||= 0
|
69
|
+
$query_count += 1 unless IGNORED_SQL.any? { |r| sql =~ r }
|
70
|
+
execute_without_counting(sql, name, &block)
|
71
|
+
end
|
72
|
+
|
73
|
+
alias_method_chain :execute, :counting
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'lib/activerecord_test_case'
|
3
|
+
|
4
|
+
class FinderTest < ActiveRecordTestCase
|
5
|
+
# fixtures :topics, :replies, :users, :projects, :developers_projects
|
6
|
+
|
7
|
+
def test_startup
|
8
|
+
assert true
|
9
|
+
# assert_respond_to_all Topic, %w(per_page paginate paginate_by_sql paginate_by_definition_in_class)
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
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: 31
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kip Cole
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-10-
|
18
|
+
date: 2010-10-20 00:00:00 +08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -32,6 +32,38 @@ dependencies:
|
|
32
32
|
version: "0"
|
33
33
|
type: :runtime
|
34
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
|
35
67
|
description: " Defines Array#to_table that will render an ActiveRecord result set\n as an HTML table.\n"
|
36
68
|
email:
|
37
69
|
- kipcole9@gmail.com
|
@@ -55,7 +87,14 @@ files:
|
|
55
87
|
- lib/ar_to_html_table/version.rb
|
56
88
|
- lib/locale/cldr_additions.yml
|
57
89
|
- lib/locale/en.yml
|
58
|
-
- lib/tasks/
|
90
|
+
- lib/tasks/ar_to_html_tables_tasks.rake
|
91
|
+
- test/boot.rb
|
92
|
+
- test/database.yml
|
93
|
+
- test/fixtures/schema.rb
|
94
|
+
- test/helper.rb
|
95
|
+
- test/lib/activerecord_test_case.rb
|
96
|
+
- test/lib/activerecord_test_connector.rb
|
97
|
+
- test/test_ar_to_html_table.rb
|
59
98
|
has_rdoc: true
|
60
99
|
homepage: http://github.com/kipcole9/ar_to_html_table
|
61
100
|
licenses: []
|
@@ -89,6 +128,12 @@ rubyforge_project: ar_to_html_table
|
|
89
128
|
rubygems_version: 1.3.7
|
90
129
|
signing_key:
|
91
130
|
specification_version: 3
|
92
|
-
summary: Render
|
93
|
-
test_files:
|
94
|
-
|
131
|
+
summary: Render an ActiveRecord result set as an HTML table
|
132
|
+
test_files:
|
133
|
+
- test/boot.rb
|
134
|
+
- test/database.yml
|
135
|
+
- test/fixtures/schema.rb
|
136
|
+
- test/helper.rb
|
137
|
+
- test/lib/activerecord_test_case.rb
|
138
|
+
- test/lib/activerecord_test_connector.rb
|
139
|
+
- test/test_ar_to_html_table.rb
|