class RouteMechanic::Testing::ErrorAggregator

Attributes

unused_actions_errors[R]
unused_routes_errors[R]

Public Class Methods

new(routes, controllers) click to toggle source

@param [Array<ActionDispatch::Journey::Route>] routes @param [Array<Controller>] controllers

# File lib/route_mechanic/testing/error_aggregator.rb, line 10
def initialize(routes, controllers)
  @routes = routes
  @controllers = controllers
  @config_routes = []
  @controller_routes = []
  @unused_routes_errors = []
  @unused_actions_errors = []
end

Public Instance Methods

aggregate(unused_actions: true, unused_routes: true) click to toggle source

@param [Boolean] unused_actions @param [Boolean] unused_routes

# File lib/route_mechanic/testing/error_aggregator.rb, line 21
def aggregate(unused_actions: true, unused_routes: true)
  collect_unused_actions_errors(unused_actions)
  collect_unused_routes_errors(unused_routes)
  self
end
all_routes() click to toggle source

@return [Array<ActionDispatch::Journey::Route>]

# File lib/route_mechanic/testing/error_aggregator.rb, line 28
def all_routes
  @config_routes + @controller_routes
end
error_message() click to toggle source

@return [String]

# File lib/route_mechanic/testing/error_aggregator.rb, line 38
def error_message
  ErrorInspector.new(self).message
end
no_error?() click to toggle source

@return [Boolean]

# File lib/route_mechanic/testing/error_aggregator.rb, line 33
def no_error?
  [@unused_routes_errors, @unused_actions_errors].all?(&:empty?)
end

Private Instance Methods

collect_unused_actions_errors(report_error) click to toggle source
# File lib/route_mechanic/testing/error_aggregator.rb, line 44
def collect_unused_actions_errors(report_error)
  @controllers.each do |controller|
    controller_path = controller.controller_path
    controller.action_methods.each do |action_method|
      journey_routes = @routes.select do |route|
        route.defaults[:controller].to_sym == controller_path.to_sym && route.defaults[:action].to_sym == action_method.to_sym
      end

      if journey_routes.empty?
        @unused_actions_errors << { controller: controller, action: action_method } if report_error
      else
        wrappers = journey_routes.map { |r| RouteWrapper.new(r) }
        @controller_routes.concat(wrappers)
      end
    end
  end
end
collect_unused_routes_errors(report_error) click to toggle source
# File lib/route_mechanic/testing/error_aggregator.rb, line 62
def collect_unused_routes_errors(report_error)
  @routes.each do |journey_route|
    wrapper = RouteWrapper.new journey_route
    @config_routes << wrapper

    matched_controller_exist = @controller_routes.any? do |w|
      wrapper.controller == w.controller && wrapper.action == w.action && wrapper.path == w.path
    end

    @unused_routes_errors << wrapper if !matched_controller_exist && report_error
  end
end