<commit_before>module Martyr
  module Schema
    class QueryLevelDefinition < BaseLevelDefinition

      LABEL_EXPRESSION_ALIAS = 'martyr_label_expression'

      attr_accessor :scope, :primary_key, :label_key, :parent_association_name

      # @param collection [DimensionDefinitionCollection]
      # @param name [String, Symbol]
      # @param scope [Proc]
      # @option primary_key [String]
      # @option label_key [String]
      # @option fact_key [String]
      # @option fact_alias [String]
      def initialize(collection, name, scope = nil, **options)
        @collection = collection
        @scope = scope || default_scope
        super name: name.to_s,
              primary_key: options[:primary_key] || 'id',
              label_key: options[:label_key] || name.to_s,
              label_expression: options[:label_expression],
              fact_key: options[:fact_key] || "#{dimension_name}_#{name}_id",
              fact_alias: options[:fact_alias] || "#{dimension_name}_#{name}_id",
              parent_association_name: options[:parent_association_name]

        add_label_expression_to_scope
      end

      def query?
        true
      end

      def degenerate?
        false
      end

      def parent_association_name_with_default
        (parent_association_name || level_above.try(:name)).to_s.presence
      end

      def label_field
        label_expression ? LABEL_EXPRESSION_ALIAS : label_key
      end

      def build(collection)
        Runtime::QueryLevelScope.new(collection, self)
      end

      # @param record [ActiveRecord::Base]
      def record_value(record)
        record.send(label_field)
      end

      private

      # @return [Proc] a lambda object representing running #all on the guessed-class
      def default_scope
        begin
          klass = dimension_name.classify.constantize
          ->{ klass.all }
        rescue => e<commit_msg>Fixed issue when primary keys of a supported level are nil<commit_after>module Martyr
  module Schema
    class QueryLevelDefinition < BaseLevelDefinition

      LABEL_EXPRESSION_ALIAS = 'martyr_label_expression'

      attr_accessor :scope, :primary_key, :label_key, :parent_association_name

      # @param collection [DimensionDefinitionCollection]
      # @param name [String, Symbol]
      # @param scope [Proc]
      # @option primary_key [String]
      # @option label_key [String]
      # @option fact_key [String]
      # @option fact_alias [String]
      def initialize(collection, name, scope = nil, **options)
        @collection = collection
        @scope = scope || default_scope
        super name: name.to_s,
              primary_key: options[:primary_key] || 'id',
              label_key: options[:label_key] || name.to_s,
              label_expression: options[:label_expression],
              fact_key: options[:fact_key] || "#{dimension_name}_#{name}_id",
              fact_alias: options[:fact_alias] || "#{dimension_name}_#{name}_id",
              parent_association_name: options[:parent_association_name]

        add_label_expression_to_scope
      end

      def query?
        true
      end

      def degenerate?
        false
      end

      def parent_association_name_with_default
        (parent_association_name || level_above.try(:name)).to_s.presence
      end

      def label_field
        label_expression ? LABEL_EXPRESSION_ALIAS : label_key
      end

      def build(collection)
        Runtime::QueryLevelScope.new(collection, self)
      end

      # @param record [ActiveRecord::Base]
      def record_value(record)
        record.try(label_field)
      end

      private

      # @return [Proc] a lambda object representing running #all on the guessed-class
      def default_scope
        begin
          klass = dimension_name.classify.constantize
          ->{ klass.all }
        rescue => e
{'fraction_non_alphanumeric': 0.08558091286307054, 'fraction_numerical': 0.0, 'mean_word_length': 2.56140350877193, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '5396153', 'n_tokens_mistral': 1095, 'n_tokens_neox': 1070, 'n_words': 247}