Sha256: 827a651a4dc76e8ea0f7e0a6b5aedba0191586d4bf4df9eb71fc5bf7cbc3d5a9

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

# == Schema Information
#
# Table name: recurrent_discounts
#
#  id          :integer          not null, primary key
#  title       :text
#  description :text
#  url         :string(255)
#  interval    :integer
#  is_visible  :boolean
#  created_at  :datetime
#  updated_at  :datetime
#

class RecurrentDiscount < ActiveRecord::Base
  scope :visible, -> { where(is_visible: true) }

  validates :title, :url, :interval, presence: true
  validates :interval, numericality: { greater_than: 0 }

  has_one :picture, as: :assetable, dependent: :destroy
  fileuploads :picture

  def key
    @key ||= [
      Settings.satellite_name.gsub(/[^a-z0-9]/i, '_').downcase,
      'banner',
      id,
      updated_at.to_i.to_s(16)
    ].join('_')
  end

  def expires
    Time.zone.now + interval.hours
  end

  def self.random_item(cookies = {})
    rand = visible.select { |i| cookies[i.key].blank? }.sample
    if rand.present?
      cookies[rand.key] = {
        value: true,
        expires: rand.expires
      }
    end
    rand
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
translation_cms-0.1.5 app/models/recurrent_discount.rb