Can delete stats

This commit is contained in:
Trevor Vallender 2024-06-10 20:39:46 +01:00
parent 781893f12f
commit 7ba4ae2b50
11 changed files with 33 additions and 1 deletions

View File

@ -18,6 +18,7 @@ class CharacterSheetSectionsController < ApplicationController
def create
@section = @character.character_sheet_sections.new(character_sheet_section_params)
@editable = true
unless @section.save
@parent_section = @section.parent_section
render :new, status: :unprocessable_entity

View File

@ -15,6 +15,12 @@ class StatsController < ApplicationController
end
end
def destroy
stat = Current.user.stats.find(params[:id])
@id = helpers.dom_id(stat)
stat.destroy
end
private
def set_character
@character = @section.character

View File

@ -5,6 +5,7 @@ class Character < ApplicationRecord
belongs_to :game_system
belongs_to :user
has_many :character_sheet_sections, dependent: :destroy
has_many :stats, through: :character_sheet_sections
validates :name, presence: true,
length: { maximum: 200 }

View File

@ -9,6 +9,7 @@ class User < ApplicationRecord
has_many :character_sheet_sections, through: :characters
has_many :owned_tables, foreign_key: :owner_id, class_name: "Table"
has_many :players, dependent: :destroy
has_many :stats, through: :character_sheet_sections
has_many :tables, through: :players
has_rich_text :profile
has_one_attached :avatar do |attachable|

View File

@ -4,7 +4,7 @@
data: { turbo_stream: true } %>
<% if section.present? %>
<%= link_to t(".add_stat"),
new_character_sheet_section_stat_path(section), data: { turbo_stream: true } %>
new_character_sheet_section_stat_path(section), data: { turbo_stream: true }, class: "add-stat" %>
<% end %>
<% unless id == "character_sheet_add_section" %>
<%= link_to t(".delete_section", name: section.name), character_sheet_section_path(section),

View File

@ -1,4 +1,7 @@
<div class="stat" id="<%= dom_id(stat) %>">
<% if @editable %>
<%= link_to(t(".delete"), stat, data: { turbo_method: :delete, turbo_confirm: t(".confirm_delete", name: stat.name) }) %>
<% end %>
<h6><%= stat.name %></h6>
<input type="number" value="<%= stat.value %>" step="1">
</div>

View File

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

View File

@ -133,6 +133,9 @@ en:
log_out: Log out
success: "You have signed out."
stats:
stat:
delete: Delete
confirm_delete: Are you sure you want to delete %{name}?
new:
create_stat: Create stat
table_invite_mailer:

View File

@ -21,6 +21,7 @@ Rails.application.routes.draw do
resources :characters do
resources :character_sheet_sections, only: [ :index, :new, :create ]
end
resources :stats, only: [ :destroy ]
resources :table_invites, only: [ :index, :edit, :update ]
resources :tables do
resources :table_invites, only: [ :new, :create ]

View File

@ -17,4 +17,12 @@ class StatsControllerTest < ActionDispatch::IntegrationTest
as: :turbo_stream
end
end
test "should delete stat" do
sign_in users(:trevor)
assert_difference "Stat.count", -1 do
delete stat_url(Stat.first), as: :turbo_stream
assert_response :success
end
end
end

View File

@ -11,6 +11,8 @@ class CharacterSheetTest < ApplicationSystemTestCase
click_on I18n.t("characters.show.sheet")
assert_text character.name
click_on I18n.t("character_sheet_sections.index.edit")
click_link(I18n.t("character_sheet_sections.index.add_section"))
fill_in attr_name(CharacterSheetSection, :name), with: "Test Section"
click_button(I18n.t("character_sheet_sections.new.create_section"))
@ -19,5 +21,10 @@ class CharacterSheetTest < ApplicationSystemTestCase
click_link(I18n.t("character_sheet_sections.edit_links.delete_section", name: "Test Section"))
accept_confirm
assert_no_text "Test Section"
first(".add-stat").click
fill_in attr_name(Stat, :name), with: "Test Stat"
click_button I18n.t("stats.new.create_stat")
assert_text "Test Stat"
end
end