module Sequel::Plugins::Audited

Given a Post model with these fields:

[:id, :category_id, :title, :body, :author_id, :created_at, :updated_at]

All fields

plugin :audited
  #=> [:category_id, :title, :body, :author_id]  # NB! excluding @default_ignore_attrs
  #=> [:id, :created_at, :updated_at]

Single field

plugin :audited, only: :title
plugin :audited, only: [:title]
  #=> [:title]
  #+> [:id, :category_id, :body, :author_id, :created_at, :updated_at] # ignored fields

Multiple fields

plugin :audited, only: [:title, :body]
  #=> [:title, :body] # tracked fields
  #=> [:id, :category_id, :author_id, :created_at, :updated_at] # ignored fields

All fields except certain fields

plugin :audited, except: :title
plugin :audited, except: [:title]
  #=> [:id, :category_id, :author_id, :created_at, :updated_at] # tracked fields
  #=> [:title] # ignored fields

Public Class Methods

configure(model, opts = {}) click to toggle source

called by the model it is included into:

Post.plugin :audited
# File lib/sequel/plugins/audited.rb, line 74
def self.configure(model, opts = {})
  model.instance_eval do
    # add support for :dirty attributes tracking
    plugin(:dirty)

    # set the default ignored columns or revert to defaults
    set_default_ignored_columns(opts)
    # sets the name of the current User method or revert to default: :current_user
    # specifically for the audited model on a per model basis
    set_user_method(opts)

    only    = opts.fetch(:only, [])
    except  = opts.fetch(:except, [])

    unless only.empty?
      # we should only track the provided column
      included_columns = [only].flatten
      # subtract the 'only' columns from all columns to get excluded_columns
      excluded_columns = columns - included_columns
    else # except:
      # all columns minus any excepted columns and default ignored columns
      included_columns = [
        [columns - [except].flatten].flatten - @audited_default_ignored_columns
      ].flatten.uniq

      # except_columns = except.empty? ? [] : [except].flatten
      excluded_columns = [columns - included_columns].flatten.uniq
      # excluded_columns = [columns - [except_columns, included_columns].flatten].flatten.uniq
    end

    @audited_included_columns = included_columns
    @audited_ignored_columns  = excluded_columns

    # create versions association
    one_to_many :versions, class: audit_model_name, key: :item_uuid, primary_key: :id
  end # /.instance_eval
end