Create templates

This commit is contained in:
Trevor Vallender 2024-07-02 18:07:06 +01:00
parent d4b982ea16
commit 79e3e79b12
10 changed files with 98 additions and 6 deletions

View File

@ -0,0 +1,47 @@
# frozen_string_literal: true
class TemplatesController < ApplicationController
before_action :set_character_sheet_section, only: [ :new ]
before_action :set_template, only: [ :show ]
def new
@template = Template.new
end
def create
@section = CharacterSheetSection.find(template_params[:character_sheet_section_id])
@template = TemplateBuilder.create!(
name: template_params[:name],
from: @section,
game_system: GameSystem.find(template_params[:game_system_id]),
)
if @template.persisted?
redirect_to @template
else
flash.now[:alert] = t(".error")
render :new, status: :unprocessable_entity
end
end
def show
end
private
def set_template
@template = Template.find(params[:id])
end
def set_character_sheet_section
@section = CharacterSheetSection.find(params[:character_sheet_section_id])
end
def template_params
params.require(:template).permit(
:name,
:character_sheet_section_id,
:game_system_id,
:klass,
)
end
end

View File

@ -3,7 +3,7 @@
class TemplateBuilder
class << self
def create!(name:, from:, game_system:)
Template.create!(
Template.create(
name:,
klass: from.class.name,
game_system:,

View File

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

View File

@ -1,5 +1,5 @@
<div id=<%= dom_id(character) %> class="character">
<h5><%= link_to character.name, character %></h5>
<h5><%= link_to character.name, character, data: { turbo_frame: "_top" } %></h5>
<ul>
<li><%= character.game_system.name %></li>
<li><%= character.user.username %></li>

View File

@ -0,0 +1,13 @@
<section class="inset">
<%= form_with model: @template, url: character_sheet_section_templates_path(@section) do |f| %>
<%= f.hidden_field :character_sheet_section_id, value: @section.id %>
<%= f.hidden_field :game_system_id, value: @section.character.game_system.id %>
<%= f.hidden_field :klass, value: "CharacterSheetSection" %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= display_form_errors(@section, :name) %>
<%= f.submit t(".save") %>
<% end %>
</section>

View File

@ -0,0 +1,6 @@
<% content_for :title, t(".create_template") %>
<h1><%= t(".create_template") %></h1>
<%= render partial: "templates/form",
locals: { template: @template, button_text: t(".create_template") } %>

View File

@ -0,0 +1,10 @@
<% content_for :title, @template.name %>
<h1><%= @template.name %></h1>
<dl>
<dt><%= t(".game_system") %>:</dt>
<dd><%= @template.game_system.name %></dd>
<dt><%= t(".content") %>:</dt>
<dd><%= @template.content %></dd>
</dl>

View File

@ -66,6 +66,7 @@ en:
confirm_delete: Are you sure you want to delete this section?
add_stat: Add stat
add_text_field: Add text field
save_as_template: Save as template
index:
edit: Edit character sheet
stop_editing: Stop editing
@ -141,7 +142,7 @@ en:
stats:
show:
roll: Roll!
roll_type_html: Roll %{name}! <br> <small>%{command}</small>
roll_type_html: Roll %{name}! (%{command})
min_allowed: Min
max_allowed: Max
stat:
@ -220,6 +221,17 @@ en:
destroy:
success: Deleted table “%{name}”.
error: Failed to delete table.
templates:
new:
create_template: Create template
create:
success: The template “%{name}” has been created
error: Failed to create template
form:
save: Create template
show:
game_system: Game system
content: Content
text_fields:
text_field:
delete: Delete text field

View File

@ -23,6 +23,7 @@ Rails.application.routes.draw do
end
resources :character_sheet_sections, only: [ :destroy ] do
resources :stats, only: [ :new, :create ]
resources :templates, only: [ :new, :create ]
resources :text_fields, only: [ :new, :create ]
end
resources :characters do
@ -38,6 +39,7 @@ Rails.application.routes.draw do
resources :events, only: [ :index ]
resources :table_invites, only: [ :new, :create ]
end
resources :templates, only: [ :show ]
resources :text_fields, only: [ :show, :update, :destroy ]
resources :admin, only: [ :index ]

View File

@ -1,6 +1,6 @@
- Custom features
- CustomFeature basically a CharacterSheetSection
- belongs_to character_sheet_section becomes polymorphic
- Templates
- Add from template
- Edit/delete templates
- Lists
- request invite
- improve dice roll parsing to allow e.g. choose highest