Sha256: 060ff2b8df0c53b810f49a1bf8e29c3d44eb7b432814d78e5e52634b507dfca8

Contents?: true

Size: 1.78 KB

Versions: 12

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module ComfortableMexicanSofa::HasRevisions

  def self.included(base)
    base.send :extend, ClassMethods
  end

  module ClassMethods

    def cms_has_revisions_for(*fields)
      include ComfortableMexicanSofa::HasRevisions::InstanceMethods

      attr_accessor :revision_data

      has_many :revisions,
        as:         :record,
        dependent:  :destroy,
        class_name: "Comfy::Cms::Revision"

      before_save :prepare_revision
      after_save  :create_revision

      define_method(:revision_fields) do
        fields.collect(&:to_s)
      end
    end

  end

  module InstanceMethods

    # Preparing revision data. A bit of a special thing to grab page blocks
    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

    # Revision is created only if relevant data changed
    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

    # Assigning whatever is found in revision data and attempting to save the object
    def restore_from_revision(revision)
      return unless revision.record == self
      update!(revision.data)
    end

  end

end

ActiveSupport.on_load :active_record do
  include ComfortableMexicanSofa::HasRevisions
end

Version data entries

12 entries across 12 versions & 6 rubygems

Version Path
comfortable_mexican_sofa-2.0.19 lib/comfortable_mexican_sofa/extensions/has_revisions.rb
PixelForce_ETS-0.0.2 lib/comfortable_mexican_sofa/extensions/has_revisions.rb
PixelForce_ETS-0.0.1 lib/comfortable_mexican_sofa/extensions/has_revisions.rb
kcyEtsTest2-0.0.4 lib/comfortable_mexican_sofa/extensions/has_revisions.rb
kcyEtsTest2-0.0.3 lib/comfortable_mexican_sofa/extensions/has_revisions.rb
kcyEtsTest2-0.0.2 lib/comfortable_mexican_sofa/extensions/has_revisions.rb
kcyEtsTest2-0.0.1 lib/comfortable_mexican_sofa/extensions/has_revisions.rb
kcyEtsTest-0.0.1 lib/comfortable_mexican_sofa/extensions/has_revisions.rb
kcyEtsTest-2.2.19 lib/comfortable_mexican_sofa/extensions/has_revisions.rb
ComfortableMexicanSofa-2.0.18 lib/comfortable_mexican_sofa/extensions/has_revisions.rb
kcy1-1.0 lib/comfortable_mexican_sofa/extensions/has_revisions.rb
comfortable_mexican_sofa-2.0.18 lib/comfortable_mexican_sofa/extensions/has_revisions.rb