Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class User < ActiveRecord::Base
validate_on_create :referrer_must_be_existing_user
...
def self.find_valid_referral(username)
case
when username.blank? then Elder.first(:group => "distribution_count ASC").user_id
when !username.blank? && self.exists?(:username => username) then User.find_by_username(username).id
else nil
end
end
...
protected
def referrer_must_be_existing_user
errors.add(:referral_id, "is not a valid user") if referral_id.blank?
end
end #User
class EarlyAdopter < ActiveRecord::Base
def self.increment_distribution_count(user_id)
find_by_user_id(user_id).increment!(:distribution_count)
end
end
class UsersController < ApplicationController
...
def create
@user = User.new(params[:user].merge(:referral_id => User.find_valid_referral(params[:user][:referral])))
if @user.save
redirect_to root_path
flash[:notice] = "You're awesome."
Elder.increment_distribution_count(@user.referral_id) if params[:user][:referral].blank?
else
flash[:error] = "Woops!"
render :action => 'new'
end
end
...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.