Sha256: 1976b8508aeaac011da06b9f90e86d2fb3858da8c9a9072266e805fd77247898

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

class Ecm::Sliders::Slider < ActiveRecord::Base
  # associations
  has_many :items, -> { order(:position) },
           dependent: :destroy,
           inverse_of: :slider

  accepts_nested_attributes_for :items, allow_destroy: true

  # scopes
  scope :for_locale, ->(locale) { where(self.arel_table[:locale].eq(locale).or(self.arel_table[:locale].eq(nil))).order(locale: :desc) }

  # callbacks
  after_initialize :set_defaults, if: :new_record?

  # friendly id
  extend FriendlyId
  friendly_id :name, use: :slugged

  # validations
  validates :locale, inclusion: I18n.available_locales.map(&:to_s),
                     if: proc { |slider| slider.locale.present? }
  validates :name, presence: true,
                   uniqueness: { scope: [:locale] }

  def interval_in_milliseconds
    (interval * 1000).to_i
  end

  def human
    "#{name} (#{locale})"
  end

  private

  def set_defaults
    self.auto_start = Ecm::Sliders::Configuration.slider_auto_start if auto_start.nil?
    self.interval ||= Ecm::Sliders::Configuration.slider_interval
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ecm_sliders2-0.0.4 app/models/ecm/sliders/slider.rb