<commit_before>class Search::CmsSerializer < Search::BaseSerializer
  include Comfy::CmsHelper

  def initialize(search, opts={})
    super(search, opts)
  end

  DEFAULT_RESULT = {
    date: nil,
    fileUrl: nil,
    linkTitle: nil,
    linkUrl: nil,
    title: '',
    url: '',
    summary: '',
    image: ''
  }.freeze

  DEFAULT_OBJ = {
    current_page: 1,
    page_items_start: 1,
    page_items_end: 1,
    total_items: 0,
    results: [DEFAULT_RESULT]
  }.freeze

  def serialize
    return DEFAULT_OBJ.to_json unless @search

    all_objects = @results.objects.values.compact.flatten
    per_page = @options[:per_page].to_i
    DEFAULT_OBJ.merge(
      {
        current_page: @page,
        page_items_start: @search.page_items_start(page: @page, per_page: per_page, for_display: true),
        page_items_end: @search.page_items_end(page: @page, per_page: per_page, for_display: true),
        total_items: @results.count || 0,
        results: all_objects.map do |record|
          {
            date: date(record),
            fileUrl: file(record),
            linkUrl: link(record),
            linktTile: link_title(record), 
            title: strip_html(record.respond_to?(:label) ? record.label : record.name),
            url: url(record),
            summary: strip_html(record.respond_to?(:summary) ? record.summary : record.name),
            image: image(record)
          }
        end
      }
    ).to_json
  end

  private

  def date(page)
    _date = cms_fragment_content(:published_date, page)
    _date.present? ? _date.strftime('%d %B %y') : _date
  end

  def file(page)
    cms_fragment_render(:file, page)
  end

  def link(page)
    cms_fragment_render(:link, page)
  end

  def link_title(page)
    cms_fragment_render(:link_title, page)
  end
end<commit_msg>Update serch results properties<commit_after>class Search::CmsSerializer < Search::BaseSerializer
  include Comfy::CmsHelper

  def initialize(search, opts={})
    super(search, opts)
  end

  DEFAULT_RESULT = {
    date: nil,
    fileUrl: nil,
    linkTitle: nil,
    linkUrl: nil,
    title: '',
    url: '',
    summary: '',
    image: ''
  }.freeze

  DEFAULT_OBJ = {
    total: 0,
    totalPages: 1,
    results: [DEFAULT_RESULT]
  }.freeze

  def serialize
    return DEFAULT_OBJ.to_json unless @search

    all_objects = @results.objects.values.compact.flatten
    per_page = @options[:per_page].to_i
    DEFAULT_OBJ.merge(
      {
        total: total,
        totalPages: total_pages,
        results: all_objects.map do |record|
          {
            date: date(record),
            fileUrl: file(record),
            linkUrl: link(record),
            linktTile: link_title(record), 
            title: strip_html(record.respond_to?(:label) ? record.label : record.name),
            url: url(record),
            summary: strip_html(record.respond_to?(:summary) ? record.summary : record.name),
            image: image(record)
          }
        end
      }
    ).to_json
  end

  private

  def date(page)
    _date = cms_fragment_content(:published_date, page)
    _date.present? ? _date.strftime('%d %B %y') : _date
  end

  def file(page)
    cms_fragment_render(:file, page)
  end

  def link(page)
    cms_fragment_render(:link, page)
  end

  def link_title(page)
    cms_fragment_render(:link_title, page)
  end

  def total
    @total ||= @results.count || 0
  end

  DEFAULT_PAGE_SIZE = {
    resources: 9.0,
    news_and_stories: 6.0
  }.freeze
  def total_pages
    (total / default_page_size(cms_root_page.try(:slug))).ceil
  end

  def default_page_size(slug)
    DEFAULT_PAGE_SIZE[slug.underscore.to_sym] || 9
  end

  def cms_root_page
    _root_page_id = @search.options.dig(:filters, :ancestor)
    @cms_root_page ||= Comfy::Cms::Page.find_by(id: _root_page_id)
  end
end
{'fraction_non_alphanumeric': 0.11441346659652814, 'fraction_numerical': 0.0034192530247238295, 'mean_word_length': 2.6922330097087377, 'pattern_counts': {'":': 0, '<': 5, '<?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': '118212', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1264, 'n_words': 258}