public
Fork of choonkeat/poormans-trends
Description: Plug and play trend visualisation for your data.
Homepage: http://blog.choonkeat.com/weblog/2008/04/poor-mans-trend.html
Clone URL: git://github.com/dustin/poormans-trends.git
Search Repo:
Lookup associations when building charts.
dustin (author)
Thu Apr 24 10:22:32 -0700 2008
dustin (committer)
Thu Apr 24 10:50:24 -0700 2008
commit  69942f696b597152a9f1db62b755fa13ebd57612
tree    d0b690e2e4d20573d03e611f69a341e827cc8e53
parent  e96eb8e3064b6da4905c0c695a2c5beb8066271a
...
40
41
42
 
 
43
44
 
45
46
47
...
75
76
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
79
...
40
41
42
43
44
45
46
47
48
49
50
...
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
0
@@ -40,8 +40,11 @@
0
           type_cols = @all_columns.select {|x| x =~ /(^type|_type)$/ }
0
           @columns = foreign_keys + type_cols
0
           @top_values = {}
0
+ @ass_lookup = {}
0
+ @ass_lookup['_total_'] = Proc.new { |i| 'Total' }
0
         
0
           @columns.each do |col|
0
+ @ass_lookup[col] = create_lookup(klass, col)
0
             @top_values[col] = klass.find(:all, {
0
               :select => "COUNT(*) as #{col}_count, #{col}",
0
               :conditions => ["FLOOR(DATEDIFF(NOW(), #{date_col}) / 7) <= ?", @max_cols],
0
@@ -75,6 +78,20 @@
0
       @classname=params[:id]
0
       render :file => File.join(File.dirname(__FILE__), '..', 'views', 'error.html.erb')
0
     end
0
+
0
+ def create_lookup(klass, colname)
0
+ Proc.new do |i|
0
+ type = colname.gsub(/_id$/, '').to_sym
0
+ rel = klass.reflect_on_association(type)
0
+ if rel && i
0
+ sym = rel.klass.respond_to?(:get_cache) ? :get_cache : :find
0
+ ob = rel.klass.send(sym, i)
0
+ else
0
+ i ? i : 'nil'
0
+ end
0
+ end
0
+ end
0
+
0
   end
0
 end
...
1
2
3
4
 
5
6
7
...
18
19
20
21
 
22
23
24
...
1
2
3
 
4
5
6
7
...
18
19
20
 
21
22
23
24
0
@@ -1,7 +1,7 @@
0
 <div>
0
   <h1 style="margin-bottom: 0px; "><%= params[:id] %></h1>
0
   <h2 style="margin-top: 0px; "><%=h @all_columns.inspect %></h2>
0
-
0
+
0
   <% @columns.each_with_index do |col, index| %>
0
   <table id="dataTable<%= index %>" summary="<%=h col %>">
0
     <caption><%=h col %></caption>
0
@@ -18,7 +18,7 @@
0
     <tbody>
0
     <% @top_values[col].each do |hash| %>
0
       <tr>
0
- <th headers="members"><%=h hash.keys.first || 'nil' %></th>
0
+ <th headers="members"><%= h(@ass_lookup[col].call(hash.keys.first)) %></th>
0
         <% (0..@max_cols).to_a.reverse.each do |val| %>
0
         <td headers="<%= col %>_<%= val %>"><%=h hash.values.first[val.to_s] || 0 %></td>
0
        <% end %>

Comments

    No one has commented yet.