Rename instantiate! to build, no more raising in build, no saving in build, no seeding of aspects in build.

This commit is contained in:
Raphael 2010-10-20 11:03:47 -07:00
parent f97f63e2d1
commit 961510a8ed
7 changed files with 44 additions and 43 deletions

View file

@ -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(', ')

View file

@ -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

View file

@ -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")

View file

@ -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")

View file

@ -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

View file

@ -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

View file

@ -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