socialforge/app/models/open_source_project.rb

149 lines
5.0 KiB
Ruby
Raw Normal View History

2014-03-29 10:57:36 +08:00
class OpenSourceProject < ActiveRecord::Base
attr_accessible :name
2014-04-03 14:41:04 +08:00
include Redmine::SafeAttributes
has_many :applies, :class_name => "ApplyProjectMaster", :as => :apply, :dependent => :delete_all
has_many :relation_topics, :class_name => 'RelativeMemoToOpenSourceProject', :foreign_key => 'osp_id', :order => "#{RelativeMemo.table_name}.created_at DESC", :dependent => :destroy
# 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 :topics, :through => :relation_topics, :class_name => 'RelativeMemo'
# 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
2014-05-22 17:04:45 +08:00
has_many :bugs_to_osp, :class_name => 'BugToOsp', :foreign_key => 'osp_id', :dependent => :destroy
has_many :bugs, :through => :bugs_to_osp, :class_name => "RelativeMemo", :order => "#{RelativeMemo.table_name}.created_at DESC"
validates_uniqueness_of :name
acts_as_taggable
scope :applied_by, lambda { |user_id|
{ :include => :apply_project_master,
:conditions => ["#{ApplyProjectMaster.table_name}.user_id = ?", user_id]
}
}
scope :like, lambda {|arg|
if arg.blank?
where(nil)
else
pattern = "%#{arg.to_s.strip.downcase}%"
where("LOWER(name) LIKE :p OR LOWER(description) LIKE :p ", :p => pattern)
end
}
2014-04-08 09:02:12 +08:00
def filter(app_dir, language, created_at)
filter_app_dir(app_dir).filter_language(language).filter_time(created_at)
end
2014-04-08 09:02:12 +08:00
def self.filter(app_dir, language, created_at)
self.filter_app_dir(app_dir).filter_language(language).filter_time(created_at)
end
2014-04-08 09:02:12 +08:00
scope :filter_app_dir, lambda {|args|
nil
}
2014-04-08 09:02:12 +08:00
scope :filter_language, lambda {|*arg|
if arg[0].nil?
where(nil)
else
tagged_with(arg).order('updated_at desc')
end
}
2014-04-08 09:02:12 +08:00
scope :filter_time, lambda {|args|
where("YEAR(#{OpenSourceProject.table_name}.created_at) = ?", args) unless args.nil?
2014-04-08 09:02:12 +08:00
}
2014-04-08 09:02:12 +08:00
# def filter_app_dir(app_dir)
# nil
2014-04-08 09:02:12 +08:00
# end
#
2014-04-08 09:02:12 +08:00
# def self.filter_app_dir(app_dir)
# nil
2014-04-08 09:02:12 +08:00
# end
#
2014-04-08 09:02:12 +08:00
# def filter_language(language)
# nil
2014-04-08 09:02:12 +08:00
# end
#
2014-04-08 09:02:12 +08:00
# def self.filter_language(language)
# nil
2014-04-08 09:02:12 +08:00
# end
#
2014-04-08 09:02:12 +08:00
# def filter_time(created_at)
# nil
2014-04-08 09:02:12 +08:00
# end
#
2014-04-08 09:02:12 +08:00
# def self.filter_time(created_at)
# nil
2014-04-08 09:02:12 +08:00
# end
2014-03-29 10:57:36 +08:00
def short_description(length = 255)
2014-07-18 16:41:55 +08:00
description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description
#description.gsub(/<\/?.*?>/,"").html_safe if description
# 不再使用短描述
2014-07-18 16:41:55 +08:00
# description
2014-03-29 10:57:36 +08:00
end
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
2014-04-06 22:12:16 +08:00
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
2014-04-06 22:12:16 +08:00
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
2014-04-06 22:12:16 +08:00
end
end
2014-04-03 14:41:04 +08:00
def reset_counters!
self.class.reset_counters!(id)
end
2014-04-03 14:41:04 +08:00
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])
update_all("topic_count = (SELECT COUNT(*) FROM #{RelativeMemoToOpenSourceProject.table_name} WHERE osp_id=#{osp_id})",
# +
# " 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})",
2014-04-03 14:41:04 +08:00
["id = ?", osp_id])
end
2014-03-29 10:57:36 +08:00
end