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.
61 lines
1.6 KiB
61 lines
1.6 KiB
class Library < ActiveRecord::Base
|
|
include AASM
|
|
|
|
belongs_to :user
|
|
belongs_to :cover, class_name: 'Attachment', foreign_key: :cover_id
|
|
|
|
has_many :library_applies, dependent: :delete_all
|
|
has_many :attachments, as: :container
|
|
has_many :library_library_tags, dependent: :delete_all
|
|
has_many :library_tags, through: :library_library_tags
|
|
|
|
has_one :praise_tread_cache, as: :object
|
|
|
|
attr_accessible :title, :content, :author_name, :author_school_name, :cover_id
|
|
|
|
validates :title, presence: true, length: { maximum: 255 }
|
|
validates :content, presence: true
|
|
validates :uuid, presence: true, uniqueness: true
|
|
validates :author_name, presence: true, length: { maximum: 10 }
|
|
validates :author_school_name, presence: true, length: { maximum: 50 }
|
|
|
|
acts_as_attachable
|
|
|
|
aasm(:status) do
|
|
state :pending, initiali: true
|
|
state :processing
|
|
state :refused
|
|
state :published
|
|
|
|
event :submit do
|
|
transitions from: [:pending, :refused], to: :processing
|
|
end
|
|
|
|
event :refuse do
|
|
transitions from: :processing, to: :refused
|
|
end
|
|
|
|
event :publish do
|
|
transitions from: :processing, to: :published
|
|
end
|
|
end
|
|
|
|
def generate_uuid
|
|
uuid = Util.generate_time_uuid
|
|
while Library.exists?(uuid: uuid)
|
|
uuid = Util.generate_time_uuid
|
|
end
|
|
|
|
self.uuid = uuid
|
|
end
|
|
|
|
def increment_visited_count!(num = 1)
|
|
increment_column!(:visited_count, num)
|
|
end
|
|
|
|
private
|
|
|
|
def increment_column!(column, num = 1)
|
|
self.class.connection.execute("update #{self.class.table_name} set #{column} = COALESCE(#{column}, 0) + #{num} where id = #{id}")
|
|
end
|
|
end |