class Library < ActiveRecord::Base include AASM belongs_to :user has_many :library_applies, dependent: :delete_all has_many :attachments, as: :container attr_accessible :title, :content validates :title, presence: true validates :content, presence: true validates :uuid, presence: true, uniqueness: true 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