class Dhatu::Service < Dhatu::ApplicationRecord # Set Table Name self.table_name = "services" # Including the State Machine Methods include Publishable # Constants COVER_IMAGE_INSTRUCTIONS = [ "the file extension should be .jpg / .jpeg or .png format", "the image should be in resolution 750 pixels by 368 pixels, (Portrait Format). (most cameras and camera phones will produce images bigger than this)", "the file size should not be less than 20 Kb, or bigger than 1 MB" ] # Constants GALLERY_IMAGE_INSTRUCTIONS = [ "the file extension should be .jpg / .jpeg or .png format", "the image should be in resolution 750 pixels by 368 pixels, (Portrait Format). (most cameras and camera phones will produce images bigger than this)", "the file size should not be less than 20 Kb, or bigger than 1 MB" ] # Validations validate_string :name, mandatory: true, min_length: 2, format: /.*/i validate_string :one_liner, mandatory: false, format: /.*/i validate_string :permalink, mandatory: true, format: /.*/i, min_length: 4, max_length: 128 validates :category, presence: true # Associations belongs_to :category has_one :cover_image, :as => :imageable, :dependent => :destroy, :class_name => "Image::CoverImage" has_many :gallery_images, :as => :imageable, :dependent => :destroy, :class_name => "Image::GalleryImage" # ------------------ # Class Methods # ------------------ scope :search, lambda {|query| where("LOWER(name) LIKE LOWER('%#{query}%') OR\ LOWER(one_liner) LIKE LOWER('%#{query}%') OR\ LOWER(permalink) LIKE LOWER('%#{query}%') OR\ LOWER(description) LIKE LOWER('%#{query}%')") } scope :filter_by_category, lambda { |c| where("category_id = ?", (c.is_a? Dhatu::Category ? c.id : c)) } scope :upcoming, lambda { where("created_at >= ?", Time.now) } scope :past, lambda { where("created_at < ?", Time.now) } # ------------------ # Instance variables # ------------------ # Generic Methods # --------------- def to_param "#{id}-#{name.parameterize[0..32]}" end def display_name "#{name_was}" end # Permission Methods # ------------------ def can_be_edited? status?(:published) or status?(:unpublished) end def can_be_deleted? status?(:removed) end end