Class: PaperTrailHistory::VersionDecorator

Inherits:
Object
  • Object
show all
Defined in:
app/models/paper_trail_history/version_decorator.rb

Overview

Prepares one PaperTrail version for the interface: translated labels, dates in the format of the current language, and a list of changed attributes that hides the values which the host application filters.

The decorator passes the usual reader methods of a version through, thus it can stand in the place of the version itself in a view.

Examples:

decorated = PaperTrailHistory::VersionDecorator.decorate(version)
decorated.event_label    # => "Updated"
decorated.attribute_changes
# => [{ attribute: 'name', old_value: 'old', new_value: 'new' }]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ VersionDecorator

Returns a new instance of VersionDecorator.

Parameters:

  • version (ActiveRecord::Base)

    a PaperTrail version



46
47
48
# File 'app/models/paper_trail_history/version_decorator.rb', line 46

def initialize(version)
  @version = version
end

Instance Attribute Details

#versionActiveRecord::Base (readonly)

Returns the version that this object decorates.

Returns:

  • (ActiveRecord::Base)

    the version that this object decorates



18
19
20
# File 'app/models/paper_trail_history/version_decorator.rb', line 18

def version
  @version
end

Class Method Details

.decorate(version) ⇒ VersionDecorator

Decorates a version. A version that is already decorated stays as it is.

Parameters:

Returns:



54
55
56
57
58
# File 'app/models/paper_trail_history/version_decorator.rb', line 54

def self.decorate(version)
  return version if version.is_a?(VersionDecorator)

  new(version)
end

.decorate_collection(versions) ⇒ Array<VersionDecorator>

Decorates each version of a collection.

Parameters:

  • versions (Enumerable)

Returns:



64
65
66
# File 'app/models/paper_trail_history/version_decorator.rb', line 64

def self.decorate_collection(versions)
  versions.map { |version| decorate(version) }
end

Instance Method Details

#attribute_changesArray<Hash>

The attributes that this version changed.

An attribute that the host application filters gets a placeholder for both values, thus a password digest or an API token does not appear.

The name is not changed_attributes. ActiveModel gives that name to a method with a different result, and the decorator passes methods of the version through, thus the two would be easy to mix up.

Returns:

  • (Array<Hash>)

    each entry has :attribute, :old_value and :new_value



127
128
129
130
131
132
133
# File 'app/models/paper_trail_history/version_decorator.rb', line 127

def attribute_changes
  return [] unless changeset

  changeset.map do |attr, (old_val, new_val)|
    build_change(attr, old_val, new_val)
  end
end

#can_restore?Boolean

Tells if the engine can restore this version.

Returns:

  • (Boolean)

    false for the version that created the record



138
139
140
# File 'app/models/paper_trail_history/version_decorator.rb', line 138

def can_restore?
  version.event != 'create'
end

#changesetHash

Returns attribute name to a pair of old and new value.

Returns:

  • (Hash)

    attribute name to a pair of old and new value



42
43
# File 'app/models/paper_trail_history/version_decorator.rb', line 42

delegate :id, :event, :item_type, :item_id, :whodunnit, :object, :object_changes,
:created_at, :updated_at, :item, :changeset, to: :version

#created_atActiveSupport::TimeWithZone

Returns:

  • (ActiveSupport::TimeWithZone)


42
43
# File 'app/models/paper_trail_history/version_decorator.rb', line 42

delegate :id, :event, :item_type, :item_id, :whodunnit, :object, :object_changes,
:created_at, :updated_at, :item, :changeset, to: :version

#eventString

Returns 'create', 'update' or 'destroy'.

Returns:

  • (String)

    'create', 'update' or 'destroy'



42
43
# File 'app/models/paper_trail_history/version_decorator.rb', line 42

delegate :id, :event, :item_type, :item_id, :whodunnit, :object, :object_changes,
:created_at, :updated_at, :item, :changeset, to: :version

#event_classString

The Bootstrap suffix for the colour of the event.

Returns:

  • (String)

    'success', 'warning', 'danger' or 'info'



95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/paper_trail_history/version_decorator.rb', line 95

