<commit_before>module Cmsimple
  class Page < ActiveRecord::Base
    serialize :content, Hash

    attr_accessible :slug,
                    :uri,
                    :title,
                    :content,
                    :template,
                    :parent_id,
                    :is_root,
                    :position,
                    :published,
                    :keywords,
                    :description,
                    :browser_title

    belongs_to :parent,   :class_name => '::Cmsimple::Page', :foreign_key => 'parent_id'
    has_many   :children, :class_name => '::Cmsimple::Page', :foreign_key => 'parent_id', :dependent => :destroy
    has_many   :paths,    :dependent => :destroy
    has_many   :versions, :dependent => :destroy

    validates :uri,
              :slug,
              :title,
              :presence => true

    validates :uri, :uniqueness => true

    validate :is_root_set

    before_validation :set_current_uri
    before_save :ensure_only_one_root
    after_save  :create_path

    def self.for_parent_select(page)
      scope = scoped
      unless page.new_record?
        scope = scope.where('id NOT IN (?)', page.descendants.map(&:id) + [page.id])
      end
      scope.order('position').all
    end

    # TODO: move this to a view delegator/presenter
    def select_label
      "#{'&nbsp;&nbsp;' * (uri ? (uri.count('/') - 1) : 0)}#{title}".html_safe
    end

    def self.root
      where('cmsimple_pages.is_root = ?', true).limit(1)<commit_msg>Explicitly order page versions to prevent DB ordering failures<commit_after>module Cmsimple
  class Page < ActiveRecord::Base
    serialize :content, Hash

    attr_accessible :slug,
                    :uri,
                    :title,
                    :content,
                    :template,
                    :parent_id,
                    :is_root,
                    :position,
                    :published,
                    :keywords,
                    :description,
                    :browser_title

    belongs_to :parent,   :class_name => '::Cmsimple::Page', :foreign_key => 'parent_id'
    has_many   :children, :class_name => '::Cmsimple::Page', :foreign_key => 'parent_id', :dependent => :destroy
    has_many   :paths,    :dependent => :destroy
    has_many   :versions, :dependent => :destroy, :order => 'created_at ASC'

    validates :uri,
              :slug,
              :title,
              :presence => true

    validates :uri, :uniqueness => true

    validate :is_root_set

    before_validation :set_current_uri
    before_save :ensure_only_one_root
    after_save  :create_path

    def self.for_parent_select(page)
      scope = scoped
      unless page.new_record?
        scope = scope.where('id NOT IN (?)', page.descendants.map(&:id) + [page.id])
      end
      scope.order('position').all
    end

    # TODO: move this to a view delegator/presenter
    def select_label
      "#{'&nbsp;&nbsp;' * (uri ? (uri.count('/') - 1) : 0)}#{title}".html_safe
    end

    def self.root
      where('cmsimple_pages.is_root = ?', true).limit(1)
{'fraction_non_alphanumeric': 0.11574523116715163, 'fraction_numerical': 0.0019398642095053346, 'mean_word_length': 1.8727948003714021, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4981612', 'n_tokens_mistral': 950, 'n_tokens_neox': 891, 'n_words': 200}