Created
October 7, 2008 21:56
-
-
Save mikehale/15396 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 ContestController < ApplicationController | |
session :off | |
no_login_required | |
skip_before_filter :verify_authenticity_token | |
def index | |
render :text => "helloworld!" | |
end | |
def create | |
c = Contestant.new(:email => params[:contestant_email]) | |
c.invite_friends(params[:friend_emails]) | |
c.save! | |
render :text => c.inspect | |
end | |
end |
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 Contestant < ActiveRecord::Base | |
has_many :invitations, :foreign_key => 'inviter_id' | |
has_many :invites, :class_name => "Invitation", :foreign_key => 'invitee_id' | |
has_many :inviters, :through => :invites | |
has_many :invitees, :through => :invitations | |
def entries | |
self.invitations.size + 1 | |
end | |
def invite_friends(friend_emails) | |
friend_emails.each{|email| | |
self.invitations << Invitation.create!(:email => email) | |
} if friend_emails | |
end | |
end |
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 Invitation < ActiveRecord::Base | |
belongs_to :inviter, :class_name => 'Contestant' | |
belongs_to :invitee, :class_name => 'Contestant' | |
def email=(email) | |
unless [:invitee].nil? | |
self.create_invitee!(:email => email) | |
else | |
[:invitee][:email] = email | |
end | |
end | |
def email | |
[:invitee][:email] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment