Skip to content

Commit

Permalink
Merge branch 'generate-resourceful-routes' of https://github.com/clln…
Browse files Browse the repository at this point in the history
…s/lotus into cllns-generate-resourceful-routes
  • Loading branch information
jodosha committed Dec 16, 2015
2 parents da5c73f + a1391fd commit 485b3fe
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 9 deletions.
19 changes: 18 additions & 1 deletion lib/lotus/commands/generate/action.rb
Expand Up @@ -32,6 +32,17 @@ class Action < Abstract
"Destroy" => "DELETE"
}.freeze

# For resourceful actions, what to add to the end of the base URL
# @since x.x.x
# @api private
RESOURCEFUL_ROUTE_URL_SUFFIXES = {
"Show" => "/:id",
"Update" => "/:id",
"Destroy" => "/:id",
"New" => "/new",
"Edit" => "/:id/edit",
}

def initialize(options, application_name, controller_and_action_name)
super(options)
if !environment.container?
Expand Down Expand Up @@ -113,7 +124,13 @@ def resourceful_http_method
# @since x.x.x
# @api private
def route_url
options.fetch(:url, "/#{ @controller_pathname }")
options.fetch(:url, "/#{ @controller_pathname }#{resourceful_route_url_suffix}")
end

# @since x.x.x
# @api private
def resourceful_route_url_suffix
RESOURCEFUL_ROUTE_URL_SUFFIXES.fetch(@action_name, "")
end

# @since x.x.x
Expand Down
66 changes: 58 additions & 8 deletions test/commands/generate/action_test.rb
Expand Up @@ -98,8 +98,38 @@
end
end

describe 'resourceful HTTP default methods' do
it 'uses POST for "create" action' do
describe 'RESTful resource routes' do
it 'makes `index` route' do
with_temp_dir do |original_wd|
setup_container_app
command = Lotus::Commands::Generate::Action.new({}, 'web', 'books/index')
capture_io { command.start }

assert_file_includes('apps/web/config/routes.rb', "get '/books', to: 'books#index")
end
end

it 'makes `show` route' do
with_temp_dir do |original_wd|
setup_container_app
command = Lotus::Commands::Generate::Action.new({}, 'web', 'books/show')
capture_io { command.start }

assert_file_includes('apps/web/config/routes.rb', "get '/books/:id', to: 'books#show'")
end
end

it 'makes `new` route' do
with_temp_dir do |original_wd|
setup_container_app
command = Lotus::Commands::Generate::Action.new({}, 'web', 'books/new')
capture_io { command.start }

assert_file_includes('apps/web/config/routes.rb', "get '/books/new', to: 'books#new'")
end
end

it 'makes `create` route' do
with_temp_dir do |original_wd|
setup_container_app
command = Lotus::Commands::Generate::Action.new({}, 'web', 'books/create')
Expand All @@ -109,33 +139,53 @@
end
end

it 'uses PATCH for "update" action' do
it 'makes `edit` route' do
with_temp_dir do |original_wd|
setup_container_app
command = Lotus::Commands::Generate::Action.new({}, 'web', 'books/edit')
capture_io { command.start }

assert_file_includes('apps/web/config/routes.rb', "get '/books/:id/edit', to: 'books#edit'")
end
end

it 'makes `update` route' do
with_temp_dir do |original_wd|
setup_container_app
command = Lotus::Commands::Generate::Action.new({}, 'web', 'books/update')
capture_io { command.start }

assert_file_includes('apps/web/config/routes.rb', "patch '/books', to: 'books#update'")
assert_file_includes('apps/web/config/routes.rb', "patch '/books/:id', to: 'books#update'")
end
end

it 'uses DELETE for "destroy" action' do
it 'makes `destroy`' do
with_temp_dir do |original_wd|
setup_container_app
command = Lotus::Commands::Generate::Action.new({}, 'web', 'books/destroy')
capture_io { command.start }

assert_file_includes('apps/web/config/routes.rb', "delete '/books', to: 'books#destroy'")
assert_file_includes('apps/web/config/routes.rb', "delete '/books/:id', to: 'books#destroy'")
end
end

it 'does not override user specified http method' do
with_temp_dir do |original_wd|
setup_container_app
command = Lotus::Commands::Generate::Action.new({method: 'get'}, 'web', 'books/create')
command = Lotus::Commands::Generate::Action.new({method: 'get'}, 'web', 'books/destroy')
capture_io { command.start }

assert_file_includes('apps/web/config/routes.rb', "get '/books', to: 'books#create'")
assert_file_includes('apps/web/config/routes.rb', "get '/books/:id', to: 'books#destroy'")
end
end

it 'does not override user specified URL' do
with_temp_dir do |original_wd|
setup_container_app
command = Lotus::Commands::Generate::Action.new({url: '/books'}, 'web', 'books/destroy')
capture_io { command.start }

assert_file_includes('apps/web/config/routes.rb', "delete '/books', to: 'books#destroy'")
end
end
end
Expand Down

0 comments on commit 485b3fe

Please sign in to comment.