Sha256: a2e59f298e71953f26592470e87326cc2afa47fd835c899b6f0e36515677458b

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

module Ecm
  module Downloads
    class DownloadCategory < ActiveRecord::Base
      # associations
      has_many :downloads, -> { order(:position) },
               dependent: :destroy,
               foreign_key: :download_category_id,
               inverse_of: :download_category

      accepts_nested_attributes_for :downloads,
                                    allow_destroy: true

      # awesome nested set
      acts_as_nested_set
      default_scope { order('lft ASC') }

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

      # validations
      validates :name, presence: true,
                       uniqueness: { scope: [:parent_id] }

      validates :locale, presence: true,
                         if: :root?

      validates :locale, inclusion: I18n.available_locales.map(&:to_s),
                         if: proc { |cc| cc.locale.present? }

      validates :locale, absence: true,
                         unless: :root?

      def downloads_count
        downloads.count
      end

      def human
        name
      end

      def to_s
        name
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ecm_downloads2-5.0.4 app/models/ecm/downloads/download_category.rb
ecm_downloads2-5.0.2 app/models/ecm/downloads/download_category.rb
ecm_downloads2-5.0.1 app/models/ecm/downloads/download_category.rb