Hi Tomas, I haven't done real testing yet, but here are some notes about
current code.
- major problem/question is where default viewstate should be
initialized (where default block should be called), I don't think it
should be done when "registering" default viewstate. i'm attaching
updated version of this patch, which probably doesn't work but express
how I think it could work.
- also I think it makes sesne to create plugin for whole viewstate thing
when it's done
On 05/10/2011 05:18 PM, tsedovic(a)redhat.com wrote:
From: Tomas Sedovic<tsedovic(a)redhat.com>
---
src/app/controllers/application_controller.rb | 1 +
src/app/controllers/pools_controller.rb | 9 +++
src/lib/viewstate.rb | 76 +++++++++++++++++++++++++
3 files changed, 86 insertions(+), 0 deletions(-)
create mode 100644 src/lib/viewstate.rb
diff --git a/src/app/controllers/application_controller.rb
b/src/app/controllers/application_controller.rb
index 6bda765..10e06dd 100644
--- a/src/app/controllers/application_controller.rb
+++ b/src/app/controllers/application_controller.rb
@@ -20,6 +20,7 @@
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
+load 'lib/viewstate.rb'
class ApplicationController< ActionController::Base
# FIXME: not sure what we're doing aobut service layer w/ deltacloud
diff --git a/src/app/controllers/pools_controller.rb
b/src/app/controllers/pools_controller.rb
index 7706902..d111c28 100644
--- a/src/app/controllers/pools_controller.rb
+++ b/src/app/controllers/pools_controller.rb
@@ -3,6 +3,15 @@ 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
@search_term = params[:q]
if @search_term.blank?
diff --git a/src/lib/viewstate.rb b/src/lib/viewstate.rb
new file mode 100644
index 0000000..05b7664
--- /dev/null
+++ b/src/lib/viewstate.rb
@@ -0,0 +1,76 @@
+module ActionController
+ class Base
+ class<< self
+ def viewstate(action,&block)
+ default_state = {}
+ block.call(default_state) if block
+ before_filter proc {|controller| controller.viewstate_setup(default_state,
action)}
+ before_filter :handle_viewstate, :only => [action]
+ end
+ end
+
+ def viewstate_setup(default_state, action)
+ @default_viewstates = {} unless @default_viewstates
+ @default_viewstates[action.to_s] = default_state
+ session[:viewstates] = {} unless session[:viewstates]
+ default_state
+ end
+
+ def viewstate_session_key
+ [controller_name, action_name].join '#'
+ end
+ def viewstate_session
+ puts session[:viewstates].inspect
+ session[:viewstates][viewstate_session_key]
+ end
+ def viewstate_session=(value)
+ session[:viewstates][viewstate_session_key] = value
+ puts session[:viewstates].inspect
+ end
+
+ def viewstate
+ if viewstate_session.nil?
+ vs = ViewState.new(:controller => controller_name,
+ :action => action_name,
+ :state => default_viewstate)
+ vs.id = rand(99999999)
+ self.viewstate_session = vs
+ end
+
+ @viewstate = viewstate_session
+ if viewstate_given? and @viewstate.id.to_s != params[:viewstate]
+ @viewstate = nil
+ end
+ @viewstate
+ end
+
+ def handle_viewstate
+ return unless (viewstate_given? or viewstate_modified?)
+
+ if viewstate
+ @viewstate.state = @viewstate.state.merge(viewstate_params)
+ end
+
+ if @viewstate and not viewstate_given?
+ params[:viewstate] = @viewstate.id
+ redirect_to params and return
+ end
+ end
+
+ def default_viewstate
+ @default_viewstates[action_name]
+ end
+
+ def viewstate_given?
+ params.include? 'viewstate'
+ end
+
+ def viewstate_modified?
+ not viewstate_params.empty?
+ end
+
+ def viewstate_params
+ params.reject {|k,v| ['controller', 'action',
'viewstate'].include? k}
+ end
+ end
+end