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
22
23
# 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
  @custom_resolvers = {}
  @custom_formatters = {}
  @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

#custom_formattersObject (readonly)

Returns the value of attribute custom_formatters.



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

def custom_formatters
  @custom_formatters
end

#custom_resolversObject (readonly)

Returns the value of attribute custom_resolvers.



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

def custom_resolvers
  @custom_resolvers
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



43
44
45
# File 'lib/paper_trail/human/configuration.rb', line 43

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

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

Yields:

  • (model_config)


29
30
31
32
33
# File 'lib/paper_trail/human/configuration.rb', line 29

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

#register_formatter(type, klass) ⇒ Object



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

def register_formatter(type, klass)
  @mutex.synchronize { @custom_formatters[type.to_sym] = klass }
end

#register_resolver(type, klass) ⇒ Object



35
36
37
# File 'lib/paper_trail/human/configuration.rb', line 35

def register_resolver(type, klass)
  @mutex.synchronize { @custom_resolvers[type.to_sym] = klass }
end

#resolve_item_name(version) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/paper_trail/human/configuration.rb', line 53

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



47
48
49
50
51
# File 'lib/paper_trail/human/configuration.rb', line 47

def resolve_whodunnit(id)
  return id unless whodunnit_resolver

  whodunnit_resolver.call(id)
end