Class: PaperTrailHistory::VersionDecorator
- Inherits:
-
Object
- Object
- PaperTrailHistory::VersionDecorator
- 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.
Instance Attribute Summary collapse
-
#version ⇒ ActiveRecord::Base
readonly
The version that this object decorates.
Class Method Summary collapse
-
.decorate(version) ⇒ VersionDecorator
Decorates a version.
-
.decorate_collection(versions) ⇒ Array<VersionDecorator>
Decorates each version of a collection.
Instance Method Summary collapse
-
#attribute_changes ⇒ Array<Hash>
The attributes that this version changed.
-
#can_restore? ⇒ Boolean
Tells if the engine can restore this version.
-
#changeset ⇒ Hash
Attribute name to a pair of old and new value.
- #created_at ⇒ ActiveSupport::TimeWithZone
-
#event ⇒ String
'create','update'or'destroy'. -
#event_class ⇒ String
The Bootstrap suffix for the colour of the event.
-
#event_label ⇒ String
The name of the event in the current language.
-
#formatted_created_at ⇒ String
The time of the version in the format of the current language.
- #id ⇒ Integer
-
#initialize(version) ⇒ VersionDecorator
constructor
A new instance of VersionDecorator.
-
#item ⇒ ActiveRecord::Base?
Nil when the record is deleted.
-
#item_display_name ⇒ String
A name for the record of this version.
- #item_id ⇒ Integer
- #item_type ⇒ String
-
#object ⇒ String?
The state before the change, as YAML.
-
#object_changes ⇒ String?
The change, as YAML.
- #updated_at ⇒ ActiveSupport::TimeWithZone?
- #whodunnit ⇒ String?
-
#whodunnit_display ⇒ String
The person who made the change.
Constructor Details
#initialize(version) ⇒ VersionDecorator
Returns a new instance of VersionDecorator.
46 47 48 |
# File 'app/models/paper_trail_history/version_decorator.rb', line 46 def initialize(version) @version = version end |
Instance Attribute Details
#version ⇒ ActiveRecord::Base (readonly)
Returns 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.
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.
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_changes ⇒ Array<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.
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.
138 139 140 |
# File 'app/models/paper_trail_history/version_decorator.rb', line 138 def can_restore? version.event != 'create' end |
#changeset ⇒ Hash
Returns 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_at ⇒ 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 |
#event ⇒ String
Returns '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_class ⇒ String
The Bootstrap suffix for the colour of the event.
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_label ⇒ String
The name of the event in the current language.
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_at ⇒ String
The time of the version in the format of the current language.
71 72 73 |
# File 'app/models/paper_trail_history/version_decorator.rb', line 71 def formatted_created_at format_time(version.created_at) end |
#id ⇒ 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 ⇒ ActiveRecord::Base?
Returns 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_name ⇒ String
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.
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_id ⇒ 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_type ⇒ 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 |
#object ⇒ String?
Returns 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_changes ⇒ String?
Returns 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_at ⇒ 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 |
#whodunnit ⇒ 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 |
#whodunnit_display ⇒ String
The person who made the change.
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 |