Sha256: 7c49c5a59e7ddf634d30a756f82cc5747ce65638d3b62bd4aff3224b66987c80

Contents?: true

Size: 1.38 KB

Versions: 13

Compression:

Stored size: 1.38 KB

Contents

module Shoulda
  module Matchers
    module ActionController
      # @private
      class RouteParams
        PARAMS_TO_SYMBOLIZE = %i{format}.freeze

        def initialize(args)
          @args = args
        end

        def normalize
          if controller_and_action_given_as_string?
            extract_params_from_string
          else
            stringify_params
          end
        end

        protected

        attr_reader :args

        def controller_and_action_given_as_string?
          args[0].is_a?(String)
        end

        def extract_params_from_string
          controller, action = args[0].split('#')
          params = (args[1] || {}).merge(controller: controller, action: action)
          normalize_values(params)
        end

        def stringify_params
          normalize_values(args[0])
        end

        def normalize_values(hash)
          hash.each_with_object({}) do |(key, value), hash_copy|
            hash_copy[key] = symbolize_or_stringify(key, value)
          end
        end

        def symbolize_or_stringify(key, value)
          if PARAMS_TO_SYMBOLIZE.include?(key)
            value.to_sym
          else
            stringify(value)
          end
        end

        def stringify(value)
          if value.is_a?(Array)
            value.map(&:to_param)
          else
            value.to_param
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
shoulda-matchers-6.4.0 lib/shoulda/matchers/action_controller/route_params.rb
shoulda-matchers-6.3.1 lib/shoulda/matchers/action_controller/route_params.rb
shoulda-matchers-6.3.0 lib/shoulda/matchers/action_controller/route_params.rb
shoulda-matchers-6.2.0 lib/shoulda/matchers/action_controller/route_params.rb
shoulda-matchers-6.1.0 lib/shoulda/matchers/action_controller/route_params.rb
shoulda-matchers-6.0.0 lib/shoulda/matchers/action_controller/route_params.rb
shoulda-matchers-5.3.0 lib/shoulda/matchers/action_controller/route_params.rb
shoulda-matchers-5.2.0 lib/shoulda/matchers/action_controller/route_params.rb
shoulda-matchers-5.1.0 lib/shoulda/matchers/action_controller/route_params.rb
shoulda-matchers-5.0.0 lib/shoulda/matchers/action_controller/route_params.rb
shoulda-matchers-5.0.0.rc1 lib/shoulda/matchers/action_controller/route_params.rb
shoulda-matchers-4.5.1 lib/shoulda/matchers/action_controller/route_params.rb
shoulda-matchers-4.5.0 lib/shoulda/matchers/action_controller/route_params.rb