Sha256: 8b11ceeb13c64f6913b3b75fc6e59384bee8cc60c6ca369c863c53e953ebded7

Contents?: true

Size: 1.39 KB

Versions: 5

Compression:

Stored size: 1.39 KB

Contents

class Dhatu::Price < Dhatu::ApplicationRecord

  # Set Table Name
  self.table_name = "prices"

  # Including the State Machine Methods
  include Publishable
  include Featureable
  
  # Validations
  validate_string :title, mandatory: true, min_length: 2, format: /.*/i
  validate_string :sub_title, mandatory: false, format: /.*/i
  
  validates :price, presence: true
  validates :category, presence: true
  
  # Associations
  belongs_to :category
  has_one :cover_image, :as => :imageable, :dependent => :destroy, :class_name => "Image::CoverImage"
  
  # ------------------
  # Class Methods
  # ------------------

  scope :search, lambda {|query| where("LOWER(title) LIKE LOWER('%#{query}%') OR\
                                        LOWER(sub_title) LIKE LOWER('%#{query}%') OR")
  }
  
  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}-#{title.parameterize[0..32]}"
  end

  def display_name
    "#{title_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/price.rb
dhatu-0.1.9 app/models/dhatu/price.rb
dhatu-0.1.8 app/models/dhatu/price.rb
dhatu-0.1.7 app/models/dhatu/price.rb
dhatu-0.1.6 app/models/dhatu/price.rb