2014-03-29 10:57:36 +08:00
class OpenSourceProject < ActiveRecord :: Base
attr_accessible :String
2014-04-01 08:51:43 +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-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
2014-04-06 16:34:23 +08:00
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-04-06 16:34:23 +08:00
2014-04-01 08:51:43 +08:00
acts_as_taggable
2014-04-06 16:34:23 +08:00
scope :applied_by , lambda { | user_id |
{ :include = > :apply_project_master ,
:conditions = > [ " #{ ApplyProjectMaster . table_name } .user_id = ? " , user_id ] }
}
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
def self . filter ( app_dir , language , created_at )
self . filter_app_dir ( app_dir ) . filter_language ( language ) . filter_time ( created_at )
end
scope :filter_app_dir , lambda { | args |
nil
}
scope :filter_language , lambda { | * arg |
if arg [ 0 ] . nil?
where ( nil )
else
tagged_with ( arg ) . order ( 'updated_at desc' )
end
}
scope :filter_time , lambda { | args |
nil
}
# def filter_app_dir(app_dir)
# nil
# end
#
# def self.filter_app_dir(app_dir)
# nil
# end
#
# def filter_language(language)
# nil
# end
#
# def self.filter_language(language)
# nil
# end
#
# def filter_time(created_at)
# nil
# end
#
# def self.filter_time(created_at)
# nil
# end
2014-04-06 16:34:23 +08:00
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
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
return true
end
end
2014-04-03 20:47:09 +08:00
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
2014-04-03 20:47:09 +08:00
false
end
2014-04-06 16:34:23 +08:00
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