Sha256: 9514104e6f3843cdb68f3bbc6f089e9f5e2c2f7632db8205d6605bdfccd29fc4

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

module StickyParams
  class SessionParams
    attr_reader :controller
    attr_accessor :prefix

    def initialize(controller)
      @controller = controller
      @prefix = "#{controller.controller_name}_#{controller.action_name}_"
    end

    def [](name)
      session_param_name = "#{prefix}#{name}"
      controller.session['sticky_params'] ||= {}
      if controller.params[name]
        if controller.params[name].present?
          controller.session['sticky_params'][session_param_name] = controller.params[name]
        else
          controller.session['sticky_params'].delete session_param_name
          nil
        end
      elsif controller.session['sticky_params'][session_param_name]
        controller.session['sticky_params'][session_param_name]
      end
    end

    def []=(name, value)
      session_param_name = "#{prefix}#{name}"
      controller.session['sticky_params'] ||= {}
      controller.session['sticky_params'][session_param_name] = controller.params[name] = value
    end

    # clears all sticky params for the current controller/action name
    def clear!
      if controller.session['sticky_params'].present?
        controller.session['sticky_params'].reject! do |key, _value|
          key.index(prefix) == 0
        end
      end
    end

    # clears all sticky parameters
    def clear_all!
      controller.session.delete('sticky_params')
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sticky_params-2.1.0 lib/sticky_params/session_params.rb
sticky_params-2.0.0 lib/sticky_params/session_params.rb
sticky_params-1.0.1 lib/sticky_params/session_params.rb