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" } %>