Parent

Included Modules

Files

Thoth::Page

Public Class Methods

name_unique?(name) click to toggle source

Returns true if the specified page name is already taken or is a reserved name.

# File lib/thoth/model/page.rb, line 64
def self.name_unique?(name)
  !PageController.methods.include?(name) &&
      !PageController.instance_methods.include?(name) &&
      !Page[:name => name.to_s.downcase]
end
name_valid?(name) click to toggle source

Returns true if the specified page name consists of valid characters and is not too long or too short.

# File lib/thoth/model/page.rb, line 72
def self.name_valid?(name)
  !!(name =~ /^[0-9a-z_-]{1,64}$/) && !(name =~ /^[0-9]+$/)
end
suggest_name(title) click to toggle source

Returns a valid, unique page name based on the specified title.

# File lib/thoth/model/page.rb, line 77
def self.suggest_name(title)
  index = 1

  # Remove HTML entities and non-alphanumeric characters, replace spaces
  # with hyphens, and truncate the name at 64 characters.
  name = title.to_s.strip.downcase.gsub(/&[^\s;]+;/, '_').
      gsub(/[^\s0-9a-z-]/, '').gsub(/\s+/, '-')[0..63]

  # Strip off any trailing non-alphanumeric characters.
  name.gsub!(/[_-]+$/, '')

  # If the name consists solely of numeric characters, add an alpha
  # character to prevent name/id ambiguity.
  name += 'a' unless name =~ /[a-z_-]/

  # Ensure that the name doesn't conflict with any methods on the Page
  # controller and that no two pages have the same name.
  until self.name_unique?(name)
    if name[-1] == index
      name[-1] = (index += 1).to_s
    else
      name = name[0..62] if name.size >= 64
      name += (index += 1).to_s
    end
  end

  return name
end

Public Instance Methods

body=(body) click to toggle source
# File lib/thoth/model/page.rb, line 110
def body=(body)
  self[:body]          = body.strip
  self[:body_rendered] = RedCloth.new(wiki_to_html(body.dup.strip)).to_html
end
created_at(format = nil) click to toggle source

Gets the creation time of this page. If format is provided, the time will be returned as a formatted String. See Time.strftime for details.

# File lib/thoth/model/page.rb, line 117
def created_at(format = nil)
  if new?
    format ? Time.now.strftime(format) : Time.now
  else
    format ? self[:created_at].strftime(format) : self[:created_at]
  end
end
name=(name) click to toggle source
# File lib/thoth/model/page.rb, line 125
def name=(name)
  self[:name] = name.strip.downcase unless name.nil?
end
title=(title) click to toggle source
# File lib/thoth/model/page.rb, line 129
def title=(title)
  title.strip!

  # Set the page name if it isn't already set.
  if self[:name].nil? || self[:name].empty?
    self[:name] = Page.suggest_name(title)
  end

  self[:title] = title
end
updated_at(format = nil) click to toggle source

Gets the time this page was last updated. If format is provided, the time will be returned as a formatted String. See Time.strftime for details.

# File lib/thoth/model/page.rb, line 142
def updated_at(format = nil)
  if new?
    format ? Time.now.strftime(format) : Time.now
  else
    format ? self[:updated_at].strftime(format) : self[:updated_at]
  end
end
url() click to toggle source

URL for this page.

# File lib/thoth/model/page.rb, line 151
def url
  Config.site.url.chomp('/') + R(PageController, name)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.