diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index ebd8d90..cb761f5 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -9,9 +9,9 @@ def index path.blank? or path == '/' end if home_page - @locations = Location.where(front_page: true).where.not(code: 'all') + @locations = Location.where(front_page: true).where.not(code: 'all', suppress_display: true) else - @locations = Location.where.not(code: 'all') + @locations = Location.where.not(code: 'all', suppress_display: true) end @locations = @locations.order(:name) @now = Time.current @@ -59,6 +59,13 @@ def destroy end def show + unless @location && (current_user || !@location.suppress_display) + respond_to do |format| + format.html { render "errors/not_found", status: 404, layout: "public" } + format.json { render json: {}, status: 404 } + end + return + end @secondary_locations = Location.where(primary_location: @location).load @date = params[:date] ? Date.parse(params[:date]) : Date.current respond_to do |format| @@ -129,7 +136,7 @@ def create_params def update_params if current_user.administrator? - params.require(:location).permit(:name, :code, :comment, :comment_two, :url, :summary, :primary, :primary_location_id, :front_page, :short_note, :short_note_url) + params.require(:location).permit(:name, :code, :comment, :comment_two, :url, :summary, :primary, :primary_location_id, :front_page, :short_note, :short_note_url, :suppress_display) else params.require(:location).permit(:comment, :comment_two, :url, :summary, :short_note, :short_note_url) end diff --git a/app/views/errors/internal_server_error.html.erb b/app/views/errors/internal_server_error.html.erb new file mode 100644 index 0000000..881855f --- /dev/null +++ b/app/views/errors/internal_server_error.html.erb @@ -0,0 +1,5 @@ +
+

Internal Server Error

+ +

Please contact a site administrator to report an error at this location.

+
diff --git a/app/views/errors/not_found.html.erb b/app/views/errors/not_found.html.erb new file mode 100644 index 0000000..749522c --- /dev/null +++ b/app/views/errors/not_found.html.erb @@ -0,0 +1,5 @@ +
+

Location not found

+ +

Contact a site administrator if you think this is an error.

+
diff --git a/app/views/locations/_location_form.html.erb b/app/views/locations/_location_form.html.erb index c0dde19..2c31684 100644 --- a/app/views/locations/_location_form.html.erb +++ b/app/views/locations/_location_form.html.erb @@ -18,6 +18,15 @@ <% end %> +
+ <%= f.label :suppress_display %> + <% if current_user.administrator? %> + <%= f.check_box :suppress_display %> + <% elsif @location.suppress_display %> +

<%= t("location.suppressed") %>

+ <% end %> +
+ <% if current_user.administrator? %>
<%= f.label :front_page %> diff --git a/app/views/locations/show.html.erb b/app/views/locations/show.html.erb index ac90ec7..b6da047 100644 --- a/app/views/locations/show.html.erb +++ b/app/views/locations/show.html.erb @@ -4,6 +4,9 @@

<%= link_to @location.name, @location.url %>

+ <% if @location.suppress_display %> +

Public display of this location is suppressed.

+ <% end %> <% if @location.primary_location %>

Check hours for: <%= link_to @location.primary_location.name, location_path(@location.primary_location.code) %>

