Class: Secretary::Version

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/secretary/version.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate(object) ⇒ Object

Builds a new version for the passed-in object Passed-in object is a dirty object. Version will be saved when the object is saved.

If you must generate a version manually, this method should be used instead of ‘Version.create`. I didn’t want to override the public ActiveRecord API.



22
23
24
25
26
27
28
29
30
# File 'app/models/secretary/version.rb', line 22

def generate(object)
  changes = object.send(:__versioned_changes)

  object.versions.create({
    :user_id          => object.logged_user_id,
    :description      => generate_description(object, changes.keys),
    :object_changes   => changes
  })
end

Instance Method Details

#attribute_diffsObject

The attribute diffs for this version



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/secretary/version.rb', line 61

def attribute_diffs
  @attribute_diffs ||= begin
    changes           = self.object_changes.dup
    attribute_diffs   = {}

    # Compare each of object_b's attributes to object_a's attributes
    # And if there is a difference, add it to the Diff
    changes.each do |attribute, values|
      # values is [previous_value, new_value]
      diff = Diffy::Diff.new(values[0].to_s, values[1].to_s)
      attribute_diffs[attribute] = diff
    end

    attribute_diffs
  end
end

#titleObject

A simple title for this version. Example: “Article #125 v6”



80
81
82
83
# File 'app/models/secretary/version.rb', line 80

def title
  "#{self.versioned.class.name.titleize} " \
  "##{self.versioned.id} v#{self.version_number}"
end