From a7791635aa98f012d35f97c3c5288494bbb28e38 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Wed, 13 Mar 2024 18:03:43 +0100 Subject: Initial commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miquel Sabaté Solà --- app/models/thing.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 app/models/thing.rb (limited to 'app/models/thing.rb') diff --git a/app/models/thing.rb b/app/models/thing.rb new file mode 100644 index 0000000..5c758fb --- /dev/null +++ b/app/models/thing.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +class Thing < ApplicationRecord + validates :title, presence: true, uniqueness: true + validates :target, presence: true, uniqueness: true + validates :authors, presence: true + validates :rate, numericality: { in: 0..10 } + + belongs_to :user + + has_many :comments, dependent: :destroy + has_many :tag_references, as: :taggable, dependent: :destroy + has_many :tags, through: :tag_references + + enum :status, %i[read notread tobepublished], validate: true + enum :kind, %i[other poetry theater essay shorts novel paper], validate: true + + # Returns all things which match the given text for any of the string columns + # from the table. + def self.like(text:) + Thing.where('target LIKE ?', "%#{text}%") + .or(where('title LIKE ?', "%#{text}%")) + .or(where('publisher LIKE ?', "%#{text}%")) + .or(where('address LIKE ?', "%#{text}%")) + .or(where('url LIKE ?', "%#{text}%")) + .or(where('location LIKE ?', "%#{text}%")) + .or(where('insideof LIKE ?', "%#{text}%")) + .or(where('pages LIKE ?', "%#{text}%")) + .or(where('note LIKE ?', "%#{text}%")) + .or(where('authors LIKE ?', "%#{text}%")) + end +end -- cgit v1.2.3