@@ -14,7 +14,12 @@ def controllers
1414 Rails . application . eager_load!
1515
1616 logger . info "Collecting controllers"
17- ActionController ::Base . descendants + ActionController ::API . descendants
17+ if defined? ( ActionController ::API )
18+ ActionController ::Base . descendants + ActionController ::API . descendants
19+ else
20+ # older rails
21+ ActionController ::Base . descendants
22+ end
1823 end
1924 end
2025
@@ -35,6 +40,7 @@ def controller_class_by_name(controller_name)
3540 rescue NameError
3641 @missing_controllers << controller_name
3742 logger . warn "Controller #{ controller_name } looks not existing"
43+ nil
3844 end
3945
4046 def existing_actions_usage_hash
@@ -45,14 +51,18 @@ def existing_actions_usage_hash
4551 controller_name = controller . name . sub ( /Controller$/ , "" ) . underscore
4652 controller . action_methods . map { |action | "#{ controller_name } ##{ action } " }
4753 end
48- controller_actions . flatten . to_h { |action | [ action , 0 ] }
54+ controller_actions . flatten . map { |action | [ action , 0 ] } . to_h
4955 end
5056 end
5157
58+ def all_routes
59+ # NB: there're no engines
60+ @all_routes ||= RoutesCoverage . _collect_all_routes
61+ end
62+
5263 def perform
5364 require 'routes_coverage'
54- # NB: there're no engines
55- routes = RoutesCoverage . _collect_all_routes
65+ routes = all_routes
5666
5767 @missing_actions = Hash . new ( 0 )
5868 @existing_actions_usage_hash = nil
@@ -104,19 +114,33 @@ def unused_actions
104114 end
105115
106116 def print_missing_actions
107- logger . info "Missing #{ missing_actions . count } actions:"
117+ logger . info "\n Missing #{ missing_actions . count } actions:"
108118
109- # NB: для `resource` могут лезть лишние index в преложениях
119+ # NB: for singular `resource` there may be unnecessary ` index` in suggestions
110120 restful_actions = %w[ index new create show edit update destroy ] . freeze
121+
122+ declared_restful = all_routes . select { |route |
123+ route . respond_to? ( :requirements ) && route . requirements [ :controller ] &&
124+ restful_actions . include? ( route . requirements [ :action ] )
125+ } . group_by { |route | route . requirements [ :controller ] }
126+
111127 missing_actions . keys . map { |action | action . split ( '#' , 2 ) } . group_by ( &:first ) . each do |( controller , actions ) |
112128 missing = actions . map ( &:last )
113- if ( restful_actions & missing ) . any?
114- logger . info "#{ controller } , except: %i[#{ ( restful_actions & missing ) . join ( ' ' ) } ], " \
115- "only: %i[#{ ( restful_actions - missing ) . join ( ' ' ) } ]"
116- end
117-
118- missing_custom = missing - restful_actions
119- logger . info "#{ controller } missing custom: #{ missing_custom . join ( ', ' ) } " if missing_custom . any?
129+ next if missing . empty?
130+
131+ undeclared_restful = restful_actions - declared_restful [ controller ] . map { |r | r . requirements [ :action ] }
132+ logger . info ( [
133+ "#{ controller } :" ,
134+ ( if ( restful_actions & missing ) . any?
135+ "#{ ( missing & restful_actions ) . join ( ', ' ) } " \
136+ ", except: %i[#{ ( restful_actions & ( missing + undeclared_restful ) ) . join ( ' ' ) } ]" \
137+ ", only: %i[#{ ( restful_actions - ( missing + undeclared_restful ) ) . join ( ' ' ) } ]"
138+ end ) ,
139+ ( if ( missing - restful_actions ) . any?
140+ ", Missing custom: #{ ( missing - restful_actions ) . join ( ', ' ) } "
141+ end )
142+
143+ ] . compact . join ( ' ' ) )
120144 end
121145 end
122146
0 commit comments