Sha256: 5b4a1d7872c6d418096b7f287ed62abb05a74a5ce04498035e92eea93baf19f0

Contents?: true

Size: 1.72 KB

Versions: 5

Compression:

Stored size: 1.72 KB

Contents

class Dhatu::Service < Dhatu::ApplicationRecord

  # Set Table Name
  self.table_name = "services"

  # Including the State Machine Methods
  include Publishable
  include Featureable

  # 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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dhatu-0.1.10 app/models/dhatu/service.rb
dhatu-0.1.9 app/models/dhatu/service.rb
dhatu-0.1.8 app/models/dhatu/service.rb
dhatu-0.1.7 app/models/dhatu/service.rb
dhatu-0.1.6 app/models/dhatu/service.rb