Sha256: 6cd4ac45def7d6491347da5e71e5ac92a86105ef9dc2d81e5e7dd9c6fc61b15e

Contents?: true

Size: 832 Bytes

Versions: 1

Compression:

Stored size: 832 Bytes

Contents

require 'active_support'

module Preserve
  def preserve(*parameter_names)
    options = parameter_names.extract_options!

    allow_blank = options.delete(:allow_blank)
    prefix = options.delete(:prefix)

    parameter_names.each do |name|
      key = [prefix, controller_name, name].compact.join('_')
      predicate = allow_blank ? :nil? : :blank?

      _set_preserve_callback(options) do
        if params[name].send(predicate)
          params[name] = session[key] if session.key?(key)
        else
          session[key] = params[name]
        end
      end
    end
  end

  def _set_preserve_callback(options, &block)
    if respond_to?(:before_action)
      before_action(options, &block)
    else
      before_filter(options, &block)
    end
  end
end

ActiveSupport.on_load(:action_controller) do
  extend Preserve
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
preserve-1.0.0 lib/preserve.rb