Commit 1a911a8b authored by zhitomirskiyi's avatar zhitomirskiyi

translation of invitation message, better checking of existing users and...

translation of invitation message, better checking of existing users and unique index on user(invitation_service and invitation_identifier)
parent 6242ac64
......@@ -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 = <<MSG
Diaspora* is the social network that puts you in control of your information. You decide what you'd like to share, and with whom. You retain full ownership of all your information, including friend lists, messages, photos, and profile details.
Click here to accept your invitation:
#{accept_invitation_url(invited_user, :invitation_token => 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
......
......@@ -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?
......
......@@ -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')
......
......@@ -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"
......
......@@ -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,"
......
......@@ -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
......
......@@ -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
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 'returns an existing user' do
context 'active users' do
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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment