2013-08-01 10:33:49 +08:00
|
|
|
####by fq
|
|
|
|
class Bid < ActiveRecord::Base
|
2013-09-13 21:52:24 +08:00
|
|
|
attr_accessible :author_id, :budget, :deadline, :name, :description, :homework_type
|
2013-08-20 16:32:45 +08:00
|
|
|
include Redmine::SafeAttributes
|
2013-08-05 22:47:01 +08:00
|
|
|
|
2013-08-01 10:33:49 +08:00
|
|
|
belongs_to :author, :class_name => 'User', :foreign_key => :author_id
|
2013-09-06 16:39:38 +08:00
|
|
|
belongs_to :course
|
2013-08-01 10:33:49 +08:00
|
|
|
has_many :biding_projects, :dependent => :destroy
|
|
|
|
has_many :projects, :through => :biding_projects
|
|
|
|
has_many :journals_for_messages, :as => :jour, :dependent => :destroy
|
2013-08-12 15:37:42 +08:00
|
|
|
has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy
|
2013-09-12 10:41:15 +08:00
|
|
|
has_many :homework_for_courses, :dependent => :destroy
|
|
|
|
has_many :courses, :through => :homework_for_courses, :source => :project
|
2013-09-13 21:52:24 +08:00
|
|
|
has_many :homeworks, :class_name => 'HomeworkAttach', :dependent => :destroy
|
|
|
|
|
|
|
|
acts_as_attachable
|
2013-08-05 22:47:01 +08:00
|
|
|
|
2013-08-01 10:33:49 +08:00
|
|
|
NAME_LENGTH_LIMIT = 60
|
|
|
|
DESCRIPTION_LENGTH_LIMIT = 250
|
2013-08-05 22:47:01 +08:00
|
|
|
|
2013-08-01 10:33:49 +08:00
|
|
|
validates_length_of :name, :maximum => NAME_LENGTH_LIMIT
|
|
|
|
validates_length_of :description, :maximum => DESCRIPTION_LENGTH_LIMIT
|
2013-09-12 10:41:15 +08:00
|
|
|
validates_presence_of :author_id, :name, :deadline
|
2013-08-20 16:32:45 +08:00
|
|
|
# validates_presence_of :deadline, :message => 'test'
|
2013-08-05 22:47:01 +08:00
|
|
|
# validates_format_of :deadline, :with =>
|
2013-08-20 16:32:45 +08:00
|
|
|
validates_format_of :deadline, :with => /^[\d]{4}[-][\d]{1,2}[-][\d]{1,2}$/
|
|
|
|
validates_format_of :budget, :with => /^(\d+)$|^(\d+).([0-9]{2})|^(\d+).([0-9]{1})$/, :if => Proc.new { |p| p.reward_type == 1 }
|
2013-08-20 22:05:11 +08:00
|
|
|
validates_format_of :budget, :with => /^(\d+)$|^(\d+).([0-9]{1})$/, :if => Proc.new { |p| p.reward_type == 3 }
|
2013-08-01 10:33:49 +08:00
|
|
|
validate :validate_user
|
2013-08-20 16:32:45 +08:00
|
|
|
validate :validate_reward_type
|
2013-08-12 17:30:31 +08:00
|
|
|
after_create :act_as_activity
|
2013-08-11 10:36:55 +08:00
|
|
|
|
2013-08-05 22:47:01 +08:00
|
|
|
scope :visible, lambda {|*args|
|
|
|
|
nil
|
|
|
|
}
|
|
|
|
|
|
|
|
scope :like, lambda {|arg|
|
|
|
|
if arg.blank?
|
|
|
|
where(nil)
|
|
|
|
else
|
|
|
|
pattern = "%#{arg.to_s.strip.downcase}%"
|
2013-08-06 21:43:06 +08:00
|
|
|
where("LOWER(id) LIKE :p OR LOWER(name) LIKE :p OR LOWER(description) LIKE :p", :p => pattern)
|
2013-08-05 22:47:01 +08:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2013-08-01 10:33:49 +08:00
|
|
|
acts_as_watchable
|
|
|
|
acts_as_taggable
|
|
|
|
|
2013-08-05 22:47:01 +08:00
|
|
|
acts_as_event :title => Proc.new {|o| "#{l(:label_requirement)} ##{o.id}: #{o.name}" },
|
|
|
|
:description => :description,
|
|
|
|
:author => :author,
|
|
|
|
:url => Proc.new {|o| {:controller => 'bids', :action => 'show', :id => o.id}}
|
|
|
|
|
2013-09-15 09:33:51 +08:00
|
|
|
acts_as_activity_provider :find_options => {:include => [:projects, :author]},
|
2013-08-05 22:47:01 +08:00
|
|
|
:author_key => :author_id
|
2013-08-20 16:32:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
safe_attributes 'name',
|
|
|
|
'description',
|
|
|
|
'budget',
|
|
|
|
'deadline'
|
2013-09-12 10:41:15 +08:00
|
|
|
|
|
|
|
# safe_attributes 'name',
|
|
|
|
# 'description',
|
|
|
|
# 'deadline'
|
2013-08-05 22:47:01 +08:00
|
|
|
|
2013-08-06 22:26:52 +08:00
|
|
|
def add_jour(user, notes, reference_user_id = 0)
|
|
|
|
self.journals_for_messages << JournalsForMessage.new(:user_id => user.id, :notes => notes, :reply_id => reference_user_id)
|
2013-08-01 10:33:49 +08:00
|
|
|
end
|
2013-08-05 22:47:01 +08:00
|
|
|
|
2013-08-20 16:32:45 +08:00
|
|
|
def self.creat_bids(budget, deadline, name, description=nil, reward_type)
|
2013-08-01 10:33:49 +08:00
|
|
|
self.create(:author_id => User.current.id, :budget => budget,
|
2013-08-20 16:32:45 +08:00
|
|
|
:deadline => deadline, :name => name, :description => description, :commit => 0, :reward_type => reward_type)
|
2013-08-12 17:30:31 +08:00
|
|
|
# self.acts << Activity.new(:user_id => self.author_id)
|
2013-08-01 10:33:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_bids(budget, deadline, name, description=nil)
|
|
|
|
if(User.current.id == self.author_id)
|
2013-08-05 22:47:01 +08:00
|
|
|
self.name = name
|
|
|
|
self.budget = budget
|
|
|
|
self.deadline = deadline
|
|
|
|
self.description = description
|
|
|
|
self.save
|
|
|
|
end
|
2013-08-01 10:33:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_bids
|
|
|
|
unless self.nil?
|
|
|
|
if User.current.id == self.author_id
|
2013-08-05 22:47:01 +08:00
|
|
|
self.destroy
|
|
|
|
end
|
|
|
|
end
|
2013-08-01 10:33:49 +08:00
|
|
|
end
|
2013-08-05 22:47:01 +08:00
|
|
|
|
2013-08-01 10:33:49 +08:00
|
|
|
def set_commit(commit)
|
2013-08-05 22:47:01 +08:00
|
|
|
self.update_attribute(:commit, commit)
|
2013-08-01 10:33:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2013-08-05 22:47:01 +08:00
|
|
|
|
2013-08-01 10:33:49 +08:00
|
|
|
def validate_user
|
|
|
|
errors.add :author_id, :invalid if author.nil? || !author.active?
|
|
|
|
end
|
2013-08-11 10:36:55 +08:00
|
|
|
|
2013-08-20 16:32:45 +08:00
|
|
|
def validate_reward_type
|
|
|
|
errors.add :reward_type, :invalid if self.reward_type == 0
|
|
|
|
end
|
|
|
|
|
2013-08-12 15:37:42 +08:00
|
|
|
def act_as_activity
|
|
|
|
self.acts << Activity.new(:user_id => self.author_id)
|
|
|
|
end
|
2013-08-01 10:33:49 +08:00
|
|
|
end
|