Class: PaperTrail::Human::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/paper_trail/human/configuration.rb

Constant Summary collapse

DEFAULT_IGNORED_FIELDS =
%w[id created_at updated_at].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



12
13
14
15
16
17
18
19
20
21
# File 'lib/paper_trail/human/configuration.rb', line 12

def initialize
  @model_configs = {}
  @ignored_fields = DEFAULT_IGNORED_FIELDS.dup
  @whodunnit_resolver = nil
  @field_name_resolver = nil
  @translate_events = false
  @extend_version_model = false
  @after_format = nil
  @mutex = Mutex.new
end

Instance Attribute Details

#after_formatObject

Returns the value of attribute after_format.



8
9
10
# File 'lib/paper_trail/human/configuration.rb', line 8

def after_format
  @after_format
end

#extend_version_modelObject

Returns the value of attribute extend_version_model.



8
9
10
# File 'lib/paper_trail/human/configuration.rb', line 8

def extend_version_model
  @extend_version_model
end

#field_name_resolverObject

Returns the value of attribute field_name_resolver.



8
9
10
# File 'lib/paper_trail/human/configuration.rb', line 8

def field_name_resolver
  @field_name_resolver
end

#ignored_fieldsObject

Returns the value of attribute ignored_fields.



10
11
12
# File 'lib/paper_trail/human/configuration.rb', line 10

def ignored_fields
  @ignored_fields
end

#translate_eventsObject

Returns the value of attribute translate_events.



8
9
10
# File 'lib/paper_trail/human/configuration.rb', line 8

def translate_events
  @translate_events
end

#whodunnit_resolverObject

Returns the value of attribute whodunnit_resolver.



8
9
10
# File 'lib/paper_trail/human/configuration.rb', line 8

def whodunnit_resolver
  @whodunnit_resolver
end

Instance Method Details

#config_for(model_name) ⇒ Object



33
34
35
# File 'lib/paper_trail/human/configuration.rb', line 33

def config_for(model_name)
  @model_configs[model_name.to_s]
end

#register(model_name) {|model_config| ... } ⇒ Object

Yields:

  • (model_config)


27
28
29
30
31
# File 'lib/paper_trail/human/configuration.rb', line 27

def register(model_name, &)
  model_config = ModelConfig.new
  yield(model_config)
  @mutex.synchronize { @model_configs[model_name.to_s] = model_config.freeze }
end

#resolve_item_name(version) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/paper_trail/human/configuration.rb', line 43

def resolve_item_name(version)
  model_config = config_for(version.item_type)
  return nil unless model_config&.item_name_attribute

  attr = model_config.item_name_attribute
  return attr.call(version) if attr.respond_to?(:call)

  item = find_item(version)
  item&.public_send(attr)
rescue NoMethodError, ActiveRecord::RecordNotFound
  nil
end

#resolve_whodunnit(id) ⇒ Object



37
38
39
40
41
# File 'lib/paper_trail/human/configuration.rb', line 37

def resolve_whodunnit(id)
  return id unless whodunnit_resolver

  whodunnit_resolver.call(id)
end