Sha256: 6a89b87a2f55a9063d7e5b5bcafdbb2724f68c6c1abee0ec9ef22eab79193035

Contents?: true

Size: 670 Bytes

Versions: 7

Compression:

Stored size: 670 Bytes

Contents

module Memento
  class Action::Create < Memento::Action::Base

    def fetch;end

    def undo
      if record.nil?
        build_fake_object
      elsif record_was_changed?
        was_changed
      else
        destroy_record
      end
    end

    private

    def record_was_changed?
      record.updated_at > record.created_at rescue false
    end

    def build_fake_object
      new_object do |object|
        object.id = state.record_id
      end
    end

    def was_changed
      record.errors.add(:memento_undo, ActiveSupport::StringInquirer.new("was_changed"))
      record
    end

    def destroy_record
      record.destroy
      record
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
memento-0.5.2 lib/memento/action/create.rb
memento-0.5.1 lib/memento/action/create.rb
memento-0.5.0 lib/memento/action/create.rb
memento-0.4.3 lib/memento/action/create.rb
memento-0.4.2 lib/memento/action/create.rb
memento-0.4.1 lib/memento/action/create.rb
memento-0.4.0 lib/memento/action/create.rb