From 5483d160a9851a5d888a4ed444c3500cd8aca9d0 Mon Sep 17 00:00:00 2001 From: andrew_r Date: Mon, 11 Mar 2019 13:34:14 +0200 Subject: [PATCH] Replace handmade router with rails-routes gem :8ball: --- Gemfile | 1 + Gemfile.lock | 4 +++- config/application.rb | 2 -- config/initializers/router.rb | 7 ------- config/router.rb | 33 ------------------------------ config/routes.rb | 11 ---------- config/routes/articles.rb | 8 ++------ config/routes/v1/articles.rb | 10 +++------ config/routes/v2/admin/articles.rb | 11 ---------- 9 files changed, 9 insertions(+), 78 deletions(-) delete mode 100644 config/initializers/router.rb delete mode 100644 config/router.rb delete mode 100644 config/routes/v2/admin/articles.rb diff --git a/Gemfile b/Gemfile index 9479deb..2dbad35 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,7 @@ gem 'bootsnap', '>= 1.1.0', require: false gem 'mysql2', '>= 0.3.18', '< 0.5' gem 'puma', '~> 3.11' gem 'rails', '~> 5.1.5' +gem 'rails-routes' gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby] # === FEATURES === diff --git a/Gemfile.lock b/Gemfile.lock index 25d3742..41e9aa2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -151,6 +151,7 @@ GEM nokogiri (>= 1.6) rails-html-sanitizer (1.0.4) loofah (~> 2.2, >= 2.2.2) + rails-routes (0.1.2) railties (5.1.5) actionpack (= 5.1.5) activesupport (= 5.1.5) @@ -243,6 +244,7 @@ DEPENDENCIES puma (~> 3.11) pundit rails (~> 5.1.5) + rails-routes rspec-rails (~> 3.6) rubocop (~> 0.52.1) shoulda-matchers (~> 3.1) @@ -256,4 +258,4 @@ RUBY VERSION ruby 2.5.0p0 BUNDLED WITH - 1.16.1 + 1.17.3 diff --git a/config/application.rb b/config/application.rb index 003538c..681eb68 100644 --- a/config/application.rb +++ b/config/application.rb @@ -1,7 +1,5 @@ require_relative 'boot' -require_relative 'router' - require 'rails' require 'active_model/railtie' require 'active_job/railtie' diff --git a/config/initializers/router.rb b/config/initializers/router.rb deleted file mode 100644 index 2da48f4..0000000 --- a/config/initializers/router.rb +++ /dev/null @@ -1,7 +0,0 @@ -module ActionDispatch - module Routing - class Mapper - include Router - end - end -end diff --git a/config/router.rb b/config/router.rb deleted file mode 100644 index d877580..0000000 --- a/config/router.rb +++ /dev/null @@ -1,33 +0,0 @@ -module Router - ROUTES_PATH = 'config/routes/**/*.rb'.freeze - - def specify(route_name) - extend(route_module(route_name)) - - call - end - - class << self - def included(_) - routes.each(&method(:require)) - end - - private - - def routes - Dir[Rails.root.join(ROUTES_PATH)] - end - end - - private - - def route_module(route_name) - "Routes::#{resource(route_name)}".constantize - end - - def resource(route_name) - [@scope.namespace, route_name.to_s.camelize] - .reject(&:blank?) - .join('::') - end -end diff --git a/config/routes.rb b/config/routes.rb index 5f9c57a..1daf9a4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,13 +1,2 @@ Rails.application.routes.draw do - scope :v1, module: :v1, defaults: { format: :json } do - specify :articles - end - - scope :v2, module: :v2 do - scope :admin, module: :admin do - specify :articles - end - end - - specify :articles end diff --git a/config/routes/articles.rb b/config/routes/articles.rb index 91707b2..ac4b23d 100644 --- a/config/routes/articles.rb +++ b/config/routes/articles.rb @@ -1,7 +1,3 @@ -module Routes - module Articles - def call - resources :articles, only: %i[show] - end - end +Rails.application.routes.draw do + resources :articles, only: %i[show] end diff --git a/config/routes/v1/articles.rb b/config/routes/v1/articles.rb index 04f280b..39fea4d 100644 --- a/config/routes/v1/articles.rb +++ b/config/routes/v1/articles.rb @@ -1,9 +1,5 @@ -module Routes - module V1 - module Articles - def call - resources :articles, only: %i[index] - end - end +Rails.application.routes.draw do + scope :v1, module: :v1, defaults: { format: :json } do + resources :articles, only: %i[index] end end diff --git a/config/routes/v2/admin/articles.rb b/config/routes/v2/admin/articles.rb deleted file mode 100644 index df6833f..0000000 --- a/config/routes/v2/admin/articles.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Routes - module V2 - module Admin - module Articles - def call - resources :articles, only: %i[show destroy] - end - end - end - end -end