lib/chusaku.rb in chusaku-1.4.0 vs lib/chusaku.rb in chusaku-1.4.1

- old
+ new

@@ -27,18 +27,11 @@ exclusion_pattern = @flags[:exclusion_pattern] || DEFAULT_EXCLUSION_PATTERN controllers_paths = FileList .new(Rails.root.join(controllers_pattern)) .exclude(Rails.root.join(exclusion_pattern)) - @routes.each do |controller, actions| - next unless controller - - controller_class = "#{controller.underscore.camelize}Controller".constantize - action_method_name = actions.keys.first&.to_sym - next unless !action_method_name.nil? && controller_class.method_defined?(action_method_name) - - source_path = controller_class.instance_method(action_method_name).source_location&.[](0) + source_paths_map.each do |source_path, actions| next unless controllers_paths.include?(source_path) annotate_file(path: source_path, actions: actions) end @@ -54,10 +47,38 @@ end end private + # Maps source paths to their respective routes. + # + # Example output: + # + # { + # "/path/to/users_controller.rb" => { + # "edit" => [...], + # "update" => [...] + # } + # } + # + # @return [Hash] Source paths mapped to their respective routes + def source_paths_map + map = {} + + @routes.each do |controller, actions| + actions.each do |action, data| + data.each do |datum| + map[datum[:source_path]] ||= {} + map[datum[:source_path]][action] ||= [] + map[datum[:source_path]][action].push(datum) + end + end + end + + map + end + # Adds annotations to the given file. # # @param path [String] Path to file # @param actions [Hash<String, Hash>] List of valid action data for the controller # @return [void] @@ -108,11 +129,12 @@ # # @param verb [String] HTTP verb for route # @param path [String] Rails path for route # @param name [String] Name used in route helpers # @param defaults [Hash] Default parameters for route + # @param source_path [String] Path to controller file # @return [String] "@route <verb> <path> {<defaults>} (<name>)" - def annotate_route(verb:, path:, name:, defaults:) + def annotate_route(verb:, path:, name:, defaults:, source_path:) annotation = "@route #{verb} #{path}" if defaults&.any? defaults_str = defaults .map { |key, value| "#{key}: #{value.inspect}" }