public
Fork of halorgium/mephisto
Description: A refactored Mephisto that has multiple spam detection engines.
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/francois/mephisto.git
mephisto / app / models / section.rb
100644 105 lines (87 sloc) 2.95 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
class Section < ActiveRecord::Base
  ARTICLES_COUNT_SQL = 'INNER JOIN assigned_sections ON contents.id = assigned_sections.article_id INNER JOIN sections ON sections.id = assigned_sections.section_id'.freeze unless defined?(ARTICLES_COUNT)
  before_validation :set_archive_path
  before_validation_on_create :create_path
  validates_presence_of :name, :site_id, :archive_path
  validates_format_of :archive_path, :with => Format::STRING
  validates_exclusion_of :path, :in => [nil]
  validates_uniqueness_of :path, :case_sensitive => false, :scope => :site_id
  belongs_to :site
  has_many :assigned_sections, :dependent => :delete_all
  has_many :articles, :order => 'position', :through => :assigned_sections do
    def find_by_position(options = {})
      find(:first, { :conditions => ['contents.published_at <= ? AND contents.published_at IS NOT NULL', Time.now.utc] } \
        .merge(options))
    end
 
    def find_by_permalink(permalink, options = {})
      find(:first, { :conditions => ['contents.permalink = ? AND published_at <= ? AND contents.published_at IS NOT NULL',
                                      permalink, Time.now.utc] }.merge(options))
    end
  end
 
  class << self
    # scopes a find operation to return only paged sections
    def find_paged(options = {})
      with_scope :find => { :conditions => ['show_paged_articles = ?', true] } do
        block_given? ? yield : find(:all, options)
      end
    end
 
    def articles_count
      Article.count :all, :group => :section_id, :joins => ARTICLES_COUNT_SQL
    end
    
    def permalink_for(str)
      str.gsub(/[^\w\/]|[!\(\)\.]+/, ' ').strip.downcase.gsub(/\ +/, '-')
    end
  end
 
  def find_comments(options = {})
    Comment.find_all_by_section(self, options)
  end
 
  def to_liquid(current = false)
    SectionDrop.new self, current
  end
 
  # orders articles assigned to this section
  def order!(*sorted_ids)
    transaction do
      sorted_ids.flatten.each_with_index do |article, pos|
        assigned_sections.detect { |s| s.article_id.to_s == article.to_s }.update_attributes(:position => pos)
      end
      save
    end
  end
 
  def hash_for_url(options = {})
    { :path => to_url }.merge(options)
  end
 
  def home?
    path.blank?
  end
 
  def to_url
    path.split('/')
  end
 
  def paged?
    show_paged_articles?
  end
  
  def blog?
    !show_paged_articles?
  end
 
  def to_page_url(page_name)
    to_url << (page_name.respond_to?(:permalink) ? page_name.permalink : page_name)
  end
 
  def to_feed_url
    to_page_url 'atom.xml'
  end
  
  def to_comments_url
    to_page_url 'comments.xml'
  end
  
  protected
    def set_archive_path
      self.archive_path = 'archives' if archive_path.blank?
      archive_path.downcase!
    end
 
    def create_path
      self.path =
        case path
          when nil then ''
          when '' then self.class.permalink_for(name.to_s)
          else path
        end
    end
end