<%= image_tag "eye.svg", aria: { hidden: true }, size: 36, class: "colorize--black" %>
diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb
index b90644d1..b2fc57e7 100644
--- a/app/views/books/show.html.erb
+++ b/app/views/books/show.html.erb
@@ -73,25 +73,27 @@
- <%= arrangement_tag @book, class: "arrangement__container toc__container full-width txt-align-center" do %>
+ <%= arrangement_tag @book, class: "arrangement__container toc__container full-width txt-align-center #{'toc__container--force-' + @book.default_view unless @book.allow_view_selector?}" do %>
" data-controller="toc-view" data-toc-view-id-value="<%= dom_id(@book) %>">
-
-
-
+
" data-controller="toc-view" data-toc-view-id-value="<%= dom_id(@book) %>" data-toc-view-default-value="<%= @book.default_view %>">
+ <% if @book.allow_view_selector? %>
+
+
+
+ <% end %>
<% if @book.editable? %>
<%= render "books/create_buttons", book: @book %>
diff --git a/db/migrate/20260317235838_add_view_settings_to_books.rb b/db/migrate/20260317235838_add_view_settings_to_books.rb
new file mode 100644
index 00000000..47cb681f
--- /dev/null
+++ b/db/migrate/20260317235838_add_view_settings_to_books.rb
@@ -0,0 +1,6 @@
+class AddViewSettingsToBooks < ActiveRecord::Migration[8.0]
+ def change
+ add_column :books, :default_view, :string, default: "grid", null: false
+ add_column :books, :allow_view_selector, :boolean, default: true, null: false
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index e0331993..cf0b7839 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,42 +10,42 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema[8.0].define(version: 2024_09_28_005927) do
+ActiveRecord::Schema[8.2].define(version: 2026_03_17_235838) do
create_table "accesses", force: :cascade do |t|
- t.integer "user_id", null: false
t.integer "book_id", null: false
- t.string "level", null: false
t.datetime "created_at", null: false
+ t.string "level", null: false
t.datetime "updated_at", null: false
+ t.integer "user_id", null: false
t.index ["book_id"], name: "index_accesses_on_book_id"
t.index ["user_id", "book_id"], name: "index_accesses_on_user_id_and_book_id", unique: true
t.index ["user_id"], name: "index_accesses_on_user_id"
end
create_table "accounts", force: :cascade do |t|
- t.string "name", null: false
- t.string "join_code", null: false
t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
t.text "custom_styles"
+ t.string "join_code", null: false
+ t.string "name", null: false
+ t.datetime "updated_at", null: false
end
create_table "action_text_markdowns", force: :cascade do |t|
- t.string "record_type", null: false
- t.integer "record_id", null: false
- t.string "name", null: false
t.text "content", default: "", null: false
t.datetime "created_at", null: false
+ t.string "name", null: false
+ t.integer "record_id", null: false
+ t.string "record_type", null: false
t.datetime "updated_at", null: false
t.index ["record_type", "record_id"], name: "index_action_text_markdowns_on_record"
end
create_table "active_storage_attachments", force: :cascade do |t|
- t.string "name", null: false
- t.string "record_type", null: false
- t.bigint "record_id", null: false
t.bigint "blob_id", null: false
t.datetime "created_at", null: false
+ t.string "name", null: false
+ t.bigint "record_id", null: false
+ t.string "record_type", null: false
t.string "slug"
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
@@ -53,14 +53,14 @@
end
create_table "active_storage_blobs", force: :cascade do |t|
- t.string "key", null: false
- t.string "filename", null: false
- t.string "content_type"
- t.text "metadata"
- t.string "service_name", null: false
t.bigint "byte_size", null: false
t.string "checksum"
+ t.string "content_type"
t.datetime "created_at", null: false
+ t.string "filename", null: false
+ t.string "key", null: false
+ t.text "metadata"
+ t.string "service_name", null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end
@@ -71,24 +71,26 @@
end
create_table "books", force: :cascade do |t|
- t.string "title", null: false
- t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
- t.string "subtitle"
+ t.boolean "allow_view_selector", default: true, null: false
t.string "author"
+ t.datetime "created_at", null: false
+ t.string "default_view", default: "grid", null: false
+ t.boolean "everyone_access", default: true, null: false
t.boolean "published", default: false, null: false
t.string "slug", null: false
- t.boolean "everyone_access", default: true, null: false
+ t.string "subtitle"
t.string "theme", default: "blue", null: false
+ t.string "title", null: false
+ t.datetime "updated_at", null: false
t.index ["published"], name: "index_books_on_published"
end
create_table "edits", force: :cascade do |t|
- t.integer "leaf_id", null: false
- t.string "leafable_type", null: false
- t.integer "leafable_id", null: false
t.string "action", null: false
t.datetime "created_at", null: false
+ t.integer "leaf_id", null: false
+ t.integer "leafable_id", null: false
+ t.string "leafable_type", null: false
t.datetime "updated_at", null: false
t.index ["leaf_id"], name: "index_edits_on_leaf_id"
t.index ["leafable_type", "leafable_id"], name: "index_edits_on_leafable"
@@ -96,13 +98,13 @@
create_table "leaves", force: :cascade do |t|
t.integer "book_id", null: false
- t.string "leafable_type", null: false
+ t.datetime "created_at", null: false
t.integer "leafable_id", null: false
+ t.string "leafable_type", null: false
t.float "position_score", null: false
t.string "status", null: false
- t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
t.string "title", null: false
+ t.datetime "updated_at", null: false
t.index ["book_id"], name: "index_leaves_on_book_id"
t.index ["leafable_type", "leafable_id"], name: "index_leafs_on_leafable"
end
@@ -113,37 +115,37 @@
end
create_table "pictures", force: :cascade do |t|
+ t.string "caption"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
- t.string "caption"
end
create_table "sections", force: :cascade do |t|
+ t.text "body"
t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
t.string "theme"
- t.text "body"
+ t.datetime "updated_at", null: false
end
create_table "sessions", force: :cascade do |t|
- t.integer "user_id", null: false
- t.string "token", null: false
+ t.datetime "created_at", null: false
t.string "ip_address"
- t.string "user_agent"
t.datetime "last_active_at", null: false
- t.datetime "created_at", null: false
+ t.string "token", null: false
t.datetime "updated_at", null: false
+ t.string "user_agent"
+ t.integer "user_id", null: false
t.index ["token"], name: "index_sessions_on_token", unique: true
t.index ["user_id"], name: "index_sessions_on_user_id"
end
create_table "users", force: :cascade do |t|
- t.string "name", null: false
+ t.boolean "active", default: true
+ t.datetime "created_at", null: false
t.string "email_address", null: false
+ t.string "name", null: false
t.string "password_digest", null: false
t.integer "role", null: false
- t.boolean "active", default: true
- t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email_address"], name: "index_users_on_email_address", unique: true
t.index ["name"], name: "index_users_on_name", unique: true
diff --git a/test/controllers/books_controller_test.rb b/test/controllers/books_controller_test.rb
index 81d5f486..3a900348 100644
--- a/test/controllers/books_controller_test.rb
+++ b/test/controllers/books_controller_test.rb
@@ -114,4 +114,49 @@ class BooksControllerTest < ActionDispatch::IntegrationTest
assert_response :success
assert_select "link[rel=\"alternate\"][type=\"text/markdown\"][href=\"#{book_slug_path(books(:handbook), format: :md)}\"]"
end
+
+ test "update persists view settings" do
+ book = books(:handbook)
+
+ patch book_path(book), params: { book: { default_view: "list", allow_view_selector: false } }
+
+ assert_redirected_to book_slug_path(book)
+ book.reload
+ assert_equal "list", book.default_view
+ assert_not book.allow_view_selector?
+ end
+
+ test "show hides view selector when disabled" do
+ book = books(:handbook)
+ book.update!(allow_view_selector: false)
+
+ get book_slug_path(book)
+
+ assert_response :success
+ assert_not_in_body "List view"
+ assert_not_in_body "Page view"
+ assert_not_in_body "view-list"
+ assert_not_in_body "view-grid"
+ end
+
+ test "show applies forced view class when selector is disabled" do
+ book = books(:handbook)
+ book.update!(allow_view_selector: false, default_view: "list")
+
+ get book_slug_path(book)
+
+ assert_response :success
+ assert_in_body "toc__container--force-list"
+ end
+
+ test "show shows view selector when enabled" do
+ book = books(:handbook)
+ book.update!(allow_view_selector: true)
+
+ get book_slug_path(book)
+
+ assert_response :success
+ assert_in_body "List view"
+ assert_in_body "Page view"
+ end
end
diff --git a/test/models/book_test.rb b/test/models/book_test.rb
index be0e37d2..db2d8567 100644
--- a/test/models/book_test.rb
+++ b/test/models/book_test.rb
@@ -40,4 +40,15 @@ class BookTest < ActiveSupport::TestCase
test "markable returns empty string for book with no leaves" do
assert_equal "", books(:manual).markable
end
+
+ test "default_view defaults to grid" do
+ book = Book.create!(title: "Test Book")
+ assert_equal "grid", book.default_view
+ assert book.grid_default_view?
+ end
+
+ test "allow_view_selector defaults to true" do
+ book = Book.create!(title: "Test Book")
+ assert book.allow_view_selector?
+ end
end