Class: Page

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Annotatable, DeprecatedTags, Radiant::Taggable, StandardTags
Defined in:
app/models/page.rb

Direct Known Subclasses

FileNotFoundPage

Defined Under Namespace

Classes: MissingRootPageError

Constant Summary collapse

DEFAULT_PAGE_PARTS =
%w[body]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Annotatable

included

Methods included from Radiant::Taggable

included, #render_tag, #tag_descriptions, #tags

Methods included from LocalTime

#adjust_time

Instance Attribute Details

#pagination_parametersObject

Returns the value of attribute pagination_parameters.



44
45
46
# File 'app/models/page.rb', line 44

def pagination_parameters
  @pagination_parameters
end

#requestObject

Returns the value of attribute request.



44
45
46
# File 'app/models/page.rb', line 44

def request
  @request
end

#responseObject

Returns the value of attribute response.



44
45
46
# File 'app/models/page.rb', line 44

def response
  @response
end

Class Method Details

.date_column_namesObject



225
226
227
# File 'app/models/page.rb', line 225

def date_column_names
  self.columns.collect{|c| c.name if c.sql_type =~ /(date|time)/}.compact
end

.descendant_class(class_name) ⇒ Object

Raises:

  • (ArgumentError)


285
286
287
288
289
290
291
292
# File 'app/models/page.rb', line 285

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

.display_name(string = nil) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'app/models/page.rb', line 229

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

.display_name=(string) ⇒ Object



244
245
246
# File 'app/models/page.rb', line 244

def display_name=(string)
  display_name(string)
end

.find_by_path(path, live = true) ⇒ Object



215
216
217
218
219
# File 'app/models/page.rb', line 215

def find_by_path(path, live = true)
  root = find_by_parent_id(nil)
  raise MissingRootPageError unless root
  root.find_by_path(path, live)
end

.find_by_url(*args) ⇒ Object



220
221
222
223
# File 'app/models/page.rb', line 220

def find_by_url(*args)
  ActiveSupport::Deprecation.warn("`find_by_url' has been deprecated; use `find_by_path' instead.", caller)
  find_by_path(*args)
end

.is_descendant_class_name?(class_name) ⇒ Boolean

Returns:

  • (Boolean)


281
282
283
# File 'app/models/page.rb', line 281

def is_descendant_class_name?(class_name)
  (Page.descendants.map(&:to_s) + [nil, "", "Page"]).include?(class_name)
end

.load_subclassesObject



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'app/models/page.rb', line 248

def load_subclasses
  unless Rails::Application.config.cache_classes
    ActiveSupport::Dependencies.autoload_paths.grep(/\bmodels$/).each do |path|
      Dir["#{path}/*_page.rb"].each do |file|
        klass = file.sub("#{path}/", '').gsub('.rb','')
        require_dependency klass
        ActiveSupport::Dependencies.explicitly_unloadable_constants << klass.camelize
      end
    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 |page_klass|
      begin
        page_klass.constantize
      rescue NameError, LoadError
        Object.const_set page_klass, Class.new(Page) {
          def self.missing?() true end
        }
      end
    end
  end
end

.missing?Boolean

Returns:

  • (Boolean)


294
295
296
# File 'app/models/page.rb', line 294

def missing?
  false
end

.new_with_defaults(config = Radiant::Config) ⇒ Object



272
273
274
275
276
277
278
279
# File 'app/models/page.rb', line 272

def new_with_defaults(config = Radiant::Config)
  page = new
  page.parts.concat default_page_parts(config)
  page.fields.concat default_page_fields(config)
  default_status = config['defaults.page.status']
  page.status = Status[default_status] if default_status
  page
end

Instance Method Details

#cache?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'app/models/page.rb', line 67

def cache?
  true
end

#child_path(child) ⇒ Object Also known as: child_url



71
72
73
# File 'app/models/page.rb', line 71

def child_path(child)
  clean_path(path + '/' + child.slug)
end

#descriptionObject



59
60
61
# File 'app/models/page.rb', line 59

def description
  self["description"]
end

#description=(value) ⇒ Object



63
64
65
# File 'app/models/page.rb', line 63

def description=(value)
  self["description"] = value
end

#field(name) ⇒ Object



101
102
103
104
105
106
107
# File 'app/models/page.rb', line 101

def field(name)
  if new_record? or fields.any?(&:new_record?)
    fields.detect { |f| f.name.downcase == name.to_s.downcase }
  else
    fields.find_by_name name.to_s
  end
