Sha256: e4ad70ca95ed58a4ce18af2e0256bdb7051c3a91bcd2f95a01bffc67ab9b24de

Contents?: true

Size: 1.83 KB

Versions: 2

Compression:

Stored size: 1.83 KB

Contents

class Kuhsaft::LocalizedPage < ActiveRecord::Base
  belongs_to :page
  has_many :page_parts, :class_name => 'Kuhsaft::PagePart::Content', :autosave => true
  scope :published, lambda{ where('published = ? OR published_at < ?', Kuhsaft::PublishState::PUBLISHED, DateTime.now) }
  scope :search, lambda{ |term| published.where('fulltext LIKE ?', "%#{term}%").where('locale = ?', Kuhsaft::Page.current_translation_locale) }
  
  before_validation :create_slug, :create_url, :collect_fulltext
  delegate :childs, :to => :page
  
  validates :title, :presence => true
  validates :locale, :presence => true
  validates :slug, :presence => true, :unless => :allow_empty_slug
  
  accepts_nested_attributes_for :page_parts, :allow_destroy => true
  
  def published?
    published > 0 || (published_at < DateTime.now if published_at.present?)
  end
  
  def locale
    read_attribute(:locale).to_sym unless read_attribute(:locale).nil?
  end
  
  def create_url
    return if self.page_type == Kuhsaft::PageType::REDIRECT
    complete_slug = ''
    if page.present? && page.parent.present?
      complete_slug << page.parent.url.to_s
    else
      complete_slug = "#{self.locale}"
    end
    complete_slug << "/#{self.slug}" unless self.page_type == Kuhsaft::PageType::NAVIGATION
    self.url = complete_slug
  end
  
  def create_slug
    has_slug = title.present? && slug.blank?
    write_attribute(:slug, read_attribute(:title).downcase.parameterize) if has_slug
  end
  
  def collect_fulltext
    self.fulltext = page_parts.inject('') do |text, page_part|
      page_part.class.searchable_attributes.each do |attr|
        text << ' '
        text << page_part.send(attr).to_s
      end
      text
    end
    self.fulltext << [title.to_s, keywords.to_s, description.to_s].join(' ')
  end
  
  def allow_empty_slug
    self.page_type == Kuhsaft::PageType::NAVIGATION
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kuhsaft-0.1.1 app/models/kuhsaft/localized_page.rb
kuhsaft-0.1.0 app/models/kuhsaft/localized_page.rb