Add sections based on templates

This commit is contained in:
Trevor Vallender 2024-07-03 17:28:09 +01:00
parent 79e3e79b12
commit a8007cb26b
11 changed files with 60 additions and 11 deletions

View File

@ -181,3 +181,7 @@ footer {
margin: 0 auto; margin: 0 auto;
} }
} }
.subsections {
margin: .5em;
}

View File

@ -15,6 +15,7 @@ class CharacterSheetSectionsController < ApplicationController
end end
def new def new
@templates = Template.game_system(@character.game_system)
if params[:parent_section_id].present? if params[:parent_section_id].present?
@parent_section = @character.character_sheet_sections.find_by(id: params[:parent_section_id]) @parent_section = @character.character_sheet_sections.find_by(id: params[:parent_section_id])
end end
@ -22,7 +23,16 @@ class CharacterSheetSectionsController < ApplicationController
end end
def create def create
@section = @character.character_sheet_sections.new(character_sheet_section_params) @templates = Template.game_system(@character.game_system)
if params[:template_id].present?
@section = CharacterSheetSection.new_from_template(
template: Template.find(params[:template_id]),
params: character_sheet_section_params,
character: @character,
)
else
@section = @character.character_sheet_sections.new(character_sheet_section_params)
end
@editable = true @editable = true
unless @section.save unless @section.save
@parent_section = @section.parent_section @parent_section = @section.parent_section

View File

@ -14,7 +14,7 @@ class CharactersController < ApplicationController
def create def create
@character = Current.user.characters.new(character_params) @character = Current.user.characters.new(character_params)
if @character.save if @character.save
redirect_to @character, notice: t(".success") redirect_to @character, notice: t(".success", name: @character.name)
else else
flash.now[:alert] = t(".error") flash.now[:alert] = t(".error")
render :new, status: :unprocessable_entity render :new, status: :unprocessable_entity

View File

@ -18,6 +18,13 @@ class CharacterSheetSection < ApplicationRecord
scope :top_level, -> { where.missing(:parent_section) } scope :top_level, -> { where.missing(:parent_section) }
def self.new_from_template(template:, character:, params:)
section = CharacterSheetSection.deserialize(JSON.parse(template.content))
section.assign_attributes(params)
section.character = character
section
end
def lowest_order_index def lowest_order_index
character_sheet_features.minimum(:order_index) || 1 character_sheet_features.minimum(:order_index) || 1
end end

View File

@ -9,4 +9,6 @@ class Template < ApplicationRecord
validates :content, presence: true validates :content, presence: true
validates :klass, presence: true, validates :klass, presence: true,
length: { maximum: 200 } length: { maximum: 200 }
scope :game_system, ->(game_system) { where(game_system:) }
end end

View File

@ -14,4 +14,8 @@
</div> </div>
<% end %> <% end %>
</div> </div>
<div id="<%= dom_id(character_sheet_section) %>_subsections" class="subsections">
<%= render character_sheet_section.character_sheet_subsections %>
</div>
</div> </div>

View File

@ -6,6 +6,10 @@
<%= f.text_field :name %> <%= f.text_field :name %>
<%= display_form_errors(@section, :name) %> <%= display_form_errors(@section, :name) %>
<%= label_tag :template_id, t(".template") %>
<%= collection_select nil, :template_id, @templates, :id, :name, include_blank: " " %>
<%= display_form_errors(@section, :template) %>
<%= f.submit button_text %> <%= f.submit button_text %>
<% end %> <% end %>
</section> </section>

View File

@ -1,4 +1,4 @@
<% content_target = @section.parent_section.present? ? "#{dom_id(@section.parent_section)}_sections" <% content_target = @section.parent_section.present? ? "#{dom_id(@section.parent_section)}_subsections"
: "character_sheet" %> : "character_sheet" %>
<%= turbo_stream.append(content_target) do %> <%= turbo_stream.append(content_target) do %>
<%= render @section %> <%= render @section %>

View File

@ -5,7 +5,7 @@
<% form_target = "#{dom_id(@section)}_add_section" %> <% form_target = "#{dom_id(@section)}_add_section" %>
<%= turbo_stream.replace(form_target) do %> <%= turbo_stream.replace(form_target) do %>
<div id="<% dom_id(@section) %>_add_section"> <div id="<%= dom_id(@section) %>_add_section">
<%= render partial: "character_sheet_sections/edit_links", locals: { section: @section, parent: @section.parent_section, id: nil} %> <%= render partial: "character_sheet_sections/edit_links", locals: { section: @section, parent: @section.parent_section, id: nil} %>
</div> </div>
<% end %> <% end %>

View File

@ -75,6 +75,8 @@ en:
add_section: Add section add_section: Add section
new: new:
create_section: Create section create_section: Create section
form:
template: From template (leave blank for none)
characters: characters:
index: index:
my_characters: My characters my_characters: My characters

30
todo.md
View File

@ -1,20 +1,36 @@
- Templates Character sheets
- Add from template = Must haves
- Edit/delete templates - Refer to stat in roll (Skill in Troika)
- Lists - Lists
- request invite - Reorganise sections
- Edit additional roll types
- improve dice roll parsing to allow e.g. choose highest - improve dice roll parsing to allow e.g. choose highest
= Nice to haves
- Calculate from result - e.g. troika damage roll should calc. damage amount from die roll:w
- Add min/max when creating stats
- Better sheet layout (multiple small sections per line)
- Display empty text fields prettier
- Rename to SectionTemplate
- Easy add amount to stat - Easy add amount to stat
- icons on features
- auto save text fields - auto save text fields
- indicate save status on titlebar - indicate save status on titlebar
- icons on features
- Cancel adding subsection, stat etc.
Templates
- Template accessibility level
- Users can add their own, "official" ones are always available
- Unofficial ones can be added to a local list of favourites
- Edit/delete templates
- Sheet section color
Misc
- request invite
- Character avatars - Character avatars
- default avatars - default avatars
- add uuid/slug to characters and any other url-visible ids - add uuid/slug to characters and any other url-visible ids
- shared/private notes - shared/private notes
- NPCs - NPCs
- Character sheets prototypes
- notifications
- chat - chat
- maps - maps
- show errors from invalid sheet bits - show errors from invalid sheet bits