<commit_before># frozen_string_literal: true

class ActiveSet
  module Filtering
    class ActiveRecordStrategy
      def initialize(set, attribute_instruction)
        @set = set
        @attribute_instruction = attribute_instruction
      end

      def execute
        return false unless @set.respond_to? :to_sql

        if execute_where_operation?
          statement = where_operation
        elsif execute_merge_operation?
          statement = merge_operation
        else
          return false
        end

        return false if throws?(ActiveRecord::StatementInvalid) { statement.load }

        statement
      end

      private

      def execute_where_operation?
        return false unless attribute_model
        return false unless attribute_model.respond_to?(:attribute_names)
        return false unless attribute_model.attribute_names.include?(@attribute_instruction.attribute)

        true
      end

      def execute_merge_operation?
        return false unless attribute_model
        return false unless attribute_model.respond_to?(@attribute_instruction.attribute)
        return false if attribute_model.method(@attribute_instruction.attribute).arity.zero?

        true
      end

      def where_operation
        arel_table = Arel::Table.new(attribute_model.table_name)
        arel_column = arel_table[@attribute_instruction.attribute]

        initial_relation
          .where(
            arel_column.send(
              @attribute_instruction.operator(default: 'eq'),<commit_msg>Use the `arel_table` method on the ActiveRecord model, which helps with typecasting<commit_after># frozen_string_literal: true

class ActiveSet
  module Filtering
    class ActiveRecordStrategy
      def initialize(set, attribute_instruction)
        @set = set
        @attribute_instruction = attribute_instruction
      end

      def execute
        return false unless @set.respond_to? :to_sql

        if execute_where_operation?
          statement = where_operation
        elsif execute_merge_operation?
          statement = merge_operation
        else
          return false
        end

        return false if throws?(ActiveRecord::StatementInvalid) { statement.load }

        statement
      end

      private

      def execute_where_operation?
        return false unless attribute_model
        return false unless attribute_model.respond_to?(:attribute_names)
        return false unless attribute_model.attribute_names.include?(@attribute_instruction.attribute)

        true
      end

      def execute_merge_operation?
        return false unless attribute_model
        return false unless attribute_model.respond_to?(@attribute_instruction.attribute)
        return false if attribute_model.method(@attribute_instruction.attribute).arity.zero?

        true
      end

      def where_operation
        arel_column = attribute_model.arel_table[@attribute_instruction.attribute]

        initial_relation
          .where(
            arel_column.send(
              @attribute_instruction.operator(default: 'eq'),
{'fraction_non_alphanumeric': 0.05132396207911082, 'fraction_numerical': 0.0, 'mean_word_length': 2.6778846153846154, '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': '670086', 'n_tokens_mistral': 755, 'n_tokens_neox': 712, 'n_words': 182}