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/library.rb

61 lines
1.6 KiB

class Library < ActiveRecord::Base
include AASM
belongs_to :user
5 years ago
belongs_to :cover, class_name: 'Attachment', foreign_key: :cover_id
has_many :library_applies, dependent: :delete_all
has_many :attachments, as: :container
5 years ago
has_many :library_library_tags, dependent: :delete_all
has_many :library_tags, through: :library_library_tags
5 years ago
has_one :praise_tread_cache, as: :object
5 years ago
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
5 years ago
validates :author_name, presence: true, length: { maximum: 10 }
5 years ago
validates :author_school_name, presence: true, length: { maximum: 50 }
5 years ago
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
5 years ago
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