end

#find_by_path(path, live = true, clean = true) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/models/page.rb', line 170

def find_by_path(path, live = true, clean = true)
  return nil if virtual?
  path = clean_path(path) if clean
  my_path = self.path
  if (my_path == path) && (not live or published?)
    self
  elsif (path =~ /^#{Regexp.quote(my_path)}([^\/]*)/)
    slug_child = children.find_by_slug($1)
    if slug_child
      found = slug_child.find_by_url(path, live, clean) # TODO: set to find_by_path after deprecation
      return found if found
    end
    children.each do |child|
      found = child.find_by_url(path, live, clean) # TODO: set to find_by_path after deprecation
      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

#find_by_urlObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/models/page.rb', line 193

def find_by_path(path, live = true, clean = true)
  return nil if virtual?
  path = clean_path(path) if clean
  my_path = self.path
  if (my_path == path) && (not live or published?)
    self
  elsif (path =~ /^#{Regexp.quote(my_path)}([^\/]*)/)
    slug_child = children.find_by_slug($1)
    if slug_child
      found = slug_child.find_by_url(path, live, clean) # TODO: set to find_by_path after deprecation
      return found if found
    end
    children.each do |child|
      found = child.find_by_url(path, live, clean) # TODO: set to find_by_path after deprecation
      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

#has_or_inherits_part?(name) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'app/models/page.rb', line 93

def has_or_inherits_part?(name)
  has_part?(name) || inherits_part?(name)
end

#has_part?(name) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'app/models/page.rb', line 89

def has_part?(name)
  !part(name).nil?
end

#headersObject



76
77
78
79
# File 'app/models/page.rb', line 76

def headers
  # Return a blank hash that child classes can override or merge
  { }
end

#inherits_part?(name) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'app/models/page.rb', line 97

def inherits_part?(name)
  !has_part?(name) && self.ancestors.any? { |page| page.has_part?(name) }
end

#layout_with_inheritanceObject



50
51
52
53
54
55
56
# File 'app/models/page.rb', line 50

def layout_with_inheritance
  unless layout_without_inheritance
    parent.layout if parent?
  else
    layout_without_inheritance
  end
end

#part(name) ⇒ Object



81
82
83
84
85
86
87
# File 'app/models/page.rb', line 81

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

#pathObject Also known as: url



125
126
127
128
129
130
131
# File 'app/models/page.rb', line 125

def path
  if parent?
    parent.child_path(self)
  else
    clean_path(slug)
  end
end

#process(request, response) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'app/models/page.rb', line 134

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

#published?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'app/models/page.rb', line 109

def published?
  status == Status[:published]
end

#renderObject



149
150
151
152
153
154
155
# File 'app/models/page.rb', line 149

def render
  if layout
    parse_object(layout)
  else
    render_part(:body)
  end
end

#render_part(part_name) ⇒ Object



157
158
159
160
161
162
163
164
# File 'app/models/page.rb', line 157

def render_part(part_name)
  part = part(part_name)
  if part
    parse_object(part)
  else
    ''
  end
end

#render_snippet(snippet) ⇒ Object



166
167
168
# File 'app/models/page.rb', line 166

def render_snippet(snippet)
  parse_object(snippet)
end

#response_codeObject



145
146
147
# File 'app/models/page.rb', line 145

def response_code
  200
end

#scheduled?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'app/models/page.rb', line 113

def scheduled?
  status == Status[:scheduled]
end

#statusObject



117
118
119
# File 'app/models/page.rb', line 117

def status
 Status.find(self.status_id)
end

#status=(value) ⇒ Object



121
122
123
# File 'app/models/page.rb', line 121

def status=(value)
  self.status_id = value.id
end

#to_xml(options = {}, &block) ⇒ Object



207
208
209
# File 'app/models/page.rb', line 207

def to_xml(options={}, &block)
  super(options.reverse_merge(:include => :parts), &block)
end

#update_statusObject



195
196
197
198
199
200
201
202
203
204
# File 'app/models/page.rb', line 195

def update_status
  self.published_at = Time.zone.now if published? && self.published_at == nil
  
  if self.published_at != nil && (published? || scheduled?)
    self[:status_id] = Status[:scheduled].id if self.published_at  > Time.zone.now
    self[:status_id] = Status[:published].id if self.published_at <= Time.zone.now
  end

  true    
end