<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index cf9b342..098952c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -30,4 +30,5 @@ # available at https://guides.rubyonrails.org/i18n.html. en: - hello: "Hello world" + location: + suppressed: "Public display of this location is suppressed." diff --git a/db/migrate/20240710150406_add_location_suppress_display_switch.rb b/db/migrate/20240710150406_add_location_suppress_display_switch.rb new file mode 100644 index 0000000..7e1767f --- /dev/null +++ b/db/migrate/20240710150406_add_location_suppress_display_switch.rb @@ -0,0 +1,9 @@ +class AddLocationSuppressDisplaySwitch < ActiveRecord::Migration[6.0] + def up + add_column :locations, :suppress_display, :boolean, default: false + end + + def down + remove_column :locations, :suppress_display + end +end diff --git a/db/schema.rb b/db/schema.rb index 77dbe38..fce2d8b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -2,53 +2,53 @@ # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # -# This file is the source Rails uses to define your schema when running `rails -# db:schema:load`. When creating a new database, `rails db:schema:load` tends to +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to # be faster and is potentially less error prone than running all of your # migrations from scratch. Old migrations may fail to apply correctly if those # migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_02_28_151512) do - - create_table "locations", charset: "utf8", force: :cascade do |t| +ActiveRecord::Schema[7.0].define(version: 2024_07_10_150406) do + create_table "locations", force: :cascade do |t| t.string "name", null: false t.string "code", null: false t.string "url" t.text "comment" t.text "comment_two" t.string "summary" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", precision: nil, null: false + t.datetime "updated_at", precision: nil, null: false t.bigint "primary_location_id" t.boolean "primary", default: false t.boolean "front_page", default: false, null: false t.string "short_note" t.text "short_note_url" + t.boolean "suppress_display", default: false t.index ["code"], name: "index_locations_on_code" t.index ["front_page"], name: "index_locations_on_front_page" t.index ["primary"], name: "index_locations_on_primary" t.index ["primary_location_id"], name: "index_locations_on_primary_location_id" end - create_table "permissions", charset: "utf8", force: :cascade do |t| + create_table "permissions", force: :cascade do |t| t.bigint "user_id" t.string "role", null: false t.string "subject_class" t.integer "subject_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", precision: nil, null: false + t.datetime "updated_at", precision: nil, null: false t.index ["user_id"], name: "index_permissions_on_user_id" end - create_table "timetables", charset: "utf8", force: :cascade do |t| + create_table "timetables", force: :cascade do |t| t.date "date" - t.datetime "open" - t.datetime "close" + t.datetime "open", precision: nil + t.datetime "close", precision: nil t.text "notes" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", precision: nil, null: false + t.datetime "updated_at", precision: nil, null: false t.boolean "tbd", default: false, null: false t.boolean "closed", default: false, null: false t.string "note" @@ -60,17 +60,17 @@ t.index ["open"], name: "index_timetables_on_open" end - create_table "users", charset: "utf8", force: :cascade do |t| + create_table "users", force: :cascade do |t| t.string "reset_password_token" - t.datetime "reset_password_sent_at" - t.datetime "remember_created_at" + t.datetime "reset_password_sent_at", precision: nil + t.datetime "remember_created_at", precision: nil t.integer "sign_in_count", default: 0, null: false - t.datetime "current_sign_in_at" - t.datetime "last_sign_in_at" + t.datetime "current_sign_in_at", precision: nil + t.datetime "last_sign_in_at", precision: nil t.string "current_sign_in_ip" t.string "last_sign_in_ip" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", precision: nil, null: false + t.datetime "updated_at", precision: nil, null: false t.string "uid" t.string "provider" t.string "email", default: "", null: false diff --git a/spec/features/locations_spec.rb b/spec/features/locations_spec.rb index b583161..dde090e 100644 --- a/spec/features/locations_spec.rb +++ b/spec/features/locations_spec.rb @@ -4,6 +4,8 @@ let(:lehman) { FactoryBot.create(:lehman) } let(:underbutler) { FactoryBot.create(:underbutler) } let(:butler) { underbutler.primary_location } + let(:duanereade) { FactoryBot.create(:duanereade, suppress_display: true) } + context 'when user without role logged in' do # initialize at least one library before { lehman } @@ -28,6 +30,11 @@ expect(page).to have_css("h2", text: underbutler.name) expect(page).to have_css("p", text: butler.name) end + + it "does not display suppressed locations", js: false do + visit("/locations/#{duanereade.code}") + expect(page.status_code).to eq(404) + end end context 'when administrator logged in' do @@ -84,6 +91,20 @@ end it "can update primary location" + + it "displays suppressed locations", js: false do + visit("/locations/#{duanereade.code}") + expect(page).to have_content("Public display of this location is suppressed.") + end + + it "can update suppress_display" do + expect(duanereade.suppress_display).to be true + visit edit_location_path(duanereade.code) + uncheck "Suppress" + click_on "Update Location" + expect(page).to have_content("Location successfully updated") + expect(duanereade.reload.suppress_display).to be false + end end context 'when manager logged in' do @@ -103,6 +124,11 @@ visit new_location_path expect(page).to have_content("Unauthorized") end + + it "displays suppressed locations", js: false do + visit("/locations/#{duanereade.code}") + expect(page).to have_content("Public display of this location is suppressed.") + end end context 'when editor logged in' do @@ -131,5 +157,10 @@ end it "cannot delete lehman" + + it "displays suppressed locations", js: false do + visit("/locations/#{duanereade.code}") + expect(page).to have_content("Public display of this location is suppressed.") + end end end