diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb
new file mode 100644
index 0000000..a8a6ff8
--- /dev/null
+++ b/app/controllers/templates_controller.rb
@@ -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
diff --git a/app/services/template_builder.rb b/app/services/template_builder.rb
index addbdbf..df10098 100644
--- a/app/services/template_builder.rb
+++ b/app/services/template_builder.rb
@@ -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:,
diff --git a/app/views/character_sheet_sections/_edit_links.html.erb b/app/views/character_sheet_sections/_edit_links.html.erb
index 9d5c58b..dda6d7d 100644
--- a/app/views/character_sheet_sections/_edit_links.html.erb
+++ b/app/views/character_sheet_sections/_edit_links.html.erb
@@ -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),
diff --git a/app/views/characters/_character.html.erb b/app/views/characters/_character.html.erb
index 62f0560..6302371 100644
--- a/app/views/characters/_character.html.erb
+++ b/app/views/characters/_character.html.erb
@@ -1,5 +1,5 @@
class="character">
-
<%= link_to character.name, character %>
+
<%= link_to character.name, character, data: { turbo_frame: "_top" } %>
- <%= character.game_system.name %>
- <%= character.user.username %>
diff --git a/app/views/templates/_form.html.erb b/app/views/templates/_form.html.erb
new file mode 100644
index 0000000..6b033d4
--- /dev/null
+++ b/app/views/templates/_form.html.erb
@@ -0,0 +1,13 @@
+
+ <%= 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 %>
+
diff --git a/app/views/templates/new.html.erb b/app/views/templates/new.html.erb
new file mode 100644
index 0000000..5954fa6
--- /dev/null
+++ b/app/views/templates/new.html.erb
@@ -0,0 +1,6 @@
+<% content_for :title, t(".create_template") %>
+
+<%= t(".create_template") %>
+
+<%= render partial: "templates/form",
+ locals: { template: @template, button_text: t(".create_template") } %>
diff --git a/app/views/templates/show.html.erb b/app/views/templates/show.html.erb
new file mode 100644
index 0000000..c019108
--- /dev/null
+++ b/app/views/templates/show.html.erb
@@ -0,0 +1,10 @@
+<% content_for :title, @template.name %>
+
+<%= @template.name %>
+
+
+ - <%= t(".game_system") %>:
+ - <%= @template.game_system.name %>
+ - <%= t(".content") %>:
+ - <%= @template.content %>
+
diff --git a/config/locales/en.yml b/config/locales/en.yml
index f082184..5e4701a 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -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}!
%{command}
+ 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
diff --git a/config/routes.rb b/config/routes.rb
index 2912b05..9ebb35c 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -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 ]
diff --git a/todo.md b/todo.md
index d0a1603..f362b6c 100644
--- a/todo.md
+++ b/todo.md
@@ -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