Class: Cally::InvitationsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/cally/invitations_controller.rb

Instance Method Summary collapse

Methods included from Methods

#admin_or_same_user?, #logged_in_as_admin?, #same_user?, #set_mailgun_prefix, #test_env?

Methods included from ApplicationHelper

#current_user, #is_admin?, #is_first_user?, #logged_in?

Instance Method Details

#createObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/cally/invitations_controller.rb', line 24

def create
  # Here it will add the e-mail address and a random generated key into the database.
  # If this is not invitation only then redirects to the signup path.
  if @invitation_only == 'true'
    @invitation = Invitation.new(invitation_params)
    @invitation.invitation_key = OmwRandomString.generate(32)

    if @invitation.save
      flash[:success] = "Created invitation request for '#{@invitation.email}'."
      redirect_to 
    else
      flash[:error] = 'Error create invitation request.'
      render 'new'
    end
  else
    flash[:error] = 'This is an open subscription, no invitation needed.'
    redirect_to new_user_path
  end
end

#destroyObject



49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/cally/invitations_controller.rb', line 49

def destroy
  # removes a invitation from the database.
  invite = Invitation.find(params[:id])

  if invite.destroy
    flash[:success] = 'Invitation successfully deleted.'
    redirect_to all_invitations_path
  else
    flash[:error] = 'Error deleting invitation.'
    redirect_to all_invitations_path
  end
end

#indexObject



44
45
46
47
# File 'app/controllers/cally/invitations_controller.rb', line 44

def index
  # Shows all the pending invitations. 20 per page
  @invitations = Invitation.paginate(page: params[:page], per_page: 20)
end

#newObject



13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/cally/invitations_controller.rb', line 13

def new
  # if this website is invitation only then you can enter an e-mail address to request
  # an invitation. Else it redirects to the signup path.
  if @invitation_only == 'true'
    @invitation = Invitation.new
  else
    flash[:error] = 'This is an open subscription, no invitation needed.'
    redirect_to new_user_path
  end
end

#send_invitationObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/cally/invitations_controller.rb', line 62

def send_invitation
  # Here you can send the created invitation the the user's e-mail address given when created
  # a new invitation.

  # set_mailgun_prefix first checks if a custom prefix is set in an environment variable.
  # If not it uses the default as given in lib/cally/methods.rb
  set_mailgun_prefix
  invitation = Invitation.find(params[:id])

  # The invitation url is the url which is send to the user's e-mail address given when creating a new
  # invitation. 
  # If the user clicks on the url in his received e-mail it take's the user
  # to the signup page where he/she can fill his/her information and create a password.
  invitation_url = request.base_url + "/#{@mailgun_prefix}invitation/#{invitation.invitation_key}"

  if invitation.update(sent: true) && send_invite(invitation.email, invitation_url) 
    flash[:success] = "Sent invitation to '#{invitation.email}'" 
    redirect_to all_invitations_path
  else
    flash[:error] = "Error sending invitation url to '#{invitation.email}'"
    redirect_to all_invitations_path
  end
end