On 03/11/2010 05:28 PM, Jason Guiditta wrote:
> This registration service may be able to be abstracted/
> resued in future patches.
> ---
> src/app/controllers/users_controller.rb | 3 +-
> src/app/services/registration_service.rb | 29 +++++++++++++
> src/features/authentication.feature | 1 +
> src/features/step_definitions/authentication.rb | 5 ++
> src/spec/controllers/users_controller_spec.rb | 49
> +++++++++++++++++++---
> src/spec/models/registration_service_spec.rb | 48
> ++++++++++++++++++++++
> 6 files changed, 127 insertions(+), 8 deletions(-)
> create mode 100644 src/app/services/registration_service.rb
> create mode 100644 src/spec/models/registration_service_spec.rb
>
> diff --git a/src/app/controllers/users_controller.rb
> b/src/app/controllers/users_controller.rb
> index 310edf6..6c6c021 100644
> --- a/src/app/controllers/users_controller.rb
> +++ b/src/app/controllers/users_controller.rb
> @@ -29,7 +29,8 @@ class UsersController< ApplicationController
>
> def create
> @user = User.new(params[:user])
> - if @user.save
> + @registration = RegistrationService.new(@user)
> + if @registration.save
> flash[:notice] = "User registered!"
> redirect_back_or_default account_url
> else
> diff --git a/src/app/services/registration_service.rb
> b/src/app/services/registration_service.rb
> new file mode 100644
> index 0000000..e8d2e65
> --- /dev/null
> +++ b/src/app/services/registration_service.rb
> @@ -0,0 +1,29 @@
> +class RegistrationService
> +
> + def initialize(user)
> + @user = user
> + end
> +
> + def save
> + return false unless valid?
> + begin
> + User.transaction do
> + @user.save!
> + PortalPool.transaction do
> + @portal_pool = PortalPool.create!({ :name => @user.login, :owner =>
> @user})
> + Permission.transaction do
> + Permission.create!({:user => @user,
> + :role => Role.find_by_name("Self-service Pool
> User"),
> + :permission_object => @portal_pool})
> + end
> + end
> + end
>
Are there supposed to be three transactions here? Don't you just need
one saying editing these three tables atomically, if any one table
fails, rollback all changes? Or am I misunderstanding how rails
transactions work?
<snip>
> diff --git a/src/spec/models/registration_service_spec.rb
> b/src/spec/models/registration_service_spec.rb
> new file mode 100644
> index 0000000..eeea5c8
> --- /dev/null
> +++ b/src/spec/models/registration_service_spec.rb
> @@ -0,0 +1,48 @@
> +require 'spec_helper'
> +
> +describe RegistrationService do
> + #fixtures :all
> + before(:each) do
> + @tuser = Factory :tuser
> + end
> +
> + it "should initialize a new instance given valid attributes" do
> + RegistrationService.new(@tuser)
> + end
> +
> + describe "#save" do
> +
> + context "adding valid user with no errors" do
> + it "should create user, portal_pool and self-service permission" do
> + user = User.new({:login => 'gooduser',
> + :email => '[email protected]',
> + :password => 'password',
> + :password_confirmation => 'password'})
> + r = RegistrationService.new(user)
> + Rails::logger.info("ERRORS - Printing errors on user object:")
> + Rails::logger.info(
> + user.errors.each_full { |msg| "ERROR: #{msg}" }
> + )
> + r.save.should be_true
>
Is this logging code here intentionally or is it just leftover debugging
artifacts?
-Mo
_______________________________________________
deltacloud-devel mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel