/
invites_controller.rb
120 lines (104 loc) · 3.23 KB
/
invites_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# encoding: utf-8
class InvitesController < ApplicationController
requires_authentication :except => [:accept]
requires_user :except => [:accept]
requires_user_admin :only => [:all]
respond_to :html, :mobile, :xml, :json
before_filter :load_invite, :only => [:show, :edit, :update, :destroy]
before_filter :verify_invites, :only => [:new, :create]
protected
# Finds the requested invite
def load_invite
begin
@invite = Invite.find(params[:id])
rescue ActiveRecord::RecordNotFound
render_error 404 and return
end
end
# Verifies that the user has available invites
def verify_invites
unless @current_user && @current_user.available_invites?
respond_to do |format|
format.any(:html, :mobile) do
flash[:notice] = "You don't have any invites!"
redirect_to online_users_url and return
end
format.any(:xml, :json) do
render :text => "You don't have any invites!", :status => :method_not_allowed
end
end
end
end
public
# Show active invites
def index
respond_with(@invites = @current_user.invites.active)
end
# Show everyone's invites
def all
respond_with(@invites = Invite.find_active)
end
# Accept an invite
def accept
@invite = Invite.find_by_token(params[:id])
session[:invite_token] = nil
if @invite && @invite.expired?
@invite.destroy
flash[:notice] ||= "Your invite has expired!"
elsif @invite
session[:invite_token] = @invite.token
redirect_to new_user_by_token_url(:token => @invite.token) and return
else
flash[:notice] ||= "That's not a valid invite!"
end
redirect_to login_users_url and return
end
# Create a new invite
def new
respond_with(@invite = @current_user.invites.new)
end
# Create a new invite
def create
@invite = @current_user.invites.create(params[:invite])
if @invite.valid?
begin
Mailer.invite(@invite, accept_invite_url(:id => @invite.token)).deliver
flash[:notice] = "Your invite has been sent to #{@invite.email}"
rescue
flash[:notice] = "There was a problem sending your invite to #{@invite.email}, it has been cancelled."
@invite.destroy
end
redirect_to invites_url and return
else
render :action => :new
end
end
# def show
# if verify_user(:user => @invite.user, :user_admin => true)
# render :action => :edit
# end
# end
#
# def edit
# verify_user(:user => @invite.user, :user_admin => true)
# end
#
# def update
# if verify_user(:user => @invite.user, :user_admin => true)
# if @invite.update_attributes(params[:invite])
# flash[:notice] = "Invite was updated"
# redirect_to invites_url and return
# else
# render :action => :edit
# end
# end
# end
# Delete an invite
def destroy
if verify_user(:user => @invite.user, :user_admin => true)
@invite.destroy
flash[:notice] = "Your invite has been cancelled."
redirect_to invites_url and return
end
end
end