Class: PaperTrailHistory::VersionDecorator

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

Overview

Decorator for PaperTrail::Version objects providing formatted display methods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ VersionDecorator

Returns a new instance of VersionDecorator.



12
13
14
# File 'app/models/paper_trail_history/version_decorator.rb', line 12

def initialize(version)
  @version = version
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



6
7
8
# File 'app/models/paper_trail_history/version_decorator.rb', line 6

def version
  @version
end

Class Method Details

.decorate(version) ⇒ Object



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

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

  new(version)
end

.decorate_collection(versions) ⇒ Object



22
23
24
# File 'app/models/paper_trail_history/version_decorator.rb', line 22

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

Instance Method Details

#can_restore?Boolean

Returns:

  • (Boolean)


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

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

#changed_attributesObject



60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/paper_trail_history/version_decorator.rb', line 60

def changed_attributes
  return [] unless changeset

  changeset.map do |attr, (old_val, new_val)|
    {
      attribute: attr,
      old_value: format_value(old_val),
      new_value: format_value(new_val)
    }
  end
end

#event_classObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/paper_trail_history/version_decorator.rb', line 43

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

#event_labelObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/paper_trail_history/version_decorator.rb', line 30

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_atObject



26
27
28
# File 'app/models/paper_trail_history/version_decorator.rb', line 26

def formatted_created_at
  version.created_at.strftime('%B %d, %Y at %I:%M %p')
end

#item_display_nameObject



76
77
78
79
80
81
82
# File 'app/models/paper_trail_history/version_decorator.rb', line 76

def item_display_name
  if version.item
    version.item.try(:name) || version.item.try(:title) || "#{version.item_type} ##{version.item_id}"
  else
    "#{version.item_type} ##{version.item_id} (deleted)"
  end
end

#whodunnit_displayObject



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

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