Skip to content

Instantly share code, notes, and snippets.

@zenom
Created August 8, 2010 20:17
Show Gist options
  • Save zenom/514482 to your computer and use it in GitHub Desktop.
Save zenom/514482 to your computer and use it in GitHub Desktop.
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
class Twitter
# TODO
# I think the problem might be the field names and how i am doing them in the forms
include Mongoid::Document
include Mongoid::Timestamps # adds created_at and updated_at fields
field :username, :type => String
field :uid, :type => String
field :name, :type => String
before_update :get_uid
embedded_in :source, :inverse_of => :twitter
def get_uid
puts "*" * 150
puts "THIS WAS CALLED"
puts "*" * 150
self.uid = "123123123123123"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment