Last active
September 14, 2016 21:24
-
-
Save eduardopoleo/a4e30014a713d0d1b71bf821c49f54c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersController < ApplicationController | |
def create | |
@user = User.new(user_params) | |
if @user.save | |
#Sends the invitation email using the regular Rails mailers | |
AppMailer.welcome_email(@user) | |
if params[:invitation_token].present? | |
invitation = Invitation.find_by(token: params[:invitation_token]) | |
# each invitation is associated to the user who issued the invitation | |
invitation_user = invitation.user | |
# the new user now will follow the invitation user | |
@user.leaders << invitation_user | |
# the new user is now listed as a follower of the inviations user | |
invitation_user.followers << @user | |
# stales the invitation so that it can't be used twice | |
invitation.update_attribute(:token, nil) | |
# rewards the invitation_user for bringing more users yei! | |
invitation_user.update_points(500) | |
end | |
# Inform admins that their milestone has been reached | |
if User.count == 50000 | |
admins = User.where(admin: true) | |
AppMailer.community_milestone_reached(admins) | |
end | |
flash[:success] = "Welcome to Monchitos. Hope you enjoy it!" | |
redirect_to posts_path | |
else | |
flash[:error] = "Sorry your registration could not be completed" | |
render :new | |
end | |
end | |
private | |
def user_params | |
params.require(:user).permit(:email, :name, :username) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment