<commit_before>module TypeStation
  module Concerns
    module PathGenerator
      extend ActiveSupport::Concern

      included do

        field :title, type: String, default: 'Untitled'
        field :slug, type: String
        field :path, type: String

        # VALIDATIONS

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

         # 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?

      end

      module ClassMethods

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

      end

      private

      # Generates a slug based of the title give by the user
      def generate_slug
        self.slug = root? ? "" : title.parameterize
        rebuild_path
      end

      # Rebuild a path based of a ancestors slugs
      def rebuild_path
        self.path = root? ? "/" : self.ancestors_and_self.collect(&:slug).join('/')
      end

      def rebuild_child_paths
        self.children.each do |child|
          child.send :rebuild_path
          child.save
        end
      end

    end
  end
end<commit_msg>should only rebuild path is the child respond to rebuild_path<commit_after>module TypeStation
  module Concerns
    module PathGenerator
      extend ActiveSupport::Concern

      included do

        field :title, type: String, default: 'Untitled'
        field :slug, type: String
        field :path, type: String

        # VALIDATIONS

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

         # 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?

      end

      module ClassMethods

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

      end

      private

      # Generates a slug based of the title give by the user
      def generate_slug
        self.slug = root? ? "" : title.parameterize
        rebuild_path
      end

      # Rebuild a path based of a ancestors slugs
      def rebuild_path
        self.path = root? ? "/" : self.ancestors_and_self.collect(&:slug).join('/')
      end

      def rebuild_child_paths
        self.children.each do |child|
          if child.respond_to?(:rebuild_path)
            child.send :rebuild_path
            child.save
          end
        end
      end

    end
  end
end
{'fraction_non_alphanumeric': 0.06916724019270475, 'fraction_numerical': 0.0, 'mean_word_length': 2.1258064516129034, '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': '813898', 'n_tokens_mistral': 846, 'n_tokens_neox': 807, 'n_words': 245}