class ParamsKeeper::Resolver

Public Class Methods

new(controller, url_options) click to toggle source
# File lib/params_keeper/resolver.rb, line 3
def initialize(controller, url_options)
  @controller = controller
  @url_options = url_options
  @cache = {}
end

Public Instance Methods

call() click to toggle source
# File lib/params_keeper/resolver.rb, line 9
def call
  return {} if configs.blank? || skip_url_options?

  configs.each_with_object({}) do |config, params|
    if target_config?(config)
      params.merge!(extract_params(config))
    end
  end
end
url_options_hash() click to toggle source
# File lib/params_keeper/resolver.rb, line 19
def url_options_hash
  if @url_options.is_a?(Hash)
    @url_options
  else
    recognize_path(base_url_for(@url_options))
  end
end

Private Instance Methods

base_url_for(url_options) click to toggle source
# File lib/params_keeper/resolver.rb, line 83
def base_url_for(url_options)
  @controller.method(:url_for).super_method.call(url_options)
end
configs() click to toggle source
# File lib/params_keeper/resolver.rb, line 29
def configs
  @controller.class.keep_params_configs
end
current_controllers() click to toggle source
# File lib/params_keeper/resolver.rb, line 68
def current_controllers
  [@controller.controller_name, @controller.controller_path]
end
destination_controllers(url_options_hash) click to toggle source
# File lib/params_keeper/resolver.rb, line 60
def destination_controllers(url_options_hash)
  if url_options_hash[:controller].present?
    [url_options_hash[:controller].to_s]
  else
    current_controllers
  end
end
extract_params(config) click to toggle source
# File lib/params_keeper/resolver.rb, line 78
def extract_params(config)
  params = @controller.request.params.deep_symbolize_keys
  params.slice(*config.keys).merge(config.url_options)
end
recognize_path(url) click to toggle source
# File lib/params_keeper/resolver.rb, line 72
def recognize_path(url)
  @cache[url] ||= Rails.application.routes.recognize_path(url)
rescue ActionController::RoutingError
  {}
end
skip_url_options?() click to toggle source
# File lib/params_keeper/resolver.rb, line 33
def skip_url_options?
  if @url_options.is_a?(Hash)
    @url_options.delete(:keep_params) == false
  else
    false
  end
end
target_config?(config) click to toggle source
# File lib/params_keeper/resolver.rb, line 41
def target_config?(config)
  target_url_options?(config) && target_controller?(config)
end
target_controller?(config) click to toggle source
# File lib/params_keeper/resolver.rb, line 51
def target_controller?(config)
  dests = destination_controllers(url_options_hash)
  if config.to.present?
    (dests & config.to.map(&:to_s)).present?
  else
    (dests & current_controllers).present?
  end
end
target_url_options?(config) click to toggle source
# File lib/params_keeper/resolver.rb, line 45
def target_url_options?(config)
  (config.for.include?(:hash) && @url_options.is_a?(Hash)) ||
    (config.for.include?(:string) && @url_options.is_a?(String)) ||
    (config.for.include?(:model) && @url_options.class.respond_to?(:model_name))
end