Sha256: 237e81e059ccea1dd4b1e53a5e1c7816280c5467b11121d48b80140436d0d9cf

Contents?: true

Size: 1.11 KB

Versions: 43

Compression:

Stored size: 1.11 KB

Contents

module RSpec
  module Rails
    # Builds paths for view specs using a particular route set.
    class ViewPathBuilder
      def initialize(route_set)
        self.class.send(:include, route_set.url_helpers)
      end

      # Given a hash of parameters, build a view path, if possible.
      # Returns nil if no path can be built from the given params.
      #
      # @example
      #     # path can be built because all required params are present in the hash
      #     view_path_builder = ViewPathBuilder.new(::Rails.application.routes)
      #     view_path_builder.path_for({ :controller => 'posts', :action => 'show', :id => '54' })
      #     # => "/post/54"
      #
      # @example
      #     # path cannot be built because the params are missing a required element (:id)
      #     view_path_builder.path_for({ :controller => 'posts', :action => 'delete' })
      #     # => ActionController::UrlGenerationError: No route matches {:action=>"delete", :controller=>"posts"}
      def path_for(path_params)
        url_for(path_params.merge(only_path: true))
      rescue => e
        e.message
      end
    end
  end
end

Version data entries

43 entries across 40 versions & 7 rubygems

Version Path
rspec-rails-7.1.1 lib/rspec/rails/view_path_builder.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/rspec-rails-7.1.0/lib/rspec/rails/view_path_builder.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/rspec-rails-7.1.0/lib/rspec/rails/view_path_builder.rb
rspec-rails-7.1.0 lib/rspec/rails/view_path_builder.rb
rspec-rails-7.0.2 lib/rspec/rails/view_path_builder.rb
rspec-rails-7.0.1 lib/rspec/rails/view_path_builder.rb
rspec-rails-7.0.0 lib/rspec/rails/view_path_builder.rb
rspec-rails-6.1.5 lib/rspec/rails/view_path_builder.rb
rspec-rails-6.1.4 lib/rspec/rails/view_path_builder.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rspec-rails-6.1.3/lib/rspec/rails/view_path_builder.rb
rspec-rails-6.1.3 lib/rspec/rails/view_path_builder.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/rspec-rails-6.0.4/lib/rspec/rails/view_path_builder.rb
rspec-rails-6.1.2 lib/rspec/rails/view_path_builder.rb
rspec-rails-6.1.1 lib/rspec/rails/view_path_builder.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rspec-rails-5.1.1/lib/rspec/rails/view_path_builder.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rspec-rails-5.1.2/lib/rspec/rails/view_path_builder.rb
rspec-rails-6.1.0 lib/rspec/rails/view_path_builder.rb
rspec-rails-6.0.4 lib/rspec/rails/view_path_builder.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rspec-rails-5.1.1/lib/rspec/rails/view_path_builder.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rspec-rails-5.1.2/lib/rspec/rails/view_path_builder.rb