Sha256: 4d1625f2623ceff79d9207aa347bcbcdb2f7a5bba2a76aa498f4cd9e42ea9ec7

Contents?: true

Size: 1.75 KB

Versions: 22

Compression:

Stored size: 1.75 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

  validates_presence_of :title
  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

  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

#  def title
#    owner.try('title') ? owner.title : read_attribute(:title)
#  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
sbdevcore-0.3.10 app/models/index.rb
sbdevcore-0.3.9 app/models/index.rb
sbdevcore-0.3.8 app/models/index.rb
sbdevcore-0.3.7 app/models/index.rb
sbdevcore-0.3.6 app/models/index.rb
sbdevcore-0.3.5 app/models/index.rb
sbdevcore-0.3.4 app/models/index.rb
sbdevcore-0.3.3 app/models/index.rb
sbdevcore-0.3.2 app/models/index.rb
sbdevcore-0.3.1 app/models/index.rb
sbdevcore-0.3.0 app/models/index.rb
sbdevcore-0.2.26 app/models/index.rb
sbdevcore-0.2.25 app/models/index.rb
sbdevcore-0.2.24 app/models/index.rb
sbdevcore-0.2.23 app/models/index.rb
sbdevcore-0.2.22 app/models/index.rb
sbdevcore-0.2.21 app/models/index.rb
sbdevcore-0.2.20 app/models/index.rb
sbdevcore-0.2.19 app/models/index.rb
sbdevcore-0.2.18 app/models/index.rb