From 46bd36fbe82215e6c396b454403fcd85f3375895 Mon Sep 17 00:00:00 2001 From: Xero Date: Sun, 10 Mar 2024 18:20:33 -0400 Subject: [PATCH] wishlist controller + scaffold --- app/controllers/wishlists_controller.rb | 70 +++++++++++++++++++ app/helpers/wishlists_helper.rb | 2 + app/views/wishlists/_form.html.erb | 19 +++++ app/views/wishlists/_wishlist.html.erb | 28 ++++++++ app/views/wishlists/_wishlist.json.jbuilder | 2 + app/views/wishlists/edit.html.erb | 8 +++ app/views/wishlists/index.html.erb | 37 ++++++++++ app/views/wishlists/index.json.jbuilder | 1 + app/views/wishlists/new.html.erb | 7 ++ app/views/wishlists/show.html.erb | 15 ++++ app/views/wishlists/show.json.jbuilder | 1 + spec/routing/wishlists_routing_spec.rb | 38 ++++++++++ .../wishlists/edit.html.tailwindcss_spec.rb | 18 +++++ .../wishlists/index.html.tailwindcss_spec.rb | 15 ++++ .../wishlists/new.html.tailwindcss_spec.rb | 14 ++++ .../wishlists/show.html.tailwindcss_spec.rb | 11 +++ test/controllers/wishlists_controller_test.rb | 48 +++++++++++++ test/fixtures/wishlists.yml | 3 + 18 files changed, 337 insertions(+) create mode 100644 app/controllers/wishlists_controller.rb create mode 100644 app/helpers/wishlists_helper.rb create mode 100644 app/views/wishlists/_form.html.erb create mode 100644 app/views/wishlists/_wishlist.html.erb create mode 100644 app/views/wishlists/_wishlist.json.jbuilder create mode 100644 app/views/wishlists/edit.html.erb create mode 100644 app/views/wishlists/index.html.erb create mode 100644 app/views/wishlists/index.json.jbuilder create mode 100644 app/views/wishlists/new.html.erb create mode 100644 app/views/wishlists/show.html.erb create mode 100644 app/views/wishlists/show.json.jbuilder create mode 100644 spec/routing/wishlists_routing_spec.rb create mode 100644 spec/views/wishlists/edit.html.tailwindcss_spec.rb create mode 100644 spec/views/wishlists/index.html.tailwindcss_spec.rb create mode 100644 spec/views/wishlists/new.html.tailwindcss_spec.rb create mode 100644 spec/views/wishlists/show.html.tailwindcss_spec.rb create mode 100644 test/controllers/wishlists_controller_test.rb create mode 100644 test/fixtures/wishlists.yml diff --git a/app/controllers/wishlists_controller.rb b/app/controllers/wishlists_controller.rb new file mode 100644 index 0000000..ad21450 --- /dev/null +++ b/app/controllers/wishlists_controller.rb @@ -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 diff --git a/app/helpers/wishlists_helper.rb b/app/helpers/wishlists_helper.rb new file mode 100644 index 0000000..1fb0b3a --- /dev/null +++ b/app/helpers/wishlists_helper.rb @@ -0,0 +1,2 @@ +module WishlistsHelper +end diff --git a/app/views/wishlists/_form.html.erb b/app/views/wishlists/_form.html.erb new file mode 100644 index 0000000..49e19bf --- /dev/null +++ b/app/views/wishlists/_form.html.erb @@ -0,0 +1,19 @@ +<%= form_with(model: wishlist, class: "contents") do |form| %> + <% if wishlist.errors.any? %> +
+

<%= pluralize(wishlist.errors.count, "error") %> prohibited this wishlist from being saved:

+ + +
+ <% end %> + + <%= form.text_field :name %> + +
+ <%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %> +
+<% end %> diff --git a/app/views/wishlists/_wishlist.html.erb b/app/views/wishlists/_wishlist.html.erb new file mode 100644 index 0000000..00e2cf1 --- /dev/null +++ b/app/views/wishlists/_wishlist.html.erb @@ -0,0 +1,28 @@ +
+ <% 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" %> +
+ <% end %> + + + + + + + + + + + + + + + + + + + + +
AttributeValue
ID<%= @wishlist.id %>
Name<%= @wishlist.name %>
+
diff --git a/app/views/wishlists/_wishlist.json.jbuilder b/app/views/wishlists/_wishlist.json.jbuilder new file mode 100644 index 0000000..22039d7 --- /dev/null +++ b/app/views/wishlists/_wishlist.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! wishlist, :id, :name, :created_at, :updated_at +json.url wishlist_url(wishlist, format: :json) diff --git a/app/views/wishlists/edit.html.erb b/app/views/wishlists/edit.html.erb new file mode 100644 index 0000000..5fa1485 --- /dev/null +++ b/app/views/wishlists/edit.html.erb @@ -0,0 +1,8 @@ +
+

