Gets the Post with the specified name, where name can be either a name or an id.
# File lib/thoth/model/post.rb, line 75 def self.get(name) return Post[name] if name.is_a?(Numeric) name = name.to_s.downcase name =~ /^\d+$/ ? Post[name] : Post[:name => name] end
Returns true if the specified post name is already taken or is a reserved name.
# File lib/thoth/model/post.rb, line 83 def self.name_unique?(name) !PostController.methods.include?(name) && !PostController.instance_methods.include?(name) && !Post[:name => name.to_s.downcase] end
Returns true if the specified post name consists of valid characters and is not too long or too short.
# File lib/thoth/model/post.rb, line 91 def self.name_valid?(name) !!(name =~ /^[0-9a-z_-]{1,64}$/) && !(name =~ /^[0-9]+$/) end
Gets a paginated dataset of recent posts sorted in reverse order by creation time.
# File lib/thoth/model/post.rb, line 97 def self.recent(page = 1, limit = 10) reverse_order(:created_at).paginate(page, limit) end
Returns a valid, unique post name based on the specified title.
# File lib/thoth/model/post.rb, line 102 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 Post # controller and that no two posts 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
Gets the Atom feed URL for this post.
# File lib/thoth/model/post.rb, line 136 def atom_url Config.site.url.chomp('/') + R(PostController, :atom, name) end
# File lib/thoth/model/post.rb, line 140 def body=(body) self[:body] = body.strip self[:body_rendered] = RedCloth.new(wiki_to_html(body.dup.strip)).to_html end
Gets the creation time of this post. If format is provided, the time will be returned as a formatted String. See Time.strftime for details.
# File lib/thoth/model/post.rb, line 153 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
# File lib/thoth/model/post.rb, line 161 def name=(name) self[:name] = name.to_s.strip.downcase unless name.nil? end
# File lib/thoth/model/post.rb, line 211 def title=(title) title.strip! # Set the post's name if it isn't already set. if self[:name].nil? || self[:name].empty? self[:name] = Post.suggest_name(title) end self[:title] = title end
Gets the time this post 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/post.rb, line 225 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
Generated with the Darkfish Rdoc Generator 2.
Gets a dataset of comments attached to this post, ordered by creation time.