Skip to content

Commit

Permalink
Add a spots and a spot seeds
Browse files Browse the repository at this point in the history
  • Loading branch information
iarobinson committed Feb 21, 2024
1 parent 12b6eea commit 80ecfcc
Show file tree
Hide file tree
Showing 19 changed files with 306 additions and 1 deletion.
70 changes: 70 additions & 0 deletions app/controllers/spots_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class SpotsController < ApplicationController
before_action :set_spot, only: %i[ show edit update destroy ]

# GET /spots or /spots.json
def index
@spots = Spot.all
end

# GET /spots/1 or /spots/1.json
def show
end

# GET /spots/new
def new
@spot = Spot.new
end

# GET /spots/1/edit
def edit
end

# POST /spots or /spots.json
def create
@spot = Spot.new(spot_params)

respond_to do |format|
if @spot.save
format.html { redirect_to spot_url(@spot), notice: "Spot was successfully created." }
format.json { render :show, status: :created, location: @spot }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @spot.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /spots/1 or /spots/1.json
def update
respond_to do |format|
if @spot.update(spot_params)
format.html { redirect_to spot_url(@spot), notice: "Spot was successfully updated." }
format.json { render :show, status: :ok, location: @spot }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @spot.errors, status: :unprocessable_entity }
end
end
end

# DELETE /spots/1 or /spots/1.json
def destroy
@spot.destroy

respond_to do |format|
format.html { redirect_to spots_url, notice: "Spot was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_spot
@spot = Spot.find(params[:id])
end

# Only allow a list of trusted parameters through.
def spot_params
params.require(:spot).permit(:name, :latitude, :longitude)
end
end
2 changes: 2 additions & 0 deletions app/helpers/spots_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module SpotsHelper
end
2 changes: 2 additions & 0 deletions app/models/spot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Spot < ApplicationRecord
end
32 changes: 32 additions & 0 deletions app/views/spots/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<%= form_with(model: spot) do |form| %>
<% if spot.errors.any? %>
<div style="color: red">
<h2><%= pluralize(spot.errors.count, "error") %> prohibited this spot from being saved:</h2>

<ul>
<% spot.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div>
<%= form.label :name, style: "display: block" %>
<%= form.text_field :name %>
</div>

<div>
<%= form.label :latitude, style: "display: block" %>
<%= form.text_field :latitude %>
</div>

<div>
<%= form.label :longitude, style: "display: block" %>
<%= form.text_field :longitude %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
17 changes: 17 additions & 0 deletions app/views/spots/_spot.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div id="<%= dom_id spot %>">
<p>
<strong>Name:</strong>
<%= spot.name %>
</p>

<p>
<strong>Latitude:</strong>
<%= spot.latitude %>
</p>

<p>
<strong>Longitude:</strong>
<%= spot.longitude %>
</p>

</div>
2 changes: 2 additions & 0 deletions app/views/spots/_spot.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! spot, :id, :name, :latitude, :longitude, :created_at, :updated_at
json.url spot_url(spot, format: :json)
10 changes: 10 additions & 0 deletions app/views/spots/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Editing spot</h1>

<%= render "form", spot: @spot %>

<br>

<div>
<%= link_to "Show this spot", @spot %> |
<%= link_to "Back to spots", spots_path %>
</div>
14 changes: 14 additions & 0 deletions app/views/spots/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p style="color: green"><%= notice %></p>

<h1>Spots</h1>

<div id="spots">
<% @spots.each do |spot| %>
<%= render spot %>
<p>
<%= link_to "Show this spot", spot %>
</p>
<% end %>
</div>

<%= link_to "New spot", new_spot_path %>
1 change: 1 addition & 0 deletions app/views/spots/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @spots, partial: "spots/spot", as: :spot
9 changes: 9 additions & 0 deletions app/views/spots/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>New spot</h1>

<%= render "form", spot: @spot %>

<br>

<div>
<%= link_to "Back to spots", spots_path %>
</div>
10 changes: 10 additions & 0 deletions app/views/spots/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<p style="color: green"><%= notice %></p>

<%= render @spot %>

<div>
<%= link_to "Edit this spot", edit_spot_path(@spot) %> |
<%= link_to "Back to spots", spots_path %>
<%= button_to "Destroy this spot", @spot, method: :delete %>
</div>
1 change: 1 addition & 0 deletions app/views/spots/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "spots/spot", spot: @spot
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :spots
get 'errors/not_found'
get 'errors/internal_server_error'

Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20240221183917_create_spots.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateSpots < ActiveRecord::Migration[7.0]
def change
create_table :spots do |t|
t.string :name
t.float :latitude
t.float :longitude

t.timestamps
end
end
end
10 changes: 9 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,18 @@
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)

