Created
January 21, 2009 18:39
-
-
Save cilquirm/50097 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'cgi' | |
class Topic | |
include DataMapper::Resource | |
Source = Enum[:web,:mobile,:partner,:migrated,:admin,:testing] | |
property :id, Integer, :serial => true | |
property :subject, String, :nullable => false, :length => 200 | |
property :coded_subject, String, :length => 200 | |
property :spark, Text, :lazy => false | |
property :body, Text, :lazy => false | |
property :current_revision, Integer, :nullable => false, :default => 0 | |
property :alternate_wording, String, :nullable => true, :length => 250 | |
property :posted_anonymously, Boolean, :default => false | |
property :created_at, DateTime | |
property :updated_at, DateTime | |
property :ip, String | |
property :source, Source, :default => :web | |
property :comment_count, Integer, :default => 0 | |
belongs_to :network, :class_name => 'Network', :child_key => [:network_id], :parent_key => [:id] | |
property :network_id, Integer | |
belongs_to :group, :class_name => 'Group', :child_key => [:group_id], :parent_key => [:id] | |
property :group_id, Integer, :nullable => false, :message => "Group must be specified" | |
belongs_to :started_by, :class_name => 'User', :child_key => [ :started_by_id], :parent_key => [:id] | |
property :started_by_id, Integer | |
belongs_to :most_recent_editor, :class_name => 'User', :child_key => [ :most_recent_editor_id ], :parent_key => [:id] | |
property :most_recent_editor_id, Integer | |
# has n, :topic_groups | |
# has n, :groups, :through => :topic_groups | |
has n, :topic_histories | |
has n, :aliases, :class_name => 'TopicAlias' | |
has n, :comments | |
remix 1, :features, :class_name => 'FeatureTopic', :as => 'feature' | |
attr_accessor :last_subject | |
attr_accessor :body_dirty | |
attr_accessor :publish_to_facebook | |
validates_with_method :unique_in_group | |
before :valid?, :encode_subject | |
before :subject=, :set_last_subject | |
after :body=, :mark_body_dirty | |
# before :valid?, :sanitize_content | |
before :valid?, :set_started_by | |
before :valid?, :record_spark | |
before :valid?, :alias_old_subject | |
before :valid?, :remove_conflicting_alias | |
after :valid?, :update_current_revision | |
after :save, :save_topic_history | |
# def serialize_properties | |
# { :body => ( self.body ? CGI.escape(self.body).gsub('+',' ') : nil ) } | |
# | |
# end | |
def publish_to_facebook=(var) | |
@publish_to_facebook = ( var == "1" || var == "on") | |
end | |
def network_coded_name | |
network.coded_name | |
end | |
def unique_in_group | |
same_subject = Topic.first( :coded_subject => self.coded_subject, :group_id => self.group_id, :id.not => self.id ) | |
if not same_subject.nil? | |
[false, "Topic is already taken - <a href='http://#{SERVER}/#{same_subject.network.coded_name}/groups/#{same_subject.group.coded_name}/topics/#{same_subject.coded_subject}'>#{same_subject.subject}</a>" ] | |
else | |
true | |
end | |
end | |
# xss_terminate :sanitize => [:body, :subject, :spark] | |
#copy started_by from most_recent_editor | |
def sanitize_content | |
subject.sanitize_html! if subject | |
body.sanitize_html! if body | |
spark.sanitize_html! if spark | |
end | |
def update_current_revision | |
if self.body_dirty | |
self.current_revision += 1 | |
end | |
end | |
def save_topic_history(*params) | |
if self.body_dirty | |
TopicHistory.create( :revision => self.current_revision, :subject => self.subject, :body => self.body, :topic => self, :user => User.get( (self.most_recent_editor || self.started_by).id ), :ip => self.ip) | |
end | |
end | |
def set_started_by( context = :default ) | |
self.started_by ||= User.get self.most_recent_editor.id | |
end | |
def set_last_subject( new_subject ) | |
self.last_subject = self.subject # if attribute_dirty?(:subject) # I think this could fix the issue described in alias_old_subject | |
end | |
def mark_body_dirty | |
self.body_dirty = true | |
end | |
# copy body to spark if there is none | |
def record_spark(context = :default) | |
self.spark ||= body | |
end | |
def alias_old_subject(context = :default) | |
return if last_subject.nil? | |
if not self.new_record? # NEED NEW CONDITION (probably) now that last_subject will always be true whether the name has changed or not # this is a rename, not a new topic | |
@ta = TopicAlias.create :coded_subject => last_subject.seoify, :network => Network.get(network.id), :topic => self, :user => User.get(self.most_recent_editor.id ) | |
end | |
end | |
def encode_subject(context = :default) | |
if last_subject || (coded_subject.nil? && !subject.blank?) | |
self.coded_subject = subject.seoify | |
end | |
end | |
def remove_conflicting_alias(context = :default) | |
@alias = TopicAlias.first :conditions => ['coded_subject = ? and network_id = ?', coded_subject, network.id] | |
@alias.destroy if @alias | |
end | |
class << self | |
def find_by_coded_subject subj, net | |
begin | |
Topic.first(:coded_subject => subj, Topic.network.coded_name => net) || | |
TopicAlias.first(:coded_subject => subj, TopicAlias.network.coded_name => net).topic | |
rescue | |
nil | |
end | |
end | |
def newest | |
all( :order => [:updated_at.desc] ) | |
end | |
def in_network(network) | |
all(:network_id => network) | |
end | |
def started_by_user(user_id) | |
all(:started_by_id => user_id) | |
end | |
def self.count_in_networks( networks) | |
query[Topic.network.id] = Array(networks) | |
Topic.count( query ) | |
end | |
def count_in_networks( networks) | |
query[Topic.network.id] = Array(networks) | |
Topic.count( query ) | |
end | |
def most_active | |
all(:order => [:comment_count.desc] ) | |
end | |
def featured | |
now = DateTime.now | |
all(Topic.feature.priority.gt => 0, Topic.feature.start_date.lte => now, Topic.feature.end_date.gt => now ).sort{ |a,b| a.feature.priority <=> b.feature.priority} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment