<commit_before>module Statesman
  module Adapters
    module ActiveRecordQueries
      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods
        def in_state(*states)
          states = states.map(&:to_s)

          joins(transition1_join)
            .joins(transition2_join)
            .where(state_inclusion_where(states), states)
            .where("transition2.id" => nil)
        end

        def not_in_state(*states)
          states = states.map(&:to_s)

          joins(transition1_join)
            .joins(transition2_join)
            .where("NOT (#{state_inclusion_where(states)})", states)
            .where("transition2.id" => nil)
        end

        private

        def transition_class
          raise NotImplementedError, "A transition_class method should be " \
                                     "defined on the model"
        end

        def initial_state
          raise NotImplementedError, "An initial_state method should be " \
                                     "defined on the model"
        end

        def transition_name
          transition_class.table_name.to_sym
        end

        def model_foreign_key
          reflections[transition_name].foreign_key
        end

        def transition1_join
          "LEFT OUTER JOIN #{transition_name} transition1
             ON transition1.#{model_foreign_key} = #{table_name}.id"
        end

        def transition2_join<commit_msg>Use reflect_on_association, rather than referring to reflections directly (https://github.com/rails/rails/issues/16928)<commit_after>module Statesman
  module Adapters
    module ActiveRecordQueries
      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods
        def in_state(*states)
          states = states.map(&:to_s)

          joins(transition1_join)
            .joins(transition2_join)
            .where(state_inclusion_where(states), states)
            .where("transition2.id" => nil)
        end

        def not_in_state(*states)
          states = states.map(&:to_s)

          joins(transition1_join)
            .joins(transition2_join)
            .where("NOT (#{state_inclusion_where(states)})", states)
            .where("transition2.id" => nil)
        end

        private

        def transition_class
          raise NotImplementedError, "A transition_class method should be " \
                                     "defined on the model"
        end

        def initial_state
          raise NotImplementedError, "An initial_state method should be " \
                                     "defined on the model"
        end

        def transition_name
          transition_class.table_name.to_sym
        end

        def model_foreign_key
          reflect_on_association(transition_name).foreign_key
        end

        def transition1_join
          "LEFT OUTER JOIN #{transition_name} transition1
             ON transition1.#{model_foreign_key} = #{table_name}.id"
        end

        def transition2_join
{'fraction_non_alphanumeric': 0.07107762856207009, 'fraction_numerical': 0.008188666885031117, 'mean_word_length': 1.8277777777777777, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6194429', 'n_tokens_mistral': 834, 'n_tokens_neox': 788, 'n_words': 177}