Sha256: 7e1ebe648464a520b143e50c1f65e65cef7c93b2b30b35909f4eed6ab96a88bd

Contents?: true

Size: 1.25 KB

Versions: 8

Compression:

Stored size: 1.25 KB

Contents

require "awesome_nested_set"
require "adva/has_permalink"

class Category < ActiveRecord::Base
  acts_as_nested_set :scope => :section_id

  include Adva::HasPermalink
  has_permalink :title, :url_attribute => :permalink, :sync_url => true, :only_when_blank => true, :scope => :section_id

  belongs_to :section, :foreign_key => 'section_id'
  has_many :categorizations, :dependent => :delete_all
  has_many :contents, :through => :categorizations, :source => :categorizable, :source_type => 'Content'

  before_save  :update_path
  after_create :update_paths

  validates_presence_of :section, :title
  validates_uniqueness_of :permalink, scope: :section_id, case_sensitive: true

  def owners
    owner.owners << owner
  end

  def owner
    section
  end

  def all_contents
    Content.by_category(self).order(published_at: :desc)
  end

  protected

    def update_path
      if permalink_changed?
        new_path = build_path
        unless self.path == new_path
          self.path = new_path
          @paths_dirty = true
        end
      end
    end

    def build_path
      self_and_ancestors.map(&:permalink).join('/')
    end

    def update_paths
      if parent_id
        move_to_child_of(parent)
        section.categories.update_paths!
      end
    end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
adva-0.3.2 app/models/category.rb
adva-0.3.1 app/models/category.rb
adva-0.3.0 app/models/category.rb
adva-0.2.4 app/models/category.rb
adva-0.2.3 app/models/category.rb
adva-0.2.2 app/models/category.rb
adva-0.2.1 app/models/category.rb
adva-0.2.0 app/models/category.rb