Sha256: 1cd38804d97079b73f863b15920c4d327f5b96e8aeee506989ce9fe748a69f92

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

class Ecm::News::Item < ActiveRecord::Base
  # associations
  has_many :attached_pictures, as: :pictureable,
                               class_name: Ecm::Pictures::AttachedPicture,
                               dependent: :destroy
  has_many :pictures, through: :attached_pictures,
                      class_name: Ecm::Pictures::Picture

  accepts_nested_attributes_for :attached_pictures, allow_destroy: true
  accepts_nested_attributes_for :pictures, allow_destroy: true

  # acts as markup
  acts_as_markup language: :variable, columns: [:body]

  # acts as published
  include ActsAsPublished::ActiveRecord
  acts_as_published

  # attributes
  attr_accessible :attached_pictures_attributes,
                  :body,
                  :link_to_more,
                  :locale,
                  :markup_language,
                  :pictures_attributes,
                  :published,
                  :published_at,
                  :slug,
                  :title if respond_to?(:attr_accessible)

  # callbacks
  after_initialize :set_defaults

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

  # validations
  validates :title, presence: true, uniqueness: true
  validates :locale, presence: true,
                     inclusion: I18n.available_locales.map(&:to_s)
  validates :body, presence: true
  validates :markup_language, presence: true,
                              inclusion: Ecm::News::Configuration.markup_languages.map(&:to_s)

  def human
    title
  end

  def preview_picture
    pictures.first
  end

  def to_s
    human
  end

  private

  def set_defaults
    if self.new_record?
      self.markup_language ||= Ecm::News::Configuration.default_markup_language
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ecm_news2-2.0.10 app/models/ecm/news/item.rb
ecm_news2-2.0.9 app/models/ecm/news/item.rb
ecm_news2-2.0.8 app/models/ecm/news/item.rb