Class: Teleport::ApiGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/teleport/api.rb

Instance Method Summary collapse

Instance Method Details

#addObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/generators/teleport/api.rb', line 5

def add
  begin
    gem 'jbuilder'
  rescue
    nil
  end

  Bundler.with_clean_env do
    run "bundle install"
  end

  copy_file 'api/v1/base_controller.rb', 'app/controllers/api/v1/base_controller.rb'

  @model_name = ask 'Type model name (ex.: Post)'
  template 'api/v1/posts_controller.rb', "app/controllers/api/v1/#{@model_name.pluralize.underscore}_controller.rb"

  route %Q{
  namespace :api, path: '/', constraints: { subdomain: 'api' } do
namespace :v1 do
  resources :#{@model_name.pluralize.underscore}, only: :index
end
  end
  }

  copy_file 'api/views/index.json.jbuilder', "app/views/api/v1/#{@model_name.underscore}/index.json.jbuilder"

  p 'for rspec use'
  p 'before(:each) { request.host = "api.example.com" }'
end