<commit_before>module TypeStation
  class Page
    include ::Mongoid::Document
    include ::Mongoid::Tree

    # RELATIONS

    embeds_many :contents, class_name: 'TypeStation::Content'

    # FIELDS
    field :title, type: String, default: 'Untitled'
    field :template_name, type: String, default: 'undefined'

    field :redirect_to, type: String, default: nil

    field :slug, type: String
    field :path, type: String

    # VALIDATIONS

    validates :slug, uniqueness: true, on: :update
    validates :path, presence: true, uniqueness: true, on: :update

    # Validate that they is only one root page
    validates_each :parent_id do |model, attr, value|
      root_page = TypeStation::Page.root
      model.errors.add(attr, 'already have a root page') if value == nil && root_page.present? && model.id != root_page.id
    end

    # CALLBACKS
    before_create :generate_slug
    before_destroy :destroy_children
    after_rearrange :rebuild_path
    
    # Rebuild self and children paths upon title change
    before_save :generate_slug, if: :title_changed?
    after_save :rebuild_child_paths, if: :has_children?

    # CLASS METHODS

    def self.find_by_path(path)
      self.where(path: File.join('',path)).first
    end

    # INSTANT METHODS

    def update_contents(params)
      params.each do |data|
        if content?(data[:field])
          set(data[:field], data[:value])
        else
          contents.build(name: data[:field], type: data[:type]).set(data[:value])
        end
      end

      save
    end

    def get(key)
      content_attributes[key].get
    end

    def set(key, value)
      content_attributes[key].set value
    end

    def content?(key)
      content_attributes[key].present?
    end

    def content_attributes
      @content_attributes ||= Hash[self.contents.map {|c| [c.name, c]}]
    end

    def redirect?<commit_msg>Make sure we set model values<commit_after>module TypeStation
  class Page
    include ::Mongoid::Document
    include ::Mongoid::Tree

    # RELATIONS

    embeds_many :contents, class_name: 'TypeStation::Content'

    # FIELDS
    field :title, type: String, default: 'Untitled'
    field :template_name, type: String, default: 'undefined'

    field :redirect_to, type: String, default: nil

    field :slug, type: String
    field :path, type: String

    # VALIDATIONS

    validates :slug, uniqueness: true, on: :update
    validates :path, presence: true, uniqueness: true, on: :update

    # Validate that they is only one root page
    validates_each :parent_id do |model, attr, value|
      root_page = TypeStation::Page.root
      model.errors.add(attr, 'already have a root page') if value == nil && root_page.present? && model.id != root_page.id
    end

    # CALLBACKS
    before_create :generate_slug
    before_destroy :destroy_children
    after_rearrange :rebuild_path
    
    # Rebuild self and children paths upon title change
    before_save :generate_slug, if: :title_changed?
    after_save :rebuild_child_paths, if: :has_children?

    # CLASS METHODS

    def self.find_by_path(path)
      self.where(path: File.join('',path)).first
    end

    # INSTANT METHODS

    def update_contents(params)
      params.each do |data|
        if content?(data[:field])
          set(data[:field], data[:value])
        else
          if self[data[:field]].present?
            self[data[:field]] = data[:value]
          else
            contents.build(name: data[:field], type: data[:type]).set(data[:value])
          end
        end
      end

      save
    end

    def get(key)
      content_attributes[key].get
    end

    def set(key, value)
      content_attributes[key].set value
    end

    def content?(key)
      content_attributes[key].present?
    end

    def content_attributes
      @content_attributes ||= Hash[self.contents.map {|c| [c.name, c]}]
    end

    def redirect?
{'fraction_non_alphanumeric': 0.10334190231362468, 'fraction_numerical': 0.0, 'mean_word_length': 2.8832335329341316, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4987654', 'n_tokens_mistral': 1229, 'n_tokens_neox': 1174, 'n_words': 326}