Module: Sequel::Plugins::Auditer

Defined in:
lib/sequel/plugins/auditer.rb

Overview

Given a Post model with these fields:

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

All fields

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

Single field

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

Multiple fields

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

All fields except certain fields

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

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.configure(model, opts = {}) ⇒ Object

called when



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/sequel/plugins/auditer.rb', line 98

def self.configure(model, opts = {})
  model.instance_eval do
    # add support for :dirty attributes tracking & JSON serializing of data
    plugin(:dirty)
    plugin(:json_serializer)
    plugin(:polymorphic)

    # 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)
    set_additional_info_method(opts)
    set_owner_method(opts)

    set_reference_method(opts)

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

    if only.empty? # except:
      # all columns minus any excepted columns and default ignored columns
      included_columns = [
        [columns - [except].flatten].flatten - @auditer_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
    else
      # 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
    end

    @auditer_included_columns = included_columns
    @auditer_ignored_columns  = excluded_columns

    # each included model will have an associated versions
    one_to_many(
      :versions,
      class: audit_model_name,
      as: 'associated'
    )
  end
end