From acc9d83556ecd42109407873f77d31bb263b3475 Mon Sep 17 00:00:00 2001 From: Chris <76159444+hunchr@users.noreply.github.com> Date: Fri, 27 Jun 2025 09:04:00 +0000 Subject: [PATCH 1/3] Add home controller --- app/controllers/hotsheet/home_controller.rb | 9 +++++++++ app/controllers/hotsheet/sheets_controller.rb | 4 ---- app/views/hotsheet/{sheets => home}/error.html.erb | 0 .../{sheets/root.html.erb => home/show.html.erb} | 0 config/routes.rb | 5 +++-- 5 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 app/controllers/hotsheet/home_controller.rb rename app/views/hotsheet/{sheets => home}/error.html.erb (100%) rename app/views/hotsheet/{sheets/root.html.erb => home/show.html.erb} (100%) diff --git a/app/controllers/hotsheet/home_controller.rb b/app/controllers/hotsheet/home_controller.rb new file mode 100644 index 0000000..89d5e7a --- /dev/null +++ b/app/controllers/hotsheet/home_controller.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class Hotsheet::HomeController < Hotsheet::ApplicationController + def show; end + + def error + render "error", status: :not_found + end +end diff --git a/app/controllers/hotsheet/sheets_controller.rb b/app/controllers/hotsheet/sheets_controller.rb index d85c684..62013bc 100644 --- a/app/controllers/hotsheet/sheets_controller.rb +++ b/app/controllers/hotsheet/sheets_controller.rb @@ -18,10 +18,6 @@ def update end end - def error - render "error", status: :not_found - end - private def set_sheet diff --git a/app/views/hotsheet/sheets/error.html.erb b/app/views/hotsheet/home/error.html.erb similarity index 100% rename from app/views/hotsheet/sheets/error.html.erb rename to app/views/hotsheet/home/error.html.erb diff --git a/app/views/hotsheet/sheets/root.html.erb b/app/views/hotsheet/home/show.html.erb similarity index 100% rename from app/views/hotsheet/sheets/root.html.erb rename to app/views/hotsheet/home/show.html.erb diff --git a/config/routes.rb b/config/routes.rb index facf11e..b515a75 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,10 +3,11 @@ Hotsheet::Engine.routes.draw do next unless defined? Rails::Server + root "home#show" + Hotsheet.sheets.each_key do |sheet_name| resources sheet_name, sheet_name:, controller: :sheets, only: %i[index update] end - root "sheets#root" - match "*path", to: "sheets#error", via: :all + match "*path", to: "home#error", via: :all end From 76e56309aeb1dd082d8526147564f5a09c53cfa1 Mon Sep 17 00:00:00 2001 From: Chris <76159444+hunchr@users.noreply.github.com> Date: Fri, 27 Jun 2025 09:15:38 +0000 Subject: [PATCH 2/3] Refactor routing --- app/controllers/hotsheet/sheets_controller.rb | 2 ++ app/helpers/hotsheet/application_helper.rb | 7 ------- app/views/hotsheet/shared/_nav.html.erb | 2 +- config/routes.rb | 6 ++---- spec/requests/hotsheet/sheets_controller_spec.rb | 6 +++--- spec/support/utils.rb | 2 -- spec/system/hotsheet/sheets_controller_spec.rb | 2 +- 7 files changed, 9 insertions(+), 18 deletions(-) delete mode 100644 app/helpers/hotsheet/application_helper.rb diff --git a/app/controllers/hotsheet/sheets_controller.rb b/app/controllers/hotsheet/sheets_controller.rb index 62013bc..4d6240c 100644 --- a/app/controllers/hotsheet/sheets_controller.rb +++ b/app/controllers/hotsheet/sheets_controller.rb @@ -22,6 +22,8 @@ def update def set_sheet @sheet = Hotsheet.sheets[params[:sheet_name]] + + render "hotsheet/home/error", status: :not_found if @sheet.nil? end def set_column diff --git a/app/helpers/hotsheet/application_helper.rb b/app/helpers/hotsheet/application_helper.rb deleted file mode 100644 index 3d18a5e..0000000 --- a/app/helpers/hotsheet/application_helper.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -module Hotsheet::ApplicationHelper - def sheet_path_for(sheet_name, **args) - hotsheet.public_send :"#{sheet_name}_path", **args - end -end diff --git a/app/views/hotsheet/shared/_nav.html.erb b/app/views/hotsheet/shared/_nav.html.erb index 3ab941c..d965af5 100644 --- a/app/views/hotsheet/shared/_nav.html.erb +++ b/app/views/hotsheet/shared/_nav.html.erb @@ -6,7 +6,7 @@ <% Hotsheet.sheets.each_with_index do |entry, index| %>
  • <% sheet_name, sheet = entry %> - <%= link_to sheet.model.model_name.human(count: 2), sheet_path_for(sheet_name), + <%= link_to sheet.model.model_name.human(count: 2), sheets_path(sheet_name), class: ("active" if params[:sheet_name] == sheet_name), data: { x: index } %>
  • <% end %> diff --git a/config/routes.rb b/config/routes.rb index b515a75..f84ed08 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,12 +1,10 @@ # frozen_string_literal: true Hotsheet::Engine.routes.draw do - next unless defined? Rails::Server - root "home#show" - Hotsheet.sheets.each_key do |sheet_name| - resources sheet_name, sheet_name:, controller: :sheets, only: %i[index update] + scope ":sheet_name" do + resources controller: :sheets, only: %i[index update], as: :sheets end match "*path", to: "home#error", via: :all diff --git a/spec/requests/hotsheet/sheets_controller_spec.rb b/spec/requests/hotsheet/sheets_controller_spec.rb index 48aed76..5bf985a 100644 --- a/spec/requests/hotsheet/sheets_controller_spec.rb +++ b/spec/requests/hotsheet/sheets_controller_spec.rb @@ -12,14 +12,14 @@ describe "#index" do it "shows a table with all values" do - get hotsheet.users_path + get hotsheet.sheets_path :users expect(response).to be_successful expect(response.body).to include user.name end end describe "#update" do - let(:path) { hotsheet.user_path(user.id) } + let(:path) { hotsheet.sheet_path :users, user } it "updates the resource" do expect { put path, params: { column_name: :name, value: "Bob" } } @@ -43,7 +43,7 @@ end context "with nonexistent id" do - let(:path) { hotsheet.user_path(0) } + let(:path) { hotsheet.sheet_path :users, 0 } it "does not update the resource" do expect { put path, params: { column_name: :name, value: "Bob" } }.not_to change(user, :reload) diff --git a/spec/support/utils.rb b/spec/support/utils.rb index d337095..074d2aa 100644 --- a/spec/support/utils.rb +++ b/spec/support/utils.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true def prepare(&) - stub_const "Rails::Server", true Hotsheet.instance_variable_set :@sheets, nil # reset memoized sheets yield - Rails.application.reload_routes! end diff --git a/spec/system/hotsheet/sheets_controller_spec.rb b/spec/system/hotsheet/sheets_controller_spec.rb index 5e5f7ff..4bce3e0 100644 --- a/spec/system/hotsheet/sheets_controller_spec.rb +++ b/spec/system/hotsheet/sheets_controller_spec.rb @@ -10,7 +10,7 @@ before { prepare { Hotsheet.configure { sheet :User } } } it "updates the cell value" do - visit hotsheet.users_path + visit hotsheet.sheets_path :users find(".cell", text: user.name).click.send_keys(:enter).fill_in(with: "Admin").send_keys :enter expect { find(".cell", text: user.email).click } From b5a51bed314a230664338427b1e4315bd063ca92 Mon Sep 17 00:00:00 2001 From: Chris <76159444+hunchr@users.noreply.github.com> Date: Fri, 27 Jun 2025 09:23:45 +0000 Subject: [PATCH 3/3] Fix specs --- spec/requests/hotsheet/home_controller_spec.rb | 13 +++++++++++++ .../hotsheet/sheets_controller_spec.rb | 18 +++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 spec/requests/hotsheet/home_controller_spec.rb diff --git a/spec/requests/hotsheet/home_controller_spec.rb b/spec/requests/hotsheet/home_controller_spec.rb new file mode 100644 index 0000000..95c99a6 --- /dev/null +++ b/spec/requests/hotsheet/home_controller_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe Hotsheet::HomeController do + describe "#error" do + it "shows an error page" do + get "/hotsheet/invalid/route" + expect(response).to be_not_found + expect(response.body).to include I18n.t("hotsheet.errors.not_found") + end + end +end diff --git a/spec/requests/hotsheet/sheets_controller_spec.rb b/spec/requests/hotsheet/sheets_controller_spec.rb index 5bf985a..1daaa03 100644 --- a/spec/requests/hotsheet/sheets_controller_spec.rb +++ b/spec/requests/hotsheet/sheets_controller_spec.rb @@ -11,11 +11,19 @@ before { prepare { config } } describe "#index" do - it "shows a table with all values" do + it "shows a spreadsheet with all values" do get hotsheet.sheets_path :users expect(response).to be_successful expect(response.body).to include user.name end + + context "when the sheet does not exist" do + it "shows an error page" do + get "/hotsheet/authors" + expect(response).to be_not_found + expect(response.body).to include I18n.t("hotsheet.errors.not_found") + end + end end describe "#update" do @@ -65,12 +73,4 @@ end end end - - describe "#error" do - it "shows an error page" do - get "/hotsheet/authors" - expect(response).to be_not_found - expect(response.body).to include I18n.t("hotsheet.errors.not_found") - end - end end