Last active
September 14, 2016 13:57
-
-
Save eduardopoleo/2585642ddbce8f6656e55775ccd54c94 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 = UserSignupManager.perform(user_params, invitation_token) | |
| if @user.errors.blank? | |
| flash[:success] = "Welcome to Raysurfing. Hope you enjoy it!" | |
| redirect_to posts_path | |
| else | |
| flash[:error] = "Sorry there was an error with your regitration" | |
| render :new | |
| end | |
| end | |
| private | |
| def user_params | |
| params.require(:user).permit(:email, :name, :username) | |
| end | |
| def invitation_token | |
| params[:invitation_token] | |
| end | |
| end |
This file contains hidden or 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 UserSignupManager | |
| attr_reader :invitation_token, :status, :user | |
| def initialize(user_params, invitation_token) | |
| @user = User.new(user_params) | |
| @invitation_token = invitation_token | |
| end | |
| def self.perform | |
| new(user_params, params[:invitation_token]).perform | |
| end | |
| def perform | |
| begin | |
| persistance_actions | |
| mailer_actions | |
| user | |
| rescue ActiveRecord::RecordInvalid => e | |
| user.errors[:base] << "#{e}" | |
| user | |
| end | |
| end | |
| private | |
| def persistance_actions | |
| ActiveRecord::Base.transaction do | |
| @user = user.save | |
| InvitationHandler.handle(user, invitation_token) | |
| end | |
| end | |
| def mailer_actions | |
| MilestoneChecker.check | |
| AppMailer.welcome_email(@user) | |
| end | |
| end |
This file contains hidden or 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 InvitationHandler | |
| attr_reader :user, :invitation_token | |
| def initialize(user, invitation_token) | |
| @user = user | |
| @invitation_token = invitation_token | |
| end | |
| def self.handle | |
| new.(user, invitation_token).handle | |
| end | |
| def handle | |
| return unless invitation_token | |
| follow_relationships | |
| stale_invitation | |
| end | |
| private | |
| def follow_relationships | |
| user.leaders << invitation_user | |
| invitation_user.followers << user | |
| invitation_user.update_points(500) | |
| end | |
| def stale_invitation | |
| invitation.update_attribute(:token, nil) | |
| end | |
| def inivitation_user | |
| @inivitation_user ||= Invitation.find_by(token: params[:invitation_token]).user | |
| end | |
| end |
This file contains hidden or 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 MilestoneChecker | |
| def self.check | |
| return unless User.count == 50000 | |
| admins = User.where(admin: true) | |
| AppMailer.community_milestone_reached(admins) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment