2007-09-27 02:19:14 -04:00
|
|
|
##
|
|
|
|
# This is a representation of a page of the wiki. My wiki is awfully simple
|
|
|
|
# because i don't like too many features. This works and it has many different
|
|
|
|
# associations. All of the major models has_one of these and it belongs_to
|
|
|
|
# some kind of polymorphic owner.
|
|
|
|
#
|
|
|
|
# Bonuses:
|
|
|
|
# * Uses RedCloth markup.
|
|
|
|
# * Automatically converts/caches HTML
|
|
|
|
# * Allows any character or space in the name
|
|
|
|
# * Uses [[ and ]] for WikiWord representation.
|
|
|
|
#
|
|
|
|
class Page < ActiveRecord::Base
|
|
|
|
acts_as_versioned
|
2007-11-17 01:13:45 -05:00
|
|
|
acts_as_taggable
|
2007-09-27 02:19:14 -04:00
|
|
|
|
|
|
|
belongs_to :owner, :polymorphic => true
|
2007-11-28 01:17:25 -05:00
|
|
|
has_many :discussions, :order => 'discussions.created_at ASC',
|
|
|
|
:dependent => :destroy
|
|
|
|
|
2007-09-27 02:19:14 -04:00
|
|
|
validates_presence_of :title
|
2007-11-10 23:52:23 -05:00
|
|
|
validates_uniqueness_of :title, :scope => 'owner_type'
|
2007-11-11 01:19:43 -05:00
|
|
|
validates_format_of :title, :with => /^([A-Za-z0-9 ])+$/,
|
2007-09-27 02:19:14 -04:00
|
|
|
:message => 'may only contain letters, numbers and spaces'
|
|
|
|
before_save :update_html
|
|
|
|
|
2007-11-28 01:17:25 -05:00
|
|
|
attr_protected :allow_discussions
|
|
|
|
|
2007-10-03 02:34:21 -04:00
|
|
|
##
|
|
|
|
# Returns an url-friendly title for making links.
|
|
|
|
#
|
|
|
|
def title_for_url
|
|
|
|
self.title.gsub(/ /, '_')
|
|
|
|
end
|
2007-10-04 00:54:54 -04:00
|
|
|
|
2007-10-03 02:34:21 -04:00
|
|
|
##
|
|
|
|
# Gets a title from an url name.
|
|
|
|
#
|
|
|
|
def self.title_from_url(title)
|
|
|
|
title.to_s.gsub(/_/, ' ')
|
|
|
|
end
|
2007-11-10 23:52:23 -05:00
|
|
|
|
2007-11-28 02:16:41 -05:00
|
|
|
##
|
|
|
|
# Returns a list of Tag models for a type of Page.
|
|
|
|
#
|
|
|
|
# Can take options of :order, :limit, and :owner_type
|
|
|
|
#
|
|
|
|
def self.tags(options = {})
|
|
|
|
query = "select tags.id, name, count(*) as count"
|
|
|
|
query << " from tags_pages, tags, pages"
|
|
|
|
query << " where tags.id = tag_id"
|
|
|
|
if options[:owner_type].nil?
|
|
|
|
query << " and pages.owner_type IS NULL"
|
|
|
|
else
|
|
|
|
query << " and pages.owner_type = '#{options[:owner_type]}'"
|
|
|
|
end
|
|
|
|
query << " and tags_pages.page_id = pages.id"
|
|
|
|
query << " group by tags.id, tags.name"
|
|
|
|
query << " order by #{options[:order]}" if options[:order] != nil
|
|
|
|
query << " limit #{options[:limit]}" if options[:limit] != nil
|
|
|
|
tags = Tag.find_by_sql(query)
|
|
|
|
end
|
|
|
|
|
2007-09-27 02:19:14 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
##
|
|
|
|
# Updates the HTML chunk from the RedCloth source.
|
|
|
|
#
|
|
|
|
def update_html
|
|
|
|
# need to filter HTML first... remove <script> and chunks and the like...
|
2007-10-03 01:43:33 -04:00
|
|
|
res = RedCloth.new(self.redcloth.to_s.strip_tags, [ :no_span_caps ])
|
2007-09-27 02:19:14 -04:00
|
|
|
self.html = res.to_html(
|
|
|
|
# no link references. messes up lines starting with [[WikiWord]]
|
|
|
|
:block_textile_table, :block_textile_lists, :block_textile_prefix,
|
|
|
|
:inline_textile_image, :inline_textile_link, :inline_textile_code,
|
|
|
|
:inline_textile_span, :glyphs_textile)
|
|
|
|
end
|
|
|
|
end
|