Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix controller generator for ruby 1.9 hash syntax routes #51

Closed
wants to merge 1 commit into from

2 participants

@tombu

As already mentioned in #44 I encountered problems with the new_controller generator when using the new hash syntax (Ruby >= 1.9). I took a look at the generator code and found the problems origin. It´s this regular expression: /api_version.*:module\s*(=>|:)\s*("|')#{module_name_for_route(module_name)}("|')/ in https://github.com/bploetz/versionist/blob/master/lib/generators/versionist/new_controller/new_controller_generator.rb#L15

I added a ? in between the : and module to make the colon optional.

Ruby < 1.9:

api_version(:module => 'v1', :header => 'Accept', :value => 'application/vnd.mycompany.com; version=1') do
  ...
end

=> http://rubular.com/r/yUvIUVf79l

Ruby >= 1.9:

api_version(module: 'v1', header: 'Accept', value: 'application/vnd.mycompany.com; version=1') do
  ...
end

=> http://rubular.com/r/yeXkMTf6Et

Do you want me to add specs for that? If yes, do you have a suggestions to do it without introducing a lot of duplicated code?

@bploetz
Owner

@tombu #44 was due to an incompatibility with module name cases introduced in the Rails inflector in Rails 4. What specific error are you getting due to the new hash syntax? (And while we're at it, have you had a chance to try the fix for #44 yet? I'd really like to release that fix, but want a second person to verify it fixes the case problem for them in Rails 4).

Let me know what error you're getting due to the hash syntax so I have some context for the PR above. Thanks!

@tombu

When I use the generator like this:
$ rails g versionist:new_controller foos V1

I get the following output:

      create  app/controllers/v1/foos_controller.rb
/Users/Thommy/.rvm/gems/ruby-2.0.0-p0/bundler/gems/versionist-18602b9eabd1/lib/generators/versionist/new_controller/new_controller_generator.rb:19:in `block in new_controller': API version doesn't exist in config/routes.rb. Please run 'rails generate versionist:new_api_version' generator first (RuntimeError)

This error led me to the regular expression I mentioned above.

@bploetz
Owner

@tombu ah ha, I see the issue now, thanks. This same problem exists in the copy_api_version generator as well. I'll merge your change in and add specs and fix the other generator as well. Stay tuned....

@tombu

Nice, thanks. I haven´t used the copy_api_version generator yet hence I didn´t recognize the issue there. Thanks for your help. I appreciate your work!

@bploetz bploetz referenced this pull request from a commit
Brian Ploetz Issue #51: Add fix to copy_api_version generator and add specs 7905a16
@bploetz
Owner

versionist 1.2.1 has this fix. Thanks again.

@bploetz bploetz closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  lib/generators/versionist/new_controller/new_controller_generator.rb
@@ -12,7 +12,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_for_path(module_name)}")
template 'new_controller.rb', File.join("app", "controllers", "#{module_name_for_path(module_name)}", "#{file_name}_controller.rb")
- api_version_block = /api_version.*:module\s*(=>|:)\s*("|')#{module_name_for_route(module_name)}("|')/
+ api_version_block = /api_version.*:?module\s*(=>|:)\s*("|')#{module_name_for_route(module_name)}("|')/
new_route = " resources :#{file_name}\n"
matching_version_blocks = File.readlines("config/routes.rb").grep(api_version_block)
if matching_version_blocks.empty?
Something went wrong with that request. Please try again.