Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SimpleCov and active-model-serializers controller/routing setup. #40

Open
wants to merge 5 commits into
base: active-model-serializers
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ group :development, :test do
gem 'rspec-rails'
gem 'shoulda-matchers'
gem "factory_girl_rails", "~> 4.0"
gem 'simplecov', '~> 0.11'
end

# To use ActiveModel has_secure_password
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ GEM
builder (3.2.2)
coderay (1.1.0)
diff-lcs (1.2.5)
docile (1.1.5)
erubis (2.7.0)
factory_girl (4.5.0)
activesupport (>= 3.0.0)
Expand Down Expand Up @@ -117,6 +118,11 @@ GEM
rspec-support (3.3.0)
shoulda-matchers (3.1.1)
activesupport (>= 4.0.0)
simplecov (0.11.2)
docile (~> 1.1.0)
json (~> 1.8)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.0)
slop (3.6.0)
spring (1.3.6)
sprockets (3.3.4)
Expand All @@ -143,6 +149,7 @@ DEPENDENCIES
rails (= 4.2.3)
rspec-rails
shoulda-matchers
simplecov (~> 0.11)
spring
sqlite3

Expand Down
6 changes: 5 additions & 1 deletion app/controllers/locations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
class LocationsController < ApplicationController
def index
locations = Location.all

render json: locations
end

def show
location = Location.find(params[:id])
render json: location
end
end
8 changes: 8 additions & 0 deletions app/controllers/properties_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
class PropertiesController < ApplicationController
def index
properties = Property.all
render json: properties
end

def show
property = Property.find(params[:id])
render json: property
end
end
8 changes: 8 additions & 0 deletions app/controllers/things_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
class ThingsController < ApplicationController
def index
things = Thing.all
render json: things
end

def show
thing = Thing.find(params[:id])
render json: thing
end
end
1 change: 0 additions & 1 deletion app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ class Location < ActiveRecord::Base
validates :name, uniqueness: true, presence: true

has_many :things

end
17 changes: 17 additions & 0 deletions app/serializers/thing_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class ThingSerializer < ActiveModel::Serializer
attributes :name, :status, :reason

belongs_to :location

has_many :properties

def initialize(model, context = nil)
super model, context
model.properties.each do |prop|
self.class.send(:define_method, prop.name.to_sym) do
prop.value
end
self.class.send(:attribute, prop.name.to_sym)
end
end
end
12 changes: 9 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
# jsonapi_resources :things
# jsonapi_resources :properties

resources :locations
resources :things
resources :properties
resources :locations do
resources :things
end
resources :things do
resources :properties
end
resources :properties do
resources :things
end
end
3 changes: 3 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'simplecov'
SimpleCov.start

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
Expand Down
8 changes: 8 additions & 0 deletions spec/requests/locations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
end

describe "GET /locations without locations" do
it "fails with unauthorized headers" do
get "/locations", {}, unauthorized_headers

expect(response.status).to eq 401
end

it "returns an emtpy data array" do

get "/locations", {}, authorized_headers
Expand Down Expand Up @@ -54,6 +60,8 @@

expect(body['data']).not_to eq nil
body['data'].each do |thing|
puts "This is the thing"
puts thing
expect(thing['id']).not_to eq nil
expect(thing['type']).not_to eq nil
expect(thing['links']).not_to eq nil
Expand Down
7 changes: 7 additions & 0 deletions spec/support/request_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ def authorized_preflight_headers
'ORIGIN' => 'http://otherdomain.test/'
}
end

def unauthorized_headers
{
'HTTP_ACCEPT' => 'application/vnd.api+json',
'HTTP_AUTHORIZATION' => 'Token badtokenauth'
}
end
end

module ActionDispatch::Integration::RequestHelpers
Expand Down