Skip to content

Commit

Permalink
Create StarsController with #index view
Browse files Browse the repository at this point in the history
  • Loading branch information
rhannequin committed May 23, 2017
1 parent d6949a5 commit d73b186
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 3 deletions.
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ end
gem 'rails', '~> 5.1.1'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
Expand All @@ -25,6 +23,7 @@ gem 'puma', '~> 3.7'

gem 'pg'
gem 'figaro'
gem 'jbuilder'
gem 'coveralls', require: false

group :development, :test do
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ GEM
globalid (0.4.0)
activesupport (>= 4.2.0)
i18n (0.8.1)
jbuilder (2.6.4)
activesupport (>= 3.0.0)
multi_json (>= 1.2)
json (2.1.0)
json_spec (1.1.5)
multi_json (~> 1.0)
Expand Down Expand Up @@ -170,6 +173,7 @@ DEPENDENCIES
coveralls
database_cleaner
figaro
jbuilder
json_spec
listen (>= 3.0.5, < 3.2)
pg
Expand Down
1 change: 1 addition & 0 deletions app/controllers/v1/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module V1
class ApplicationController < ActionController::API
include ActionView::Rendering
end
end
7 changes: 7 additions & 0 deletions app/controllers/v1/stars_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module V1
class StarsController < ApplicationController
def index
@stars = Star.all
end
end
end
1 change: 1 addition & 0 deletions app/views/v1/stars/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @stars, :name
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Rails.application.routes.draw do
namespace :v1 do
root to: 'root#index'
resources :stars, only: :index
end
end
File renamed without changes.
21 changes: 21 additions & 0 deletions spec/controllers/v1/stars_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'rails_helper'

RSpec.describe V1::StarsController, type: :controller do
describe 'GET #index' do
before do
Star.create!(name: 'Sun')
get :index, format: :json
end

it 'returns a successful response' do
expect(response).to be_success
expect(response).to have_http_status(200)
end

it 'contains stars records' do
json = response.body
expect(json).to have_json_size(1)
expect(parse_json(json).first['name']).to eq('Sun')
end
end
end
4 changes: 3 additions & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!

require 'database_cleaner'

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
Expand All @@ -20,7 +22,7 @@
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
Expand Down
3 changes: 3 additions & 0 deletions spec/support/render_view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RSpec.configure do |config|
config.render_views = true
end

0 comments on commit d73b186

Please sign in to comment.