Can add new stats

This commit is contained in:
Trevor Vallender 2024-06-07 16:53:32 +01:00
parent 8ac1a845f6
commit 7753e6d268
13 changed files with 139 additions and 7 deletions

View File

@ -49,3 +49,29 @@
padding: 1em;
}
}
.stats {
display: flex;
flex-wrap: wrap;
gap: .2em;
justify-content: center;
}
.stat {
display: flex;
flex-direction: column;
border-radius: var(--border-radius);
h6 {
font-size: .8em;
text-align: center;
background-color: var(--background-color);
color: var(--header-text-color);
margin: 0;
padding: .5em;
}
input {
text-align: center;
font-size: 3em;
width: 2.5em;
}
}

View File

@ -0,0 +1,35 @@
# frozen_string_literal: true
class StatsController < ApplicationController
before_action :set_section, only: [ :new, :create ]
before_action :set_character, only: [ :new, :create ]
def new
@stat = @section.stats.new
end
def create
@stat = @section.stats.new(stat_params)
unless @stat.save
render :new, status: :unprocessable_entity
end
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 stat_params
params.require(:stat).permit(
:name,
:value,
:roll_command,
:character_sheet_section_id,
)
end
end

View File

@ -4,6 +4,9 @@
<h4><%= character_sheet_section.name %></h4>
<div class="content">
<div id="<%= dom_id(character_sheet_section) %>_stats" class="stats">
<%= render character_sheet_section.stats %>
</div>
<div id="<%= dom_id(character_sheet_section) %>_sections">
<%= render character_sheet_section.character_sheet_subsections %>
</div>

View File

@ -1,8 +1,12 @@
<%# locals: (section:, parent:, id:) -%>
<%= link_to t(".add_subsection"),
new_character_character_sheet_section_path(@character, parent_section_id: parent&.id),
data: { turbo_stream: true } %>
<% unless id == "character_sheet_add_section" %>
<%= link_to t(".delete_section", name: section.name), character_sheet_section_path(section),
data: { turbo_method: :delete, turbo_confirm: t(".confirm_delete", name: section.name) } %>
new_character_character_sheet_section_path(@character, parent_section_id: parent&.id),
data: { turbo_stream: true } %>
<% if section.present? %>
<%= link_to t(".add_stat"),
new_character_sheet_section_stat_path(section), data: { turbo_stream: true } %>
<% end %>
<% unless id == "character_sheet_add_section" %>
<%= link_to t(".delete_section", name: section.name), character_sheet_section_path(section),
data: { turbo_method: :delete, turbo_confirm: t(".confirm_delete", name: section.name) } %>
<% end %>

View File

@ -0,0 +1,15 @@
<section class="inset">
<%= form_with model: @stat, url: character_sheet_section_stats_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.label :roll_command %>
<%= f.text_field :roll_command %>
<%= display_form_errors(@section, :roll_command) %>
<%= f.submit button_text %>
<% end %>
</section>

View File

@ -0,0 +1,4 @@
<div class="stat" id="<%= dom_id(stat) %>">
<h6><%= stat.name %></h6>
<input type="number" value="<%= stat.value %>" step="1">
</div>

View File

@ -0,0 +1,11 @@
<%= content_target = "#{dom_id(@section)}_stats" %>
<%= turbo_stream.append(content_target) do %>
<%= render @stat %>
<% 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,6 @@
<% target = "#{dom_id(@stat.character_sheet_section)}_add_section" %>
<%= turbo_stream.replace(target) do %>
<div id="<%= target %>">
<%= render partial: "form", locals: { button_text: t(".create_stat") } %>
</div>
<% end %>

View File

@ -60,6 +60,7 @@ en:
add_subsection: Add subsection
delete_section: Delete %{name}
confirm_delete: Are you sure you want to delete this section?
add_stat: Add stat
index:
character_sheet: "%{name}s character sheet"
no_sections: This character sheet has no content
@ -129,6 +130,9 @@ en:
destroy:
log_out: Log out
success: "You have signed out."
stats:
new:
create_stat: Create stat
table_invite_mailer:
invite_new_user:
subject: Youve been invited to join a game on Tabletop Companion!

View File

@ -15,7 +15,9 @@ Rails.application.routes.draw do
resources :password_resets, only: [ :new, :create, :edit, :update ]
resources :sessions, only: [ :new, :create, :destroy ]
resources :character_sheet_sections, only: [ :destroy ]
resources :character_sheet_sections, only: [ :destroy ] do
resources :stats, only: [ :new, :create ]
end
resources :characters do
resources :character_sheet_sections, only: [ :index, :new, :create ]
end

View File

@ -6,7 +6,7 @@ class CreateStats < ActiveRecord::Migration[7.1]
t.string :name, null: false
t.string :slug, null: false
t.belongs_to :character_sheet_section, null: false, foreign_key: true
t.decimal :value, null: false, default: 0
t.integer :value, null: false, default: 0
t.string :roll_command
t.timestamps

View File

@ -0,0 +1,20 @@
# frozen_string_literal: true
require "test_helper"
class StatsControllerTest < ActionDispatch::IntegrationTest
test "should render new turbo stream" do
sign_in users(:trevor)
get new_character_sheet_section_stat_url(character_sheet_sections(:stats)), as: :turbo_stream
assert_response :success
end
test "should create stat" do
sign_in users(:trevor)
assert_difference "Stat.count", 1 do
post character_sheet_section_stats_url(character_sheet_sections(:stats)),
params: { stat: { name: "Wisdom", character_sheet_section_id: character_sheet_sections(:stats).id } },
as: :turbo_stream
end
end
end

View File

@ -1,3 +1,5 @@
- fix inconsistencies in nested create: do we need to submit the id or use in url?
- add stat stimulus controller to update stats
- default avatars
- add uuid/slug to characters and any other url-visible ids
- shared/private notes