[Ovirt-devel] [PATCH server] converted the quota controller to use the service layer.
Scott Seago
sseago at redhat.com
Mon May 11 16:24:23 UTC 2009
Signed-off-by: Scott Seago <sseago at redhat.com>
---
src/app/controllers/quota_controller.rb | 61 ++++--------------
src/app/services/quota_service.rb | 100 +++++++++++++++++++++++++++++++
2 files changed, 114 insertions(+), 47 deletions(-)
create mode 100644 src/app/services/quota_service.rb
diff --git a/src/app/controllers/quota_controller.rb b/src/app/controllers/quota_controller.rb
index d4fbcd0..fcdd672 100644
--- a/src/app/controllers/quota_controller.rb
+++ b/src/app/controllers/quota_controller.rb
@@ -18,78 +18,45 @@
# also available at http://www.gnu.org/copyleft/gpl.html.
class QuotaController < ApplicationController
+ include QuotaService
# GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
verify :method => :post, :only => [ :destroy, :create, :update ],
:redirect_to => { :controller => 'dashboard' }
- def redirect_to_parent
- redirect_to :controller => @quota.pool.get_controller, :action => 'show', :id => @quota.pool
- end
-
def show
- @quota = Quota.find(params[:id])
- authorize_view
+ svc_show(params[:id])
end
def new
+ svc_new(params[:pool_id])
render :layout => 'popup'
end
def create
- begin
- @quota.save!
- render :json => { :object => "quota", :success => true,
- :alert => "Quota was successfully created." }
- rescue
- render :json => { :object => "quota", :success => false,
- :errors => @quota.errors.localize_error_messages.to_a}
- end
+ alert = svc_create(params[:quota])
+ render :json => { :object => "quota", :success => true, :alert => alert }
end
def edit
+ svc_modify(params[:id])
render :layout => 'popup'
end
def update
- begin
- @quota.update_attributes!(params[:quota])
- render :json => { :object => "quota", :success => true,
- :alert => "Quota was successfully updated." }
- rescue
- render :json => { :object => "quota", :success => false,
- :errors => @quota.errors.localize_error_messages.to_a,
- :alert => $!.to_s}
- end
+ alert = svc_update(params[:id], params[:quota])
+ render :json => { :object => "quota", :success => true, :alert => alert }
end
def destroy
- pool = @quota.pool
- if @quota.destroy
- alert="Quota was successfully deleted."
- success=true
- else
- alert="Failed to delete quota."
- success=false
- end
- render :json => { :object => "quota", :success => success, :alert => alert }
+ alert = svc_destroy(params[:id])
+ render :json => { :object => "quota", :success => true, :alert => alert }
end
- protected
- def pre_new
- @quota = Quota.new( { :pool_id => params[:pool_id]})
- set_perms(@quota.pool)
- end
- def pre_create
- @quota = Quota.new(params[:quota])
- set_perms(@quota.pool)
- end
- def pre_show
- @quota = Quota.find(params[:id])
- set_perms(@quota.pool)
+ # FIXME: remove these when service transition is complete. these are here
+ # to keep from running permissions checks and other setup steps twice
+ def tmp_pre_update
end
- def pre_edit
- @quota = Quota.find(params[:id])
- set_perms(@quota.pool)
+ def tmp_authorize_admin
end
end
diff --git a/src/app/services/quota_service.rb b/src/app/services/quota_service.rb
new file mode 100644
index 0000000..ca6f2db
--- /dev/null
+++ b/src/app/services/quota_service.rb
@@ -0,0 +1,100 @@
+#
+# Copyright (C) 2009 Red Hat, Inc.
+# Written by Scott Seago <sseago at redhat.com>,
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301, USA. A copy of the GNU General Public License is
+# also available at http://www.gnu.org/copyleft/gpl.html.
+# Mid-level API: Business logic around quotas
+module QuotaService
+ include ApplicationService
+
+ # Load the Quota with +id+ for viewing
+ #
+ # === Instance variables
+ # [<tt>@quota</tt>] stores the Quota with +id+
+ # === Required permissions
+ # [<tt>Privilege::VIEW</tt>] on quota's Pool
+ def svc_show(id)
+ lookup(id,Privilege::VIEW)
+ end
+
+ # Load the Quota with +id+ for editing
+ #
+ # === Instance variables
+ # [<tt>@quota</tt>] stores the Quota with +id+
+ # === Required permissions
+ # [<tt>Privilege::MODIFY</tt>] on quota's Pool
+ def svc_modify(id)
+ lookup(id,Privilege::MODIFY)
+ end
+
+ # Update attributes for the Quota with +id+
+ #
+ # === Instance variables
+ # [<tt>@quota</tt>] stores the Quota with +id+
+ # === Required permissions
+ # [<tt>Privilege::MODIFY</tt>] for the Quota's Pool
+ def svc_update(id, quota_hash)
+ lookup(id,Privilege::MODIFY)
+ @quota.update_attributes!(quota_hash)
+ return "Quota was successfully updated."
+ end
+
+ # Load a new Quota for creating
+ #
+ # === Instance variables
+ # [<tt>@quota</tt>] loads a new Quota object into memory
+ # === Required permissions
+ # [<tt>Privilege::MODIFY</tt>] for the quota's Pool as specified by
+ # +pool_id+
+ def svc_new(pool_id)
+ @quota = Quota.new( { :pool_id => pool_id})
+ authorized!(Privilege::MODIFY, at quota.pool)
+
+ end
+
+ # Save a new Quota
+ #
+ # === Instance variables
+ # [<tt>@quota</tt>] the newly saved quota
+ # === Required permissions
+ # [<tt>Privilege::MODIFY</tt>] for the quota's Pool as specified by
+ # +pool_id+
+ def svc_create(quota_hash)
+ @quota = Quota.new( quota_hash)
+ authorized!(Privilege::MODIFY, at quota.pool)
+ @quota.save!
+ return "Quota was successfully created."
+ end
+
+ # Destroys for the Quota with +id+
+ #
+ # === Instance variables
+ # [<tt>@quota</tt>] stores the Quota with +id+
+ # === Required permissions
+ # [<tt>Privilege::MODIFY</tt>] on quota's Pool
+ def svc_destroy(id)
+ lookup(id,Privilege::MODIFY)
+ @quota.destroy
+ return "Quota was successfully deleted."
+ end
+
+ private
+ def lookup(id, priv)
+ @quota = Quota.find(id)
+ authorized!(priv, at quota.pool)
+ end
+
+end
--
1.6.0.6
More information about the ovirt-devel
mailing list