rails_routes_analyzer 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9583c3be5fc1f04a24b558d4ff440fbd07a74126
4
+ data.tar.gz: 8ae1e79bd2f253902b087d950bb77a8b529de713
5
+ SHA512:
6
+ metadata.gz: a39ca46f44f864fcc6a25dd39141ba1cef9cd67c2a561078356d7359bf8f5437e2105ddca5dc879110abb4d81b642326faadc49e6d801d7c07fc786ab1b2b412
7
+ data.tar.gz: c7eb4f526012dbcce947293f63d2d99eca76fd4c86400d32c0c357b2c351cf5a3dc6d1cd9d03a77d82799f08d3a421d4bf061bfa92f0b54c399e17ec0cb78a0a
data/.autotest ADDED
@@ -0,0 +1,6 @@
1
+ require 'autotest/restart'
2
+ require "autotest/suffix"
3
+
4
+ Autotest.add_hook :initialize do |at|
5
+ at.testlib = "minitest/autorun"
6
+ end
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /Gemfile*.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ /test/dummy/test.log
12
+ /.byebug_history
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ sudo: false
2
+ language: ruby
3
+ cache: bundler
4
+ bundler_args: --quiet
5
+ rvm:
6
+ - 2.1.10
7
+ - 2.2.3
8
+ - 2.3.1
9
+ gemfile:
10
+ - Gemfile_3.2
11
+ - Gemfile_4.2
12
+ - Gemfile
13
+ matrix:
14
+ exclude:
15
+ - rvm: 2.1.10
16
+ gemfile: Gemfile
17
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails-routes-analyzer.gemspec
4
+ gemspec
5
+
6
+ gem 'rails', '~> 5.0'
data/Gemfile_3.2 ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails-routes-analyzer.gemspec
4
+ gemspec
5
+
6
+ gem 'rails', '~> 3.2'
data/Gemfile_4.2 ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails-routes-analyzer.gemspec
4
+ gemspec
5
+
6
+ gem 'rails', '~> 4.2'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Bear Metal OÜ
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ [![Build Status](https://travis-ci.org/bear-metal/rails_routes_analyzer.svg)](https://travis-ci.org/bear-metal/rails_routes_analyzer)
2
+
3
+ Adds rake tasks to detect extraneous routes and unreachable controller actions Ruby on Rails applications. It's also able to provide suggestions on how to change routes.rb to avoid defining dead routes and can generate an annotated version of a route file with suggestions for each line added as comments.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rails_routes_analyzer'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ ``` sh
20
+ rake routes:dead
21
+ ```
22
+
23
+ Without any additional options this will scan all the routes and check for a matching action for each. For single routes created with methods such as `get' or `post' it will tell you when that line could be removed.
24
+
25
+ For multi-route calls like `resource' and `resources' it can also let you know if and how to set the :except or :only parameter to better match the actions a controller actually provides. When suggesting :only/:except by default the one that provides a shorter list will be used.
26
+
27
+ For complex cases where for example a routes are created in a loop for multiple controllers a suggestion will be provided for each iteration but only if that specific iteration created a dead route. Every such suggestion will identify the exact controller to which it applies.
28
+
29
+ ``` sh
30
+ rake routes:annotate_dead [ANNOTATE=path/to/routes.rb]
31
+ ```
32
+
33
+ Will output an annotated version of config/routes.rb or any other routes file as provided in the ANNOTATE parameter.
34
+
35
+
36
+ #### Additional options:
37
+
38
+ * ONLY\_ONLY=1 - suggestions for resource routes will only generate "only:" regardless of how many elements are listed.
39
+ * ONLY\_EXCEPT=1 - suggestions for resource routes will only generate "except:" regardless of how many elements are listed.
40
+ * VERBOSE=1 - more verbosity, currently this means listing which non-existing actions a given call provides routes for.
41
+
42
+ ```sh
43
+ rake routes:missing
44
+ ```
45
+
46
+ #### Additional options:
47
+
48
+ Lists all action methods for all controllers which have no route pointing to them. Uses the (maybe not so well known) ActionController#Base.action\_methods method which usually returns a list of all public methods of the controller class excluding any special Rails provided methods. To make the output of ActionController#Base.action\_methods it would be ideal to try to make all application-provided controller methods non-public if they are not meant to be callable as an action. Alternatively it's also possible (but less desirable) to override the action\_methods call in any controller class to explicitly remove mis-characterised methods.
49
+
50
+ * STRICT=1 - causes controller base class provided public methods to be considered as actions for a subclass controller and thus reported as errors if they lack routes. Enabling this can generate a lot of noise for applications that have public non-actions in a controller base class.
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bear-metal/rails_routes_analyzer.
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
59
+
60
+ Copyright (c) 2016 [Bear Metal](http://bearmetal.eu)
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails/routes/analyzer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ require "rails_routes_analyzer/version"
2
+ require "rails_routes_analyzer/rails_routes_analyzer"
3
+ require "rails_routes_analyzer/route_interceptor"
4
+ require "rails_routes_analyzer/route_issue"
5
+ require "rails_routes_analyzer/route_analysis"
6
+ require "rails_routes_analyzer/route_file_annotator"
7
+
@@ -0,0 +1,46 @@
1
+ require 'rails/railtie'
2
+
3
+ module RailsRoutesAnalyzer
4
+
5
+ class Railtie < ::Rails::Railtie
6
+ rake_tasks do
7
+ load File.join(File.dirname(__FILE__), '../tasks/rails_routes_analyzer.rake')
8
+ end
9
+ end
10
+
11
+ MULTI_METHODS = %w[resource resources].freeze
12
+ SINGLE_METHODS = %w[match get head post patch put delete options root].freeze
13
+ ROUTE_METHODS = (MULTI_METHODS + SINGLE_METHODS).freeze
14
+
15
+ def self.get_full_filename(filename)
16
+ return filename.to_s if filename.to_s.starts_with?('/')
17
+ Rails.root.join(filename).to_s
18
+ end
19
+
20
+ RESOURCE_ACTIONS = [:index, :create, :new, :show, :update, :destroy, :edit]
21
+
22
+ def self.identify_route_issues
23
+ RouteAnalysis.new
24
+ end
25
+
26
+ def self.get_all_defined_routes
27
+ identify_route_issues[:implemented_routes]
28
+ end
29
+
30
+ def self.get_all_action_methods(ignore_parent_provided: true)
31
+ [].tap do |result|
32
+ ApplicationController.descendants.each do |controller_class|
33
+ action_methods = controller_class.action_methods
34
+
35
+ if ignore_parent_provided && (super_class_actions = controller_class.superclass.try(:action_methods)).present?
36
+ action_methods -= super_class_actions
37
+ end
38
+
39
+ action_methods.each do |action_method|
40
+ result << [controller_class.name, action_method.to_sym]
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ end
@@ -0,0 +1,131 @@
1
+ module RailsRoutesAnalyzer
2
+
3
+ class RouteAnalysis
4
+ attr_accessor :app, :verbose, :only_only, :only_except
5
+ attr_accessor :route_log
6
+ attr_writer :all_issues
7
+
8
+ def initialize(app: Rails.application, verbose: false, only_only: false, only_except: false)
9
+ self.app = app
10
+ self.verbose = verbose
11
+ self.only_only = only_only
12
+ self.only_except = only_except
13
+
14
+ analyze!
15
+ end
16
+
17
+ def analyze!
18
+ app.eager_load! # all controller classes need to be loaded
19
+
20
+ ::ActionDispatch::Routing::Mapper::Mapping.prepend RouteInterceptor
21
+
22
+ RouteInterceptor.route_log.clear
23
+
24
+ app.reload_routes!
25
+
26
+ all_issues = []
27
+
28
+ RouteInterceptor.route_data.each do |(file_location, route_creation_method, controller_name), action_names|
29
+ controller_class_name = "#{controller_name}_controller".camelize
30
+
31
+ action_names = action_names.uniq.sort
32
+
33
+ opts = {
34
+ file_location: file_location,
35
+ route_creation_method: route_creation_method,
36
+ controller_name: controller_name,
37
+ controller_class_name: controller_class_name,
38
+ action_names: action_names,
39
+ }
40
+
41
+ controller = nil
42
+ begin
43
+ controller = Object.const_get(controller_class_name)
44
+ rescue LoadError, RuntimeError, NameError => e
45
+ all_issues << RouteIssue.new(opts.merge(type: :no_controller, error: e.message))
46
+ next
47
+ end
48
+
49
+ if controller.nil?
50
+ all_issues << RouteIssue.new(opts.merge(type: :no_controller))
51
+ next
52
+ end
53
+
54
+ present, missing = action_names.partition {|name| controller.action_methods.include?(name.to_s) }
55
+ extra = action_names - RESOURCE_ACTIONS
56
+
57
+ if present.any?
58
+ all_issues << RouteIssue.new(opts.merge(type: :non_issue, present_actions: present))
59
+ end
60
+
61
+ if SINGLE_METHODS.include?(route_creation_method)
62
+ # NOTE a single call like 'get' can add multiple actions if called in a loop
63
+ if missing.present?
64
+ all_issues << RouteIssue.new(opts.merge(type: :no_action, missing_actions: missing))
65
+ end
66
+
67
+ next
68
+ end
69
+
70
+ next if missing.empty? # Everything is perfect
71
+
72
+ if present.sort == RESOURCE_ACTIONS.sort
73
+ unless missing.empty?
74
+ raise "shouldn't get all methods being present and missing at the same time: #{present.inspect} #{missing.inspect}"
75
+ end
76
+ next
77
+ end
78
+
79
+ suggested_param = if (present.size < 4 || only_only) && !only_except
80
+ "only: [#{present.sort.map {|x| ":#{x}" }.join(', ')}]"
81
+ else
82
+ "except: [#{(RESOURCE_ACTIONS - present).sort.map {|x| ":#{x}" }.join(', ')}]"
83
+ end
84
+
85
+ if verbose
86
+ verbose_message = "This route currently covers unimplemented actions: [#{missing.sort.map {|x| ":#{x}" }.join(', ')}]"
87
+ end
88
+
89
+ all_issues << RouteIssue.new(opts.merge(type: :resources, suggested_param: suggested_param, verbose_message: verbose_message))
90
+ end
91
+
92
+ self.route_log = RouteInterceptor.route_log.dup
93
+ self.all_issues = all_issues
94
+ end
95
+
96
+ def all_issues
97
+ @all_issues || []
98
+ end
99
+
100
+ def issues
101
+ all_issues.select(&:issue?)
102
+ end
103
+
104
+ def non_issues
105
+ all_issues.reject(&:issue?)
106
+ end
107
+
108
+ def all_unique_issues_file_names
109
+ all_issues.map { |issue| issue.full_filename }.uniq.sort
110
+ end
111
+
112
+ def unique_issues_file_names
113
+ issues.map { |issue| issue.full_filename }.uniq.sort
114
+ end
115
+
116
+ def all_issues_for_file_name(full_filename)
117
+ all_issues.select { |issue| issue.full_filename == full_filename.to_s }
118
+ end
119
+
120
+ def implemented_routes
121
+ Set.new.tap do |implemented_routes|
122
+ non_issues.each do |non_issue|
123
+ non_issue.present_actions.each do |action|
124
+ implemented_routes << [non_issue.controller_class_name, action]
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
130
+
131
+ end
@@ -0,0 +1,74 @@
1
+ module RailsRoutesAnalyzer
2
+
3
+ class RouteFileAnnotator
4
+ def initialize(analysis: RailsRoutesAnalyzer::RouteAnalysis.new)
5
+ @analysis = analysis
6
+ end
7
+
8
+ def annotated_file_content(route_filename)
9
+ relevant_issues = @analysis.all_issues_for_file_name(route_filename)
10
+
11
+ if relevant_issues.none?(&:issue?)
12
+ log_notice { "Didn't find any route issues for file: #{route_filename}, only have references to: #{@analysis.all_unique_issues_file_names.join(', ')}" }
13
+ end
14
+
15
+ log_notice { "Annotating #{route_filename}" }
16
+
17
+ lines = File.readlines(route_filename)
18
+ issue_map = relevant_issues.group_by { |issue| issue.line_number }
19
+
20
+ "".tap do |output|
21
+ File.readlines(route_filename).each_with_index do |line, index|
22
+ suggestion = combined_suggestion_for(issue_map[index + 1])
23
+
24
+ if suggestion.present?
25
+ output << line.sub(/( # SUGGESTION.*)?$/, " # SUGGESTION #{suggestion}")
26
+ else
27
+ output << line
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ def log_notice(message=nil, &block)
34
+ return if ENV['RAILS_ENV'] == 'test'
35
+ message ||= block.call if block
36
+ STDERR.puts "# #{message}" if message.present?
37
+ end
38
+
39
+ def combined_suggestion_for(all_issues)
40
+ return if all_issues.nil? || all_issues.none?(&:issue?)
41
+
42
+ issues, non_issues = all_issues.partition(&:issue?)
43
+
44
+ context = {
45
+ non_issues: non_issues.present?,
46
+ num_controllers: all_issues.map(&:controller_class_name).uniq.count,
47
+ }
48
+
49
+ issues.map { |issue| issue.suggestion(**context) }.join(', ')
50
+ end
51
+
52
+ def annotate_routes_file(filename)
53
+ filenames = @analysis.unique_issues_file_names
54
+
55
+ if filename.blank?
56
+ if filenames.size > 1
57
+ STDERR.puts "Please specify file to annotate with ANNOTATE='path/routes.rb' as you have more than one:\n#{filenames.join("\n ")}"
58
+ exit 1
59
+ end
60
+ filename = filenames.first
61
+ end
62
+
63
+ filename = RailsRoutesAnalyzer.get_full_filename(filename)
64
+
65
+ unless File.exists?(filename)
66
+ STDERR.puts "Can't routes find file: #{filename}"
67
+ exit 1
68
+ end
69
+
70
+ puts annotated_file_content(filename)
71
+ end
72
+ end
73
+
74
+ end
@@ -0,0 +1,82 @@
1
+ require 'rails'
2
+
3
+ module RailsRoutesAnalyzer
4
+
5
+ # Plugs into ActionDispatch::Routing::Mapper::Mapping to help get detailed information
6
+ # on which route was generated, exactly where and if there is a matching controller action
7
+ module RouteInterceptor
8
+ ROUTE_METHOD_REGEX = /action_dispatch\/routing\/mapper.rb:[0-9]+:in `(#{Regexp.union(*::RailsRoutesAnalyzer::ROUTE_METHODS)})'\z/
9
+
10
+ def self.route_data
11
+ {}.tap do |result|
12
+ route_log.each do |(location, controller_name, action, request_methods)|
13
+ (result[location] ||= []) << action
14
+ end
15
+ end
16
+ end
17
+
18
+ def self.route_log
19
+ @route_log ||= []
20
+ end
21
+
22
+ # Finds the most interesting Rails.root file from the backtrace that called a method in mapper.rb
23
+ def get_routes_rb_location
24
+ bt = caller
25
+ base = 0
26
+ while true
27
+ index = bt[base..-1].index {|l| l =~ ROUTE_METHOD_REGEX }
28
+ return "" if index.nil?
29
+
30
+ next_line = bt[base + index + 1]
31
+
32
+ if next_line =~ /action_dispatch\/routing\/mapper.rb/
33
+ base += index + 1
34
+ next
35
+ else
36
+ file_location = next_line[/:?\A#{Rails.root}\/(.*:[0-9]+)/, 1] || next_line
37
+
38
+ bt[base + index] =~ ROUTE_METHOD_REGEX
39
+ route_creation_method = $1
40
+
41
+ return [file_location, route_creation_method]
42
+ end
43
+ end
44
+ end
45
+
46
+ if Rails.version =~ /\A3[.]/
47
+ def initialize(*args)
48
+ super.tap do
49
+ record_route(@options[:controller], @options[:action], conditions[:request_method])
50
+ end
51
+ end
52
+ elsif Rails.version =~ /\A4\./
53
+ def initialize(*args)
54
+ super.tap do
55
+ record_route(@defaults[:controller], @defaults[:action], conditions[:request_method])
56
+ end
57
+ end
58
+ else # Rails 5+
59
+ def initialize(*args)
60
+ super.tap do
61
+ record_route(@defaults[:controller], @defaults[:action], request_method.map(&:verb))
62
+ end
63
+ end
64
+ end
65
+
66
+ def record_route(controller_name, action, request_methods)
67
+ return unless controller_name && action
68
+
69
+ location = get_routes_rb_location + [controller_name]
70
+
71
+ if location[0].nil?
72
+ puts "Failed to find call location for: #{controller_name}/#{action}"
73
+ else
74
+ record = [location, controller_name, action.to_sym, request_methods]
75
+
76
+ RouteInterceptor.route_log << record
77
+ end
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -0,0 +1,109 @@
1
+ module RailsRoutesAnalyzer
2
+
3
+ class RouteIssue < Hash
4
+ %i[
5
+ action
6
+ action_names
7
+ missing_actions
8
+ present_actions
9
+ controller_class_name
10
+ controller_name
11
+ error
12
+ file_location
13
+ route_creation_method
14
+ suggested_param
15
+ type
16
+ verbose_message
17
+ ].each do |name|
18
+ define_method(name) { self[name] }
19
+ end
20
+
21
+ def initialize(opts={})
22
+ self.update(opts)
23
+ end
24
+
25
+ def issue?
26
+ type != :non_issue
27
+ end
28
+
29
+ def resource?
30
+ type == :resources
31
+ end
32
+
33
+ def full_filename
34
+ RailsRoutesAnalyzer.get_full_filename(file_location.sub(/:[0-9]*\z/, ''))
35
+ end
36
+
37
+ def line_number
38
+ file_location[/:([0-9]+)\z/, 1].to_i
39
+ end
40
+
41
+ def human_readable
42
+ case self[:type]
43
+ when :non_issue
44
+ ''
45
+ when :no_controller
46
+ "`#{route_creation_method}' call at #{file_location} there is no controller: #{controller_class_name} for '#{controller_name}' (actions: #{action_names.inspect})".tap do |msg|
47
+ msg << " error: #{error}" if error.present?
48
+ end
49
+ when :no_action
50
+ missing_actions.map do |action|
51
+ "`#{route_creation_method} :#{action}' call at #{file_location} there is no matching action in #{controller_class_name}"
52
+ end.tap do |result|
53
+ return nil if result.size == 0
54
+ return result[0] if result.size == 1
55
+ end
56
+ when :resources
57
+ "`#{route_creation_method}' call at #{file_location} for #{controller_class_name} should use #{suggested_param}"
58
+ else
59
+ raise ArgumentError, "Unknown issue_type: #{self[:type].inspect}"
60
+ end.tap do |message|
61
+ message << "| #{verbose_message}" if verbose_message
62
+ end
63
+ end
64
+
65
+ def suggestion(non_issues:, num_controllers:)
66
+ case self[:type]
67
+ when :non_issue
68
+ nil
69
+ when :no_controller
70
+ if non_issues
71
+ "remove case for #{controller_class_name} as it doesn't exist"
72
+ else
73
+ "delete, #{controller_class_name} not found"
74
+ end
75
+ when :no_action
76
+ actions = format_actions(missing_actions)
77
+ if non_issues
78
+ "remove case#{'s' if missing_actions.size > 1} for #{actions}"
79
+ else
80
+ "delete line, #{actions} matches nothing"
81
+ end.tap do |message|
82
+ message << " for controller #{controller_class_name}" if num_controllers > 1
83
+ end
84
+ when :resources
85
+ "use #{suggested_param}".tap do |message|
86
+ if num_controllers > 1
87
+ message << " only for #{controller_class_name}"
88
+ end
89
+ end
90
+ else
91
+ raise ArgumentError, "Unknown issue_type: #{self[:type].inspect}"
92
+ end.tap do |message|
93
+ message << "| #{verbose_message}" if verbose_message
94
+ end
95
+ end
96
+
97
+ def format_actions(actions)
98
+ case actions.size
99
+ when 0
100
+ when 1
101
+ ":#{actions.first}"
102
+ else
103
+ list = actions.map { |action| ":#{action}" }.sort.join(', ')
104
+ "[#{list}]"
105
+ end
106
+ end
107
+ end
108
+
109
+ end
@@ -0,0 +1,3 @@
1
+ module RailsRoutesAnalyzer
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,62 @@
1
+ namespace :routes do
2
+
3
+ desc 'Scan for dead-end routes including bad map.resource(s) :only/:except parameters'
4
+ task dead: :environment do
5
+ analysis = RailsRoutesAnalyzer::RouteAnalysis.new(
6
+ verbose: !ENV['VERBOSE'].nil?,
7
+ only_only: !ENV['ONLY_ONLY'].nil?,
8
+ only_except: !ENV['ONLY_EXCEPT'].nil?)
9
+
10
+ if analysis.issues.empty?
11
+ puts "No route issues found"
12
+ return
13
+ end
14
+
15
+ analysis.issues.each do |issue|
16
+ puts issue.human_readable
17
+ end
18
+ end
19
+
20
+ desc "Output a routes file with suggested modifications in comments (NOTE: doesn't touch the original file)"
21
+ task annotate_dead: :environment do
22
+ annotator = RailsRoutesAnalyzer::RouteFileAnnotator.new
23
+ annotator.annotate_routes_file(ENV['ANNOTATE'])
24
+ end
25
+
26
+ desc 'Scan for controller action methods that are missing a route (pass STRICT=1 to account for inherited actions)'
27
+ task missing: :environment do
28
+ analysis = RailsRoutesAnalyzer::RouteAnalysis.new
29
+ implemented_routes = analysis.implemented_routes
30
+ all_action_methods = RailsRoutesAnalyzer.get_all_action_methods(ignore_parent_provided: ENV['STRICT'].blank?)
31
+
32
+ missing = all_action_methods.to_a - implemented_routes.to_a
33
+
34
+ if missing.any?
35
+ puts "NOTE Some gems, such as Devise, are expected to provide actions that have no matching"
36
+ puts " routes in case a particular feature is not enabled, this is normal and expected."
37
+ puts "NOTE If any non-action methods are reported please consider making those non-public"
38
+ puts " or using another solution that would make #action_methods not return those."
39
+ puts ""
40
+
41
+ unused_controllers = Set.new(all_action_methods.to_a.map(&:first) - implemented_routes.to_a.map(&:first))
42
+
43
+ if unused_controllers.any?
44
+ puts "Controllers with no routes pointing to them:"
45
+ unused_controllers.to_a.sort.each do |controller_name|
46
+ puts " #{controller_name}"
47
+ end
48
+ puts ""
49
+ end
50
+
51
+ puts "Actions without a route:"
52
+ missing.sort.each do |(controller_name, action_name)|
53
+ unless unused_controllers.include?(controller_name)
54
+ puts " #{controller_name}::#{action_name}"
55
+ end
56
+ end
57
+ else
58
+ puts "There are no actions without a route"
59
+ end
60
+ end
61
+
62
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_routes_analyzer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_routes_analyzer"
8
+ spec.version = RailsRoutesAnalyzer::VERSION
9
+ spec.authors = ["Tarmo Tänav"]
10
+ spec.email = ["tarmo@bearmetal.eu"]
11
+
12
+ spec.summary = %q{Helps clean up rails routes}
13
+ #spec.description = %q{}
14
+ spec.homepage = "https://github.com/bear-metal/rails_routes_analyzer"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "minitest", "~> 5.0"
27
+ spec.add_development_dependency "rails", "~> 5.0"
28
+ spec.add_development_dependency "minitest-reporters", "~> 1.1.12"
29
+ spec.add_development_dependency "minitest-color", "~> 0.0.2"
30
+ spec.add_development_dependency "byebug"
31
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_routes_analyzer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tarmo Tänav
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-reporters
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.1.12
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.1.12
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest-color
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.0.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.0.2
97
+ - !ruby/object:Gem::Dependency
98
+ name: byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - tarmo@bearmetal.eu
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".autotest"
119
+ - ".gitignore"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - Gemfile_3.2
123
+ - Gemfile_4.2
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - lib/rails_routes_analyzer.rb
130
+ - lib/rails_routes_analyzer/rails_routes_analyzer.rb
131
+ - lib/rails_routes_analyzer/route_analysis.rb
132
+ - lib/rails_routes_analyzer/route_file_annotator.rb
133
+ - lib/rails_routes_analyzer/route_interceptor.rb
134
+ - lib/rails_routes_analyzer/route_issue.rb
135
+ - lib/rails_routes_analyzer/version.rb
136
+ - lib/tasks/rails_routes_analyzer.rake
137
+ - rails_routes_analyzer.gemspec
138
+ homepage: https://github.com/bear-metal/rails_routes_analyzer
139
+ licenses:
140
+ - MIT
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.4.5.1
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Helps clean up rails routes
162
+ test_files: []