diff --git a/Gemfile b/Gemfile index 78638a6..57ec319 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 0f2b260..d633d26 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -143,6 +149,7 @@ DEPENDENCIES rails (= 4.2.3) rspec-rails shoulda-matchers + simplecov (~> 0.11) spring sqlite3 diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index ef84d9e..aa16275 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -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 diff --git a/app/controllers/properties_controller.rb b/app/controllers/properties_controller.rb index c2c9881..d285b87 100644 --- a/app/controllers/properties_controller.rb +++ b/app/controllers/properties_controller.rb @@ -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 diff --git a/app/controllers/things_controller.rb b/app/controllers/things_controller.rb index e7da9e4..4eed1d8 100644 --- a/app/controllers/things_controller.rb +++ b/app/controllers/things_controller.rb @@ -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 diff --git a/app/models/location.rb b/app/models/location.rb index c41b469..b72a4d5 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -2,5 +2,4 @@ class Location < ActiveRecord::Base validates :name, uniqueness: true, presence: true has_many :things - end diff --git a/app/serializers/thing_serializer.rb b/app/serializers/thing_serializer.rb new file mode 100644 index 0000000..d8981e0 --- /dev/null +++ b/app/serializers/thing_serializer.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 007e434..9158eb7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 6a77a4a..cf4fc11 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -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__) diff --git a/spec/requests/locations_spec.rb b/spec/requests/locations_spec.rb index 8259e7e..b683bc7 100644 --- a/spec/requests/locations_spec.rb +++ b/spec/requests/locations_spec.rb @@ -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 @@ -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 diff --git a/spec/support/request_helper.rb b/spec/support/request_helper.rb index c1262bd..fcb9e30 100644 --- a/spec/support/request_helper.rb +++ b/spec/support/request_helper.rb @@ -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