Add text fields to character sheets

This commit is contained in:
Trevor Vallender 2024-06-14 12:59:35 +01:00
parent de37a7e81f
commit 8e1a16bb06
17 changed files with 126 additions and 7 deletions

View File

@ -11,6 +11,7 @@ class CountersController < ApplicationController
def create
@counter = @section.counters.new(counter_params)
@editable = true
unless @counter.save
render :new, status: :unprocessable_entity
end

View File

@ -11,6 +11,7 @@ class StatsController < ApplicationController
def create
@stat = @section.stats.new(stat_params)
@editable = true
unless @stat.save
render :new, status: :unprocessable_entity
end

View File

@ -0,0 +1,49 @@
# frozen_string_literal: true
class TextFieldsController < ApplicationController
before_action :set_section, only: [ :new, :create ]
before_action :set_character, only: [ :new, :create ]
before_action :set_text_field, only: [ :update, :destroy ]
def new
@text_field = @section.text_fields.new
end
def create
@text_field = @section.text_fields.new(text_field_params)
@editable = true
unless @text_field.save
render :new, status: :unprocessable_entity
end
end
def update
@text_field.update(text_field_params)
end
def destroy
@id = helpers.dom_id(@text_field)
@text_field.destroy
end
private
def set_character
@character = @section.character
end
def set_section
@section = Current.user.character_sheet_sections.find(params[:character_sheet_section_id])
end
def set_text_field
@text_field = Current.user.text_fields.find(params[:id])
end
def text_field_params
params.require(:text_field).permit(
:name,
:content,
:character_sheet_section_id,
)
end
end

View File

@ -12,6 +12,7 @@ class User < ApplicationRecord
has_many :counters, through: :character_sheet_sections
has_many :stats, through: :character_sheet_sections
has_many :tables, through: :players
has_many :text_fields, through: :character_sheet_sections
has_rich_text :profile
has_one_attached :avatar do |attachable|
attachable.variant :standard, resize_to_limit: [ 100, 100 ], preprocessed: true

View File

@ -10,6 +10,9 @@
<div id="<%= dom_id(character_sheet_section) %>_counters" class="counters">
<%= render character_sheet_section.counters %>
</div>
<div id="<%= dom_id(character_sheet_section) %>_text_fields" class="text-fields">
<%= render character_sheet_section.text_fields %>
</div>
<div id="<%= dom_id(character_sheet_section) %>_sections">
<%= render character_sheet_section.character_sheet_subsections %>
</div>

View File

@ -7,6 +7,8 @@
new_character_sheet_section_stat_path(section), data: { turbo_stream: true }, class: "add-stat" %>
<%= link_to t(".add_counter"),
new_character_sheet_section_counter_path(section), data: { turbo_stream: true }, class: "add-counter" %>
<%= link_to t(".add_text_field"),
new_character_sheet_section_text_field_path(section), data: { turbo_stream: true }, class: "add-text-field" %>
<% end %>
<% unless id == "character_sheet_add_section" %>
<%= link_to t(".delete_section", name: section.name), character_sheet_section_path(section),

View File

@ -1,11 +1,11 @@
<%= content_target = "#{dom_id(@section)}_counters" %>
<% content_target = "#{dom_id(@section)}_counters" %>
<%= turbo_stream.append(content_target) do %>
<%= render @counter %>
<% end %>
<%= form_target = "#{dom_id(@section)}_add_section" %>
<% form_target = "#{dom_id(@section)}_add_section" %>
<%= turbo_stream.replace(form_target) do %>
<div id="#{dom_id(@section)}_add_section">
<div id="<%= dom_id(@section) %>_add_section">
<%= render partial: "character_sheet_sections/edit_links", locals: { section: @section, parent: @section.parent_section, id: nil} %>
</div>
<% end %>

View File

