Skip to content

Commit

Permalink
Accounting for double underscores in module names for Rails' inflector
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Ploetz committed Feb 22, 2012
1 parent 385b348 commit 019da12
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/generators/versionist/new_controller/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Description:
Creates a new controller class with the given name under the given module namespace

Example:
rails generate versionist:new_controller foo V3_4
rails generate versionist:new_controller test V2_0_0

This will create:
app/controllers/V3_4/foo_controller.rb
app/controllers/V2_0_0/test_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def new_controller
raise "API module namespace #{module_name} doesn't exist. Please run \'rails generate versionist:new_api_version\' generator first" if !File.exists?("app/controllers/#{module_name.underscore}")
template 'new_controller.rb', File.join("app", "controllers", "#{module_name.underscore}", "#{file_name}_controller.rb")

api_version_block = /api_version.*:module\s*(=>|:)\s*("|')#{module_name}("|').*do/
api_version_block = /api_version.*:module\s*(=>|:)\s*("|')#{module_name.gsub(/_{1}/, "__")}("|').*do/
new_route = " resources :#{file_name}\n"
matching_version_blocks = File.readlines("config/routes.rb").grep(api_version_block)
if matching_version_blocks.empty?
Expand Down
6 changes: 3 additions & 3 deletions spec/generators/new_controller_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
context "#{name} => #{mod}" do
before :each do
::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/controllers/#{mod.underscore}", __FILE__))
::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{mod}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-v1\") do\n end\nend"}
::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{mod.gsub(/_{1}/, "__")}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-v1\") do\n end\nend"}
run_generator [name, mod]
end

Expand Down Expand Up @@ -73,15 +73,15 @@
context "#{name} => #{mod}" do
before :each do
::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/controllers/#{mod.underscore}", __FILE__))
::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{mod}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-v1\") do\n end\nend"}
::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{mod.gsub(/_{1}/, "__")}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-v1\") do\n end\nend"}
run_generator [name, mod]
end

it "should add the new resource to the existing scope in routes.rb" do
assert_file "config/routes.rb"
expected = <<-CONTENTS
Test::Application.routes.draw do
api_version(:module => "#{mod}", :header => "Accept", :value => "application/vnd.mycompany.com-v1") do
api_version(:module => "#{mod.gsub(/_{1}/, "__")}", :header => "Accept", :value => "application/vnd.mycompany.com-v1") do
resources :#{name}
end
end
Expand Down

0 comments on commit 019da12

Please sign in to comment.