<commit_before>module Locomotive
  class PagePresenter < BasePresenter

    delegate :title, :slug, :fullpath, :handle, :raw_template, :published, :listed, :templatized, :templatized_from_parent, :redirect, :redirect_url, :template_changed, :cache_strategy, :response_type, :position, :to => :source

    delegate :title=, :slug=, :fullpath=, :handle=, :raw_template=, :published=, :listed=, :templatized=, :templatized_from_parent=, :redirect=, :redirect_url=, :cache_strategy=, :response_type=, :position=, :to => :source

    attr_writer :editable_elements

    def escaped_raw_template
      h(self.source.raw_template)
    end

    def editable_elements
      self.source.enabled_editable_elements.collect(&:as_json)
    end

    def set_editable_elements
      return unless @editable_elements

      # Need to parse to get the right editable elements first
      self.source.send(:_parse_and_serialize_template)

      @editable_elements.each do |editable_element_hash|
        slug = editable_element_hash['slug']
        block = editable_element_hash['block']

        block = nil if block && block.empty?

        el = self.source.find_editable_element(block, slug)
        el.to_presenter.assign_attributes(editable_element_hash) if el

        # FIXME: Not sure why we need to do this...
        if el.respond_to?(:source)
          self.source.save
          self.source.reload
          el = self.source.find_editable_element(block, slug)
          el.source = editable_element_hash['source']
        end
      end
    end

    def parent_fullpath
      self.source.parent.fullpath
    end

    def parent_fullpath=(parent_fullpath)
      current_site = self.source.site
      self.source.parent = current_site.pages.where(:fullpath => parent_fullpath).first
    end

    def included_methods
      super + %w(title slug fullpath handle raw_template published listed templatized templatized_from_parent redirect redirect_url cache_strategy response_type position template_changed editable_elements localized_fullpaths parent_fullpath)
    end

    def localized_fullpaths
      site = self.source.site

      {}.tap do |hash|
        site.locales.each do |locale|
          hash[locale] = site.localized_page_fullpath(self.source, locale)
        end
      end
    end

    def as_json_for_html_view
      methods = included_methods.clone - %w(raw_template)
      self.as_json(methods)
    end

    def save
      set_editable_elements
      super<commit_msg>Included page depth in API pulled info<commit_after>module Locomotive
  class PagePresenter < BasePresenter

    delegate :title, :slug, :fullpath, :handle, :raw_template, :published, :listed, :templatized, :templatized_from_parent, :redirect, :redirect_url, :template_changed, :cache_strategy, :response_type, :depth, :position, :to => :source

    delegate :title=, :slug=, :fullpath=, :handle=, :raw_template=, :published=, :listed=, :templatized=, :templatized_from_parent=, :redirect=, :redirect_url=, :cache_strategy=, :response_type=, :position=, :to => :source

    attr_writer :editable_elements

    def escaped_raw_template
      h(self.source.raw_template)
    end

    def editable_elements
      self.source.enabled_editable_elements.collect(&:as_json)
    end

    def set_editable_elements
      return unless @editable_elements

      # Need to parse to get the right editable elements first
      self.source.send(:_parse_and_serialize_template)

      @editable_elements.each do |editable_element_hash|
        slug = editable_element_hash['slug']
        block = editable_element_hash['block']

        block = nil if block && block.empty?

        el = self.source.find_editable_element(block, slug)
        el.to_presenter.assign_attributes(editable_element_hash) if el

        # FIXME: Not sure why we need to do this...
        if el.respond_to?(:source)
          self.source.save
          self.source.reload
          el = self.source.find_editable_element(block, slug)
          el.source = editable_element_hash['source']
        end
      end
    end

    def parent_fullpath
      self.source.parent.fullpath
    end

    def parent_fullpath=(parent_fullpath)
      current_site = self.source.site
      self.source.parent = current_site.pages.where(:fullpath => parent_fullpath).first
    end

    def included_methods
      super + %w(title slug fullpath handle raw_template published listed templatized templatized_from_parent redirect redirect_url cache_strategy response_type depth position template_changed editable_elements localized_fullpaths parent_fullpath)
    end

    def localized_fullpaths
      site = self.source.site

      {}.tap do |hash|
        site.locales.each do |locale|
          hash[locale] = site.localized_page_fullpath(self.source, locale)
        end
      end
    end

    def as_json_for_html_view
      methods = included_methods.clone - %w(raw_template)
      self.as_json(methods)
    end

    def save
      set_editable_elements
      super
{'fraction_non_alphanumeric': 0.08505516549648946, 'fraction_numerical': 0.0, 'mean_word_length': 3.3622047244094486, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '4615804', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1495, 'n_words': 339}