From: Tomas Sedovic <tsedovic(a)redhat.com>
---
src/app/controllers/application_controller.rb | 44 +++++++++++++++++++++++++
src/app/controllers/pools_controller.rb | 10 ++++++
2 files changed, 54 insertions(+), 0 deletions(-)
diff --git a/src/app/controllers/application_controller.rb
b/src/app/controllers/application_controller.rb
index 6bda765..4fc852b 100644
--- a/src/app/controllers/application_controller.rb
+++ b/src/app/controllers/application_controller.rb
@@ -213,3 +213,47 @@ class ApplicationController < ActionController::Base
session[:return_to] = nil
end
end
+
+#TODO: move this to a separate file
+module ViewStateExtension
+ def viewstate(action, &block)
+ default_state = {}
+ block.call(default_state) if block
+ before_filter proc {|controller| controller.viewstate_setup(default_state, action)}
+ end
+end
+
+ApplicationController.send(:extend, ViewStateExtension)
+
+ApplicationController.send :define_method, :viewstate_setup do |default_state, action|
+ @default_viewstates = {} unless @default_viewstates
+ @default_viewstates[action.to_s] = default_state
+ session[:viewstates] = {} unless session[:viewstates]
+ default_state
+end
+
+ApplicationController.send :define_method, :viewstate do
+ if session[:viewstates][action_name].nil?
+ viewstate = ViewState.new( :controller => controller_name,
+ :action => action_name,
+ :state => default_viewstate)
+ viewstate.id = rand(99999999)
+ params[:viewstate] = viewstate.id
+ session[:viewstates][action_name] = viewstate
+ end
+
+ @viewstate = session[:viewstates][action_name]
+ @viewstate.state = @viewstate.state.merge(viewstate_params)
+end
+
+ApplicationController.send :define_method, :default_viewstate do
+ @default_viewstates[action_name]
+end
+
+ApplicationController.send :define_method, :viewstate_given? do
+ params.include? 'viewstate' or not viewstate_params.empty?
+end
+
+ApplicationController.send :define_method, :viewstate_params do
+ params.reject {|k,v| ['controller', 'action',
'viewstate'].include? k}
+end
diff --git a/src/app/controllers/pools_controller.rb
b/src/app/controllers/pools_controller.rb
index 7706902..f7cf696 100644
--- a/src/app/controllers/pools_controller.rb
+++ b/src/app/controllers/pools_controller.rb
@@ -3,7 +3,17 @@ class PoolsController < ApplicationController
before_filter :set_params_and_header, :only => [:index, :show]
before_filter :load_pools, :only => [:show]
+ viewstate :index do |default|
+ default.merge!({
+ :pretty_view => true,
+ :order_field => 'name',
+ :order_dir => 'asc',
+ :page => 1
+ })
+ end
+
def index
+ viewstate if viewstate_given?
@search_term = params[:q]
if @search_term.blank?
load_pools
--
1.7.4.4