Sha256: 465419ec4fb6a202103572a77b95b99d3a6f70595f1ea934316df4183801c848

Contents?: true

Size: 1.84 KB

Versions: 5

Compression:

Stored size: 1.84 KB

Contents

class Dhatu::Promotion < Dhatu::ApplicationRecord

  # Set Table Name
  self.table_name = "promotions"

  # Including the State Machine Methods
  include Publishable
  include Featureable

  # Validations
  validates :title, presence: true, length: {minimum: 5, maximum: 256}, allow_blank: false
  validates :code, length: {minimum: 5, maximum: 64}, allow_blank: true
  validates :short_description, length: {minimum: 3, maximum: 512}, allow_blank: true
  
  # Associations
  has_many :promotion_attributes
  has_one :cover_image, :as => :imageable, :dependent => :destroy, :class_name => "Image::CoverImage"
  
  # Callbacks
  after_create :generate_and_save_code

  # ------------------
  # Class Methods
  # ------------------

  scope :search, lambda { |query| where("LOWER(title) LIKE LOWER('%#{query}%') OR\
                                        LOWER(code) LIKE LOWER('%#{query}%') OR\
                                        LOWER(short_description) LIKE LOWER('%#{query}%')")}

  scope :upcoming, lambda { where("starts_at >= ?", Time.now) }
  scope :past, lambda { where("starts_at < ?", Time.now) }
  
  # ------------------
  # Instance variables
  # ------------------

  # Generic Methods
  # ---------------
  def to_param
    "#{id}-#{title.parameterize[0..32]}"
  end

  def display_name
    "#{title_was}"
  end

  def promotion_attributes_with_values
    hsh = {}
    self.promotion_attributes.each do |pa|
      hsh[pa.name] = { values: pa.values, selected: "selected", data_type: pa.data_type, mandatory: pa.mandatory}
    end
    hsh
  end

  # Permission Methods
  # ------------------

  def can_be_edited?
    status?(:published) or status?(:unpublished)
  end

  def can_be_deleted?
    status?(:removed)
  end

  # Callback Methods
  # -----------------

  def generate_and_save_code
    self.update_attribute(:code, SecureRandom.hex(4))
  end
  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dhatu-0.1.16 app/models/dhatu/promotion.rb
dhatu-0.1.15 app/models/dhatu/promotion.rb
dhatu-0.1.14 app/models/dhatu/promotion.rb
dhatu-0.1.13 app/models/dhatu/promotion.rb
dhatu-0.1.12 app/models/dhatu/promotion.rb