Created
November 16, 2010 07:34
-
-
Save charlie/701559 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
# app/models/*.rb | |
class Business < ActiveRecord::Base | |
has_many :verifications, :as => :object | |
has_many :owners, :through => :verifications, :source => :verifiable, :source_type => "User", :conditions => "verified_at IS NOT NULL" | |
has_many :email_subscribers, :through => :verifications, :source => :verifiable, :source_type => "Email", :conditions => "verified_at IS NOT NULL" | |
has_many :sms_subscribers, :through => :verifications, :source => :verifiable, :source_type => "Phone", :conditions => "verified_at IS NOT NULL" | |
end | |
class Email < ActiveRecord::Base | |
belongs_to :verifications, :polymorphic => true | |
end | |
class Phone < ActiveRecord::Base | |
belongs_to :verifications, :polymorphic => true | |
end | |
class User < ActiveRecord::Base | |
has_many :verifications, :as => :object | |
has_many :emails, :through => :verifications, :source => :verifiable, :source_type => "Email", :conditions => "verified_at IS NOT NULL" | |
has_many :phones, :through => :verifications, :source => :verifiable, :source_type => "Phone", :conditions => "verified_at IS NOT NULL" | |
belongs_to :verification, :polymorphic => true | |
end | |
class Verification < ActiveRecord::Base | |
belongs_to :object, :polymorphic => true | |
belongs_to :verifiable, :polymorphic => true | |
attr_accessible :code, :verified_at, :object, :verifiable | |
end | |
# test/unit/verification_test.rb | |
require 'test_helper' | |
class VerificationTest < ActiveSupport::TestCase | |
def test_business_ownership_ | |
b = Business.create(:handle => "Charlie's Truck Stop") | |
u = User.create(:handle => "Bonnie") | |
assert_equal [], b.owners | |
v = b.verifications.create(:verifiable => u) | |
b.reload | |
assert_equal [], b.owners | |
v.verified_at = Time.now | |
v.save | |
b.reload | |
assert_equal [u], b.owners | |
v.destroy | |
b.reload | |
assert_equal [], b.owners | |
assert_equal "w00t", "w00t" | |
end | |
def test_phone_ownership | |
p = Phone.create(:handle => "8675309") | |
u = User.create(:handle => "Bonnie") | |
assert_equal [], u.phones | |
v = u.verifications.create(:verifiable => p) | |
u.reload | |
assert_equal [], u.phones | |
v.verified_at = Time.now | |
v.save | |
u.reload | |
assert_equal [p], u.phones | |
v.destroy | |
u.reload | |
assert_equal [], u.phones | |
assert_equal "w00t", "w00t" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment