|
|
|
class Library < ActiveRecord::Base
|
|
|
|
include AASM
|
|
|
|
|
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
attr_accessible :title, :content
|
|
|
|
|
|
|
|
validates :title, presence: true
|
|
|
|
validates :content, presence: true
|
|
|
|
validates :uuid, presence: true, uniqueness: true
|
|
|
|
|
|
|
|
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!
|
|
|
|
Library.connection.execute("update libraries set visited_count = COALESCE(visited_count, 0) + 1 where id = #{id}")
|
|
|
|
end
|
|
|
|
end
|