Sha256: 4fb5fe905dcadaeaeff9525448f3240be6041aecdef42689681ee5000c740c4e

Contents?: true

Size: 1.5 KB

Versions: 7

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module RailsBestPractices
  module Core
    class Routes < Array
      # add a route.
      #
      # @param [Array] namesapces
      # @param [String] controller name
      # @param [String] action name
      def add_route(namespaces, controller_name, action_name)
        if namespaces.present? || controller_name.present?
          self << Route.new(namespaces, controller_name, action_name)
        end
      end
    end

    class Route
      attr_reader :namespaces, :controller_name, :action_name

      def initialize(namespaces, controller_name, action_name)
        @namespaces = namespaces

        # mappings can be specified by e.g.
        #   post 'some/:pattern' => 'controller#action'
        if action_name.is_a?(String) && action_name =~ /\A(\w+)#(\w+)\z/
          controller_name = Regexp.last_match(1)
          action_name = Regexp.last_match(2)
        end

        if controller_name
          entities = controller_name.split('/')
          @namespaces += entities[0..-2] if entities.size > 1
          @controller_name = entities.last
        end
        @action_name = action_name
      end

      def controller_name_with_namespaces
        if controller_name
          namespaces.map { |namespace| "#{namespace.camelize}::" }.join('') + "#{controller_name.camelize}Controller"
        else
          namespaces.map(&:camelize).join('::') + 'Controller'
        end
      end

      def to_s
        "#{controller_name_with_namespaces}\##{action_name}"
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rails_best_practices-1.23.2 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.23.1 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.23.0 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.22.1 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.22.0 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.21.0 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.20.1 lib/rails_best_practices/core/routes.rb