# seeds.rb

Spot.create([
{ name: "Pipeline (Banzai Pipeline)", latitude: 21.6610, longitude: -158.0560 },
{ name: "Jeffreys Bay (J-Bay)", latitude: -34.0476, longitude: 24.9211 },
{ name: "Teahupo'o", latitude: -17.8482, longitude: -149.2688 },
{ name: "Uluwatu", latitude: -8.8245, longitude: 115.1015 },
{ name: "Cloudbreak", latitude: -17.8795, longitude: 177.1908 },
{ name: "Snapper Rocks", latitude: -28.1075, longitude: 153.4844 },
{ name: "Hossegor", latitude: 43.6609, longitude: -1.4438 },
{ name: "Bells Beach", latitude: -38.3652, longitude: 144.2845 },
{ name: "Mavericks", latitude: 37.4947, longitude: -122.5005 },
{ name: "Rincon", latitude: 34.3705, longitude: -119.4359 },
])
48 changes: 48 additions & 0 deletions test/controllers/spots_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "test_helper"

class SpotsControllerTest < ActionDispatch::IntegrationTest
setup do
@spot = spots(:one)
end

test "should get index" do
get spots_url
assert_response :success
end

test "should get new" do
get new_spot_url
assert_response :success
end

test "should create spot" do
assert_difference("Spot.count") do
post spots_url, params: { spot: { latitude: @spot.latitude, longitude: @spot.longitude, name: @spot.name } }
end

assert_redirected_to spot_url(Spot.last)
end

test "should show spot" do
get spot_url(@spot)
assert_response :success
end

test "should get edit" do
get edit_spot_url(@spot)
assert_response :success
end

test "should update spot" do
patch spot_url(@spot), params: { spot: { latitude: @spot.latitude, longitude: @spot.longitude, name: @spot.name } }
assert_redirected_to spot_url(@spot)
end

test "should destroy spot" do
assert_difference("Spot.count", -1) do
delete spot_url(@spot)
end

assert_redirected_to spots_url
end
end
7 changes: 7 additions & 0 deletions test/models/spot_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class SpotTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
45 changes: 45 additions & 0 deletions test/system/spots_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require "application_system_test_case"

class SpotsTest < ApplicationSystemTestCase
setup do
@spot = spots(:one)
end

test "visiting the index" do
visit spots_url
assert_selector "h1", text: "Spots"
end

test "should create spot" do
visit spots_url
click_on "New spot"

fill_in "Latitude", with: @spot.latitude
fill_in "Longitude", with: @spot.longitude
fill_in "Name", with: @spot.name
click_on "Create Spot"

assert_text "Spot was successfully created"
click_on "Back"
end

test "should update Spot" do
visit spot_url(@spot)
click_on "Edit this spot", match: :first

fill_in "Latitude", with: @spot.latitude
fill_in "Longitude", with: @spot.longitude
fill_in "Name", with: @spot.name
click_on "Update Spot"

assert_text "Spot was successfully updated"
click_on "Back"
end

test "should destroy Spot" do
visit spot_url(@spot)
click_on "Destroy this spot", match: :first

assert_text "Spot was successfully destroyed"
end
end

0 comments on commit 80ecfcc

Please sign in to comment.