Home arrow Ruby-on-Rails arrow Page 4 - Building Reports Accessible from the Internet
RUBY-ON-RAILS

Building Reports Accessible from the Internet


In this second part of a two-part series, we'll finish creating reports that can be accessed via the Internet. This article is excerpted from chapter 5 of the book Practical Reporting with Ruby and Rails, written by David Berube (Apress; ISBN: 1590599330).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
March 26, 2010
TABLE OF CONTENTS:
  1. · Building Reports Accessible from the Internet
  2. · Examining the We b Report Application
  3. · Graphical Reporting with Rails
  4. · Creating the Models for the Graphical Report
  5. · Examining the Graphical Reporting Application

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Building Reports Accessible from the Internet - Creating the Models for the Graphical Report
(Page 4 of 5 )

This example will use four models: event (app/models/event.rb), game (app/models/game.rb), play (app/models/play.rb), and player (app/models/player.rb). Their code is shown in Listings 5-13 through 5-16.

Listing 5-13. Event Model (app/models/event.rb)

class Event < ActiveRecord::Base
  belongs_to :play
end

Listing 5-14. Game Model (app/models/game.rb)

class Game < ActiveRecord::Base
  has_many :plays
end

Listing 5-15. Play Model (app/models/play.rb)

class Play < ActiveRecord::Base
  belongs_to :game
  belongs_to :player
end

Listing 5-16. Player Model (app/models/player.rb)

class Player < ActiveRecord::Base
  has_many :plays
end

Creating the View for the Graphical Report

The final pieces are the views. Place the file shown in Listing 5-17 in app/views/home/index.html.erb .

Listing 5-17. View for the Team Performance Application (app/views/home/index.html.erb)

<h1>Team Performance Reporting</h1>

<div id="top">
 
<%=select 'player', 'id',
            [['Click here to select a player',""]] +
            @available_players.map { |p|
                                    [p.name, p.id] },
            {:include_blank=>false} %>
  <%=select 'game', 'id',
            [['Click here to select a game',""]] +
            @available_games.map { |g|

                                    [g.name, g.id] },
            {:include_blank=>false} %> </div>
<div id="chart">
</div>

<script>

  function show_report(){
    $('chart').hide();
    var player_id = $('player_id').value;
    var game_id = $('game_id').value
    if( player_id && game_id ) {
      new Ajax.Updater("chart",
                       '/performance'+
                       '/' + $('game_id').value +
                       '/' + $('player_id').value,

                        {evalScripts:true,
                         method:'get',
                         onComplete:function(){
                            setTimeout("$('chart').show();",
                                     400); }
                         }
                      ); 

    }
  }
  Event.observe("player_id", "change", show_report);
  Event.observe("game_id", "change", show_report);
</script>

The code shown in Listing 5-18 goes in app/views/performance/show.html.erb.

Listing 5-18. Performance Controller Show HTML View (app/views/performance/show.html.erb)

<%if @events.length>1%>

<div>
  <% graph_params = { 'AllowScriptAccess'=>'SameDomain' } %> 
<%=flashobject_tag "/flash/openflashchart.swf",
                :size=>"850x400", 
                :parameters=>graph_params,
                :variables=>{'data'=>"/performance/#{@game.id}
                     /#{@player.id}.text"} %
  </div>

<%else%>
  <p>  <%=@player.name%> has no recorded data for <%=@game.name%>.</p>
<%end%>

The code shown in Listing 5-19 goes in app/views/layouts/show.text.erb.

Listing 5-9. Performance Controller Show Text View (app/views/performance/show.text.erb)

<%
   labels = @events.map { |e| e[:event] }
   values = @events.map { |e| e[:average_time] }
   min = 0
   max = values.max

   graph_variables = { "title"=>",{margin:10px;}", 
               "bar_3d"=>"60,#8E9BF0,#000000",
                       "values"=>"#{values.join(',')}",
                       "x_labels"=>"#{labels.join(',')}",
                       "y_max"=>max,
                       "y_min"=>min,

                       "x_axis_3d"=>"16", 
                     "tool_tip"=>"#x_label#: #val#s Average Time", 
                  "y_axis_colour"=>"#F0F0F0",
                 "y_grid_colour"=>"#E9E9E9",
              "y_label_style"=>"12,#000000",
                 "x_axis_colour"=>"#6F6F7F",
                  "x_grid_colour"=>"#E9E9E9",
               "x_label_style"=>"15,#000000",

                    "bg_colour"=>"#F8F8FF" }

   %>
&<%=graph_variables.to_a.map { |key,val| "#{key}=#{val}" }.join("& &") %>&

Finally, the code shown in Listing 5-20 goes in app/views/layouts/application.html.erb.

Listing 5-20. Layout for the Team Performance View (app/views/layouts/application.html.erb)

<html>
  <head>
    <title>Team Performance Web Analyzer</title>
    <style>
     
body { font-family: verdana; }
      h1 { margin-bottom:0.5em; }
      #top { margin-bottom: 0; width:802px;
             background-color:#efefef; padding:10px 24px; }
    </style>
    <%=javascript_include_tag :defaults %>
  </head>
 
<body>
    <%=yield%>
  </body>
</html>

And that completes the graphical reporting application. Let's take a look at it.


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form
- Ruby on Rails Templates and Layouts
- Action Pack Controller Creation
- Writing an Action Pack Controller

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials