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.32 lib/tenon/has_history/item_reverter.rb
tenon-1.0.31 lib/tenon/has_history/item_reverter.rb
tenon-1.0.30 lib/tenon/has_history/item_reverter.rb
tenon-1.0.29 lib/tenon/has_history/item_reverter.rb
tenon-1.0.28 lib/tenon/has_history/item_reverter.rb
tenon-1.0.27 lib/tenon/has_history/item_reverter.rb
tenon-1.0.26 lib/tenon/has_history/item_reverter.rb
tenon-1.0.25 lib/tenon/has_history/item_reverter.rb
tenon-1.0.24 lib/tenon/has_history/item_reverter.rb
tenon-1.0.23 lib/tenon/has_history/item_reverter.rb
tenon-1.0.22 lib/tenon/has_history/item_reverter.rb
tenon-1.0.21 lib/tenon/has_history/item_reverter.rb
tenon-1.0.20 lib/tenon/has_history/item_reverter.rb
tenon-1.0.19 lib/tenon/has_history/item_reverter.rb
tenon-1.0.18 lib/tenon/has_history/item_reverter.rb
tenon-1.0.17 lib/tenon/has_history/item_reverter.rb
tenon-1.0.16 lib/tenon/has_history/item_reverter.rb
tenon-1.0.15 lib/tenon/has_history/item_reverter.rb
tenon-1.0.14 lib/tenon/has_history/item_reverter.rb
tenon-1.0.13 lib/tenon/has_history/item_reverter.rb