From: Tomas Sedovic <tsedovic(a)redhat.com>
As described in:
https://www.aeolusproject.org/redmine/issues/1342
---
src/app/controllers/application_controller.rb | 1 +
src/app/controllers/pools_controller.rb | 9 ++
src/lib/viewstate.rb | 151 +++++++++++++++++++++++++
3 files changed, 161 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 746811b..72a88f6 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 ea09745..33ee7c2 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..16ed7a5
--- /dev/null
+++ b/src/lib/viewstate.rb
@@ -0,0 +1,151 @@
+module ActionController
+ class Base
+ class << self
+ def viewstate(action, &block)
+ action = action.to_s
+ before_filter proc {|controller| controller.viewstate_setup(block, action)}
+ before_filter :handle_viewstate, :only => [action]
+ end
+ end
+
+ def viewstate_setup(default_block, action)
+ @default_viewstates ||= {}
+ @default_viewstates[action] = default_block
+
+ setup_viewstate_routes(action)
+ end
+
+ def setup_viewstate_routes(action)
+ ActionController::Routing::Routes.reload!
+ route_url = "#{self.controller_name}/#{action}/viewstate/:id"
+
+ get_viewstate_route = ActionController::Routing::Routes.builder.build(route_url,
+ :controller
=> self.controller_name,
+ :action =>
'get_viewstate',
+ :conditions
=> {:method => :get})
+ ActionController::Routing::Routes.routes.insert(0, get_viewstate_route)
+ self.class.send(:define_method, :get_viewstate) { self.get_viewstate_body(action)
}
+
+ put_viewstate_route = ActionController::Routing::Routes.builder.build(route_url,
+ :controller
=> self.controller_name,
+ :action =>
'put_viewstate',
+ :conditions
=> {:method => :put})
+ ActionController::Routing::Routes.routes.insert(0, put_viewstate_route)
+ self.class.send(:define_method, :put_viewstate) { self.put_viewstate_body(action)
}
+
+ post_viewstate_route = ActionController::Routing::Routes.builder.build(route_url,
+ :controller
=> self.controller_name,
+ :action
=> 'post_viewstate',
+ :conditions
=> {:method => :post})
+ ActionController::Routing::Routes.routes.insert(0, post_viewstate_route)
+ self.class.send(:define_method, :post_viewstate) { self.post_viewstate_body(action)
}
+
+ delete_viewstate_route =
ActionController::Routing::Routes.builder.build(route_url,
+
:controller => self.controller_name,
+ :action
=> 'delete_viewstate',
+
:conditions => {:method => :delete})
+ ActionController::Routing::Routes.routes.insert(0, delete_viewstate_route)
+ self.class.send(:define_method, :delete_viewstate) {
self.delete_viewstate_body(action) }
+ end
+
+ def get_viewstate_body(action)
+ viewstate = session_viewstate(action)
+ render :json => viewstate.state
+ end
+
+ def put_viewstate_body(action)
+ viewstate = session_viewstate(action)
+ viewstate.state = viewstate.state.merge(viewstate_params)
+ get_viewstate_body(action)
+ end
+
+ def post_viewstate_body(action)
+ viewstate = session_viewstate(action)
+ if viewstate.name = params[:name]
+ viewstate.save!
+ render :text => viewstate.uuid, :status => 201
+ else
+ render :text => 'ViewState name must be specified', :status => 409
+ end
+ end
+
+ def delete_viewstate_body(action)
+ viewstate = session_viewstate(action)
+ default_state = {}
+ viewstate.state = @default_viewstates[action].call(default_state)
+ get_viewstate_body(action)
+ end
+
+ def session_viewstate_key(action=nil, controller=nil)
+ action ||= action_name
+ controller ||= controller_name
+ [controller, action].join '#'
+ end
+
+ def session_viewstate(action=nil)
+ session[:viewstates] ||= {}
+ session[:viewstates][session_viewstate_key(action)]
+ end
+
+ def set_session_viewstate(value, action=nil)
+ session[:viewstates] ||= {}
+ session[:viewstates][session_viewstate_key(action)] = value
+ end
+
+ def create_viewstate(action=nil)
+ action ||= action_name
+ default_state = {}
+ @default_viewstates[action].call(default_state)
+ result = ViewState.new(:action => action, :controller => controller_name,
+ :state => default_state, :user_id => current_user.id)
+ result.uuid = UUIDTools::UUID.timestamp_create.to_s
+ result
+ end
+
+ def viewstate
+ if viewstate_given?
+ if vs = session_viewstate and vs.uuid == params[:viewstate]
+ @viewstate = vs
+ elsif vs = ViewState.find_by_uuid(params[:viewstate])
+ @viewstate = vs
+ else
+ @viewstate = create_viewstate
+ end
+ else
+ if vs = session_viewstate
+ @viewstate = vs
+ else
+ @viewstate = create_viewstate
+ end
+ end
+ set_session_viewstate(@viewstate)
+ @viewstate.state
+ end
+
+ def handle_viewstate
+ viewstate
+ if viewstate_modified?
+ @viewstate.state = @viewstate.state.merge(viewstate_params)
+ end
+
+ return unless (viewstate_given? or viewstate_modified?)
+
+ if @viewstate and not viewstate_given?
+ params[:viewstate] = @viewstate.id
+ redirect_to params and return
+ end
+ 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', '_method',
'viewstate'].include? k}
+ end
+ end
+end
--
1.7.4.4