Class: RedirectWithParams::Extensions::Routing::ParamPreservingRedirect

Inherits:
ActionDispatch::Routing::Redirect
  • Object
show all
Defined in:
lib/redirect_with_params/extensions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, query_params = [], block) ⇒ ParamPreservingRedirect

Returns a new instance of ParamPreservingRedirect.



10
11
12
13
14
# File 'lib/redirect_with_params/extensions.rb', line 10

def initialize(status, query_params = [], block)
  @permitted_params = query_params
  @permitted_params.map! { |k| k.to_sym }
  super(status, block)
end

Instance Attribute Details

#permitted_params ⇒ Object (readonly)

Returns the value of attribute permitted_params.



9
10
11
# File 'lib/redirect_with_params/extensions.rb', line 9

def permitted_params
  @permitted_params
end

Instance Method Details

#path(params, request) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/redirect_with_params/extensions.rb', line 16

def path(params, request)
  path_builder = block.call(params, request)

  allowed_keys = permitted_params
  allowed_keys = request.params.keys.map(&:to_sym) if permitted_params.empty?
  allowed_keys -= params.keys(&:to_sym)

  allowed_params = request.params.select { |k,v| allowed_keys.include?(k.to_sym) }
  path_builder << "?" << allowed_params.to_query unless allowed_params.empty?

  path_builder
end