public
Description: alltom on thin
Homepage: http://alltom.com/
Clone URL: git://github.com/alltom/talltom.git
talltom / models.rb
100644 107 lines (87 sloc) 2.648 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
106
107
require "rexml/document"
 
class Page < ActiveRecord::Base
  has_many :versions, :class_name => "PageVersion",
    :order => "created_on ASC", :dependent => :destroy
  belongs_to :current_version, :class_name => "PageVersion",
    :foreign_key => "current_version_id"
 
  @@slug_format = /^[a-z\-0-9\.]+$/
 
  validates_presence_of :title
  validates_format_of :slug, :with => @@slug_format
  validates_uniqueness_of :slug
 
  def before_validation
    unless (self.slug =~ @@slug_format) || self.frozen?
      self.slug = generate_slug(self.title)
    end
  end
 
  def to_param
    self.slug
  end
 
  def modified?
    return false if current_version.nil?
    return false if versions.count <= 1
    return true
  end
 
  class << self
    def find_all
      find(:all, :include => :current_version,
          :conditions => "current_version_id IS NOT NULL",
          :order => "publish_date DESC")
    end
 
    def find_recent
      find(:all, :include => :current_version,
          :conditions => "current_version_id IS NOT NULL",
          :order => "publish_date DESC", :limit => 20)
    end
 
    def find_for_rss
      find(:all, :include => :current_version,
          :conditions => "current_version_id IS NOT NULL AND show_in_rss = 1",
          :order => "publish_date DESC", :limit => 15)
    end
  end
 
  private
 
    def generate_slug(title)
      return "" unless title
      title.downcase.delete("'\"").gsub(/[^A-Za-z0-9\.]+/,' ').strip.gsub(' ','-')
    end
end
 
class PageVersion < ActiveRecord::Base
  belongs_to :page
  
  validates_presence_of :body
  validates_presence_of :publish_date
  validates_presence_of :page
  validates_associated :page
  validates_each :body do |record, attr, value|
    begin
      REXML::Document.new("<doc>" + value.to_s + "</doc>")
    rescue REXML::ParseException
      record.errors.add attr, "is not valid XML"
    end
  end
 
  def before_save
    unless self.version
      last_version = PageVersion.maximum(:version, :conditions => [ 'page_id = ?', self.page_id ])
      self.version = (last_version || 0) + 1
    end
  end
end
 
class NewsUpdate < ActiveRecord::Base
  validates_presence_of :body
  validates_each :body do |record, attr, value|
    begin
      REXML::Document.new("<doc>" + value.to_s + "</doc>")
    rescue REXML::ParseException
      record.errors.add attr, "is not valid XML"
    end
  end
 
  def self.latest
    find(:first, :order => "created_on DESC")
  end
end
 
class Project < ActiveRecord::Base
  belongs_to :page
 
  validates_presence_of :title
  validates_associated :page
 
  def self.find_all
    find(:all, :order => "updated_at DESC, created_at DESC")
  end
end