class Source
include Mongoid::Document
include Mongoid::Slug
include Mongoid::Timestamps # adds created_at and updated_at fields
attr_reader :twitter_username, :rss_username
embeds_one :twitter, :class_name => "Twitter"
# fields
field :name, :type => String
field :url, :type => String
field :active, :type => Boolean, :default => true
field :deleted, :type => Boolean, :default => false
field :source_type, :type => String, :default => 'fighter'
slug :name
# validations
validates_inclusion_of :source_type, :in => ['fighter', 'promotion', 'news-site', 'reporter']
validates_presence_of :name, :url, :source_type
validates_uniqueness_of :url, :name
# indexes
index :name
index :active
index :deleted
index [[:active, Mongo::DESCENDING], [:deleted, Mongo::DESCENDING]]
index [[:slug, Mongo::DESCENDING], [:active, Mongo::DESCENDING], [:deleted, Mongo::DESCENDING]]
# scopes
scope :active, where(:active => true, :deleted => false)
def self.source_types
types = []
types.push(['Fighter', 'fighter'])
types.push(['Promotion', 'promotion'])
types.push(['News Source', 'news-site'])
types.push(['Reporter', 'reporter'])
end
def twitter_username
self.twitter.username
end
def rss_username
"rss"
end
end