Module: ComfortableMexicanSofa::HasRevisions::InstanceMethods
- Defined in:
- lib/comfortable_mexican_sofa/extensions/has_revisions.rb
Instance Method Summary collapse
-
#create_revision ⇒ Object
Revision is created only if relevant data changed.
-
#prepare_revision ⇒ Object
Preparing revision data.
-
#restore_from_revision(revision) ⇒ Object
Assigning whatever is found in revision data and attempting to save the object.
Instance Method Details
#create_revision ⇒ Object
Revision is created only if relevant data changed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/comfortable_mexican_sofa/extensions/has_revisions.rb', line 45 def create_revision return unless revision_data limit = ComfortableMexicanSofa.config.revisions_limit.to_i # creating revision if limit != 0 revisions.create!(data: revision_data) end # blowing away old revisions ids = [0] + revisions.order(created_at: :desc).limit(limit).pluck(:id) revisions.where("id NOT IN (?)", ids).destroy_all end |
#prepare_revision ⇒ Object
Preparing revision data. A bit of a special thing to grab page blocks
34 35 36 37 38 39 40 41 42 |
# File 'lib/comfortable_mexican_sofa/extensions/has_revisions.rb', line 34 def prepare_revision return if new_record? if (respond_to?(:fragments_attributes_changed) && fragments_attributes_changed) || !(changed & revision_fields).empty? self.revision_data = revision_fields.each_with_object({}) do |field, c| c[field] = send("#{field}_was") end end end |
#restore_from_revision(revision) ⇒ Object
Assigning whatever is found in revision data and attempting to save the object
61 62 63 64 |
# File 'lib/comfortable_mexican_sofa/extensions/has_revisions.rb', line 61 def restore_from_revision(revision) return unless revision.record == self update!(revision.data) end |