Rails and Ember app with some notes
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
app
bin
config
db Initial commit Sep 5, 2014
lib Initial commit Sep 5, 2014
log Initial commit Sep 5, 2014
public
spec
vendor/assets Initial commit Sep 5, 2014
.gitignore Initial commit Sep 5, 2014
.rspec Added some notes about module preference Sep 5, 2014
.rubocop.yml Added some notes about module preference Sep 5, 2014
Gemfile Added Jasmine Sep 5, 2014
Gemfile.lock Added Jasmine Sep 5, 2014
Guardfile
README.md Added link to REST API versioning Sep 5, 2014
Rakefile
config.ru

README.md

Rails and Ember.js App

Some known best practices

Use nested module/class definitions instead of compact style.

Preferred:

# app/controllers/api/v1/leads_controller.rb
module Api
  module V1
    class LeadsController < ApplicationController
      respond_to :json
    end
  end
end

Not preferred:

# app/controllers/api/v1/leads_controller.rb
class Api::V1::LeadsController < ApplicationController
  respond_to :json
  # methods
end

The routes should look like:

# config/routes.rb

namespace :api, defaults: { format: 'json' } do
  scope module: :v1 do
    resources :leads
  end
end

If you're not going to use modules, it would look like:

# config/routes.rb

namespace :api do
  namespace :v1 do
    resources :leads
  end
end

A JSON API document should be identified by the media type application/vnd.api+json

We achieve this by autoloading a module:

# lib/api_constraints.rb 
class ApiConstraints
  def initialize(options)
    @version = options[:version]
    @default = options[:default]
  end

  def matches?(req)
    @default || req.headers['Accept'].include?("application/vnd.myapp.v#{@version}")
  end
end

And updating routes to use this module:

namespace :api, defaults: { format: 'json' } do
  scope module: :v1, constraints: ApiConstraints.new(version: 1) do
    resources :leads
  end
end

Making this work with Ember.js is simple. Simply include headers on app/assets/javascripts/store.js.coffee

DS.RESTAdapter.reopen
  namespace: 'api'
  headers: {
    "Accept": "application/vnd.myapp.v1+json"
  }

Resources