bploetz / versionist

A plugin for versioning Rails based RESTful APIs.
MIT License
971 stars 51 forks source link

Rails4: Undefined method 'api_version' #38

Closed howcast closed 11 years ago

howcast commented 11 years ago

I'm trying to use the Rails4 branch with Rails4.0.0-beta1 but keep getting that error.

Here is the stack trace:

project/config/routes.rb:4:in `block in <top (required)>': undefined method `api_version' for #<ActionDispatch::Routing::Mapper:0x007ff194f98390> (NoMethodError)
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/actionpack-4.0.0.beta1/lib/action_dispatch/routing/route_set.rb:320:in `instance_exec'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/actionpack-4.0.0.beta1/lib/action_dispatch/routing/route_set.rb:320:in `eval_block'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/actionpack-4.0.0.beta1/lib/action_dispatch/routing/route_set.rb:298:in `draw'
    from project/config/routes.rb:3:in `<top (required)>'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:222:in `load'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:222:in `block in load'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:213:in `load_dependency'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:222:in `load'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/railties-4.0.0.beta1/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/railties-4.0.0.beta1/lib/rails/application/routes_reloader.rb:40:in `each'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/railties-4.0.0.beta1/lib/rails/application/routes_reloader.rb:40:in `load_paths'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/railties-4.0.0.beta1/lib/rails/application/routes_reloader.rb:16:in `reload!'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/railties-4.0.0.beta1/lib/rails/application/routes_reloader.rb:26:in `block in updater'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/activesupport-4.0.0.beta1/lib/active_support/file_update_checker.rb:75:in `call'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/activesupport-4.0.0.beta1/lib/active_support/file_update_checker.rb:75:in `execute'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/railties-4.0.0.beta1/lib/rails/application/routes_reloader.rb:27:in `updater'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/railties-4.0.0.beta1/lib/rails/application/routes_reloader.rb:9:in `rescue in execute_if_updated'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/railties-4.0.0.beta1/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/railties-4.0.0.beta1/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/railties-4.0.0.beta1/lib/rails/initializable.rb:30:in `instance_exec'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/railties-4.0.0.beta1/lib/rails/initializable.rb:30:in `run'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/railties-4.0.0.beta1/lib/rails/initializable.rb:55:in `block in run_initializers'
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:150:in `block in tsort_each'
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:219:in `each_strongly_connected_component_from'
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:182:in `block in each_strongly_connected_component'
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:180:in `each'
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:180:in `each_strongly_connected_component'
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/railties-4.0.0.beta1/lib/rails/initializable.rb:54:in `run_initializers'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/railties-4.0.0.beta1/lib/rails/application.rb:213:in `initialize!'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/railties-4.0.0.beta1/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from project/config/environment.rb:5:in `<top (required)>'
    from project/spec/spec_helper.rb:3:in `require'
    from project/spec/spec_helper.rb:3:in `<top (required)>'
    from project/spec/controllers/v2/categories_controller_spec.rb:1:in `require'
    from project/spec/controllers/v2/categories_controller_spec.rb:1:in `<top (required)>'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
    from ~/.rvm/gems/ruby-2.0.0-p0@platform/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
bploetz commented 11 years ago

What do your Gemfile/Gemfile.lock files look like?

howcast commented 11 years ago

Thanks for the quick response Brian!

Gemfile:

source 'https://rubygems.org'

gem 'rails', '4.0.0.beta1'

## Database
gem 'mongoid', '~> 4.0.0', git: 'https://github.com/mongoid/mongoid.git'

## Authentication
gem 'omniauth'
gem 'omniauth-oauth2'

## Utilities
gem 'kaminari'
gem 'rest-client'
gem 'multimap'
gem 'figaro'

## Mongoid Specific
gem 'mongoid_slug'
gem 'workflow_on_mongoid'

## File Uploading & Processing
gem 'fog'
gem 'blitline'

## Client Side
gem 'jquery-rails'
gem 'turbolinks'

## API
gem 'rabl'
gem 'oj'
gem 'versionist', git: 'https://github.com/bploetz/versionist.git', branch: 'rails4'

## Howcast Internal Gems & Engines
gem 'howcast_core', path: "/Users/feliks/Projects/Playground/howcast_core"

