Class: VestalVersions::Version
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- VestalVersions::Version
- Includes:
- ActiveSupport::Configurable, Comparable
- Defined in:
- lib/vestal_versions/version.rb
Overview
The ActiveRecord model representing versions.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
In conjunction with the included Comparable module, allows comparison of version records based on their corresponding version numbers, creation timestamps and IDs.
- #changes ⇒ Object
-
#initial? ⇒ Boolean
Returns whether the version has a version number of 1.
-
#original_number ⇒ Object
Returns the original version number that this version was.
- #restore ⇒ Object
- #restore! ⇒ Object
Instance Method Details
#<=>(other) ⇒ Object
In conjunction with the included Comparable module, allows comparison of version records based on their corresponding version numbers, creation timestamps and IDs.
29 30 31 |
# File 'lib/vestal_versions/version.rb', line 29 def <=>(other) [number, created_at, id].map(&:to_i) <=> [other.number, other.created_at, other.id].map(&:to_i) end |
#changes ⇒ Object
22 23 24 |
# File 'lib/vestal_versions/version.rb', line 22 def changes self[:modifications] end |
#initial? ⇒ Boolean
Returns whether the version has a version number of 1. Useful when deciding whether to ignore the version during reversion, as initial versions have no serialized changes attached. Helps maintain backwards compatibility.
36 37 38 |
# File 'lib/vestal_versions/version.rb', line 36 def initial? number == 1 end |
#original_number ⇒ Object
Returns the original version number that this version was.
41 42 43 44 45 46 47 48 |
# File 'lib/vestal_versions/version.rb', line 41 def original_number if reverted_from.nil? number else version = versioned.versions.at(reverted_from) version.nil? ? 1 : version.original_number end end |
#restore ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/vestal_versions/version.rb', line 61 def restore if tag == 'deleted' attrs = modifications class_name = attrs['type'].blank? ? versioned_type : attrs['type'] klass = class_name.constantize model = klass.new attrs.each do |k, v| begin model.send "#{k}=", v rescue NoMethodError logger.warn "Attribute #{k} does not exist on #{class_name} (Version id: #{id})." rescue nil end end model else latest_version = self.class.where(:versioned_id => versioned_id, :versioned_type => versioned_type, :tag => 'deleted').first latest_version.nil? ? nil : latest_version.restore end end |
#restore! ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/vestal_versions/version.rb', line 50 def restore! model = restore if model model.save! destroy end model end |