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
|
2007-12-27 02:20:32 -05:00
|
|
|
# * Allows any alphanumeric character or space in the name
|
2007-09-27 02:19:14 -04:00
|
|
|
# * 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-12-27 00:19:13 -05:00
|
|
|
belongs_to :created_by, :class_name => 'People', :foreign_key => 'created_by'
|
|
|
|
belongs_to :updated_by, :class_name => 'People', :foreign_key => 'updated_by'
|
|
|
|
|
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-12-27 00:19:13 -05:00
|
|
|
before_create :set_created_person
|
|
|
|
before_save :set_updated_person
|
|
|
|
|
|
|
|
attr_protected :allow_discussions, :created_by, :updated_by
|
2007-11-28 01:17:25 -05:00
|
|
|
|
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
|
|
|
|
|
2008-01-02 02:58:03 -05:00
|
|
|
##
|
|
|
|
# Determines if a Page exists.
|
|
|
|
#
|
|
|
|
def self.exists?(page_name, owner_type = nil)
|
|
|
|
cond_ary = [
|
|
|
|
'title = :page_name'
|
|
|
|
]
|
|
|
|
cond_var = {
|
|
|
|
:page_name => page_name,
|
|
|
|
:owner_type => owner_type
|
|
|
|
}
|
|
|
|
if owner_type.nil? or owner_type == 'Page'
|
|
|
|
cond_ary << 'owner_type IS NULL'
|
|
|
|
else
|
|
|
|
cond_ary << 'owner_type = :owner_type'
|
|
|
|
end
|
|
|
|
self.count(:conditions => [ cond_ary.join(' AND '), cond_var ]) > 0
|
|
|
|
end
|
|
|
|
|
2007-09-27 02:19:14 -04:00
|
|
|
protected
|
|
|
|
|
2007-12-27 00:19:13 -05:00
|
|
|
##
|
|
|
|
# Sets the People marker for created_by on creation.
|
|
|
|
#
|
|
|
|
def set_created_person
|
2007-12-27 01:12:15 -05:00
|
|
|
self[:created_by] = ApplicationController.current_people_id rescue nil
|
2008-01-02 02:58:03 -05:00
|
|
|
self[:created_by] ||= People.penguincoder.id rescue nil
|
2007-12-27 00:19:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Sets the People marker for updated_by on save.
|
|
|
|
#
|
|
|
|
def set_updated_person
|
2007-12-27 01:12:15 -05:00
|
|
|
self[:updated_by] = ApplicationController.current_people_id rescue nil
|
2008-01-02 02:58:03 -05:00
|
|
|
self[:updated_by] ||= People.penguincoder.id rescue nil
|
2007-12-27 00:19:13 -05:00
|
|
|
end
|
|
|
|
|
2007-09-27 02:19:14 -04:00
|
|
|
##
|
|
|
|
# Updates the HTML chunk from the RedCloth source.
|
|
|
|
#
|
|
|
|
def update_html
|
|
|
|
# need to filter HTML first... remove <script> and chunks and the like...
|
2008-01-02 02:58:03 -05:00
|
|
|
self.html = RedCloth.new(strip_tags(self.redcloth.to_s),
|
|
|
|
[ :no_span_caps ]).to_html(
|
2007-09-27 02:19:14 -04:00
|
|
|
# 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
|
2007-12-08 01:07:13 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# Removes HTML tags from a string
|
|
|
|
#
|
|
|
|
def strip_tags(str)
|
2008-01-02 02:58:03 -05:00
|
|
|
str.gsub(/\</, "<").gsub(/\>/, ">").gsub(/\&/, "&")
|
2007-12-08 01:07:13 -05:00
|
|
|
end
|
2007-09-27 02:19:14 -04:00
|
|
|
end
|