## Groups
group :assets do
  gem 'sass-rails',   '~> 4.0.0.beta1'
  gem 'coffee-rails', '~> 4.0.0.beta1'
  gem 'haml-rails'
  gem 'therubyracer', platforms: :ruby, require: 'v8'
  gem 'execjs'
  gem 'uglifier', '>= 1.0.3'
end

group :development, :test do
  gem 'rspec-rails'
  gem 'mongoid-rspec'
  gem 'capybara'
  gem 'fabrication'
  gem 'ffaker'
  gem 'launchy'
  gem 'database_cleaner'
  gem 'pry'
  gem "better_errors"
  gem "binding_of_caller"
end

Gemfile.lock:

GIT
  remote: https://github.com/bploetz/versionist.git
  revision: 9da9b47be825c86a24d8d784bf51b5a7de8250eb
  branch: rails4
  specs:
    versionist (2.0.0.beta1)
      rails (>= 3)
      yard (~> 0.7)

GIT
  remote: https://github.com/mongoid/mongoid.git
  revision: de244e9c323d777cdf6c260e9461200ad932185b
  specs:
    mongoid (4.0.0)
      activemodel (~> 4.0.0.beta)
      moped (~> 1.4.2)
      origin (~> 1.0)
      tzinfo (~> 0.3.22)

PATH
  remote: /Users/feliks/Projects/Playground/howcast_core
  specs:
    howcast_core (0.0.1)
      rails (~> 4.0.0.beta1)

GEM
  remote: https://rubygems.org/
  specs:
    actionmailer (4.0.0.beta1)
      actionpack (= 4.0.0.beta1)
      mail (~> 2.5.3)
    actionpack (4.0.0.beta1)
      activesupport (= 4.0.0.beta1)
      builder (~> 3.1.0)
      erubis (~> 2.7.0)
      rack (~> 1.5.2)
      rack-test (~> 0.6.2)
    activemodel (4.0.0.beta1)
      activesupport (= 4.0.0.beta1)
      builder (~> 3.1.0)
    activerecord (4.0.0.beta1)
      activemodel (= 4.0.0.beta1)
      activerecord-deprecated_finders (~> 0.0.3)
      activesupport (= 4.0.0.beta1)
      arel (~> 4.0.0.beta1)
    activerecord-deprecated_finders (0.0.3)
    activesupport (4.0.0.beta1)
      i18n (~> 0.6.2)
      minitest (~> 4.2)
      multi_json (~> 1.3)
      thread_safe (~> 0.1)
      tzinfo (~> 0.3.33)
    addressable (2.3.3)
    arel (4.0.0.beta2)
    atomic (1.1.6)
    better_errors (0.8.0)
      coderay (>= 1.0.0)
      erubis (>= 2.6.6)
    binding_of_caller (0.7.1)
      debug_inspector (>= 0.0.1)
    blitline (2.1.0)
      multi_json
    builder (3.1.4)
    capybara (2.0.3)
      mime-types (>= 1.16)
      nokogiri (>= 1.3.3)
      rack (>= 1.0.0)
      rack-test (>= 0.5.4)
      selenium-webdriver (~> 2.0)
      xpath (~> 1.0.0)
    childprocess (0.3.9)
      ffi (~> 1.0, >= 1.0.11)
    coderay (1.0.9)
    coffee-rails (4.0.0.beta1)
      coffee-script (>= 2.2.0)
      railties (>= 4.0.0.beta, < 5.0)
    coffee-script (2.2.0)
      coffee-script-source
      execjs
    coffee-script-source (1.6.2)
    database_cleaner (0.9.1)
    debug_inspector (0.0.2)
    diff-lcs (1.2.2)
    erubis (2.7.0)
    excon (0.20.1)
    execjs (1.4.0)
      multi_json (~> 1.0)
    fabrication (2.6.4)
    faraday (0.8.7)
      multipart-post (~> 1.1)
    ffaker (1.15.0)
    ffi (1.6.0)
    figaro (0.6.3)
      bundler (~> 1.0)
      rails (>= 3, < 5)
    fog (1.10.1)
      builder
      excon (~> 0.20)
      formatador (~> 0.2.0)
      mime-types
      multi_json (~> 1.0)
      net-scp (~> 1.1)
      net-ssh (>= 2.1.3)
      nokogiri (~> 1.5.0)
      ruby-hmac
    formatador (0.2.4)
    haml (4.0.2)
      tilt
    haml-rails (0.4)
      actionpack (>= 3.1, < 4.1)
      activesupport (>= 3.1, < 4.1)
      haml (>= 3.1, < 4.1)
      railties (>= 3.1, < 4.1)
    hashie (2.0.3)
    hike (1.2.1)
    httpauth (0.2.0)
    i18n (0.6.4)
    jquery-rails (2.2.1)
      railties (>= 3.0, < 5.0)
      thor (>= 0.14, < 2.0)
    json (1.7.7)
    jwt (0.1.8)
      multi_json (>= 1.5)
    kaminari (0.14.1)
      actionpack (>= 3.0.0)
      activesupport (>= 3.0.0)
    launchy (2.2.0)
      addressable (~> 2.3)
    libv8 (3.11.8.17)
    mail (2.5.3)
      i18n (>= 0.4.0)
      mime-types (~> 1.16)
      treetop (~> 1.4.8)
    method_source (0.8.1)
    mime-types (1.22)
    minitest (4.7.0)
    mongoid-rspec (1.7.0)
      mongoid (>= 3.0.1)
      rake
      rspec (>= 2.9)
    mongoid_slug (3.0.0)
      mongoid (> 3.0)
      stringex (~> 1.4)
    moped (1.4.5)
    multi_json (1.7.2)
    multimap (1.1.2)
    multipart-post (1.2.0)
    net-scp (1.1.0)
      net-ssh (>= 2.6.5)
    net-ssh (2.6.6)
    nokogiri (1.5.9)
    oauth2 (0.8.1)
      faraday (~> 0.8)
      httpauth (~> 0.1)
      jwt (~> 0.1.4)
      multi_json (~> 1.0)
      rack (~> 1.2)
    oj (2.0.10)
    omniauth (1.1.4)
      hashie (>= 1.2, < 3)
      rack
    omniauth-oauth2 (1.1.1)
      oauth2 (~> 0.8.0)
      omniauth (~> 1.0)
    origin (1.0.11)
    polyglot (0.3.3)
    pry (0.9.12)
      coderay (~> 1.0.5)
      method_source (~> 0.8)
      slop (~> 3.4)
    rabl (0.8.2)
      activesupport (>= 2.3.14)
    rack (1.5.2)
    rack-test (0.6.2)
      rack (>= 1.0)
    rails (4.0.0.beta1)
      actionmailer (= 4.0.0.beta1)
      actionpack (= 4.0.0.beta1)
      activerecord (= 4.0.0.beta1)
      activesupport (= 4.0.0.beta1)
      bundler (>= 1.3.0, < 2.0)
      railties (= 4.0.0.beta1)
      sprockets-rails (~> 2.0.0.rc3)
    railties (4.0.0.beta1)
      actionpack (= 4.0.0.beta1)
      activesupport (= 4.0.0.beta1)
      rake (>= 0.8.7)
      rdoc (~> 3.4)
      thor (>= 0.17.0, < 2.0)
    rake (10.0.4)
    rdoc (3.12.2)
      json (~> 1.4)
    ref (1.0.4)
    rest-client (1.6.7)
      mime-types (>= 1.16)
    rspec (2.13.0)
      rspec-core (~> 2.13.0)
      rspec-expectations (~> 2.13.0)
      rspec-mocks (~> 2.13.0)
    rspec-core (2.13.1)
    rspec-expectations (2.13.0)
      diff-lcs (>= 1.1.3, < 2.0)
    rspec-mocks (2.13.1)
    rspec-rails (2.13.0)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      railties (>= 3.0)
      rspec-core (~> 2.13.0)
      rspec-expectations (~> 2.13.0)
      rspec-mocks (~> 2.13.0)
    ruby-hmac (0.4.0)
    rubyzip (0.9.9)
    sass (3.2.7)
    sass-rails (4.0.0.beta1)
      railties (>= 4.0.0.beta, < 5.0)
      sass (>= 3.1.10)
      sprockets-rails (~> 2.0.0.rc0)
      tilt (~> 1.3)
    selenium-webdriver (2.31.0)
      childprocess (>= 0.2.5)
      multi_json (~> 1.0)
      rubyzip
      websocket (~> 1.0.4)
    slop (3.4.4)
    sprockets (2.9.0)
      hike (~> 1.2)
      multi_json (~> 1.0)
      rack (~> 1.0)
      tilt (~> 1.1, != 1.3.0)
    sprockets-rails (2.0.0.rc3)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      sprockets (~> 2.8)
    stringex (1.5.1)
    therubyracer (0.11.4)
      libv8 (~> 3.11.8.12)
      ref
    thor (0.18.1)
    thread_safe (0.1.0)
      atomic
    tilt (1.3.6)
    treetop (1.4.12)
      polyglot
      polyglot (>= 0.3.1)
    turbolinks (1.1.1)
      coffee-rails
    tzinfo (0.3.37)
    uglifier (2.0.1)
      execjs (>= 0.3.0)
      multi_json (~> 1.0, >= 1.0.2)
    websocket (1.0.7)
    workflow (0.8.7)
    workflow_on_mongoid (0.8.0.7)
      mongoid
      workflow (~> 0.8)
    xpath (1.0.0)
      nokogiri (~> 1.3)
    yard (0.8.5.2)

