From 7ba4ae2b5042274f60b4ad7479304ded281649be Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Mon, 10 Jun 2024 20:39:46 +0100 Subject: [PATCH] Can delete stats --- app/controllers/character_sheet_sections_controller.rb | 1 + app/controllers/stats_controller.rb | 6 ++++++ app/models/character.rb | 1 + app/models/user.rb | 1 + app/views/character_sheet_sections/_edit_links.html.erb | 2 +- app/views/stats/_stat.html.erb | 3 +++ app/views/stats/destroy.turbo_stream.erb | 1 + config/locales/en.yml | 3 +++ config/routes.rb | 1 + test/controllers/stats_controller_test.rb | 8 ++++++++ test/system/character_sheet_test.rb | 7 +++++++ 11 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 app/views/stats/destroy.turbo_stream.erb diff --git a/app/controllers/character_sheet_sections_controller.rb b/app/controllers/character_sheet_sections_controller.rb index 47a8975..2340376 100644 --- a/app/controllers/character_sheet_sections_controller.rb +++ b/app/controllers/character_sheet_sections_controller.rb @@ -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 diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 2b5fb50..b96ac25 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -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 diff --git a/app/models/character.rb b/app/models/character.rb index 8d60aa1..faa8b80 100644 --- a/app/models/character.rb +++ b/app/models/character.rb @@ -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 } diff --git a/app/models/user.rb b/app/models/user.rb index 71061c7..97a6154 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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| diff --git a/app/views/character_sheet_sections/_edit_links.html.erb b/app/views/character_sheet_sections/_edit_links.html.erb index 74cb69c..28eac2f 100644 --- a/app/views/character_sheet_sections/_edit_links.html.erb +++ b/app/views/character_sheet_sections/_edit_links.html.erb @@ -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), diff --git a/app/views/stats/_stat.html.erb b/app/views/stats/_stat.html.erb index 0175048..584b344 100644 --- a/app/views/stats/_stat.html.erb +++ b/app/views/stats/_stat.html.erb @@ -1,4 +1,7 @@
+ <% if @editable %> + <%= link_to(t(".delete"), stat, data: { turbo_method: :delete, turbo_confirm: t(".confirm_delete", name: stat.name) }) %> + <% end %>
<%= stat.name %>
diff --git a/app/views/stats/destroy.turbo_stream.erb b/app/views/stats/destroy.turbo_stream.erb new file mode 100644 index 0000000..f50ff4a --- /dev/null +++ b/app/views/stats/destroy.turbo_stream.erb @@ -0,0 +1 @@ +<%= turbo_stream.remove @id %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 2a64aa8..6230b67 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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: diff --git a/config/routes.rb b/config/routes.rb index 21eda7b..0573a27 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 ] diff --git a/test/controllers/stats_controller_test.rb b/test/controllers/stats_controller_test.rb index 3414204..8be6aa2 100644 --- a/test/controllers/stats_controller_test.rb +++ b/test/controllers/stats_controller_test.rb @@ -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 diff --git a/test/system/character_sheet_test.rb b/test/system/character_sheet_test.rb index 99085bc..9e85c82 100644 --- a/test/system/character_sheet_test.rb +++ b/test/system/character_sheet_test.rb @@ -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