From 1a911a8b996c88f678e290df7ab7b1fcf1e9a95b Mon Sep 17 00:00:00 2001 From: zhitomirskiyi Date: Wed, 26 Jan 2011 16:01:57 -0800 Subject: [PATCH] translation of invitation message, better checking of existing users and unique index on user(invitation_service and invitation_identifier) --- app/controllers/services_controller.rb | 12 +++++----- app/models/invitation.rb | 29 +++++++++++++----------- app/views/invitations/edit.html.haml | 3 +++ app/views/services/finder.html.haml | 2 +- config/locales/diaspora/en.yml | 3 +++ db/schema.rb | 3 ++- spec/models/invitation_spec.rb | 31 +++++++++++++++----------- 7 files changed, 48 insertions(+), 35 deletions(-) diff --git a/app/controllers/services_controller.rb b/app/controllers/services_controller.rb index 452cc0319..55838a21b 100644 --- a/app/controllers/services_controller.rb +++ b/app/controllers/services_controller.rb @@ -52,15 +52,13 @@ class ServicesController < ApplicationController def inviter @uid = params[:uid] - @subject = "Join me on DIASPORA*" - + @subject = t('.join_me_on_diaspora') invited_user = current_user.invite_user(params[:aspect_id], params[:provider], params[:uid]) - @message = < invited_user.invitation_token)} +#{t('.click_link_to_accept_invitation')}: +\n +\n +#{accept_invitation_url(invited_user, :invitation_token => invited_user.invitation_token)} MSG redirect_to "https://www.facebook.com/?compose=1&id=#{@uid}&subject=#{@subject}&message=#{@message}&sk=messages" end diff --git a/app/models/invitation.rb b/app/models/invitation.rb index 134cb28cf..ddee7c942 100644 --- a/app/models/invitation.rb +++ b/app/models/invitation.rb @@ -12,7 +12,8 @@ class Invitation < ActiveRecord::Base def self.invite(opts = {}) return false if opts[:identifier] == opts[:from].email - existing_user = User.where(:email => opts[:identifier]).first + + existing_user = self.find_existing_user(opts[:service], opts[:identifier]) if existing_user if opts[:from].contact_for(opts[:from].person) @@ -24,10 +25,12 @@ class Invitation < ActiveRecord::Base raise "You already invited this person" end end + + opts[:existing_user] = existing_user create_invitee(opts) end - def self.new_or_existing_user_by_service_and_identifier(service, identifier) + def self.find_existing_user(service, identifier) existing_user = User.where(:invitation_service => service, :invitation_identifier => identifier).first if service == 'email' @@ -36,20 +39,20 @@ class Invitation < ActiveRecord::Base existing_user ||= User.joins(:services).where(:services => {:provider => service, :uid => identifier}).first end - if existing_user - existing_user - else - result = User.new() - result.invitation_service = service - result.invitation_identifier = identifier - result.email = identifier if service == 'email' - result.valid? - result - end + existing_user + end + + def self.new_user_by_service_and_identifier(service, identifier) + result = User.new() + result.invitation_service = service + result.invitation_identifier = identifier + result.email = identifier if service == 'email' + result.valid? + result end def self.create_invitee(opts = {}) - invitee = new_or_existing_user_by_service_and_identifier(opts[:service], opts[:identifier]) + invitee = opts[:existing_user] || new_user_by_service_and_identifier(opts[:service], opts[:identifier]) return invitee if opts[:service] == 'email' && !opts[:identifier].match(Devise.email_regexp) invitee.invites = opts[:invites] || 0 if invitee.new_record? diff --git a/app/views/invitations/edit.html.haml b/app/views/invitations/edit.html.haml index 0377c128c..42c526137 100644 --- a/app/views/invitations/edit.html.haml +++ b/app/views/invitations/edit.html.haml @@ -13,6 +13,9 @@ %p = f.label :username , t('username') = f.text_field :username, :title => t('registrations.new.enter_username') + %p + = f.label :email , t('email') + = f.text_field :email, :title => t('registrations.new.enter_email') %p = f.label :password , t('password') = f.password_field :password, :title => t('registrations.new.enter_password') diff --git a/app/views/services/finder.html.haml b/app/views/services/finder.html.haml index 502fb49a0..1d5f17c10 100644 --- a/app/views/services/finder.html.haml +++ b/app/views/services/finder.html.haml @@ -30,7 +30,7 @@ :rel => 'facebox' - else = form_tag service_inviter_path do - = select_tag (:aspect_id, options_from_collection_for_select(@all_aspects, 'id', 'name')) + = select_tag(:aspect_id, options_from_collection_for_select(@all_aspects, 'id', 'name')) = hidden_field_tag :uid, uid = hidden_field_tag :provider, 'facebook' = submit_tag "invite" diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 3acbc3bb8..d927ba721 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -424,6 +424,9 @@ en: success: "Successfully deleted authentication." failure: error: "there was an error connecting that service" + inviter: + join_me_on_diaspora: "Join me on DIASPORA*" + click_link_to_accept_invitation: "Click this link to accept your invitation" notifier: hello: "Hello %{name}!" love: "love," diff --git a/db/schema.rb b/db/schema.rb index 85b57776e..f4baacd55 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110126225202) do +ActiveRecord::Schema.define(:version => 20110126232040) do create_table "aspect_memberships", :force => true do |t| t.integer "aspect_id" @@ -453,6 +453,7 @@ ActiveRecord::Schema.define(:version => 20110126225202) do end add_index "users", ["email"], :name => "index_users_on_email" + add_index "users", ["invitation_service", "invitation_identifier"], :name => "index_users_on_invitation_service_and_invitation_identifier", :unique => true add_index "users", ["invitation_token"], :name => "index_users_on_invitation_token" add_index "users", ["mongo_id"], :name => "index_users_on_mongo_id" add_index "users", ["username"], :name => "index_users_on_username", :unique => true diff --git a/spec/models/invitation_spec.rb b/spec/models/invitation_spec.rb index 4c7ebe3de..ba1808289 100644 --- a/spec/models/invitation_spec.rb +++ b/spec/models/invitation_spec.rb @@ -45,16 +45,8 @@ describe Invitation do @invitation.message.should == "!" end - describe '.new_or_existing_user_by_email' do - let(:inv){Invitation.new_or_existing_user_by_service_and_identifier(@type, @identifier)} - before do - @users = [] - 8.times do - @users << Factory.create(:user) - end - @user_fb_id = 'abc123' - @user_fb = Factory.create(:user, :invitation_service => "facebook", :invitation_identifier => @user_fb_id) - end + describe '.new_user_by_service_and_identifier' do + let(:inv){Invitation.new_user_by_service_and_identifier(@type, @identifier)} it 'returns User.new for a non-existent user for email' do @type = "email" @@ -77,9 +69,21 @@ describe Invitation do inv.reload }.should raise_error ActiveRecord::RecordNotFound end + end - context 'returns an existing user' do - context 'active users' do + describe '.find_existing_user' do + let(:inv){Invitation.find_existing_user(@type, @identifier)} + before do + @users = [] + 8.times do + @users << Factory.create(:user) + end + @user_fb_id = 'abc123' + @user_fb = Factory.create(:user, :invitation_service => "facebook", :invitation_identifier => @user_fb_id) + end + + context 'send a request to an existing' do + context 'active user' do it 'by email' do @identifier = @users[3].email @type = 'email' @@ -98,7 +102,7 @@ describe Invitation do end end - context 'invitated users' do + context 'invitated user' do it 'by email' do @identifier = @users[3].email @type = 'email' @@ -204,6 +208,7 @@ describe Invitation do @invitee = Invitation.create_invitee(:service => 'email', :identifier => @email) end it 'creates no user' do + @valid_params[:existing_user] = @invitee lambda { Invitation.create_invitee(@valid_params) }.should_not change(User, :count)