Sha256: 2ff6e5cbe008fd1629eb047b1b629ddb7ab3ef11a860eaf0e15510a230bff957

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

class Index < ActiveRecord::Base
  attr_accessible :name, :title, :page_title, :keywords, :page_description, :texts_attributes, :photos_attributes, :videos_attributes, :downloadables_attributes, :gallery_attributes

  delegate :asset, :to => :"photos.first", :allow_nil => true
  alias_method :photo, :asset

  before_validation :default_name

  extend FriendlyId
  friendly_id :name, :use => :slugged

  include ActsAsPrioritizable
  acts_as_prioritizable("owner", "indices")
  default_scope :order => 'priority ASC'
  after_initialize do
    self.priority ||= lowest_priority + 1
  end

  has_many :indices, :as => :owner, :dependent => :destroy
  belongs_to :owner, :polymorphic => true

  has_one :gallery, :dependent => :destroy

  has_many :texts, :dependent => :destroy
  has_many :photos, :dependent => :destroy
  has_many :videos, :dependent => :destroy
  has_many :downloadables, :dependent => :destroy

  accepts_nested_attributes_for :videos, :reject_if => lambda { |a| a[:remote_id].blank? }, :allow_destroy => true
  accepts_nested_attributes_for :photos, :reject_if => lambda { |a| a[:asset].blank? }, :allow_destroy => true
  accepts_nested_attributes_for :downloadables, :reject_if => lambda { |a| a[:asset].blank? }, :allow_destroy => true
  accepts_nested_attributes_for :texts, :reject_if => lambda { |a| a[:content].blank?}, :allow_destroy => true

  accepts_nested_attributes_for :gallery, :allow_destroy => true

  validates_presence_of :title

  def assets
    (texts + photos + videos + downloadables).sort_by{|a| a.priority || 99}
  end

  def default_name
    self.name ||= (self.title ||= self.owner.name).tableize rescue nil
    self.title ||= self.name.titleize rescue nil
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sbdevcore-0.2.10 app/models/index.rb