Sha256: 8739191084d7bbc5a815674b9b227160c43c6718ab7ff628cb896ccfda48c0ce

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require "sequel/plugins/bulk_audit/version"
require 'sequel/model'

module Sequel
  module Plugins
    module BulkAudit
      def self.apply(model, opts={})
        model.instance_eval do
          @excluded_columns = [*opts[:excluded_columns]]
        end
      end

      module SharedMethods
        def model_to_table_map
          @@model_to_table_map ||= ObjectSpace.each_object(Class).select do |klazz|
            next if klazz.name.nil?
            klazz < Sequel::Model && klazz&.plugins&.include?(Sequel::Plugins::BulkAudit)
          end.map { |c| [c.to_s, c.table_name] }.to_h.invert
        end

        def with_current_user(current_user, attributes = nil)
          self.db.transaction do
            trid = self.db.select(Sequel.function(:txid_current)).single_value
            data = self.db.select(Sequel.expr(current_user&.id || 0).as(:user_id),
                             Sequel.cast(current_user&.login || "unspecified", :text).as(:username),
                             Sequel.pg_jsonb(model_to_table_map).as(:model_map),
                             Sequel.pg_jsonb(attributes || {}).as(:data))
            self.db.create_table!(:"__audit_info_#{trid}", temp: true, as: data)
            result = yield if block_given?
            self.db.drop_table?(:"__audit_info_#{trid}")
            result
          end
        end
      end

      module ClassMethods
        include SharedMethods
      end

      module InstanceMethods
        include SharedMethods
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sequel-bulk-audit-0.2.0 lib/sequel/plugins/bulk_audit.rb