Toggle edit links on character sheets

This commit is contained in:
Trevor Vallender 2024-06-10 18:02:09 +01:00
parent 7753e6d268
commit 781893f12f
5 changed files with 39 additions and 8 deletions

View File

@ -6,6 +6,7 @@ class CharacterSheetSectionsController < ApplicationController
def index
@sections = @character.character_sheet_sections.top_level
@editable = ActiveModel::Type::Boolean.new.cast(params[:editable])
end
def new

View File

@ -10,9 +10,11 @@
<div id="<%= dom_id(character_sheet_section) %>_sections">
<%= render character_sheet_section.character_sheet_subsections %>
</div>
<% if @editable %>
<div id="<%= dom_id(character_sheet_section) %>_add_section">
<%= render partial: "edit_links",
locals: { section: character_sheet_section, parent: character_sheet_section, id: nil } %>
</div>
<% end %>
</div>
</div>

View File

@ -2,6 +2,12 @@
<h2><%= @character.name %></h2>
<% if @editable %>
<%= link_to t(".stop_editing"), character_character_sheet_sections_path(editable: false) %>
<% else %>
<%= link_to t(".edit"), character_character_sheet_sections_path(editable: true) %>
<% end %>
<div id="character_sheet">
<% if @sections.any? %>
<%= render @sections %>
@ -10,7 +16,9 @@
<% end %>
</div>
<% if @editable %>
<div id="character_sheet_add_section">
<%= link_to t(".add_section"), new_character_character_sheet_section_path(@character),
data: { turbo_stream: true } %>
</div>
<% end %>

View File

@ -62,6 +62,8 @@ en:
confirm_delete: Are you sure you want to delete this section?
add_stat: Add stat
index:
edit: Edit character sheet
stop_editing: Stop editing
character_sheet: "%{name}s character sheet"
no_sections: This character sheet has no content
add_section: Add section

View File

@ -0,0 +1,18 @@
# frozen_string_literal: true
require "test_helper"
class EditCharacterSheetTest < ActionDispatch::IntegrationTest
test "Sheet is only editable when parameter is set" do
user = users(:trevor)
sign_in user
get character_character_sheet_sections_path(characters(:nardren))
assert_response :success
assert_no_match I18n.t("character_sheet_sections.index.add_section"), response.body
get character_character_sheet_sections_path(characters(:nardren), editable: true)
assert_response :success
assert_match I18n.t("character_sheet_sections.index.add_section"), response.body
end
end