@ -5,7 +5,7 @@
<%= form_target = "#{dom_id(@section)}_add_section" %>
<%= turbo_stream.replace(form_target) do %>
<div id="#{dom_id(@section)}_add_section">
<div id="<%= dom_id(@section) %>_add_section">
<%= render partial: "character_sheet_sections/edit_links", locals: { section: @section, parent: @section.parent_section, id: nil} %>
</div>
<% end %>

View File

@ -0,0 +1,11 @@
<section class="inset">
<%= form_with model: @text_field, url: character_sheet_section_text_fields_path(@section) do |f| %>
<%= f.hidden_field :character_sheet_section_id, value: @section.id %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= display_form_errors(@section, :name) %>
<%= f.submit button_text %>
<% end %>
</section>

View File

@ -0,0 +1,17 @@
<div class="text-field" id="<%= dom_id(text_field) %>">
<% if @editable %>
<%= link_to t(".delete"), text_field,
data: { turbo_method: :delete, turbo_confirm: t(".confirm_delete", name: text_field.name) } %>
<% end %>
<h6><%= text_field.name %></h6>
<% if @editable %>
<%= form_with model: text_field, class: "text-field-form" do |f| %>
<%= f.rich_text_area :content %>
<%= f.submit t(".save") %>
<% end %>
<% else %>
<p><%= text_field.content %></p>
<% end %>
</div>

View File

@ -0,0 +1,11 @@
<% content_target = "#{dom_id(@section)}_text_fields" %>
<%= turbo_stream.append(content_target) do %>
<%= render @text_field %>
<% end %>
<% form_target = "#{dom_id(@section)}_add_section" %>
<%= turbo_stream.replace(form_target) do %>
<div id="<% dom_id(@section) %>_add_section">
<%= render partial: "character_sheet_sections/edit_links", locals: { section: @section, parent: @section.parent_section, id: nil} %>
</div>
<% end %>

View File

@ -0,0 +1 @@
<%= turbo_stream.remove @id %>

View File

@ -0,0 +1,6 @@
<% target = "#{dom_id(@text_field.character_sheet_section)}_add_section" %>
<%= turbo_stream.replace(target) do %>
<div id="<%= target %>">
<%= render partial: "form", locals: { button_text: t(".create_text_field") } %>
</div>
<% end %>

View File

@ -0,0 +1,3 @@
<%= turbo_stream.replace dom_id(@text_field) do %>
<%= render @text_field %>
<% end %>

View File

@ -65,6 +65,7 @@ en:
confirm_delete: Are you sure you want to delete this section?
add_stat: Add stat
add_counter: Add counter
add_text_field: Add text field
index:
edit: Edit character sheet
stop_editing: Stop editing
@ -213,6 +214,13 @@ en:
destroy:
success: Deleted table “%{name}”.
error: Failed to delete table.
text_fields:
text_field:
delete: Delete text field
confirm_delete: Are you sure you want to delete %{name}?
save: Save
new:
create_text_field: Create text field
users:
validations:
email_format: "must be a valid email address"

View File

@ -18,6 +18,7 @@ Rails.application.routes.draw do
resources :character_sheet_sections, only: [ :destroy ] do
resources :counters, only: [ :new, :create ]
resources :stats, only: [ :new, :create ]
resources :text_fields, only: [ :new, :create ]
end
resources :characters do
resources :character_sheet_sections, only: [ :index, :new, :create ]
@ -33,6 +34,7 @@ Rails.application.routes.draw do
resources :events, only: [ :index ]
resources :table_invites, only: [ :new, :create ]
end
resources :text_fields, only: [ :update, :destroy ]
resources :admin, only: [ :index ]
namespace :admin do

View File

@ -1,9 +1,12 @@
- don't roll when no roll set
- set orders on sheets
- easily edit text fields without entering edit mode
- add roll command to counters (e.g. luck)
- test for rolls controller & system tests
- table notifications for rolls
- roll on click
- Show individual dice on dice roll
- customise trix bar
- Sheets
- Lists
- Text fields
- Weapons/spells
- Character avatars
- default avatars