def event_class
  case version.event
  when 'create'
    'success'
  when 'update'
    'warning'
  when 'destroy'
    'danger'
  else
    'info'
  end
end

#event_labelString

The name of the event in the current language.

Returns:

  • (String)

    the translated name, or the event itself for an event that this engine does not know



79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/paper_trail_history/version_decorator.rb', line 79

def event_label
  case version.event
  when 'create'
    I18n.t('paper_trail_history.events.created')
  when 'update'
    I18n.t('paper_trail_history.events.updated')
  when 'destroy'
    I18n.t('paper_trail_history.events.deleted')
  else
    version.event.humanize
  end
end

#formatted_created_atString

The time of the version in the format of the current language.

Returns:

  • (String)


71
72
73
# File 'app/models/paper_trail_history/version_decorator.rb', line 71

def formatted_created_at
  format_time(version.created_at)
end

#idInteger

Returns:

  • (Integer)


42
43
# File 'app/models/paper_trail_history/version_decorator.rb', line 42

delegate :id, :event, :item_type, :item_id, :whodunnit, :object, :object_changes,
:created_at, :updated_at, :item, :changeset, to: :version

#itemActiveRecord::Base?

Returns nil when the record is deleted.

Returns:

  • (ActiveRecord::Base, nil)

    nil when the record is deleted



42
43
# File 'app/models/paper_trail_history/version_decorator.rb', line 42

delegate :id, :event, :item_type, :item_id, :whodunnit, :object, :object_changes,
:created_at, :updated_at, :item, :changeset, to: :version

#item_display_nameString

A name for the record of this version.

The method takes the name or the title of the record when it has one, and falls back to the type and the ID.

Returns:

  • (String)


148
149
150
151
152
# File 'app/models/paper_trail_history/version_decorator.rb', line 148

def item_display_name
  return deleted_item_name unless version.item

  version.item.try(:name) || version.item.try(:title) || item_reference
end

#item_idInteger

Returns:

  • (Integer)


42
43
# File 'app/models/paper_trail_history/version_decorator.rb', line 42

delegate :id, :event, :item_type, :item_id, :whodunnit, :object, :object_changes,
:created_at, :updated_at, :item, :changeset, to: :version

#item_typeString

Returns:

  • (String)


42
43
# File 'app/models/paper_trail_history/version_decorator.rb', line 42

delegate :id, :event, :item_type, :item_id, :whodunnit, :object, :object_changes,
:created_at, :updated_at, :item, :changeset, to: :version

#objectString?

Returns the state before the change, as YAML.

Returns:

  • (String, nil)

    the state before the change, as YAML



42
43
# File 'app/models/paper_trail_history/version_decorator.rb', line 42

delegate :id, :event, :item_type, :item_id, :whodunnit, :object, :object_changes,
:created_at, :updated_at, :item, :changeset, to: :version

#object_changesString?

Returns the change, as YAML.

Returns:

  • (String, nil)

    the change, as YAML



42
43
# File 'app/models/paper_trail_history/version_decorator.rb', line 42

delegate :id, :event, :item_type, :item_id, :whodunnit, :object, :object_changes,
:created_at, :updated_at, :item, :changeset, to: :version

#updated_atActiveSupport::TimeWithZone?

Returns:

  • (ActiveSupport::TimeWithZone, nil)


42
43
# File 'app/models/paper_trail_history/version_decorator.rb', line 42

delegate :id, :event, :item_type, :item_id, :whodunnit, :object, :object_changes,
:created_at, :updated_at, :item, :changeset, to: :version

#whodunnitString?

Returns:

  • (String, nil)


42
43
# File 'app/models/paper_trail_history/version_decorator.rb', line 42

delegate :id, :event, :item_type, :item_id, :whodunnit, :object, :object_changes,
:created_at, :updated_at, :item, :changeset, to: :version

#whodunnit_displayString

The person who made the change.

Returns:

  • (String)

    the whodunnit value, or the word for the system when PaperTrail stored no value



112
113
114
# File 'app/models/paper_trail_history/version_decorator.rb', line 112

def whodunnit_display
  version.whodunnit || I18n.t('paper_trail_history.actors.system')
end