socialforge/app/models/open_source_project.rb

26 lines
1.1 KiB
Ruby
Raw Normal View History

2014-03-29 10:57:36 +08:00
class OpenSourceProject < ActiveRecord::Base
attr_accessible :String
2014-04-03 14:41:04 +08:00
include Redmine::SafeAttributes
has_many :topics, :class_name => 'RelativeMemo', :foreign_key => 'osp_id', :conditions => "#{RelativeMemo.table_name}.parent_id IS NULL", :order => "#{RelativeMemo.table_name}.created_at DESC", :dependent => :destroy
has_many :relative_memos, :dependent => :destroy
has_many :tags, :through => :project_tags, :class_name => 'Tag'
has_many :project_tags, :class_name => 'ProjectTags'
acts_as_taggable
2014-03-29 10:57:36 +08:00
def short_description(length = 255)
description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description
end
2014-04-03 14:41:04 +08:00
def reset_counters!
self.class.reset_counters!(id)
end
def self.reset_counters!(id)
osp_id = id.to_i
update_all("topic_count = (SELECT COUNT(*) FROM #{RelativeMemo.table_name} WHERE osp_id=#{osp_id} AND parent_id IS NULL)," +
" memo_count = (SELECT COUNT(*) FROM #{RelativeMemo.table_name} WHERE osp_id=#{osp_id} AND parent_id IS NOT NULL)," +
" last_memo_id = (SELECT MAX(id) FROM #{RelativeMemo.table_name} WHERE osp_id=#{osp_id})",
["id = ?", osp_id])
end
2014-03-29 10:57:36 +08:00
end