gist: 15396 Download_button fork
public
Public Clone URL: git://gist.github.com/15396.git
contest_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
contestant.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
 
invitation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
 

Owner

mikehale

Revisions