Skip to content

Commit

Permalink
wishlist controller + scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
one-m1nd committed Mar 10, 2024
1 parent 8fdac53 commit 46bd36f
Show file tree
Hide file tree
Showing 18 changed files with 337 additions and 0 deletions.
70 changes: 70 additions & 0 deletions app/controllers/wishlists_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class WishlistsController < ApplicationController
before_action :set_wishlist, only: %i[ show edit update destroy ]

# GET /wishlists or /wishlists.json
def index
@wishlists = Wishlist.all
end

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

# GET /wishlists/new
def new
@wishlist = Wishlist.new
end

# GET /wishlists/1/edit
def edit
end

# POST /wishlists or /wishlists.json
def create
@wishlist = Wishlist.new(wishlist_params)

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

# PATCH/PUT /wishlists/1 or /wishlists/1.json
def update
respond_to do |format|
if @wishlist.update(wishlist_params)
format.html { redirect_to wishlist_url(@wishlist), notice: "Wishlist was successfully updated." }
format.json { render :show, status: :ok, location: @wishlist }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @wishlist.errors, status: :unprocessable_entity }
end
end
end

# DELETE /wishlists/1 or /wishlists/1.json
def destroy
@wishlist.destroy

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

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

# Only allow a list of trusted parameters through.
def wishlist_params
params.require(:wishlist).permit([:name])
end
end
2 changes: 2 additions & 0 deletions app/helpers/wishlists_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module WishlistsHelper
end
19 changes: 19 additions & 0 deletions app/views/wishlists/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<%= form_with(model: wishlist, class: "contents") do |form| %>
<% if wishlist.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<h2><%= pluralize(wishlist.errors.count, "error") %> prohibited this wishlist from being saved:</h2>

<ul>
<% wishlist.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form.text_field :name %>

<div class="inline">
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
</div>
<% end %>
28 changes: 28 additions & 0 deletions app/views/wishlists/_wishlist.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<div id="<%= dom_id wishlist %>">
<% if action_name != "show" %>
<%= link_to "Show this wishlist", wishlist, class: "rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to 'Edit this wishlist', edit_wishlist_path(wishlist), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %>
<hr class="mt-6">
<% end %>

<table class="table-auto border-spacing-4 border-separate border border-slate-900">
<thead>
<tr>
<th>Attribute</th>
<th>Value</th>
</tr>
</thead>
<tbody>

<tr>
<td class="border border-slate-900">ID</td>
<td class="border border-slate-900"><%= @wishlist.id %></td>
</tr>

<tr>
<td class="border border-slate-900">Name</td>
<td class="border border-slate-900"><%= @wishlist.name %></td>
</tr>
</tbody>
</table>
</div>
2 changes: 2 additions & 0 deletions app/views/wishlists/_wishlist.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! wishlist, :id, :name, :created_at, :updated_at
json.url wishlist_url(wishlist, format: :json)
8 changes: 8 additions & 0 deletions app/views/wishlists/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">Editing wishlist</h1>

<%= render "form", wishlist: @wishlist %>
<%= link_to "Show this wishlist", @wishlist, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to wishlists", wishlists_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>
37 changes: 37 additions & 0 deletions app/views/wishlists/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<div class="w-full">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>

<div class="flex justify-between items-center">
<h1 class="font-bold text-4xl">Wishlists</h1>
<%= link_to 'New wishlist', new_wishlist_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
</div>

<div id="wishlists" class="min-w-full">
<table class="table-auto border-spacing-4 border-separate border border-slate-900">
<thead>
<tr>
<th>Name</th>
<th>URL</th>
<th>Search</th>
</tr>
</thead>
<tbody>
<% @wishlists.each do |wishlist| %>
<tr>
<td class="border border-slate-900"><%= wishlist.name %></td>
<td class="border border-slate-900">
<a href="<%= wishlist_url(wishlist) %>">Show</a>
</td>
<td class="border border-slate-900">
<a href="<%= "https://www.google.com/search?q=#{wishlist.name}" %>" target="_blank">
Google
</a>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
1 change: 1 addition & 0 deletions app/views/wishlists/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @wishlists, partial: "wishlists/wishlist", as: :wishlist
7 changes: 7 additions & 0 deletions app/views/wishlists/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">New wishlist</h1>

<%= render "form", wishlist: @wishlist %>
<%= link_to 'Back to wishlists', wishlists_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>
15 changes: 15 additions & 0 deletions app/views/wishlists/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div class="mx-auto md:w-2/3 w-full flex">
<div class="mx-auto">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<%= render @wishlist %>
<%= link_to 'Edit this wishlist', edit_wishlist_path(@wishlist), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<div class="inline-block ml-2">
<%= button_to 'Destroy this wishlist', wishlist_path(@wishlist), method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
</div>
<%= link_to 'Back to wishlists', wishlists_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>
</div>
1 change: 1 addition & 0 deletions app/views/wishlists/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "wishlists/wishlist", wishlist: @wishlist
38 changes: 38 additions & 0 deletions spec/routing/wishlists_routing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require "rails_helper"

RSpec.describe WishlistsController, type: :routing do
describe "routing" do
it "routes to #index" do
expect(get: "/wishlists").to route_to("wishlists#index")
end

it "routes to #new" do
expect(get: "/wishlists/new").to route_to("wishlists#new")
end

it "routes to #show" do
expect(get: "/wishlists/1").to route_to("wishlists#show", id: "1")
end

it "routes to #edit" do
expect(get: "/wishlists/1/edit").to route_to("wishlists#edit", id: "1")
end


it "routes to #create" do
expect(post: "/wishlists").to route_to("wishlists#create")
end

it "routes to #update via PUT" do
expect(put: "/wishlists/1").to route_to("wishlists#update", id: "1")
end

it "routes to #update via PATCH" do
expect(patch: "/wishlists/1").to route_to("wishlists#update", id: "1")
end

it "routes to #destroy" do
expect(delete: "/wishlists/1").to route_to("wishlists#destroy", id: "1")
end
end
end
18 changes: 18 additions & 0 deletions spec/views/wishlists/edit.html.tailwindcss_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'rails_helper'

RSpec.describe "wishlists/edit", type: :view do
let(:wishlist) {
Wishlist.create!(name: 'Euphorbia Atrox')
}

before(:each) do
assign(:wishlist, wishlist)
end

it "renders the edit wishlist form" do
render

assert_select "form[action=?][method=?]", wishlist_path(wishlist), "post" do
end
end
end
15 changes: 15 additions & 0 deletions spec/views/wishlists/index.html.tailwindcss_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

RSpec.describe "wishlists/index", type: :view do
before(:each) do
assign(:wishlists, [
Wishlist.create!(name: 'giga'),
Wishlist.create!(name: 'omega')
])
end

it "renders a list of wishlists" do
render
cell_selector = Rails::VERSION::STRING >= '7' ? 'div>p' : 'tr>td'
end
end
14 changes: 14 additions & 0 deletions spec/views/wishlists/new.html.tailwindcss_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'rails_helper'

RSpec.describe "wishlists/new", type: :view do
before(:each) do
assign(:wishlist, Wishlist.new(name: 'omega'))
end

it "renders new wishlist form" do
render

assert_select "form[action=?][method=?]", wishlists_path, "post" do
end
end
end
11 changes: 11 additions & 0 deletions spec/views/wishlists/show.html.tailwindcss_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'rails_helper'

RSpec.describe "wishlists/show", type: :view do
before(:each) do
assign(:wishlist, Wishlist.create!(name: 'giga'))
end

it "renders attributes in <p>" do
render
end
end
48 changes: 48 additions & 0 deletions test/controllers/wishlists_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "test_helper"

class WishlistControllerTest < ActionDispatch::IntegrationTest
setup do
@wishlist = wishlists(:one)
end

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

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

test "should create wishlist" do
assert_difference("Wishlist.count") do
post wishlists_url, params: { wishlist: { name: 'PlantThree' } }
end

assert_redirected_to wishlist_url(Wishlist.last)
end

test "should show wishlist" do
get wishlist_url(@wishlist)
assert_response :success
end

test "should get edit" do
get edit_wishlist_url(@wishlist)
assert_response :success
end

test "should update wishlist" do
patch wishlist_url(@wishlist), params: { wishlist: { name: 'PlantFour' } }
assert_redirected_to wishlist_url(@wishlist)
end

test "should destroy wishlist" do
assert_difference("Wishlist.count", -1) do
delete wishlist_url(@wishlist)
end

assert_redirected_to wishlists_url
end
end
3 changes: 3 additions & 0 deletions test/fixtures/wishlists.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
one:
id: 1
name: 'Euphorbia Atrox'

0 comments on commit 46bd36f

Please sign in to comment.