Skip to content

Instantly share code, notes, and snippets.

@caike
Created January 23, 2011 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caike/791830 to your computer and use it in GitHub Desktop.
Save caike/791830 to your computer and use it in GitHub Desktop.
custom validator checking if trending topic content is suitable
class Topic < ActiveRecord::Base
before_create :set_start_trending
before_save :set_hot_topic, :if => Proc.new { |topic| topic.mentions >= HOT_TOPIC_MENTIONS }
scope :trending_top, lambda { |num| where("started_trending > ?", 1.day.ago).order('mentions desc').limit(num) }
HOT_TOPIC_MENTIONS = 1000
validate :appropriate_content
def to_param
"#{id}-#{name.parameterize}"
end
private
def appropriate_content
unless ContentModerator.is_suitable?(self.name)
self.errors.add(:name, 'is inappropriate')
end
end
def set_start_trending
self.started_trending = Time.now
end
def set_hot_topic
self.hot_topic = true #if self.mentions > HOT_TOPIC_MENTIONS
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment