Created
May 1, 2010 22:54
-
-
Save erichurst/386734 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 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