From 961510a8ed06590109a8090686355ffdcde71180 Mon Sep 17 00:00:00 2001
From: Raphael <raphael@joindiaspora.com>
Date: Wed, 20 Oct 2010 11:03:47 -0700
Subject: [PATCH] Rename instantiate! to build, no more raising in build, no
 saving in build, no seeding of aspects in build.

---
 app/controllers/registrations_controller.rb   |  9 +---
 app/models/user.rb                            |  4 +-
 db/seeds/backer.rb                            |  4 +-
 db/seeds/dev.rb                               | 10 ++--
 db/seeds/tom.rb                               |  9 ++--
 .../registrations_controller_spec.rb          |  4 +-
 spec/models/user_spec.rb                      | 47 +++++++++++--------
 7 files changed, 44 insertions(+), 43 deletions(-)

diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb
index 541563d48a..75ee3b7f74 100644
--- a/app/controllers/registrations_controller.rb
+++ b/app/controllers/registrations_controller.rb
@@ -4,15 +4,10 @@
 
 class RegistrationsController < Devise::RegistrationsController
   def create
-    begin
-      @user = User.instantiate!(params[:user])
-    rescue MongoMapper::DocumentNotValid => e
-      flash[:error] = e.message
-      redirect_to new_user_registration_path
-      return
-    end
+    @user = User.build(params[:user])
     if @user.save
       flash[:notice] = I18n.t 'registrations.create.success'
+      @user.seed_aspects
       sign_in_and_redirect(:user, @user)
     else
       flash[:error] = @user.errors.full_messages.join(', ')
diff --git a/app/models/user.rb b/app/models/user.rb
index cc9531c8c8..fd05fcb1c1 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -379,7 +379,7 @@ class User
   end
 
   ###Helpers############
-  def self.instantiate!(opts = {})
+  def self.build(opts = {})
     opts[:person][:diaspora_handle] = "#{opts[:username]}@#{APP_CONFIG[:terse_pod_url]}"
     opts[:person][:url] = APP_CONFIG[:pod_url]
 
@@ -387,8 +387,6 @@ class User
     opts[:person][:serialized_public_key] = opts[:serialized_private_key].public_key
 
     u = User.new(opts)
-    u.seed_aspects
-    u.save!
     u
   end
 
diff --git a/db/seeds/backer.rb b/db/seeds/backer.rb
index 2305dea93c..7f27c56269 100644
--- a/db/seeds/backer.rb
+++ b/db/seeds/backer.rb
@@ -25,14 +25,14 @@ def create
   require File.join(File.dirname(__FILE__), "..", "..", "config", "initializers", "_load_app_config.rb")
 
   # Create seed user
-  user = User.instantiate!(:email => "#{username}@#{username}.joindiaspora.com",
+  user = User.build(:email => "#{username}@#{username}.joindiaspora.com",
                      :username => username,
                      :password => "#{username+backer_info[backer_number]['pin'].to_s}",
                      :password_confirmation => "#{username+backer_info[backer_number]['pin'].to_s}",
                      :person => Person.new(
                        :profile => Profile.new( :first_name => backer_info[backer_number]['given_name'], :last_name => backer_info[backer_number]['family_name'],
                                              :image_url => "http://#{username}.joindiaspora.com/images/user/#{username}.jpg")
-                    ))
+                    )).save
   user.person.save!
 
   user.aspect(:name => "Presidents")
diff --git a/db/seeds/dev.rb b/db/seeds/dev.rb
index 5196369223..7eed484c0d 100644
--- a/db/seeds/dev.rb
+++ b/db/seeds/dev.rb
@@ -18,23 +18,25 @@ username = "tom"
 set_app_config username
 
 # Create seed user
-user = User.instantiate!( :email => "tom@tom.joindiaspora.com",
+user = User.build( :email => "tom@tom.joindiaspora.com",
                      :username => "tom",
                     :password => "evankorth",
                     :password_confirmation => "evankorth",
                     :person => Person.new(
                       :profile => Profile.new( :first_name => "Alexander", :last_name => "Hamiltom" ))
-                  )
+                  ).save
 user.person.save!
+user.seed_aspects
 
-user2 = User.instantiate!( :email => "korth@tom.joindiaspora.com",
+user2 = User.build( :email => "korth@tom.joindiaspora.com",
                      :username => "korth",
                     :password => "evankorth",
                     :password_confirmation => "evankorth",
                     :person => Person.new(
-                      :profile => Profile.new( :first_name => "Evan", :last_name => "Korth")))
+                      :profile => Profile.new( :first_name => "Evan", :last_name => "Korth"))).save
 
 user2.person.save!
