Sutto / rocket_pants

API building tools on top of ActionController. Also, an awesome name.
MIT License
984 stars 130 forks source link

URLs include `version` parameter #91

Open manuelmeurer opened 10 years ago

manuelmeurer commented 10 years ago

I'm using RocketPants with Rabl. When I render a URL in a Rabl template, the version parameter is always added by default:

# config/routes.rb
MyApp::Application.routes.draw do
  get 'foo' => 'foo#bar' # Only for testing
  namespace :api, path: '', constraints: { subdomain: 'api' } do
    api version: 1, require_prefix: 'v', module: 'v1' do
      resources :widgets
    end
  end
end

# app/views/api/v1/widgets/index.json.rabl
collection @widgets
node :foo do |widget|
  foo_path
end

# returned JSON:
{
  results: [
    {
      foo: "/foo?version=v1"
    }
  ]
}

When I add version: nil to the URL helper call, it disappears:

foo_path version: nil

Is this intended behavior?

Sutto commented 10 years ago

For the moment, yes - the idea being you're typically generating links internal to the API.

It might make sense to make it be smarter about generating them only for api routes (which it doesn't at the moment) but that would take a bit more work.

manuelmeurer commented 10 years ago

Is it done here? https://github.com/Sutto/rocket_pants/blob/master/lib/rocket_pants/controller/url_for.rb What is this used for?

Sutto commented 10 years ago

It's used so that when we generate links in the api to other API endpoints, it automatically includes the version the current user has specified, so they're consistent in linking.

manuelmeurer commented 10 years ago

Ah ok, makes sense. But is this the moment where the version parameter is added to non-API URLs as well? Could we not check there if the URLs is for the API, and only add the parameter if it is?

toobulkeh commented 10 years ago

@manuelmeurer I know this is an old task, but how did you get RP to render RABL files? I found another post, but it's not very elegant. Thanks!

manuelmeurer commented 10 years ago

@toobulkeh This is pretty much all it needs: https://gist.github.com/manuelmeurer/27cb13fb7bdbba49706a

toobulkeh commented 10 years ago

very similar. Thanks!