socialforge/app/models/open_source_project.rb

79 lines
2.9 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 :applies, :class_name => "ApplyProjectMaster", :as => :apply, :dependent => :delete_all
2014-04-03 14:41:04 +08:00
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, :class_name => 'RelativeMemo', :foreign_key => 'osp_id', :dependent => :destroy
has_many :tags, :through => :project_tags, :class_name => 'Tag'
has_many :project_tags, :class_name => 'ProjectTags'
2014-04-06 22:12:16 +08:00
has_many :masters, :class_name => 'ApplyProjectMaster', :as => :apply, :dependent => :delete_all, :conditions => "#{ApplyProjectMaster.table_name}.status = 2"
has_many :admin, :through => :masters, :class_name => 'User'
has_many :apply_tips, :class_name => 'ApplyProjectMaster', :as => :apply, :dependent => :delete_all, :conditions => "#{ApplyProjectMaster.table_name}.status = 1"
has_many :applicants, :class_name => 'User', :through => :apply_tips, :source => :user
acts_as_taggable
scope :applied_by, lambda { |user_id|
{ :include => :apply_project_master,
:conditions => ["#{ApplyProjectMaster.table_name}.user_id = ?", user_id] }
}
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 applied_by?(user)
2014-04-06 22:12:16 +08:00
self.applies.each do |apply|
if apply.user_id == user.id
return true
end
end
false
end
def allowed?(user)
2014-04-06 22:12:16 +08:00
self.applies.each do |apply|
if apply.user_id == user.id and apply.status == 2
return true
end
end
false
end
def set_apply(user, flag=true)
flag ? set_filter(user) : remove_filter(user)
end
def set_filter(user)
self.applies << ApplyProjectMaster.new(:user => user, :status => 1)
end
def remove_filter(user)
return nil unless user && user.is_a?(User)
ApplyProjectMaster.delete_all "apply_type = '#{self.class}' AND apply_id = #{self.id} AND user_id = #{user.id}"
end
2014-04-06 22:12:16 +08:00
def admin?(user)
if user.admin? or ApplyProjectMaster.find(:all, :conditions => ["user_id = ? and apply_type = 'OpenSourceProject' and apply_id = ? and status = ?", user.id, self.id, 2]).present?
return true
else
return false
end
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