diff --git a/app/controllers/goats_controller.rb b/app/controllers/goats_controller.rb index 1325f74..7a39173 100644 --- a/app/controllers/goats_controller.rb +++ b/app/controllers/goats_controller.rb @@ -1,4 +1,9 @@ class GoatsController < ApplicationController + # #reset needs this to access goat seed data + require 'goat_data' + include GoatData + + before_action :set_goat, only: [:show, :update, :destroy] # GET /goats @@ -38,6 +43,10 @@ def destroy @goat.destroy end + def reset + reset_goats + end + private # Use callbacks to share common setup or constraints between actions. def set_goat diff --git a/config/routes.rb b/config/routes.rb index 98e95fc..8abc872 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,8 @@ Rails.application.routes.draw do scope 'api' do - resources :goats + resources :goats do + post 'reset', on: :collection + end end # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html diff --git a/db/seeds.rb b/db/seeds.rb index 3d10333..3b4b6a8 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -7,42 +7,9 @@ # Character.create(name: 'Luke', movie: movies.first) # +# goat hard coded data moved to lib/goat_data +require 'goat_data' +include GoatData -goat_list = [ - {name: 'Jorge', - charisma: 3, - latitude: 30.703639, - longitude: -9.861236, - color: 'grey', - birthdate: 300.days.ago, - image_url: 'http://www.publicdomainpictures.net/pictures/50000/nahled/white-goat.jpg' - }, - {name: 'Maude', - charisma: 4, - latitude: 30.703641, - longitude: -9.861238, - color: 'mauve', - birthdate: 200.days.ago, - image_url: 'https://upload.wikimedia.org/wikipedia/commons/5/5f/Angora_001.jpg' - }, - {name: 'Methuselah', - charisma: 4, - latitude: 30.69585, - longitude: -9.871111, - color: 'grey', - birthdate: 3000.days.ago, - image_url: 'https://upload.wikimedia.org/wikipedia/commons/8/81/Feral_goat.jpg' - }, - {name: 'Claudia', - charisma: 8, - latitude: 30.695, - longitude: -9.87, - color: 'grey', - birthdate: 30.days.ago, - image_url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/Hausziege_04.jpg/1024px-Hausziege_04.jpg' - }, -] -goat_list.each do |goat| - Goat.create goat -end +reset_goats diff --git a/lib/goat_data.rb b/lib/goat_data.rb new file mode 100644 index 0000000..3e277c9 --- /dev/null +++ b/lib/goat_data.rb @@ -0,0 +1,46 @@ +# moved seed data here, we'll use it from the controller's reset route + +module GoatData + GOAT_LIST = [ + {name: 'Jorge', + charisma: 3, + latitude: 30.703639, + longitude: -9.861236, + color: 'grey', + birthdate: 300.days.ago, + image_url: 'http://www.publicdomainpictures.net/pictures/50000/nahled/white-goat.jpg' + }, + {name: 'Maude', + charisma: 4, + latitude: 30.703641, + longitude: -9.861238, + color: 'mauve', + birthdate: 200.days.ago, + image_url: 'https://upload.wikimedia.org/wikipedia/commons/5/5f/Angora_001.jpg' + }, + {name: 'Methuselah', + charisma: 4, + latitude: 30.69585, + longitude: -9.871111, + color: 'grey', + birthdate: 3000.days.ago, + image_url: 'https://upload.wikimedia.org/wikipedia/commons/8/81/Feral_goat.jpg' + }, + {name: 'Claudia', + charisma: 8, + latitude: 30.695, + longitude: -9.87, + color: 'grey', + birthdate: 30.days.ago, + image_url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/Hausziege_04.jpg/1024px-Hausziege_04.jpg' + }, + ] + + def reset_goats + Goat.destroy_all + + GOAT_LIST.each do |goat| + Goat.create goat + end + end +end diff --git a/spec/controllers/goats_controller_spec.rb b/spec/controllers/goats_controller_spec.rb index 29a9d5c..21b1cc7 100644 --- a/spec/controllers/goats_controller_spec.rb +++ b/spec/controllers/goats_controller_spec.rb @@ -141,4 +141,11 @@ end end + describe "POST #reset" do + it "resets the goats in the db" do + FactoryGirl.create(:goat) + expect { post :reset, valid_session }.to change{Goat.count}.from(1).to(4) + end + end + end diff --git a/spec/routing/goats_routing_spec.rb b/spec/routing/goats_routing_spec.rb index a0d1e97..c359495 100644 --- a/spec/routing/goats_routing_spec.rb +++ b/spec/routing/goats_routing_spec.rb @@ -27,5 +27,9 @@ expect(:delete => "/api/goats/1").to route_to("goats#destroy", :id => "1") end + it 'routes to POST #reset' do + expect(post: '/api/goats/reset').to route_to('goats#reset') + end + end end