+user2.seed_aspects
 
 # friending users
 aspect = user.aspect(:name => "other dudes")
diff --git a/db/seeds/tom.rb b/db/seeds/tom.rb
index 074b37caf9..2bd21e169a 100644
--- a/db/seeds/tom.rb
+++ b/db/seeds/tom.rb
@@ -18,23 +18,24 @@ set_app_config "tom"
 require 'config/initializers/_load_app_config.rb'
 
 # Create seed user
-user = User.instantiate!( :email => "tom@tom.joindiaspora.com",
+user = User.build( :email => "tom@tom.joindiaspora.com",
                      :username => "tom",
                     :password => "evankorth",
                     :password_confirmation => "evankorth",
                     :person => {
                       :profile => { :first_name => "Alexander", :last_name => "Hamiltom",
                       :image_url => "http://tom.joindiaspora.com/images/user/tom.jpg"}}
-                  )
+                  ).save!
+user.seed_aspects
 user.person.save!
 
-user2 = User.instantiate!( :email => "korth@tom.joindiaspora.com",
+user2 = User.build( :email => "korth@tom.joindiaspora.com",
                     :password => "evankorth",
                     :password_confirmation => "evankorth",
                      :username => "korth",
                     :person => {:profile => { :first_name => "Evan", :last_name => "Korth",
                       :image_url => "http://tom.joindiaspora.com/images/user/korth.jpg"}})
-
+user2.seed_aspects
 user2.person.save!
 
 # friending users
diff --git a/spec/controllers/registrations_controller_spec.rb b/spec/controllers/registrations_controller_spec.rb
index e897955326..3c9bbde07a 100644
--- a/spec/controllers/registrations_controller_spec.rb
+++ b/spec/controllers/registrations_controller_spec.rb
@@ -48,7 +48,6 @@ describe RegistrationsController do
         lambda { get :create, @invalid_params }.should_not change(User, :count)
       end
       it "assigns @user" do
-        pending "GAAAH stupid mongo mapper. Figure out why it thinks it's persisted when validations fail"
         get :create, @valid_params
         assigns(:user).should_not be_nil
       end
@@ -57,9 +56,8 @@ describe RegistrationsController do
         flash[:error].should_not be_blank
       end
       it "goes back to the form" do
-        pending "GAAAH stupid mongo mapper. Figure out why it thinks it's persisted when validations fail"
         get :create, @invalid_params
-        response.should be_success
+        response.should be_redirect
       end
     end
   end
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 536e7b68ce..427b6b74f9 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -86,17 +86,29 @@ describe User do
     end
   end
 
-  describe ".instantiate!" do
-    it "creates the user if params are valid" do
-      User.find_by_username("ohai").should be_nil
-      user = User.instantiate!({
-                                 :username => "ohai",
-                                 :email => "ohai@example.com",
-                                 :password => "password",
-                                 :password_confirmation => "password",
-                                 :person => {:profile => {:first_name => "O", :last_name => "Hai"}}})
-      user.should be_valid
-      User.find_by_username("ohai").should == user
+  describe ".build" do
+    context 'with valid params' do
+      before do
+        params = {:username => "ohai",
+                  :email => "ohai@example.com",
+                  :password => "password",
+                  :password_confirmation => "password",
+                  :person => 
+                    {:profile => 
+                      {:first_name => "O", 
+                       :last_name => "Hai"}
+                    }
+        }
+        @user = User.build(params)
+      end
+      it "makes a valid user" do
+        @user.should be_valid
+        User.find_by_username("ohai").should be_nil
+      end
+      it 'saves successfully' do
+        @user.save.should be_true
+        User.find_by_username("ohai").should == @user
+      end
     end
     describe "with invalid params" do
       before do
@@ -107,16 +119,11 @@ describe User do
           :password_confirmation => "password",
           :person => {:profile => {:first_name => "", :last_name => ""}}}
       end
-      it "raises an error" do
-        lambda { User.instantiate!(@invalid_params) }.should raise_error
+      it "raises no error" do
+        lambda { User.build(@invalid_params) }.should_not raise_error
       end
-      it "does not create the user" do
-        User.find_by_username("ohai").should be_nil
-        begin
-          User.instantiate!(@invalid_params)
-        rescue
-        end
-        User.find_by_username("ohai").should be_nil
+      it "does not save" do
+        User.build(@invalid_params).save.should be_false
       end
     end
   end
-- 
GitLab