https://svn.lrde.epita.fr/svn/ranch/trunk
Index: ChangeLog
from Nicolas Desprès <nicolas.despres(a)lrde.epita.fr>
Factorize project request in a filter.
* web/ranch/app/controllers/ranch_controller.rb,
* web/ranch/app/controllers/projects_controller.rb,
* web/ranch/app/controllers/graph_form_controller.rb: Use the
LoadProjectFilter.
* web/ranch/app/filters/load_projects_filter.rb: New. Request the
project list.
* web/ranch/app/views/layouts/_left_margin.rhtml: Use a project
attribute instead of a session key.
* web/ranch/app/models/project.rb: Query associated benches and
sort them.
* web/ranch/config/environment.rb: Lazyly preload the filters files.
app/controllers/graph_form_controller.rb | 3 +++
app/controllers/projects_controller.rb | 2 ++
app/controllers/ranch_controller.rb | 2 ++
app/filters/load_projects_filter.rb | 9 +++++++++
app/models/project.rb | 4 ++++
app/views/layouts/_left_margin.rhtml | 23 ++++++-----------------
config/environment.rb | 5 +++++
7 files changed, 31 insertions(+), 17 deletions(-)
Index: web/ranch/app/models/project.rb
--- web/ranch/app/models/project.rb (revision 36)
+++ web/ranch/app/models/project.rb (working copy)
@@ -7,6 +7,10 @@
validates_numericality_of :head_revision, :only_integer => true
validates_length_of :name, :maximum => 255
+ def bench_ordered_by_name
+ Bench.find :all, :conditions => "project_id = #{id}", :order =>
"name"
+ end
+
protected
def validate
Index: web/ranch/app/controllers/ranch_controller.rb
--- web/ranch/app/controllers/ranch_controller.rb (revision 36)
+++ web/ranch/app/controllers/ranch_controller.rb (working copy)
@@ -2,6 +2,8 @@
layout 'ranch_benches_list'
+ before_filter LoadProjectsFilter
+
def index
end
Index: web/ranch/app/filters/load_projects_filter.rb
--- web/ranch/app/filters/load_projects_filter.rb (revision 0)
+++ web/ranch/app/filters/load_projects_filter.rb (revision 0)
@@ -0,0 +1,9 @@
+class LoadProjectsFilter
+
+ def self.filter(controller)
+ controller.instance_eval do
+ @projects = Project.find :all, :order => "name"
+ end
+ end
+
+end
Index: web/ranch/app/views/layouts/_left_margin.rhtml
--- web/ranch/app/views/layouts/_left_margin.rhtml (revision 36)
+++ web/ranch/app/views/layouts/_left_margin.rhtml (working copy)
@@ -1,19 +1,9 @@
<!-- -*- html -*- -->
-<%
- if session[:projects].nil? or session[:benches].nil?
- session[:projects] = Project.find_by_sql "SELECT id, name " +
- "FROM projects " +
- "WHERE 1 " +
- "ORDER BY name"
- session[:benches] = Bench.find_by_sql "SELECT id, name, project_id " +
- "FROM benches " +
- "WHERE 1 " +
- "GROUP BY name " +
- "ORDER BY project_id, name"
- end
-%>
<table>
- <% for project in session[:projects] %>
+ <% if @projects.nil? or @projects.empty? %>
+ no projects available
+ <% else %>
+ <% for project in @projects %>
<tr>
<td><%= link_to project.name, :controller => "projects",
:action => "show",
@@ -22,8 +12,7 @@
<tr>
<td>
<table>
- <% for bench in session[:benches] %>
- <% if bench.project_id == project.id %>
+ <% for bench in project.bench_ordered_by_name %>
<tr>
<td width=10>
</td>
@@ -35,7 +24,7 @@
</td>
</tr>
<% end %>
- <% end %>
</table>
<% end %>
+ <% end %>
</table>
Index: web/ranch/config/environment.rb
--- web/ranch/config/environment.rb (revision 36)
+++ web/ranch/config/environment.rb (working copy)
@@ -52,3 +52,8 @@
require 'grapher'
require 'tempfile'
+# Preload filters lazyly.
+Pathname.glob("#{RAILS_ROOT}/app/filters/*_filter.rb") do |pathname|
+ autoload(pathname.basename.to_s.sub(/\.rb$/, '').classify.to_sym,
+ pathname.to_s)
+end
Index: web/ranch/app/controllers/projects_controller.rb
--- web/ranch/app/controllers/projects_controller.rb (revision 36)
+++ web/ranch/app/controllers/projects_controller.rb (working copy)
@@ -2,6 +2,8 @@
layout 'ranch_benches_list'
+ before_filter LoadProjectsFilter
+
def show
@project = Project.find_by_id params[:project_id]
end
Index: web/ranch/app/controllers/graph_form_controller.rb
--- web/ranch/app/controllers/graph_form_controller.rb (revision 36)
+++ web/ranch/app/controllers/graph_form_controller.rb (working copy)
@@ -1,8 +1,11 @@
class GraphFormController < ApplicationController
+
helper :graph, :graph_form
layout 'ranch_benches_list'
+ before_filter LoadProjectsFilter
+
DEFAULT_REVISION_RANGE = 100
def index