<commit_before>module ActiveRecord
  module ActsAs
    module Relation
      extend ActiveSupport::Concern

      module ClassMethods
        def acts_as(name, scope = nil, options = {})
          options, scope = scope, nil if Hash === scope

          association_method = options.delete(:association_method)
          touch              = options.delete(:touch)
          as                 = options.delete(:as) || :actable
          validates_actable  = !options.key?(:validates_actable) || options.delete(:validates_actable)

          options = options.reverse_merge(as: as, validate: false, autosave: true, inverse_of: as)

          reflections = has_one(name, scope, **options)
          default_scope -> {
            case association_method
              when :eager_load
                eager_load(name)
              when :joins
                joins(name)
              else
                includes(name)
            end
          }
          validate :actable_must_be_valid if validates_actable

          unless touch == false
            after_update :touch, if: ActiveRecord.version.to_s.to_f >= 5.1 ? :saved_changes? : :changed?
          end

          before_save do
            @_acting_as_changed = ActiveRecord.version.to_s.to_f >= 5.1 ? acting_as.has_changes_to_save? : acting_as.changed?
            true
          end
          after_commit do
            @_acting_as_changed = nil
          end
          # Workaround for https://github.com/rails/rails/issues/13609
          after_destroy do
            acting_as.destroy if acting_as && !acting_as.destroyed?
          end

          cattr_reader(:acting_as_reflection) { reflections.stringify_keys[name.to_s] }
          cattr_reader(:acting_as_name) { name.to_s }
          cattr_reader(:acting_as_model) { (options[:class_name] || name.to_s.camelize).constantize }
          class_eval "def #{name}; super || build_#{name}(acting_as_model.actable_reflection.name => self); end"
          alias_method :acting_as, name
          alias_method :acting_as=, "#{name}=".to_sym

          include ActsAs::InstanceMethods
          singleton_class.module_eval do
            include ActsAs::ClassMethods
          end
        end

        def acting_as?(other = nil)
          if respond_to?(:acting_as_reflection) &&
              acting_as_reflection.is_a?(ActiveRecord::Reflection::AssociationReflection)
            case other
            when Class
              acting_as_reflection.class_name == other.to_s
            when Symbol, String
              acting_as_reflection.class_name.underscore == other.to_s
            when NilClass
              true
            end
          else
            false
          end
        end

        def is_a?(klass)
          super || acting_as?(klass)
        end

        def actable(scope = nil, **options)
          name = options.delete(:as) || :actable

          reflections = belongs_to(name, scope, options.reverse_merge(validate: false,
                                                                      polymorphic: true,
                                                                      dependent: :destroy,<commit_msg>Fix ruby 2.7 warning, again.. sorry.<commit_after>module ActiveRecord
  module ActsAs
    module Relation
      extend ActiveSupport::Concern

      module ClassMethods
        def acts_as(name, scope = nil, options = {})
          options, scope = scope, nil if Hash === scope

          association_method = options.delete(:association_method)
          touch              = options.delete(:touch)
          as                 = options.delete(:as) || :actable
          validates_actable  = !options.key?(:validates_actable) || options.delete(:validates_actable)

          options = options.reverse_merge(as: as, validate: false, autosave: true, inverse_of: as)

          reflections = has_one(name, scope, **options)
          default_scope -> {
            case association_method
              when :eager_load
                eager_load(name)
              when :joins
                joins(name)
              else
                includes(name)
            end
          }
          validate :actable_must_be_valid if validates_actable

          unless touch == false
            after_update :touch, if: ActiveRecord.version.to_s.to_f >= 5.1 ? :saved_changes? : :changed?
          end

          before_save do
            @_acting_as_changed = ActiveRecord.version.to_s.to_f >= 5.1 ? acting_as.has_changes_to_save? : acting_as.changed?
            true
          end
          after_commit do
            @_acting_as_changed = nil
          end
          # Workaround for https://github.com/rails/rails/issues/13609
          after_destroy do
            acting_as.destroy if acting_as && !acting_as.destroyed?
          end

          cattr_reader(:acting_as_reflection) { reflections.stringify_keys[name.to_s] }
          cattr_reader(:acting_as_name) { name.to_s }
          cattr_reader(:acting_as_model) { (options[:class_name] || name.to_s.camelize).constantize }
          class_eval "def #{name}; super || build_#{name}(acting_as_model.actable_reflection.name => self); end"
          alias_method :acting_as, name
          alias_method :acting_as=, "#{name}=".to_sym

          include ActsAs::InstanceMethods
          singleton_class.module_eval do
            include ActsAs::ClassMethods
          end
        end

        def acting_as?(other = nil)
          if respond_to?(:acting_as_reflection) &&
              acting_as_reflection.is_a?(ActiveRecord::Reflection::AssociationReflection)
            case other
            when Class
              acting_as_reflection.class_name == other.to_s
            when Symbol, String
              acting_as_reflection.class_name.underscore == other.to_s
            when NilClass
              true
            end
          else
            false
          end
        end

        def is_a?(klass)
          super || acting_as?(klass)
        end

        def actable(scope = nil, **options)
          name = options.delete(:as) || :actable

          reflections = belongs_to(name, scope, **options.reverse_merge(validate: false,
                                                                      polymorphic: true,
                                                                      dependent: :destroy,
{'fraction_non_alphanumeric': 0.08203432530310187, 'fraction_numerical': 0.00314911037631869, 'mean_word_length': 1.7075873827791985, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3208028', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1664, 'n_words': 344}