<commit_before># frozen_string_literal: true

module Beholders
  require 'rails'

  class Railtie < Rails::Railtie
    initializer 'beholders.add_to_active_record' do
      ActiveSupport.on_load :active_record do
        ActiveRecord::Base.send(:extend, CallbackIntegration)
      end
    end
  end

  module CallbackIntegration
    # Pass class name as string not class, for same reason rails 5.1 deprecates
    # the latter:
    # https://github.com/rails/rails/blob/5-1-stable/activerecord/CHANGELOG.md
    def observed_by(observer)
      instance_eval do
        after_create -> { observer.constantize.trigger(:after_create, self) }
        after_update -> { observer.constantize.trigger(:after_update, self) }
        after_destroy -> { observer.constantize.trigger(:after_destroy, self) }
        after_commit -> { observer.constantize.trigger(:after_commit, self) }
        after_create_commit -> { observer.constantize.trigger(:after_create_commit, self) }
        after_update_commit -> { observer.constantize.trigger(:after_update_commit, self) }
        after_destroy_commit -> { observer.constantize.trigger(:after_destroy_commit, self) }
      end
    end
  end

end<commit_msg>Add after_save support, for easier transition from rails-observers<commit_after># frozen_string_literal: true

module Beholders
  require 'rails'

  class Railtie < Rails::Railtie
    initializer 'beholders.add_to_active_record' do
      ActiveSupport.on_load :active_record do
        ActiveRecord::Base.send(:extend, CallbackIntegration)
      end
    end
  end

  module CallbackIntegration
    BEHOLDER_CALLBACKS = %i[
      after_save
      after_create
      after_update
      after_destroy
      after_commit
      after_create_commit
      after_update_commit
      after_destroy_commit
    ].freeze

    # Pass class name as string not class, for same reason rails 5.1 deprecates
    # the latter:
    # https://github.com/rails/rails/blob/5-1-stable/activerecord/CHANGELOG.md
    def observed_by(observer)
      instance_eval do
        BEHOLDER_CALLBACKS.each do |cb|
          send cb, -> { observer.constantize.trigger(cb, self) }
        end
      end
    end
  end

end
{'fraction_non_alphanumeric': 0.08088235294117647, 'fraction_numerical': 0.003676470588235294, 'mean_word_length': 3.1946050096339116, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6684746', 'n_tokens_mistral': 668, 'n_tokens_neox': 640, 'n_words': 140}