Skip to content

Instantly share code, notes, and snippets.

@krishnasrihari
Created November 23, 2012 03:36
Show Gist options
  • Save krishnasrihari/4133896 to your computer and use it in GitHub Desktop.
Save krishnasrihari/4133896 to your computer and use it in GitHub Desktop.
Refactor the code
# Please refactor following code.
# Please post your answer as a private gist. A private gist is a gist which anyone can access but in order to access one needs to know the u
# of the gist .
#
# I something is not clear then make a judgement and move forward. No need to ask a question to get clarification. This is
# just to speed things up.
#
# Controller
class People < ActionController::Base
# ... Other REST actions
def create
@person = Person.new(params[:person])
@person.people_handler
if @person.save
@person.people_mailer
redirect_to @person, :notice => "Account added!"
else
render :new
end
end
def validateEmail
@user = Person.find_by_slug(params[:slug])
Person.validate_mailer(@user)
end
end
# Model
class Person < ActiveRecord::Base
TEAM_RAINBOW = "UnicornRainbows"
TEAM_SCORPION = "LaserScorpions"
attr_accessible :first_name, :last_name, :email, :admin, :slug, :validated, :handle, :team
scope :latest, lambda { where('created_at < ? and validated = ?', Time.now - 30.days, false) }
def people_mailer
Emails.validate_email(self).deliver
Emails.admin_new_user(admins, self).deliver
end
def people_handler
slug = "ABC123#{Time.now.to_i.to_s}1239827#{rand(10000)}"
self.slug = slug
self.admin = false
handle_team
end
def handle_team
if (Person.count + 1).odd?
handle(TEAM_RAINBOW)
else
handle(TEAM_SCORPION)
end
end
def validate_mailer(user)
if user.present?
user.validated = true
user.save
Rails.logger.info "USER: User ##{user.id} validated email successfully."
Emails.admin_user_validated(admins, user)
Emails.welcome(user).deliver!
end
end
private
def admins
Person.where(:admin => true)
end
def handler(team)
handle = team + (Person.count + 1).to_s
self.handle = handle
self.team = team
end
end
# Mailer
class Emails < ActionMailer::Base
default from: 'foo@example.com'
def welcome(person)
@person = person
mail to: @person
end
def validate_email(person)
@person = person
mail to: @person
end
def admin_user_validated(admins, user)
@admins = mail_ids(admins)
@user = user
mail to: @admins
end
def admin_new_user(admins, user)
@admins = mail_ids(admins)
@user = user
mail to: @admins
end
def admin_removing_unvalidated_users(admins, users)
@admins = mail_ids(admins)
@users = users
mail to: admins
end
private
def mail_ids(admins)
admins.collect {|a| a.email } rescue []
end
end
# Rake Task
namespace :accounts do
desc "Remove accounts where the email was never validated and it is over 30 days old"
task :remove_unvalidated do
@people = Person.latest
@people.each do |person|
Rails.logger.info "Removing unvalidated user #{person.email}"
person.destroy
end
Emails.admin_removing_unvalidated_users(Person.where(:admin => true), @people).deliver
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment