You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pgfqe6ch8/app/models/project_package.rb

102 lines
2.7 KiB

class ProjectPackage < ActiveRecord::Base
include AASM
acts_as_attachable
attr_accessible :title, :content, :project_package_category_id, :deadline_at, :contact_name, :contact_phone, :min_price, :max_price
belongs_to :creator, class_name: 'User'
belongs_to :project_package_category
has_many :project_package_applies, dependent: :destroy
has_one :process_project_package_apply, conditions: { status: :pending }, class_name: 'ProjectPackageApply'
has_many :bidding_users, dependent: :delete_all
has_many :win_bidding_users, conditions: { status: :bidding_won }, class_name: 'BiddingUser'
has_many :lose_bidding_users, conditions: { status: :bidding_lost }, class_name: 'BiddingUser'
has_many :attachments, as: :container, dependent: :destroy
validates :title, presence: true, length: { maximum: 60 }
validates :content, presence: true
validates :deadline_at, presence: true
validates :contact_name, presence: true, length: { maximum: 20 }
validates :contact_phone, presence: true, format: { with: /1\d{10}/ }
validates :min_price, numericality: { greater_than: 0 }, allow_blank: true
validates :max_price, numericality: { greater_than: ->(obj){ obj.min_price.to_f } }, allow_blank: true
aasm(:status) do
state :pending, initiali: true
state :applying
state :refused
state :published
state :bidding_ended
state :bidding_finished
event :apply do
transitions from: [:pending, :refused], to: :applying
end
event :refuse do
transitions from: :applying, to: :refused
end
event :publish do
transitions from: :applying, to: :published
end
event :end_bidding do
transitions from: :published, to: :bidding_ended
end
event :finish_bidding do
transitions from: [:bidding_ended], to: :bidding_finished
end
end
def category
5 years ago
project_package_category.en_name
end
def category_name
5 years ago
project_package_category.name
end
5 years ago
def visitable?
!editable?
end
def editable?
pending? || applying? || refused?
end
5 years ago
def deletable?
pending? || refused?
end
def deadline?
deadline_at < Time.now
end
def bidding_end?
flag = deadline?
end_bidding! if flag && may_end_bidding?
flag
end
def can_bidding?(user)
published? && !bidding_end? && user.id != creator_id && !bidding_users.exists?(user_id: user.id)
end
def increment_visit_count!
ProjectPackage.connection.execute("update project_packages set visit_count = COALESCE(visit_count, 0) + 1 where id = #{id}")
end
5 years ago
def category_text
I18n.t("project_package.category.#{category_name}")
5 years ago
end
def status_text
I18n.t("project_package.status.#{status}")
end
end