ar_to_html_table 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -82,9 +82,9 @@ bc. :exclude => EXCLUDE_COLUMNS,
82
82
  :total_many => 'tables.total_many',
83
83
  :unknown_key => 'tables.unknown',
84
84
  :not_set_key => 'tables.not_set',
85
- :heading => nil,
86
- :caption => nil,
87
- :sort => nil
85
+ :heading => nil,
86
+ :caption => nil,
87
+ :sort => nil
88
88
 
89
89
  |_. Option|_. Description|
90
90
  |:include|Array of columns that should be rendered|
@@ -101,6 +101,25 @@ bc. :exclude => EXCLUDE_COLUMNS,
101
101
  |:unknown_key|I18n key for rendering **(Unknown)**|
102
102
  |:not_set_key|I18n key for rendering **(Not Set)**|
103
103
 
104
+ h2. Using formatters outside ar_to_html_table
105
+
106
+ Once defined the formatters can be used in other places. Both class and instance methods are provided. Best explained by the following example:
107
+
108
+ bc. class Product < ActiveRecord::Base
109
+ column_format :name, :order => 1
110
+ column_format :orders, :total => :sum
111
+ column_format :revenue, :total => :sum, :order => 5, :class => 'right'
112
+ column_format :age, :total => :avg, :order => 20, :class => 'right', :formatter => :number_with_delimiter
113
+ end
114
+
115
+ # p = Product.first
116
+ => #<Product id: 55986, age: 4346, .........
117
+ # p.age
118
+ => 4346
119
+
120
+ # p.format_column(:age)
121
+ => "4,346"
122
+
104
123
  h1. License
105
124
 
106
125
  (The MIT License)
@@ -10,7 +10,31 @@ module ArToHtmlTable
10
10
  end
11
11
 
12
12
  module InstanceMethods
13
-
13
+ # Invoke the formatter on a column.
14
+ #
15
+ # ====Examples
16
+ #
17
+ # class Product < ActiveRecord::Base
18
+ # column_format :name, :order => 1
19
+ # column_format :orders, :total => :sum
20
+ # column_format :revenue, :total => :sum, :order => 5, :class => 'right'
21
+ # column_format :age, :total => :avg, :order => 20, :class => 'right', :formatter => :number_with_delimiter
22
+ # end
23
+ #
24
+ # # p = Product.first
25
+ # # p.age
26
+ # => 4346
27
+ #
28
+ # # p.format_column(:age)
29
+ # => "4,346"
30
+ #
31
+ # ====Parameters
32
+ #
33
+ # column_name: A column (attribute) on the the model instance
34
+ def format_column(column_name)
35
+ self.class.format_column(column_name, self[column_name])
36
+ end
37
+ alias :format_attribute :format_column
14
38
  end
15
39
 
16
40
  module ClassMethods
@@ -61,6 +85,31 @@ module ArToHtmlTable
61
85
  @attr_formats ||= default_formats
62
86
  @attr_formats[name.to_s] || {}
63
87
  end
88
+
89
+ # Invoke the formatter on a column.
90
+ #
91
+ # ====Examples
92
+ #
93
+ # class Product < ActiveRecord::Base
94
+ # column_format :name, :order => 1
95
+ # column_format :orders, :total => :sum
96
+ # column_format :revenue, :total => :sum, :order => 5, :class => 'right'
97
+ # column_format :age, :total => :avg, :order => 20, :class => 'right', :formatter => :number_with_delimiter
98
+ # end
99
+ #
100
+ # # Product.format_column(:age, 4346)
101
+ # => "4,346"
102
+ #
103
+ # ====Parameters
104
+ #
105
+ # column_name: A column (attribute) on the the model instance
106
+ # value: The value to be formatted
107
+ def format_column(column_name, value)
108
+ formatter = format_of(column_name)[:formatter]
109
+ raise "Column #{column_name} has no configured formatter" unless formatter && formatter.is_a?(Proc)
110
+ formatter.call(value, {})
111
+ end
112
+ alias :format_attribute :format_column
64
113
 
65
114
  private
66
115
  # Default column formats used in to_table for active_record
@@ -1,3 +1,3 @@
1
1
  module ArToHtmlTable
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  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: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kip Cole