Sha256: e9f9926f5ddd10987ead7afb0bc126a11159f5d2b235816173035e3f6f7e2aa0

Contents?: true

Size: 1.45 KB

Versions: 22

Compression:

Stored size: 1.45 KB

Contents

# encoding: utf-8
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 =~ /^(\w+)#(\w+)$/
          controller_name = $1
          action_name = $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 { |namespace| namespace.camelize }.join("::") + "Controller"
        end
      end

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

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
rails_best_practices-1.18.1 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.18.0 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.17.0 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.16.0 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.15.7 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.15.6 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.15.4 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.15.3 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.15.2 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.15.1 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.14.4 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.14.3 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.14.2 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.14.1 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.14.0 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.13.8 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.13.5 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.13.4 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.13.3 lib/rails_best_practices/core/routes.rb
rails_best_practices-1.13.2 lib/rails_best_practices/core/routes.rb