0
-class Page < DataMapper::Base
0
- property :parent_id, :integer
0
- property :user_id, :integer, :nullable => false
0
- property :title, :string, :nullable => false, :length => 255
0
- property :permalink, :string, :length => 255
0
- property :content, :text, :nullable => false
0
- property :display_in_nav, :boolean, :default => true
0
- property :position, :integer
0
- property :meta_description, :text
0
- property :meta_keywords, :text
0
- property :created_at, :datetime
0
- property :published_at, :datetime
0
- property :published, :boolean, :default => true
0
- property :formatter, :string, :default => "default"
0
+ include DataMapper::Resource
0
+ property :id, Integer, :key => true, :serial => true
0
+ property :parent_id, Integer
0
+ property :user_id, Integer, :nullable => false
0
+ property :title, String, :nullable => false, :length => 255
0
+ property :permalink, String, :length => 255
0
+ property :content, Text, :nullable => false
0
+ property :display_in_nav, Boolean, :default => true
0
+ property :position, Integer
0
+ property :meta_description, Text
0
+ property :meta_keywords, Text
0
+ property :created_at, DateTime
0
+ property :published_at, DateTime
0
+ property :published, Boolean, :default => true
0
+ property :formatter, String, :default => "default"
0
- validates_presence_of :user_id, :key => "uniq_user_id"
0
- validates_presence_of :title, :key => "uniq_title"
0
- validates_presence_of :permalink, :key => "uniq_permalink"
0
- validates_uniqueness_of :title
0
+ validates_present :user_id, :key => "uniq_user_id"
0
+ validates_present :title, :key => "uniq_title"
0
+ validates_present :permalink, :key => "uniq_permalink"
0
+ validates_is_unique :title
0
- belongs_to :parent, :class => "Page"
0
- has_many :children, :class => "Page", :foreign_key => "parent_id", :order => "position"
0
- has_many :children_nav, :class => "Page", :foreign_key => "parent_id", :order => "position", :conditions => {:display_in_nav => true}
0
+ belongs_to :parent, :class_name => "Page"
0
+ has n, :children, :class_name => "Page", :child_key => [:parent_id], :order => [:position.desc]
0
+ has n, :children_nav, :class_name => "Page", :child_key => [:parent_id], :order => [:position.desc], :display_in_nav => true
0
- before
_validation :set_published_permalink
0
+ before
:save, :set_published_permalink
0
def set_published_permalink
0
# Set the permalink, only if we haven't already
0
self.permalink = title.downcase.gsub(/[^a-z0-9]+/i, '-') if permalink.blank?
0
# Set the date, only if we haven't already
0
self.published_at = Time.now if self.published_at.nil?
0
- # We need this because the values get populated from the params
0
def self.published_pages_and_in_nav
0
-
all(:conditions => ['published = 1 AND published_at <= ? AND parent_id = 0 AND display_in_nav = 1', Time.now.utc], :order => "position")
0
+
self.all(:published => true, :published_at.lte => Time.now.utc, :parent_id => 0, :display_in_nav => true, :order => [:position.asc])
0
\ No newline at end of file
Comments
No one has commented yet.