Editing wishlist

+ + <%= 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" %> +
diff --git a/app/views/wishlists/index.html.erb b/app/views/wishlists/index.html.erb new file mode 100644 index 0000000..408c738 --- /dev/null +++ b/app/views/wishlists/index.html.erb @@ -0,0 +1,37 @@ +
+ <% if notice.present? %> +

<%= notice %>

+ <% end %> + +
+

Wishlists

+ <%= link_to 'New wishlist', new_wishlist_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %> +
+ +
+ + + + + + + + + + <% @wishlists.each do |wishlist| %> + + + + + + <% end %> + +
NameURLSearch
<%= wishlist.name %> + Show + + " target="_blank"> + Google + +
+
+
diff --git a/app/views/wishlists/index.json.jbuilder b/app/views/wishlists/index.json.jbuilder new file mode 100644 index 0000000..9e44981 --- /dev/null +++ b/app/views/wishlists/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @wishlists, partial: "wishlists/wishlist", as: :wishlist diff --git a/app/views/wishlists/new.html.erb b/app/views/wishlists/new.html.erb new file mode 100644 index 0000000..ccc332e --- /dev/null +++ b/app/views/wishlists/new.html.erb @@ -0,0 +1,7 @@ +
+

New wishlist

+ + <%= 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" %> +
diff --git a/app/views/wishlists/show.html.erb b/app/views/wishlists/show.html.erb new file mode 100644 index 0000000..79a04cd --- /dev/null +++ b/app/views/wishlists/show.html.erb @@ -0,0 +1,15 @@ +
+
+ <% if notice.present? %> +

<%= notice %>

+ <% 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" %> +
+ <%= button_to 'Destroy this wishlist', wishlist_path(@wishlist), method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 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" %> +
+
diff --git a/app/views/wishlists/show.json.jbuilder b/app/views/wishlists/show.json.jbuilder new file mode 100644 index 0000000..2ca58cb --- /dev/null +++ b/app/views/wishlists/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "wishlists/wishlist", wishlist: @wishlist diff --git a/spec/routing/wishlists_routing_spec.rb b/spec/routing/wishlists_routing_spec.rb new file mode 100644 index 0000000..fda086f --- /dev/null +++ b/spec/routing/wishlists_routing_spec.rb @@ -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 diff --git a/spec/views/wishlists/edit.html.tailwindcss_spec.rb b/spec/views/wishlists/edit.html.tailwindcss_spec.rb new file mode 100644 index 0000000..6e6a8dc --- /dev/null +++ b/spec/views/wishlists/edit.html.tailwindcss_spec.rb @@ -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 diff --git a/spec/views/wishlists/index.html.tailwindcss_spec.rb b/spec/views/wishlists/index.html.tailwindcss_spec.rb new file mode 100644 index 0000000..5cca845 --- /dev/null +++ b/spec/views/wishlists/index.html.tailwindcss_spec.rb @@ -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 diff --git a/spec/views/wishlists/new.html.tailwindcss_spec.rb b/spec/views/wishlists/new.html.tailwindcss_spec.rb new file mode 100644 index 0000000..39906e6 --- /dev/null +++ b/spec/views/wishlists/new.html.tailwindcss_spec.rb @@ -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 diff --git a/spec/views/wishlists/show.html.tailwindcss_spec.rb b/spec/views/wishlists/show.html.tailwindcss_spec.rb new file mode 100644 index 0000000..b6930e1 --- /dev/null +++ b/spec/views/wishlists/show.html.tailwindcss_spec.rb @@ -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

" do + render + end +end diff --git a/test/controllers/wishlists_controller_test.rb b/test/controllers/wishlists_controller_test.rb new file mode 100644 index 0000000..2c98455 --- /dev/null +++ b/test/controllers/wishlists_controller_test.rb @@ -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 diff --git a/test/fixtures/wishlists.yml b/test/fixtures/wishlists.yml new file mode 100644 index 0000000..df98dc6 --- /dev/null +++ b/test/fixtures/wishlists.yml @@ -0,0 +1,3 @@ +one: + id: 1 + name: 'Euphorbia Atrox' \ No newline at end of file