Module: Revisions::InstanceMethods
- Defined in:
- lib/revisions.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#apply_revision(revision = nil) ⇒ Object
maps a revision’s changes onto the main object.
- #apply_revision!(revision = nil) ⇒ Object
- #draft? ⇒ Boolean
- #latest_revision ⇒ Object
- #pending_revisions? ⇒ Boolean
- #published? ⇒ Boolean
- #revision? ⇒ Boolean
- #save_revision ⇒ Object
- #save_revision! ⇒ Object
Class Method Details
.included(klass) ⇒ Object
23 24 25 |
# File 'lib/revisions.rb', line 23 def self.included(klass) klass.extend(ClassMethods) end |
Instance Method Details
#apply_revision(revision = nil) ⇒ Object
maps a revision’s changes onto the main object
71 72 73 74 75 76 77 78 79 |
# File 'lib/revisions.rb', line 71 def apply_revision(revision=nil) revision = latest_revision if revision.nil? unless revision.nil? revised_attributes = revision.attributes.reject {|k,v| self.unrevised_attributes.include?(k)} self.attributes=revised_attributes return true end false end |
#apply_revision!(revision = nil) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/revisions.rb', line 81 def apply_revision!(revision=nil) if apply_revision(revision) save! else raise RuntimeError.new("No revision to apply!") end end |
#draft? ⇒ Boolean
35 36 37 |
# File 'lib/revisions.rb', line 35 def draft? status == 'draft' end |
#latest_revision ⇒ Object
62 63 64 |
# File 'lib/revisions.rb', line 62 def latest_revision revisions.last end |
#pending_revisions? ⇒ Boolean
66 67 68 |
# File 'lib/revisions.rb', line 66 def pending_revisions? !latest_revision.nil? && latest_revision.updated_at > self.updated_at end |
#published? ⇒ Boolean
27 28 29 |
# File 'lib/revisions.rb', line 27 def published? status == 'published' end |
#revision? ⇒ Boolean
31 32 33 |
# File 'lib/revisions.rb', line 31 def revision? status == 'revision' end |
#save_revision ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/revisions.rb', line 39 def save_revision new_copy = self.class.new cloned_attributes = self.clone_attributes self.unrevised_attributes.each {|a| cloned_attributes.delete(a) } new_copy.attributes=cloned_attributes new_copy.created_at = new_copy.updated_at = Time.zone.now new_copy.status = 'revision' new_copy.revision_of = self.id if new_copy.save true else self.errors.clear new_copy.errors.each do |attribute,| self.errors[attribute] << end false end end |
#save_revision! ⇒ Object
58 59 60 |
# File 'lib/revisions.rb', line 58 def save_revision! save_revision || raise(ActiveRecord::RecordNotSaved) end |