CRUD actions for counters

This commit is contained in:
Trevor Vallender 2024-06-12 17:51:57 +01:00
parent 05ffcf0456
commit 1b90ed389f
18 changed files with 139 additions and 4 deletions

View File

@ -50,14 +50,14 @@
} }
} }
.stats { .stats, .counters {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
gap: .2em; gap: .2em;
justify-content: center; justify-content: center;
} }
.stat { .stat, .counter {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
border-radius: var(--border-radius); border-radius: var(--border-radius);

View File

@ -41,7 +41,7 @@ form, fieldset {
} }
} }
form.stat-form { form.stat-form, form.counter-form {
display: flex; display: flex;
flex-direction: column; flex-direction: column;

View File

@ -0,0 +1,48 @@
# frozen_string_literal: true
class CountersController < ApplicationController
before_action :set_section, only: [ :new, :create ]
before_action :set_character, only: [ :new, :create ]
before_action :set_counter, only: [ :update, :destroy ]
def new
@counter = @section.counters.new
end
def create
@counter = @section.counters.new(counter_params)
unless @counter.save
render :new, status: :unprocessable_entity
end
end
def update
@counter.update(counter_params)
end
def destroy
@id = helpers.dom_id(@counter)
@counter.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_counter
@counter = Current.user.counters.find(params[:id])
end
def counter_params
params.require(:counter).permit(
:name,
:value,
:character_sheet_section_id,
)
end
end

View File

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

View File

@ -7,6 +7,9 @@
<div id="<%= dom_id(character_sheet_section) %>_stats" class="stats"> <div id="<%= dom_id(character_sheet_section) %>_stats" class="stats">
<%= render character_sheet_section.stats %> <%= render character_sheet_section.stats %>
</div> </div>
<div id="<%= dom_id(character_sheet_section) %>_counters" class="counters">
<%= render character_sheet_section.counters %>
</div>
<div id="<%= dom_id(character_sheet_section) %>_sections"> <div id="<%= dom_id(character_sheet_section) %>_sections">
<%= render character_sheet_section.character_sheet_subsections %> <%= render character_sheet_section.character_sheet_subsections %>
</div> </div>

View File

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

View File

@ -0,0 +1,11 @@
<div class="counter" id="<%= dom_id(counter) %>">
<% if @editable %>
<%= link_to(t(".delete"), counter,
data: { turbo_method: :delete, turbo_confirm: t(".confirm_delete", name: counter.name) }) %>
<% end %>
<h6><%= counter.name %></h6>
<%= form_with model: counter, class: "counter-form",
data: { controller: "auto-update", auto_update_update_url_value: counter_path(counter) } do |f| %>
<%= f.number_field :value %>
<% end %>
</div>

View File

@ -0,0 +1,11 @@
<section class="inset">
<%= form_with model: @counter, url: character_sheet_section_counters_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,11 @@
<%= content_target = "#{dom_id(@section)}_counters" %>
<%= turbo_stream.append(content_target) do %>
<%= render @counter %>
<% 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(@counter.character_sheet_section)}_add_section" %>
<%= turbo_stream.replace(target) do %>
<div id="<%= target %>">
<%= render partial: "form", locals: { button_text: t(".create_counter") } %>
</div>
<% end %>

View File

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

View File

@ -5,7 +5,7 @@
<% end %> <% end %>
<h6><%= stat.name %></h6> <h6><%= stat.name %></h6>
<%= form_with model: stat, class: "stat-form", <%= form_with model: stat, class: "stat-form",
data: { controller: "stat-update", stat_update_update_url_value: stat_path(stat) } do |f| %> data: { controller: "auto-update", auto_update_update_url_value: stat_path(stat) } do |f| %>
<%= f.number_field :value, disabled: !@editable %> <%= f.number_field :value, disabled: !@editable %>
<% end %> <% end %>
</div> </div>

View File

@ -61,6 +61,7 @@ en:
delete_section: Delete %{name} delete_section: Delete %{name}
confirm_delete: Are you sure you want to delete this section? confirm_delete: Are you sure you want to delete this section?
add_stat: Add stat add_stat: Add stat
add_counter: Add counter
index: index:
edit: Edit character sheet edit: Edit character sheet
stop_editing: Stop editing stop_editing: Stop editing
@ -97,6 +98,12 @@ en:
destroy: destroy:
success: Deleted “%{name}” success: Deleted “%{name}”
error: Could not delete your character error: Could not delete your character
counters:
counter:
delete: Delete counter
confirm_delete: Are you sure you want to delete %{name}?
new:
create_counter: Create counter
password_resets: password_resets:
new: new:
reset_password: Reset your password reset_password: Reset your password

View File

@ -16,11 +16,13 @@ Rails.application.routes.draw do
resources :sessions, only: [ :new, :create, :destroy ] resources :sessions, only: [ :new, :create, :destroy ]
resources :character_sheet_sections, only: [ :destroy ] do resources :character_sheet_sections, only: [ :destroy ] do
resources :counters, only: [ :new, :create ]
resources :stats, only: [ :new, :create ] resources :stats, only: [ :new, :create ]
end end
resources :characters do resources :characters do
resources :character_sheet_sections, only: [ :index, :new, :create ] resources :character_sheet_sections, only: [ :index, :new, :create ]
end end
resources :counters, only: [ :update, :destroy ]
resources :stats, only: [ :update, :destroy ] resources :stats, only: [ :update, :destroy ]
resources :table_invites, only: [ :index, :edit, :update ] resources :table_invites, only: [ :index, :edit, :update ]
resources :tables do resources :tables do

View File

@ -0,0 +1,28 @@
# frozen_string_literal: true
require "test_helper"
class CountersControllerTest < ActionDispatch::IntegrationTest
test "should render new turbo stream" do
sign_in users(:trevor)
get new_character_sheet_section_counter_url(character_sheet_sections(:counters)), as: :turbo_stream
assert_response :success
end
test "should create counter" do
sign_in users(:trevor)
assert_difference "Counter.count", 1 do
post character_sheet_section_counters_url(character_sheet_sections(:counters)),
params: { counter: { name: "Ammo", character_sheet_section_id: character_sheet_sections(:counters).id } },
as: :turbo_stream
end
end
test "should delete counter" do
sign_in users(:trevor)
assert_difference "Counter.count", -1 do
delete counter_url(Counter.first), as: :turbo_stream
assert_response :success
end
end
end

View File

@ -7,3 +7,4 @@
- notifications - notifications
- chat - chat
- maps - maps
- show errors from invalid sheet bits