Skip to content

Fix nullable scope path creation #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/js_rails_routes/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Route
def initialize(route)
@route = route
@name = route.name
@path = route.path.spec.to_s.split('(').first
@path = route.path.spec.to_s.sub(/\(.:format\)/, '').delete('(').delete(')')
end

# @return [Boolean]
Expand Down
18 changes: 15 additions & 3 deletions spec/js_rails_routes/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

let(:language) { instance_double('JSRailsRoutes::Language::Base', handle_route_set: body, ext: %w[js ts].sample) }
let(:body) { 'hello' }
let(:route_set_list) { [rails_route_set, engine_route_set] }
let(:route_set_list) { [rails_route_set, engine_route_set, nullable_scope_route_set] }

let(:rails_route_set) do
route_set = ActionDispatch::Routing::RouteSet.new.tap do |routes|
Expand All @@ -18,6 +18,17 @@
JSRailsRoutes::RouteSet.new('Rails', route_set)
end

let(:nullable_scope_route_set) do
route_set = ActionDispatch::Routing::RouteSet.new.tap do |routes|
routes.draw do
scope '(:/locale)' do
get '/users' => 'users#index'
end
end
end
JSRailsRoutes::RouteSet.new('Users::Engine', route_set)
end

let(:engine_route_set) do
route_set = ActionDispatch::Routing::RouteSet.new.tap do |routes|
routes.draw do
Expand All @@ -33,9 +44,10 @@
it 'returns an array of artifacts' do
expect(subject).to contain_exactly(
an_object_having_attributes(engine_name: rails_route_set.name, body: body),
an_object_having_attributes(engine_name: engine_route_set.name, body: body)
an_object_having_attributes(engine_name: engine_route_set.name, body: body),
an_object_having_attributes(engine_name: nullable_scope_route_set.name, body: body)
)
expect(language).to have_received(:handle_route_set).twice
expect(language).to have_received(:handle_route_set).exactly(3).times
end
end
end
8 changes: 8 additions & 0 deletions spec/js_rails_routes/language/javascript_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
rails_route_set = ActionDispatch::Routing::RouteSet.new.tap do |routes|
routes.draw do
resources :articles
scope '(/:locale)' do
get '/users' => 'users#index'
end
end
end
JSRailsRoutes::RouteSet.new('Rails', rails_route_set)
Expand All @@ -43,6 +46,7 @@
export function new_article_path(params) { return process('/articles/new', params, []); }
export function edit_article_path(params) { return process('/articles/' + params.id + '/edit', params, ['id']); }
export function article_path(params) { return process('/articles/' + params.id + '', params, ['id']); }
export function users_path(params) { return process('/' + params.locale + '/users', params, ['locale']); }
JAVASCRIPT
end
end
Expand All @@ -61,6 +65,7 @@
export function newArticlePath(params) { return process('/articles/new', params, []); }
export function editArticlePath(params) { return process('/articles/' + params.id + '/edit', params, ['id']); }
export function articlePath(params) { return process('/articles/' + params.id + '', params, ['id']); }
export function usersPath(params) { return process('/' + params.locale + '/users', params, ['locale']); }
JAVASCRIPT
end
end
Expand All @@ -79,6 +84,7 @@
export function NewArticlePath(params) { return process('/articles/new', params, []); }
export function EditArticlePath(params) { return process('/articles/' + params.Id + '/edit', params, ['Id']); }
export function ArticlePath(params) { return process('/articles/' + params.Id + '', params, ['Id']); }
export function UsersPath(params) { return process('/' + params.Locale + '/users', params, ['Locale']); }
JAVASCRIPT
end
end
Expand Down Expand Up @@ -111,6 +117,7 @@
export function articles_path(params) { return process('/articles', params, []); }
export function edit_article_path(params) { return process('/articles/' + params.id + '/edit', params, ['id']); }
export function article_path(params) { return process('/articles/' + params.id + '', params, ['id']); }
export function users_path(params) { return process('/' + params.locale + '/users', params, ['locale']); }
JAVASCRIPT
end
end
Expand Down Expand Up @@ -143,6 +150,7 @@
export function articles_path(params) { return process('/articles', params, []); }
export function edit_article_path(params) { return process('/articles/' + params.id + '/edit', params, ['id']); }
export function article_path(params) { return process('/articles/' + params.id + '', params, ['id']); }
export function users_path(params) { return process('/' + params.locale + '/users', params, ['locale']); }
JAVASCRIPT
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/js_rails_routes/route_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
routes.draw do
get '/articles' => 'articles#index'
get '/users' => 'users#index'
scope '(/:locale)' do
get '/users' => 'users#index'
end
end
end
end
Expand Down Expand Up @@ -48,6 +51,7 @@
it "doesn't include the excluded route" do
expect(subject).to include be_a(JSRailsRoutes::Route).and(have_attributes(name: /articles/))
expect(subject).not_to include be_a(JSRailsRoutes::Route).and(have_attributes(name: /users/))
expect(subject).not_to include be_a(JSRailsRoutes::Route).and(have_attributes(name: /:locale/))
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/js_rails_routes/route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

RSpec.describe JSRailsRoutes::Route do
subject(:route) { described_class.new(raw_route) }
subject(:nullable_scope_route) { described_class.new(nullable_scope_raw_route) }

include_context 'run in a sandbox'

Expand All @@ -13,6 +14,16 @@
end.routes.first
end

let(:nullable_scope_raw_route) do
ActionDispatch::Routing::RouteSet.new.tap do |routes|
routes.draw do
scope '(/:locale)' do
get '/users' => 'users#index'
end
end
end.routes.first
end

describe '#name' do
subject { route.name }

Expand All @@ -31,6 +42,10 @@
subject { route.path }

it { is_expected.to eq '/articles' }

it 'returns url include nullable scope' do
expect(nullable_scope_route.path).to eq '/:locale/users'
end
end

describe '#match?' do
Expand Down