2014-03-29 10:57:36 +08:00
class OpenSourceProject < ActiveRecord :: Base
2014-04-19 11:03:54 +08:00
attr_accessible :name
2014-04-22 08:21:35 +08:00
2014-04-03 14:41:04 +08:00
include Redmine :: SafeAttributes
2014-04-06 16:34:23 +08:00
has_many :applies , :class_name = > " ApplyProjectMaster " , :as = > :apply , :dependent = > :delete_all
2014-05-20 10:06:59 +08:00
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
2014-04-01 08:51:43 +08:00
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 "
2014-04-22 08:21:35 +08:00
2014-04-19 11:03:54 +08:00
validates_uniqueness_of :name
2014-04-22 08:21:35 +08:00
acts_as_taggable
2014-04-06 16:34:23 +08:00
scope :applied_by , lambda { | user_id |
{ :include = > :apply_project_master ,
2014-04-22 08:21:35 +08:00
: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-22 08:21:35 +08:00
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-22 08:21:35 +08:00
2014-04-08 09:02:12 +08:00
scope :filter_app_dir , lambda { | args |
nil
}
2014-04-22 08:21:35 +08:00
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-22 08:21:35 +08:00
2014-04-08 09:02:12 +08:00
scope :filter_time , lambda { | args |
2014-04-19 11:03:54 +08:00
where ( " YEAR( #{ OpenSourceProject . table_name } .created_at) = ? " , args ) unless args . nil?
2014-04-08 09:02:12 +08:00
}
2014-04-22 08:21:35 +08:00
2014-04-08 09:02:12 +08:00
# def filter_app_dir(app_dir)
2014-04-22 08:21:35 +08:00
# nil
2014-04-08 09:02:12 +08:00
# end
2014-04-22 08:21:35 +08:00
#
2014-04-08 09:02:12 +08:00
# def self.filter_app_dir(app_dir)
2014-04-22 08:21:35 +08:00
# nil
2014-04-08 09:02:12 +08:00
# end
2014-04-22 08:21:35 +08:00
#
2014-04-08 09:02:12 +08:00
# def filter_language(language)
2014-04-22 08:21:35 +08:00
# nil
2014-04-08 09:02:12 +08:00
# end
2014-04-22 08:21:35 +08:00
#
2014-04-08 09:02:12 +08:00
# def self.filter_language(language)
2014-04-22 08:21:35 +08:00
# nil
2014-04-08 09:02:12 +08:00
# end
2014-04-22 08:21:35 +08:00
#
2014-04-08 09:02:12 +08:00
# def filter_time(created_at)
2014-04-22 08:21:35 +08:00
# nil
2014-04-08 09:02:12 +08:00
# end
2014-04-22 08:21:35 +08:00
#
2014-04-08 09:02:12 +08:00
# def self.filter_time(created_at)
2014-04-22 08:21:35 +08:00
# nil
2014-04-08 09:02:12 +08:00
# end
2014-04-22 08:21:35 +08:00
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
2014-07-18 09:21:09 +08:00
#description.gsub(/<\/?.*?>/,"").html_safe if description
# 不再使用短描述
2014-07-18 16:41:55 +08:00
# description
2014-03-29 10:57:36 +08:00
end
2014-04-22 08:21:35 +08:00
2014-04-03 20:47:09 +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
2014-04-22 08:21:35 +08:00
return true
end
2014-04-06 22:12:16 +08:00
end
2014-04-03 20:47:09 +08:00
false
end
2014-04-22 08:21:35 +08:00
2014-04-03 20:47:09 +08:00
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
2014-04-22 08:21:35 +08:00
return true
end
2014-04-06 22:12:16 +08:00
end
2014-04-03 20:47:09 +08:00
false
end
2014-04-22 08:21:35 +08:00
2014-04-06 16:34:23 +08:00
def set_apply ( user , flag = true )
flag ? set_filter ( user ) : remove_filter ( user )
end
2014-04-22 08:21:35 +08:00
2014-04-06 16:34:23 +08:00
def set_filter ( user )
self . applies << ApplyProjectMaster . new ( :user = > user , :status = > 1 )
end
2014-04-22 08:21:35 +08:00
2014-04-06 16:34:23 +08:00
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-22 08:21:35 +08:00
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?
2014-04-22 08:21:35 +08:00
return true
else
return false
2014-04-06 22:12:16 +08:00
end
end
2014-04-22 08:21:35 +08:00
2014-04-03 14:41:04 +08:00
def reset_counters!
self . class . reset_counters! ( id )
end
2014-04-22 08:21:35 +08:00
2014-04-03 14:41:04 +08:00
def self . reset_counters! ( id )
osp_id = id . to_i
2014-05-20 10:06:59 +08:00
# 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 ] )
2014-04-22 08:21:35 +08:00
end
2014-03-29 10:57:36 +08:00
end