Module: FlipperTrail

Defined in:
lib/flipper_trail.rb,
lib/flipper_trail/actor.rb,
lib/flipper_trail/entry.rb,
lib/flipper_trail/adapter.rb,
lib/flipper_trail/current.rb,
lib/flipper_trail/railtie.rb,
lib/flipper_trail/version.rb,
lib/flipper_trail/recorder.rb,
lib/flipper_trail/middleware.rb,
lib/flipper_trail/configuration.rb,
lib/flipper_trail/storage/mongoid.rb,
lib/flipper_trail/storage/active_record.rb,
lib/flipper_trail/generators/flipper_trail/install_generator.rb

Overview

Records an append-only audit trail of Flipper feature-flag changes — who changed what, when, and the before/after state — via an adapter decorator. FlipperTrail.configure it once, then query it with FlipperTrail.history.

Defined Under Namespace

Modules: Generators, Storage Classes: Actor, Adapter, Configuration, Current, Entry, Middleware, Railtie, Recorder

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.configurationConfiguration

The memoized global configuration.

Returns:



20
21
22
# File 'lib/flipper_trail.rb', line 20

def configuration
  @configuration ||= Configuration.new
end

.configure {|config| ... } ⇒ void

This method returns an undefined value.

Configures the gem by yielding the global Configuration.

Examples:

FlipperTrail.configure do |config|
  config.storage = :active_record
  config.actor_resolver = -> { Current.user }
end

Yield Parameters:



54
55
56
# File 'lib/flipper_trail.rb', line 54

def configure
  yield configuration
end

.current_actorActor?

The actor the recorder would attribute a change to right now.

Returns:

  • (Actor, nil)

    the resolved actor, or nil when none is set



83
84
85
# File 'lib/flipper_trail.rb', line 83

def current_actor
  recorder.current_actor
end

.history(**filters) ⇒ Array<Entry>

Queries the audit trail through the configured storage backend.

Examples:

FlipperTrail.history(feature: "search", limit: 10)

Parameters:

  • filters (Hash)

    optional filters

Options Hash (**filters):

  • :feature (String)

    only entries for this feature key

  • :actor_id (String)

    only entries by this actor id

  • :since (Time)

    only entries created at or after this time

  • :until (Time)

    only entries created at or before this time

  • :limit (Integer)

    maximum number of entries (default 100)

Returns:

  • (Array<Entry>)

    the matching audit entries, newest first



70
71
72
73
74
75
76
77
78
# File 'lib/flipper_trail.rb', line 70

def history(**filters)
  configuration.storage_backend.query(
    feature: filters[:feature],
    actor_id: filters[:actor_id],
    since: filters[:since],
    until_time: filters[:until],
    limit: filters[:limit] || 100
  )
end

.recorderRecorder

The memoized global recorder that builds and persists audit entries.

Returns:

  • (Recorder)

    the shared recorder instance



27
28
29
# File 'lib/flipper_trail.rb', line 27

def recorder
  @recorder ||= Recorder.new
end

.reset!void

This method returns an undefined value.

Resets the memoized configuration, recorder, and per-request actor.



90
91
92
93
94
# File 'lib/flipper_trail.rb', line 90

def reset!
  @configuration = nil
  @recorder = nil
  Current.reset if defined?(Current)
end

.wrap(adapter, recorder: nil) ⇒ FlipperTrail::Adapter

Wrap a Flipper adapter so every write through it is audited.

Shorthand for FlipperTrail::Adapter.new(adapter); pass the result to Flipper’s config.adapter.

Examples:

Flipper.configure { |c| c.adapter { FlipperTrail.wrap(Flipper::Adapters::ActiveRecord.new) } }

Parameters:

  • adapter (Object)

    the real Flipper adapter to decorate

  • recorder (FlipperTrail::Recorder, nil) (defaults to: nil)

    optional recorder override (mainly for tests)

Returns:



40
41
42
# File 'lib/flipper_trail.rb', line 40

def wrap(adapter, recorder: nil)
  Adapter.new(adapter, recorder: recorder)
end