<commit_before>module React
  module DataHelpers
    # This module expects to be included into a controller, so that view_context resolves
    # to something that provides all the various view helpers.

    private

    def search_results_data
      return {
        results: [
          {
            type: 'investigations',
            header: view_context.t(:investigation).pluralize.titleize,
            count: @investigations_count,
            materials: materials_data(@investigations),
            pagination: {
              current_page: @investigations.current_page,
              total_pages: @investigations.total_pages,
              start_item: @investigations.offset + 1,
              end_item: @investigations.offset +  @investigations.length,
              total_items: @investigations.total_entries,
              params: {
                type: 'inv'
              }
            }
          },
          {
            type: 'activities',
            header: view_context.t(:activity).pluralize.titleize,
            count: @activities_count,
            materials: materials_data(@activities),
            pagination: {
              current_page: @activities.current_page,
              total_pages: @activities.total_pages,
              start_item: @activities.offset + 1,
              end_item: @activities.offset +  @activities.length,
              total_items: @activities.total_entries,
              params: {
                type: 'act'
              }
            }
          }
        ]
      }
    end

    def materials_data(materials)
      data = []

      materials.each do |material|
        parent_data = nil

        material_count = material.offerings_count
        if material.parent
          material_count = material_count + material.parent.offerings_count

          parent_data = {
            id: material.parent.id,
            type: view_context.t(:investigation),
            name: material.parent.name
          }
        end

        has_activities = material.respond_to?(:activities) && !material.activities.nil?
        has_pretest = material.respond_to?(:has_pretest) && material.has_pretest

        user_data = nil
        if material.user && (!material.user.name.nil?)
          user_data = {
            id: material.user.id,
            name: material.user.name
          }
        end

        mat_data = {
          id: material.id,
          name: material.name,
          description: (current_visitor.portal_teacher && material.description_for_teacher.present? ? view_context.sanitize(material.description_for_teacher) : view_context.sanitize(material.description)),
          class_name_underscored: material.class.name.underscore,
          icon: {
            url: (material.respond_to?(:icon_image) ? material.icon_image : nil),
          },
          java_requirements: material.java_requirements,
          is_official: material.is_official,
          links: links_for_material(material),
          assigned_classes: assigned_clazz_names(material),
          class_count: material_count,
          sensors: view_context.probe_types(material).map { |p| p.name },
          has_activities: has_activities,
          has_pretest: has_pretest,
          activities: material.activities.map{|a| {id: a.id, name: a.name} },
          parent: parent_data,
          user: user_data
        }<commit_msg>Fix React data helper<commit_after>module React
  module DataHelpers
    # This module expects to be included into a controller, so that view_context resolves
    # to something that provides all the various view helpers.

    private

    def search_results_data
      return {
        results: [
          {
            type: 'investigations',
            header: view_context.t(:investigation).pluralize.titleize,
            count: @investigations_count,
            materials: materials_data(@investigations),
            pagination: {
              current_page: @investigations.current_page,
              total_pages: @investigations.total_pages,
              start_item: @investigations.offset + 1,
              end_item: @investigations.offset +  @investigations.length,
              total_items: @investigations.total_entries,
              params: {
                type: 'inv'
              }
            }
          },
          {
            type: 'activities',
            header: view_context.t(:activity).pluralize.titleize,
            count: @activities_count,
            materials: materials_data(@activities),
            pagination: {
              current_page: @activities.current_page,
              total_pages: @activities.total_pages,
              start_item: @activities.offset + 1,
              end_item: @activities.offset +  @activities.length,
              total_items: @activities.total_entries,
              params: {
                type: 'act'
              }
            }
          }
        ]
      }
    end

    def materials_data(materials)
      data = []

      materials.each do |material|
        parent_data = nil

        material_count = material.offerings_count
        if material.parent
          material_count = material_count + material.parent.offerings_count

          parent_data = {
            id: material.parent.id,
            type: view_context.t(:investigation),
            name: material.parent.name
          }
        end

        has_activities = material.respond_to?(:activities) && !material.activities.nil?
        has_pretest = material.respond_to?(:has_pretest) && material.has_pretest

        user_data = nil
        if material.user && (!material.user.name.nil?)
          user_data = {
            id: material.user.id,
            name: material.user.name
          }
        end

        mat_data = {
          id: material.id,
          name: material.name,
          description: (current_visitor.portal_teacher && material.description_for_teacher.present? ? view_context.sanitize(material.description_for_teacher) : view_context.sanitize(material.description)),
          class_name_underscored: material.class.name.underscore,
          icon: {
            url: (material.respond_to?(:icon_image) ? material.icon_image : nil),
          },
          java_requirements: material.java_requirements,
          is_official: material.is_official,
          links: links_for_material(material),
          assigned_classes: assigned_clazz_names(material),
          class_count: material_count,
          sensors: view_context.probe_types(material).map { |p| p.name },
          has_activities: has_activities,
          has_pretest: has_pretest,
          activities: has_activities ? material.activities.map{|a| {id: a.id, name: a.name} } : [],
          parent: parent_data,
          user: user_data
        }
{'fraction_non_alphanumeric': 0.08800945067926758, 'fraction_numerical': 0.0005906674542232723, 'mean_word_length': 1.932034632034632, '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': '2234210', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1815, 'n_words': 334}