Skip to content

Instantly share code, notes, and snippets.

@ngw
Created February 5, 2011 21:36
Show Gist options
  • Save ngw/32c74f2a8fa42b8ebc11 to your computer and use it in GitHub Desktop.
Save ngw/32c74f2a8fa42b8ebc11 to your computer and use it in GitHub Desktop.
class Authorization
include Mongoid::Document
include Mongoid::Timestamps
attr_protected :_id
field :provider, :type => String
field :uid, :type => String
field :token, :type => String
field :secret, :type => String
field :url, :type => String
field :created_at, :type => DateTime
referenced_in :user
validates_presence_of :user, :provider, :uid, :token, :secret
validate :existence
protected
def existence
if Authorization.find( :first, :conditions => { :uid => uid, :provider => provider } )
errors.add( :base, "There's already an account registered with this #{ provider.capitalize } ID")
end
end
end
#############
class User
include Mongoid::Document
include Mongoid::Timestamps
attr_protected :_id
accepts_nested_attributes_for :authentications
field :subdomain, :type => String, :index => true
field :email, :type => String, :index => true
field :nickname, :type => String
field :given_name, :type => String
field :family_name, :type => String
field :location, :type => String, :index => true
field :url, :type => String
field :birthday, :type => Date
field :created_at, :type => DateTime, :index => true
field :updated_at, :type => DateTime, :index => true
validates_presence_of :nickname, :email, :subdomain, :authorizations
validates_uniqueness_of :nickname, :email, :subdomain
validates_associated :authorizations
references_many :authorizations, :dependent => :delete
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment