Invitation system fully working

James Peret 10 年之前
父节点
当前提交
f675b197d9

+ 1 - 0
app/assets/stylesheets/avl2_theme/panel.less

@@ -102,6 +102,7 @@
102 102
 				     margin-top: 8px;
103 103
 				}
104 104
 				.btn { margin-top: -4px; }
105
+				.date.small-text { margin-right: 10px; margin-top: 4px;}
105 106
 			}
106 107
 		}
107 108
 		.panel-divider-top { border-top: @theme-border; }

+ 32 - 5
app/controllers/invites_controller.rb

@@ -12,23 +12,50 @@ class InvitesController < ApplicationController
12 12
     @invite = Invite.where(email: @to).where(user_id: current_user.id).first
13 13
     
14 14
     if @invite == nil
15
+      original_invite = Invite.where(email: @to).first
15 16
       @invite = Invite.create!(email: @to, user_id: current_user.id, invitations_sent: 1, last_invitation_sent_date: Time.now)
17
+      if original_invite == nil
18
+        # Devise Invitable
19
+        @user = User.invite!({email: @to}, current_user) do |u|
20
+          u.skip_invitation = true
21
+        end
22
+        token = @user.raw_invitation_token
23
+        token_url = accept_invitation_url(@user, :invitation_token => token)
24
+        @invite.update(token: token_url, new_user_id: @user.id)
25
+      else
26
+        @invite.update(token: original_invite.token, new_user_id: original_invite.new_user_id)
27
+      end
16 28
     else
17
-      @invite.update(invitations_sent: (@invite.invitations_sent + 1), last_invitation_sent_date: Time.now)
29
+      @invite.update(invitations_sent: (@invite.invitations_sent.to_i + 1), last_invitation_sent_date: Time.now)
18 30
     end
31
+    
32
+    @user = User.find(@invite.new_user_id)
19 33
 
20
-    @user = User.invite!({email: @to}, current_user) do |u|
21
-      u.skip_invitation = true
34
+    if UserMailer.invite_message(@user, current_user, @message, @invite.token).deliver
35
+      @user.update(invitation_sent_at: Time.now.utc)
36
+      @analytics.track('Invite sent')
37
+      flash[:notice] = "successfully sent invite to #{@user.email}"
38
+      redirect_to invite_path
39
+    else
40
+      redirect_to invite_path, alert: (t 'invite.error_invitation_not_sent')
22 41
     end
42
+  end
43
+  
44
+  def resend_invite_message
45
+
46
+    @invite = Invite.find(params[:id])
47
+    @invite.update(invitations_sent: (@invite.invitations_sent.to_i + 1), last_invitation_sent_date: Time.now)
48
+    
49
+    @user = User.where(email: @invite.email).first
23 50
 
24
-    UserMailer.invite_message(@user, current_user, @message).deliver if @user.errors.empty?
51
+    UserMailer.invite_message(@user, current_user, @message, @invite.token).deliver
25 52
 
26 53
     if @user.errors.empty? && @user.update(invitation_sent_at: Time.now.utc)
27 54
       @analytics.track('Invite sent')
28 55
       flash[:notice] = "successfully sent invite to #{@user.email}"
29 56
       redirect_to invite_path
30 57
     else
31
-      render :invite
58
+      redirect_to invite_path, alert: (t 'invite.error_invitation_not_sent')
32 59
     end
33 60
   end
34 61
   

+ 3 - 0
app/controllers/users/invitations_controller.rb

@@ -19,6 +19,9 @@ class Users::InvitationsController < Devise::InvitationsController
19 19
         @user.update_attributes update_resource_params.except(:invitation_token)
20 20
         @analytics.track_invitation_accepted(@user)
21 21
         Resque.enqueue(SendSignupMessageSimple, @user.id) 
22
+        #Mark invites as accepted
23
+        invites = Invite.where(email: @user.email)
24
+        invites.each {|i| i.update(invited: true, ) }
22 25
         super
23 26
       end
24 27
     end

+ 2 - 3
app/mailers/user_mailer.rb

@@ -42,11 +42,10 @@ class UserMailer < ActionMailer::Base
42 42
          :body      => (t 'newsletter_subscription.message')
43 43
   end
44 44
   
45
-  def invite_message(user, from, content)
45
+  def invite_message(user, from, content, token)
46 46
       @user = user
47 47
       @from = from
48
-      @token = user.raw_invitation_token
49
-      invitation_link = accept_user_invitation_url(:invitation_token => @token)
48
+      @invite_url = token
50 49
       I18n.with_locale(from.language) do
51 50
         mail(:from => from.email, :bcc => from.email, :to => @user.email, :subject => (t 'invite_email.email_subject'))
52 51
       end

+ 1 - 0
app/models/invite.rb

@@ -1,3 +1,4 @@
1 1
 class Invite < ActiveRecord::Base
2 2
   belongs_to :user
3
+  belongs_to :user, :foreign_key => 'new_user_id'
3 4
 end

+ 9 - 2
app/views/invites/invite.html.erb

@@ -56,7 +56,7 @@
56 56
 
57 57
 	<% end %>
58 58
 	
59
-	<% if @sent_invites != nil %>
59
+	<% if @sent_invites.count > 0  %>
60 60
 		<%= content_tag(:div, class: 'row') do %>
61 61
 			<%= content_tag(:div, class: 'offset2 span8') do %>
62 62
 				<%= content_tag(:div, class: 'panel panel-default') do %>
@@ -70,7 +70,14 @@
70 70
 									<%= content_tag(:li) do %>
71 71
 										<%= content_tag(:p) do %>
72 72
 											<%= invite.email %>
73
-											<%= link_to((t 'invite.resend_invitation'), "#", class: 'btn btn-mini pull-right')%>
73
+											
74
+											<% if invite.invited %>
75
+												<%= content_tag(:span, " ", style: "width: 120px; height: 10px;", class: 'pull-right')%>
76
+												<%= content_tag(:span, t("invite.invite_accpeted"), class: 'pull-right small-text date')%>
77
+											<% else %>
78
+												<%= link_to((t 'invite.resend_invitation'), resend_invite_message_path(id: invite.id), class: 'btn btn-mini pull-right')%>
79
+												<%= content_tag(:span, ((t "invite.last_invite_sent") + " " + time_ago_in_words(invite.last_invitation_sent_date).downcase + " " + (t 'blog.ago')), class: 'pull-right small-text date')%>
80
+											<% end %>
74 81
 										<% end %>
75 82
 									<% end %>
76 83
 								<% end %>

+ 1 - 1
app/views/user_mailer/invite_message.html.erb

@@ -334,7 +334,7 @@
334 334
 									<% end %>
335 335
 									
336 336
 									<%= content_tag(:div, class: 'btn-container') do %>
337
-										<%= link_to((t 'sign_up_email.start'), accept_invitation_url(@user, :invitation_token => @token), class: 'btn btn-success') %>
337
+										<%= link_to((t 'sign_up_email.start'), @invite_url, class: 'btn btn-success') %>
338 338
 									<% end %>
339 339
 								
340 340
 								<% end %>

+ 1 - 0
config/locales/email.en.yml

@@ -21,6 +21,7 @@ en:
21 21
     start: 'Start'
22 22
   invite:
23 23
     send: 'Send Invite'
24
+    last_invite_sent: 'sent'
24 25
   invite_email:
25 26
     email_subject: 'Join the Avalanche'
26 27
     welcome: 'Join the Avalanche'

+ 1 - 0
config/locales/email.pt-BR.yml

@@ -21,6 +21,7 @@ pt-BR:
21 21
     start: 'Começar'
22 22
   invite:
23 23
     send: 'Enviar convite'
24
+    last_invite_sent: 'Enviado em'
24 25
   invite_email:
25 26
     email_subject: 'Participe da Avalanche'
26 27
     welcome: 'Participe da Avalanche'

+ 1 - 0
config/routes.rb

@@ -62,6 +62,7 @@ Avalanche2::Application.routes.draw do
62 62
   # Invites
63 63
   get 'invite' => 'invites#invite', as: :invite
64 64
   post 'send_invite_message' => 'invites#send_invite_message', as: :send_invite_message
65
+  get 'resend_invite_message/:id' => 'invites#resend_invite_message', as: :resend_invite_message
65 66
   
66 67
   # Rewards
67 68
   get 'dashboard/rewards' => 'agents#rewards', as: :user_rewards

+ 6 - 0
db/migrate/20150419000914_add_token_to_invites.rb

@@ -0,0 +1,6 @@
1
+class AddTokenToInvites < ActiveRecord::Migration
2
+  def change
3
+    add_column :invites, :token, :string
4
+    add_column :invites, :new_user_id, :integer
5
+  end
6
+end

+ 3 - 1
db/schema.rb

@@ -11,7 +11,7 @@
11 11
 #
12 12
 # It's strongly recommended that you check this file into your version control system.
13 13
 
14
-ActiveRecord::Schema.define(version: 20150417040350) do
14
+ActiveRecord::Schema.define(version: 20150419000914) do
15 15
 
16 16
   # These are extensions that must be enabled in order to support this database
17 17
   enable_extension "plpgsql"
@@ -100,6 +100,8 @@ ActiveRecord::Schema.define(version: 20150417040350) do
100 100
     t.datetime "last_invitation_sent_date"
101 101
     t.datetime "created_at"
102 102
     t.datetime "updated_at"
103
+    t.string   "token"
104
+    t.integer  "new_user_id"
103 105
   end
104 106
 
105 107
   add_index "invites", ["user_id"], name: "index_invites_on_user_id", using: :btree

二进制
dump.rdb