Skip to content

Commit

Permalink
add goats/reset route and move seed data to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
tgaff committed Apr 17, 2016
1 parent c386731 commit 5dab715
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 38 deletions.
9 changes: 9 additions & 0 deletions app/controllers/goats_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
41 changes: 4 additions & 37 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
46 changes: 46 additions & 0 deletions lib/goat_data.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions spec/controllers/goats_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions spec/routing/goats_routing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5dab715

Please sign in to comment.