PLATFORMS
  ruby

DEPENDENCIES
  better_errors
  binding_of_caller
  blitline
  capybara
  coffee-rails (~> 4.0.0.beta1)
  database_cleaner
  execjs
  fabrication
  ffaker
  figaro
  fog
  haml-rails
  howcast_core!
  jquery-rails
  kaminari
  launchy
  mongoid (~> 4.0.0)!
  mongoid-rspec
  mongoid_slug
  multimap
  oj
  omniauth
  omniauth-oauth2
  pry
  rabl
  rails (= 4.0.0.beta1)
  rest-client
  rspec-rails
  sass-rails (~> 4.0.0.beta1)
  therubyracer
  turbolinks
  uglifier (>= 1.0.3)
  versionist!
  workflow_on_mongoid
bploetz commented 11 years ago

Hmmm, looks ok. Let me try to reproduce locally. Stay tuned.....

howcast commented 11 years ago

awesome, thanks so much Brian!

bploetz commented 11 years ago

I created a fresh Rails4 app (using Ruby 2.0.0 like you are) and I'm able to reproduce this issue:

bundle exec rake routes
rake aborted!
undefined method `api_version' for #<ActionDispatch::Routing::Mapper:0x007fd4cbb22800>
/Users/bploetz/workspace/test-api-rails4/config/routes.rb:12:in `block (2 levels) in <top (required)>'

What's really concerning is that all of the versionist specs pass on Rails 4. :-/

Let me hunt this one down and push a fix up to that branch. Stay tuned.....

bploetz commented 11 years ago

Duh:

https://github.com/bploetz/versionist/commit/45f173348d814c8ee805511d3028797798c4fb07#L1L21

Run bundle update versionist and try again?

howcast commented 11 years ago

So that error went away but now i'm getting this error:

lib/action_dispatch/routing/mapper.rb:241:in `default_controller_and_action': 'V2/categories' is not a supported controller name. This can lead to potential routing problems. See http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use (ArgumentError)

I generated a controller and route with the versionist generator so that we can eliminate typos as a cause.

This is my controller:

class V2::CategoriesController < V2::BaseController
  def index
    @categories = Category.all
    respond_with @categories
  end
end

This is my routes file:

Platform::Application.routes.draw do
  api_version(:module => "V2", :header => {:name => "Accept", :value => "application/vnd.howcast.com; version=2"}, :path => {:value => "v2"}, :defaults => {:format => "json"}, :default => true) do
    resources :categories
  end
end

Thanks so much for your help with this.

bploetz commented 11 years ago

Looks like a Rails 4 issue:

https://github.com/rails/rails/issues/9231 https://github.com/rails/rails/pull/9234

Not sure if the released Rails 4 RC contains this fix or not. Can you try pointing your app to Rails edge and see if you still see the problem?

howcast commented 11 years ago

Yep, you're right. Doesn't look like this is still fixed even in master for Rails4 with Ruby 2.0. Thanks for all your help!

bploetz commented 11 years ago

They don't call it bleeding edge for nothing. ;-) Good luck......

howcast commented 11 years ago

Haha, yes that is true. I found the issue however.

The versionist generator generates the route as follows:

api_version(:module => "V2" ...)

But when I changed the module name to a lowercase letter, it worked perfectly:

api_version(:module => "v2" ...)
bploetz commented 11 years ago

OK, so clearly they changed this behavior in Rails 4 and Versionist should adapt. I'll file a separate issue for this....

howcast commented 11 years ago

Great! Thanks so much again Brian, I really appreciate you responding and working through this issue with me so quickly.