Sha256: f11f583e30afb744076aca663565cfbc784ec4232e9cee89ac28407fd9a6b1b4

Contents?: true

Size: 1.04 KB

Versions: 93

Compression:

Stored size: 1.04 KB

Contents

module Tenon
  module HasHistory
    class ItemReverter
      def self.revert(*args)
        new(*args).revert
      end

      def initialize(item, item_version)
        @item = item
        @item_version = item_version
      end

      def revert
        remove_saved_associations
        @item.assign_attributes(params.permit!)
      end

      private

      def params
        ActionController::Parameters.new(Marshal.load(@item_version.attrs))
      end

      # This inserts the existing associations into the form
      # but it marks them for destruction.  This way they don't "show"
      # to the user and if the user saves the form the old associations
      # will be deleted and the new ones saved, preventing any duplicates.
      def remove_saved_associations
        @item.has_history_includes.each do |assoc_name|
          assoc = @item.send(assoc_name)
          if assoc.respond_to?(:each)
            assoc.each(&:mark_for_destruction)
          else
            assoc.mark_for_destruction
          end
        end
      end
    end
  end
end

Version data entries

93 entries across 93 versions & 1 rubygems

Version Path
tenon-1.0.12 lib/tenon/has_history/item_reverter.rb
tenon-1.0.11 lib/tenon/has_history/item_reverter.rb
tenon-1.0.10 lib/tenon/has_history/item_reverter.rb
tenon-1.0.9 lib/tenon/has_history/item_reverter.rb
tenon-1.0.8 lib/tenon/has_history/item_reverter.rb
tenon-1.0.7 lib/tenon/has_history/item_reverter.rb
tenon-1.0.6 lib/tenon/has_history/item_reverter.rb
tenon-1.0.5 lib/tenon/has_history/item_reverter.rb
tenon-1.0.4 lib/tenon/has_history/item_reverter.rb
tenon-1.0.3 lib/tenon/has_history/item_reverter.rb
tenon-1.0.2 lib/tenon/has_history/item_reverter.rb
tenon-1.0.1 lib/tenon/has_history/item_reverter.rb
tenon-1.0 lib/tenon/has_history/item_reverter.rb