From a8b77cf868de63c054aa86046e3c21ef5de66ce9 Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 10 Aug 2010 15:35:27 -0700 Subject: [PATCH 1/3] DG RS; made an instantiate method for user which makes a person and profile --- app/controllers/users_controller.rb | 4 ++-- app/helpers/status_messages_helper.rb | 4 ---- app/models/user.rb | 12 +++++++++++- spec/models/user_spec.rb | 12 ++++++++++++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 97f8b31bc..45c80bc5a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,6 +1,6 @@ class UsersController < ApplicationController + before_filter :authenticate_user!, :except => [:new, :create] - before_filter :authenticate_user! def index @users = User.sort(:created_at.desc).all end @@ -29,7 +29,7 @@ class UsersController < ApplicationController def create @user = User.new(params[:user]) - if @user.save! + if @user.person.save! && @user.save! flash[:notice] = "Successfully signed up." redirect_to root_path else diff --git a/app/helpers/status_messages_helper.rb b/app/helpers/status_messages_helper.rb index 57d0f2f17..840e789a0 100644 --- a/app/helpers/status_messages_helper.rb +++ b/app/helpers/status_messages_helper.rb @@ -1,5 +1,4 @@ module StatusMessagesHelper - def my_latest_message unless @latest_status_message.nil? return @latest_status_message.message @@ -7,7 +6,4 @@ module StatusMessagesHelper return "No message to display." end end - - - end diff --git a/app/models/user.rb b/app/models/user.rb index df7483c22..8981fbee0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -118,7 +118,17 @@ class User ###Helpers############ - + def self.instantiate( opts = {} ) + User.create( + :email => opts[:email], + :password => opts[:password], + :password_confirmation => opts[:password_confirmation], + :person => Person.new( + :email => opts[:email], + :profile => Profile.new( + :first_name => opts[:first_name], + :last_name => opts[:last_name]))) + end def terse_url terse= self.url.gsub(/https?:\/\//, '') diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index bcc977505..bcdf97db7 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -11,6 +11,18 @@ describe User do Person.count.should == n+1 end + it 'should instantiate with a person and be valid' do + user = User.instantiate(:email => "bob@bob.com", + :password => "password", + :password_confirmation => "password", + :first_name => "bob", + :last_name => "grimm") + + user.save.should be true + user.person.should_not be nil + user.person.profile.should_not be nil + end + describe 'friend requesting' do it "should be able to accept a pending friend request" do friend = Factory.create(:person) From 8b9cd3453dd7650de5961b28460cf35843873579 Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 10 Aug 2010 15:44:14 -0700 Subject: [PATCH 2/3] RS, DG; User.instatntiate simplified --- app/models/user.rb | 11 ++--------- spec/models/user_spec.rb | 6 ++++-- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 8981fbee0..b026458da 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -119,15 +119,8 @@ class User ###Helpers############ def self.instantiate( opts = {} ) - User.create( - :email => opts[:email], - :password => opts[:password], - :password_confirmation => opts[:password_confirmation], - :person => Person.new( - :email => opts[:email], - :profile => Profile.new( - :first_name => opts[:first_name], - :last_name => opts[:last_name]))) + opts[:person][:email] = opts[:email] + User.create( opts) end def terse_url diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index bcdf97db7..b32581afc 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -15,8 +15,10 @@ describe User do user = User.instantiate(:email => "bob@bob.com", :password => "password", :password_confirmation => "password", - :first_name => "bob", - :last_name => "grimm") + :person => + {:profile => { + :first_name => "bob", + :last_name => "grimm"}}) user.save.should be true user.person.should_not be nil From 0fb55be7a09b8c3e262ff0c6cdcf2564a980f000 Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 10 Aug 2010 16:03:38 -0700 Subject: [PATCH 3/3] RS, DG; You can now sign up to be a user on a seed --- app/controllers/users_controller.rb | 6 +++--- app/models/user.rb | 5 +++++ app/views/devise/registrations/new.html.haml | 15 ++++++++++++--- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 45c80bc5a..12f87c481 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -27,9 +27,9 @@ class UsersController < ApplicationController end def create - @user = User.new(params[:user]) - - if @user.person.save! && @user.save! + @user = User.instantiate(params[:user]) + + if @user.created_at && @user.person.created_at flash[:notice] = "Successfully signed up." redirect_to root_path else diff --git a/app/models/user.rb b/app/models/user.rb index b026458da..fc04f2dc6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -120,6 +120,7 @@ class User ###Helpers############ def self.instantiate( opts = {} ) opts[:person][:email] = opts[:email] + opts[:person][:serialized_key] = generate_key User.create( opts) end @@ -148,4 +149,8 @@ class User OpenSSL::PKey::RSA::generate 1024 end + def self.generate_key + OpenSSL::PKey::RSA::generate 1024 + end + end diff --git a/app/views/devise/registrations/new.html.haml b/app/views/devise/registrations/new.html.haml index b03da3518..6ea93b780 100644 --- a/app/views/devise/registrations/new.html.haml +++ b/app/views/devise/registrations/new.html.haml @@ -3,15 +3,24 @@ = devise_error_messages! %p = f.label :email - %br/ = f.text_field :email %p = f.label :password - %br/ = f.password_field :password %p = f.label :password_confirmation - %br/ = f.password_field :password_confirmation + + = f.fields_for :person do |p| + = p.hidden_field :url, :value => "http://google.com/" + + = p.fields_for :profile do |pr| + %p + = pr.label :first_name + = pr.text_field :first_name + %p + = pr.label :last_name + = pr.text_field :last_name + %p= f.submit "Sign up" = render :partial => "devise/shared/links"