bigdir/app/models/contesting_project.rb

53 lines
1.2 KiB
Ruby
Raw Permalink Normal View History

2014-04-03 22:38:18 +08:00
class ContestingProject < ActiveRecord::Base
attr_accessible :contest_id, :description, :project_id, :user_id, :reward
belongs_to :contest
belongs_to :project
belongs_to :user
DESCRIPTION_LENGTH_LIMIT = 500
validates_length_of :description, :maximum => DESCRIPTION_LENGTH_LIMIT
validates_presence_of :user_id, :contest_id, :project_id
validate :validate_user
validate :validate_contest
validate :validate_project
validates_uniqueness_of :contest_id, :scope => :project_id
def self.cerate_contesting(contest_id, project_id, description = nil)
self.create(:user_id => User.current.id, :contest_id => contest_id,
:project_id => project_id, :description => description)
end
2014-04-11 17:16:22 +08:00
2014-04-03 22:38:18 +08:00
def update_reward(which)
self.update_attribute(:reward,which)
end
def get_reward
self.reward
end
def cancel_contesting
unless self.nil?
if User.current.id == self.user_id
self.destroy
end
end
end
private
def validate_user
errors.add :user_id, :invalid if user.nil? || !user.active?
end
def validate_contest
errors.add :contest_id, :invalid if contest.nil?
end
def validate_project
errors.add :project_id, :invalid if project.nil?
end
end