Sha256: 8cb053511ad82d01badfc1c163252967e2e9e1f12588a22e57586aca05b80367

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

class Ecm::Downloads::DownloadCategory < ActiveRecord::Base
  # db settings
  self.table_name = 'ecm_downloads_download_categories'

  # associations
  has_many :ecm_downloads_downloads, -> { order(:position) },
           :class_name => Ecm::Downloads::Download,
           :dependent => :destroy,
           :foreign_key => :ecm_downloads_download_category_id,
           inverse_of: :ecm_downloads_download_category

  accepts_nested_attributes_for :ecm_downloads_downloads,
                                :allow_destroy => true

  # attributes
  attr_accessible :description,
                  :ecm_downloads_downloads_attributes,
                  :ecm_downloads_downloads_count,
                  :locale,
                  :name,
                  :parent_id,
                  :position,
                  :slug if respond_to? :attr_accessible

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

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

  # 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.new { |cc| cc.locale.present? }

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

  # public methods

  # public methods

  def display_code
    "<%= render_download_category '#{self.name}' %>"
  end

  def human
    name
  end

  def index_name
    if self.root?
      "[#{self.locale}] #{self.name}"
    else
      "#{'&#160;&#160;&#160;&#160;' * self.level} |--&#160;#{self.name}".html_safe
    end
  end

  def to_s
    name
  end

  # def tree_name
  #   root_prefix = (self.root?) ? "[#{self.locale}] " : ""

  #   if ecm_downloads_downloads_count < 1
  #     "#{root_prefix}#{to_s}"
  #   else
  #     "#{root_prefix}#{to_s} (#{ecm_downloads_downloads_count})"
  #   end
  # end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ecm_downloads2-1.1.0 app/models/ecm/downloads/download_category.rb
ecm_downloads2-1.0.0 app/models/ecm/downloads/download_category.rb