Module: Sequel::Plugins::Audited

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

Overview

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

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

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

called when



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sequel/plugins/audited.rb', line 71

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_reference_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

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

  end


end