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
24
# 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 = {}
  @format_callbacks = []
  @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

#format_callbacksObject (readonly)

Returns the value of attribute format_callbacks.



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

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



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

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

#on_format(&block) ⇒ Object



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

def on_format(&block)
  @mutex.synchronize { @format_callbacks << block }
end

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

Yields:

  • (model_config)


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

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



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

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

#register_resolver(type, klass) ⇒ Object



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

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

#resolve_item_name(version) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/paper_trail/human/configuration.rb', line 58

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



52
53
54
55
56
# File 'lib/paper_trail/human/configuration.rb', line 52

def resolve_whodunnit(id)
  return id unless whodunnit_resolver

  whodunnit_resolver.call(id)
end