Module: Changed

Defined in:
lib/changed.rb,
lib/changed/config.rb,
lib/changed/engine.rb,
lib/changed/builder.rb,
lib/changed/version.rb,
lib/changed/auditable.rb,
app/models/changed/audit.rb,
app/models/changed/association.rb,
app/models/changed/application_record.rb

Defined Under Namespace

Modules: Auditable Classes: ApplicationRecord, Association, Audit, Builder, Config, Engine, Field, Relationship

Constant Summary collapse

VERSION =
'2.0.1'.freeze

Class Method Summary collapse

Class Method Details

.changerObject

Access the changer (this value is set as the changer within an audit and defaults to config).



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

def self.changer
  options[:changer] || config.default_changer_proc&.call
end

.changer=(changer) ⇒ Object

Customize the changer (uses a request store to only change lifeycle event).

Attributes

  • changer - A changer to use.

Examples

Changed.changer = User.current


53
54
55
# File 'lib/changed.rb', line 53

def self.changer=(changer)
  options[:changer] = changer
end

.configObject

Access the library configuration.

Examples

Changed.config.default_changer_proc = ->{ User.system }


17
18
19
# File 'lib/changed.rb', line 17

def self.config
  @config ||= Config.new
end

.optionsObject



85
86
87
# File 'lib/changed.rb', line 85

def self.options
  RequestStore.store[OPTIONS_REQUEST_STORE_KEY] ||= {}
end

.options=(options) ⇒ Object



81
82
83
# File 'lib/changed.rb', line 81

def self.options=(options)
  RequestStore.store[OPTIONS_REQUEST_STORE_KEY] = options
end

.perform(options = {}, &block) ⇒ Object

Perform a block with custom override options.

Attributes

  • options - Values for the changer and / or timestamp.

  • block - Some code to run with the new options.

Examples

Changed.perform(changer: User.system, timestamp: 2.hours.ago) do
  widget.name = "Sprocket"
  widget.save!
end


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

def self.perform(options = {}, &block)
  backup = self.options
  self.options = options
  block.call
ensure
  self.options = backup
end

.timestampObject

Access the timestamp (this value is set as the timestamp within an audit and defaults to now).



22
23
24
# File 'lib/changed.rb', line 22

def self.timestamp
  options[:timestamp] || Time.now
end

.timestamp=(timestamp) ⇒ Object

Customize the timestamp (uses a request store to only change lifeycle event).

Attributes

  • timestamp - A timestamp to use.

Examples

Changed.timestamp = 2.hours.ago


35
36
37
# File 'lib/changed.rb', line 35

def self.timestamp=(timestamp)
  options[:timestamp] = timestamp
end