102 lines
3.2 KiB
Ruby
102 lines
3.2 KiB
Ruby
####by fq
|
|
class Bid < ActiveRecord::Base
|
|
#attr_accessible :author_id, :budget, :deadline, :name, :description
|
|
include Redmine::SafeAttributes
|
|
|
|
belongs_to :author, :class_name => 'User', :foreign_key => :author_id
|
|
has_many :biding_projects, :dependent => :destroy
|
|
has_many :projects, :through => :biding_projects
|
|
has_many :journals_for_messages, :as => :jour, :dependent => :destroy
|
|
has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy
|
|
|
|
NAME_LENGTH_LIMIT = 60
|
|
DESCRIPTION_LENGTH_LIMIT = 250
|
|
|
|
validates_length_of :name, :maximum => NAME_LENGTH_LIMIT
|
|
validates_length_of :description, :maximum => DESCRIPTION_LENGTH_LIMIT
|
|
validates_presence_of :author_id, :name, :deadline, :budget
|
|
# validates_presence_of :deadline, :message => 'test'
|
|
# validates_format_of :deadline, :with =>
|
|
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 }
|
|
validate :validate_user
|
|
validate :validate_reward_type
|
|
after_create :act_as_activity
|
|
|
|
scope :visible, lambda {|*args|
|
|
nil
|
|
}
|
|
|
|
scope :like, lambda {|arg|
|
|
if arg.blank?
|
|
where(nil)
|
|
else
|
|
pattern = "%#{arg.to_s.strip.downcase}%"
|
|
where("LOWER(id) LIKE :p OR LOWER(name) LIKE :p OR LOWER(description) LIKE :p", :p => pattern)
|
|
end
|
|
}
|
|
|
|
acts_as_watchable
|
|
acts_as_taggable
|
|
|
|
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}}
|
|
|
|
acts_as_activity_provider :find_options => {:include => [:author]},
|
|
:author_key => :author_id
|
|
|
|
|
|
safe_attributes 'name',
|
|
'description',
|
|
'budget',
|
|
'deadline'
|
|
|
|
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)
|
|
end
|
|
|
|
def self.creat_bids(budget, deadline, name, description=nil, reward_type)
|
|
self.create(:author_id => User.current.id, :budget => budget,
|
|
:deadline => deadline, :name => name, :description => description, :commit => 0, :reward_type => reward_type)
|
|
# self.acts << Activity.new(:user_id => self.author_id)
|
|
end
|
|
|
|
def update_bids(budget, deadline, name, description=nil)
|
|
if(User.current.id == self.author_id)
|
|
self.name = name
|
|
self.budget = budget
|
|
self.deadline = deadline
|
|
self.description = description
|
|
self.save
|
|
end
|
|
end
|
|
|
|
def delete_bids
|
|
unless self.nil?
|
|
if User.current.id == self.author_id
|
|
self.destroy
|
|
end
|
|
end
|
|
end
|
|
|
|
def set_commit(commit)
|
|
self.update_attribute(:commit, commit)
|
|
end
|
|
|
|
private
|
|
|
|
def validate_user
|
|
errors.add :author_id, :invalid if author.nil? || !author.active?
|
|
end
|
|
|
|
def validate_reward_type
|
|
errors.add :reward_type, :invalid if self.reward_type == 0
|
|
end
|
|
|
|
def act_as_activity
|
|
self.acts << Activity.new(:user_id => self.author_id)
|
|
end
|
|
end
|