make sure invitation has been created successfully before sending it
[shapado:piglops-shapado.git] / app / controllers / invitations_controller.rb
1 class InvitationsController < ApplicationController
2   before_filter :login_required, :except => :accept
3
4   def index
5   end
6
7   def create
8     emails = params[:invitations][:emails].split(',')
9     user_role = params[:invitations][:user_role]
10     emails.each do |email|
11       invited_user = User.where(:email => email).first
12       unless email.blank? ||
13           (invited_user && current_group.is_member?(invited_user))
14         invitation = current_user.invite(email, user_role,
15                                        current_group,
16                                        params[:invitations][:body])
17         unless invitation.blank? || invitation.new?
18           Jobs::Mailer.async.on_new_invitation(invitation.id).commit!
19         end
20       end
21     end
22     flash[:notice] = t("flash_notice", :scope => "invitations.create")
23     redirect_to :back
24   end
25
26   def accept
27     @invitation = Invitation.find(params[:id])
28     @group = @invitation.group
29     if @invitation.group.is_email_only_signup? && @invitation.state?(:pending) &&
30         !logged_in?
31       redirect_to new_user_path(:invitation_id => params[:id])
32     elsif @invitation.state?(:pending) && logged_in?
33       current_user.accept_invitation(params[:id]) &&
34         @invitation.reload.connect!
35     end
36     @invitation.send(params[:step]) if params[:step]
37     if @invitation.state?(:follow_suggestions)
38       redirect_to '/'
39     end
40   end
41
42   def resend
43     invitation = Invitation.find(params[:id])
44     Jobs::Mailer.async.on_new_invitation(invitation.id).commit!
45     flash[:notice] = t("flash_notice", :scope => "invitations.create")
46     redirect_to :back
47   end
48
49   def revoke
50     invitation = Invitation.find(params[:id])
51     current_user.revoke_invite(invitation)
52     flash[:notice] = t("flash_notice", :scope => "invitations.create")
53     redirect_to :back
54   end
55 end