Sha256: 3003be4ae2ed2361afda01f40a86bef89e694641d3a7adc52056819a1c46fcdf

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

class Category < ActiveRecord::Base
  acts_as_list
  has_many :categorizations
  has_many :articles, :through => :categorizations,
    :order => "published_at DESC, created_at DESC"

  def self.find_all_with_article_counters(maxcount=nil)
    self.find_by_sql([%{ 
      SELECT categories.id, categories.name, categories.permalink, categories.position, COUNT(articles.id) AS article_counter 
      FROM #{Category.table_name} categories 
        LEFT OUTER JOIN #{Category.table_name_prefix}categorizations#{Category.table_name_suffix} articles_categories 
          ON articles_categories.category_id = categories.id 
        LEFT OUTER JOIN #{Article.table_name} articles 
          ON (articles_categories.article_id = articles.id AND articles.published = ?) 
      GROUP BY categories.id, categories.name, categories.position, categories.permalink 
      ORDER BY position 
      }, true]).each {|item| item.article_counter = item.article_counter.to_i }
  end

  def self.find(*args)
    with_scope :find => {:order => 'position ASC'} do
      super
    end
  end

  def self.find_by_permalink(*args)
    super || new
  end

  def self.to_prefix
    'category'
  end

  def stripped_name
    self.name.to_url
  end

  def self.reorder(serialized_list)
    self.transaction do
      serialized_list.each_with_index do |cid,index|
        find(cid).update_attribute "position", index
      end
    end
  end

  def self.reorder_alpha
    reorder find(:all, :order => 'UPPER(name)').collect { |c| c.id }
  end

  def published_articles
    self.articles.find_already_published
  end

  def display_name
    name
  end

  def permalink_url(anchor=nil, only_path=true)
    blog = Blog.find(1) # remove me...

    blog.url_for(
      :controller => '/articles',
      :action => 'category',
      :id => permalink
    )
  end

  protected

  before_save :set_defaults

  def set_defaults
    self.permalink ||= self.stripped_name
  end

  validates_presence_of :name
  validates_uniqueness_of :name, :on => :create
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
typo-4.1.1 app/models/category.rb