radiant / radiant

Radiant is a no-fluff, open source content management system designed for small teams.

This URL has Read+Write access

saturnflyer (author)
Mon Jun 29 18:59:10 -0700 2009
commit  7afc73e7d15ed6cd6ded0b338f4a366c975d49d9
tree    45aeaa9f0dce27e4ed9042aafd98822a771e433f
parent  4f2ba63484ce8ef7ef3b3f482317eea7d87879d5
radiant / app / models / page.rb
100644 297 lines (249 sloc) 7.889 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
class Page < ActiveRecord::Base
 
  class MissingRootPageError < StandardError
    def initialize(message = 'Database missing root page'); super end
  end
 
  # Callbacks
  before_save :update_published_at, :update_virtual
 
  # Associations
  acts_as_tree :order => 'virtual DESC, title ASC'
  has_many :parts, :class_name => 'PagePart', :order => 'id', :dependent => :destroy
  accepts_nested_attributes_for :parts, :allow_destroy => true
  belongs_to :layout
  belongs_to :created_by, :class_name => 'User'
  belongs_to :updated_by, :class_name => 'User'
 
  # Validations
  validates_presence_of :title, :slug, :breadcrumb, :status_id, :message => 'required'
 
  validates_length_of :title, :maximum => 255, :message => '{{count}}-character limit'
  validates_length_of :slug, :maximum => 100, :message => '{{count}}-character limit'
  validates_length_of :breadcrumb, :maximum => 160, :message => '{{count}}-character limit'
 
  validates_format_of :slug, :with => %r{^([-_.A-Za-z0-9]*|/)$}, :message => 'invalid format'
  validates_uniqueness_of :slug, :scope => :parent_id, :message => 'slug already in use for child of parent'
  validates_numericality_of :id, :status_id, :parent_id, :allow_nil => true, :only_integer => true, :message => 'must be a number'
 
  validate :valid_class_name
 
  include Radiant::Taggable
  include StandardTags
  include Annotatable
 
  annotate :description
  attr_accessor :request, :response
 
  set_inheritance_column :class_name
 
  def layout_with_inheritance
    unless layout_without_inheritance
      parent.layout if parent?
    else
      layout_without_inheritance
    end
  end
  alias_method_chain :layout, :inheritance
 
  def description
    self["description"]
  end
 
  def description=(value)
    self["description"] = value
  end
 
  def cache?
    true
  end
 
  def child_url(child)
    clean_url(url + '/' + child.slug)
  end
 
  def headers
    # Return a blank hash that child classes can override or merge
    { }
  end
 
  def part(name)
    if new_record? or parts.to_a.any?(&:new_record?)
      parts.to_a.find {|p| p.name == name.to_s }
    else
      parts.find_by_name name.to_s
    end
  end
 
  def has_part?(name)
    !part(name).nil?
  end
 
  def has_or_inherits_part?(name)
    has_part?(name) || inherits_part?(name)
  end
 
  def inherits_part?(name)
    !has_part?(name) && self.ancestors.any? { |page| page.has_part?(name) }
  end
 
  def published?
    status == Status[:published]
  end
 
  def status
    Status.find(self.status_id)
  end
  def status=(value)
    self.status_id = value.id
  end
 
  def url
    if parent?
      parent.child_url(self)
    else
      clean_url(slug)
    end
  end
 
  def process(request, response)
    @request, @response = request, response
    if layout
      content_type = layout.content_type.to_s.strip
      @response.headers['Content-Type'] = content_type unless content_type.empty?
    end
    headers.each { |k,v| @response.headers[k] = v }
    @response.body = render
    @response.status = response_code
  end
 
  def response_code
    200
  end
 
  def render
    if layout
      parse_object(layout)
    else
      render_part(:body)
    end
  end
 
  def render_part(part_name)
    part = part(part_name)
    if part
      parse_object(part)
    else
      ''
    end
  end
 
  def render_snippet(snippet)
    parse_object(snippet)
  end
 
  def find_by_url(url, live = true, clean = true)
    return nil if virtual?
    url = clean_url(url) if clean
    my_url = self.url
    if (my_url == url) && (not live or published?)
      self
    elsif (url =~ /^#{Regexp.quote(my_url)}([^\/]*)/)
      slug_child = children.find_by_slug($1)
      if slug_child
        found = slug_child.find_by_url(url, live, clean)
        return found if found
      end
      children.each do |child|
        found = child.find_by_url(url, live, clean)
        return found if found
      end
      file_not_found_types = ([FileNotFoundPage] + FileNotFoundPage.descendants)
      file_not_found_names = file_not_found_types.collect { |x| x.name }
      condition = (['class_name = ?'] * file_not_found_names.length).join(' or ')
      condition = "status_id = #{Status[:published].id} and (#{condition})" if live
      children.find(:first, :conditions => [condition] + file_not_found_names)
    end
  end
 
  def to_xml(options={}, &block)
    super(options.reverse_merge(:include => :parts), &block)
  end
 
  class << self
    def find_by_url(url, live = true)
      root = find_by_parent_id(nil)
      raise MissingRootPageError unless root
      root.find_by_url(url, live)
    end
 
    def display_name(string = nil)
      if string
        @display_name = string
      else
        @display_name ||= begin
          n = name.to_s
          n.sub!(/^(.+?)Page$/, '\1')
          n.gsub!(/([A-Z])/, ' \1')
          n.strip
        end
      end
      @display_name = @display_name + " - not installed" if missing? && @display_name !~ /not installed/
      @display_name
    end
    def display_name=(string)
      display_name(string)
    end
 
    def load_subclasses
      ([RADIANT_ROOT] + Radiant::Extension.descendants.map(&:root)).each do |path|
        Dir["#{path}/app/models/*_page.rb"].each do |page|
          $1.camelize.constantize if page =~ %r{/([^/]+)\.rb}
        end
      end
      if ActiveRecord::Base.connection.tables.include?('pages') && Page.column_names.include?('class_name') # Assume that we have bootstrapped
        Page.connection.select_values("SELECT DISTINCT class_name FROM pages WHERE class_name <> '' AND class_name IS NOT NULL").each do |p|
          begin
            p.constantize
          rescue NameError, LoadError
            eval(%Q{class #{p} < Page; def self.missing?; true end end}, TOPLEVEL_BINDING)
          end
        end
      end
    end
 
    def new_with_defaults(config = Radiant::Config)
      default_parts = config['defaults.page.parts'].to_s.strip.split(/\s*,\s*/)
      page = new
      default_parts.each do |name|
        page.parts << PagePart.new(:name => name, :filter_id => config['defaults.page.filter'])
      end
      default_status = config['defaults.page.status']
      page.status = Status[default_status] if default_status
      page
    end
 
    def is_descendant_class_name?(class_name)
      (Page.descendants.map(&:to_s) + [nil, "", "Page"]).include?(class_name)
    end
 
    def descendant_class(class_name)
      raise ArgumentError.new("argument must be a valid descendant of Page") unless is_descendant_class_name?(class_name)
      if ["", nil, "Page"].include?(class_name)
        Page
      else
        class_name.constantize
      end
    end
 
    def missing?
      false
    end
  end
 
  private
 
    def valid_class_name
      unless Page.is_descendant_class_name?(class_name)
        errors.add :class_name, "must be set to a valid descendant of Page"
      end
    end
 
    def attributes_protected_by_default
      super - [self.class.inheritance_column]
    end
 
    def update_published_at
      self[:published_at] = Time.now if published? and !published_at
      true
    end
 
    def update_virtual
      unless self.class == Page.descendant_class(class_name)
        self.virtual = Page.descendant_class(class_name).new.virtual?
      else
        self.virtual = virtual?
      end
      true
    end
 
    def clean_url(url)
      "/#{ url.strip }/".gsub(%r{//+}, '/')
    end
 
    def parent?
      !parent.nil?
    end
 
    def lazy_initialize_parser_and_context
      unless @parser and @context
        @context = PageContext.new(self)
        @parser = Radius::Parser.new(@context, :tag_prefix => 'r')
      end
      @parser
    end
 
    def parse(text)
      lazy_initialize_parser_and_context.parse(text)
    end
 
    def parse_object(object)
      text = object.content
      text = parse(text)
      text = object.filter.filter(text) if object.respond_to? :filter_id
      text
    end
 
end