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 TemplateBuilder
class << self class << self
def create!(name:, from:, game_system:) def create!(name:, from:, game_system:)
Template.create!( Template.create(
name:, name:,
klass: from.class.name, klass: from.class.name,
game_system:, game_system:,

View File

@ -7,6 +7,8 @@
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_text_field"), <%= link_to t(".add_text_field"),
new_character_sheet_section_text_field_path(section), data: { turbo_stream: true }, class: "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 %> <% 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

@ -1,5 +1,5 @@
<div id=<%= dom_id(character) %> class="character"> <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> <ul>
<li><%= character.game_system.name %></li> <li><%= character.game_system.name %></li>
<li><%= character.user.username %></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? confirm_delete: Are you sure you want to delete this section?
add_stat: Add stat add_stat: Add stat
add_text_field: Add text field add_text_field: Add text field
save_as_template: Save as template
index: index:
edit: Edit character sheet edit: Edit character sheet
stop_editing: Stop editing stop_editing: Stop editing
@ -141,7 +142,7 @@ en:
stats: stats:
show: show:
roll: Roll! roll: Roll!
roll_type_html: Roll %{name}! <br> <small>%{command}</small> roll_type_html: Roll %{name}! (%{command})
min_allowed: Min min_allowed: Min
max_allowed: Max max_allowed: Max
stat: stat:
@ -220,6 +221,17 @@ en:
destroy: destroy:
success: Deleted table “%{name}”. success: Deleted table “%{name}”.
error: Failed to delete table. 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_fields:
text_field: text_field:
delete: Delete text field delete: Delete text field

View File

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

View File

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