Add sections based on templates
This commit is contained in:
parent
79e3e79b12
commit
52022ce65e
|
@ -181,3 +181,7 @@ footer {
|
|||
margin: 0 auto;
|
||||
}
|
||||
}
|
||||
|
||||
.subsections {
|
||||
margin: .5em;
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ class CharacterSheetSectionsController < ApplicationController
|
|||
end
|
||||
|
||||
def new
|
||||
@templates = Template.game_system(@character.game_system)
|
||||
if params[:parent_section_id].present?
|
||||
@parent_section = @character.character_sheet_sections.find_by(id: params[:parent_section_id])
|
||||
end
|
||||
|
@ -22,7 +23,16 @@ class CharacterSheetSectionsController < ApplicationController
|
|||
end
|
||||
|
||||
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
|
||||
unless @section.save
|
||||
@parent_section = @section.parent_section
|
||||
|
|
|
@ -35,7 +35,7 @@ class CharacterSheetFeature < ApplicationRecord
|
|||
def set_order_index
|
||||
return if order_index.present?
|
||||
|
||||
if character_sheet_section.character_sheet_features.any?
|
||||
if character_sheet_section.character_sheet_features.count > 1
|
||||
self.order_index = character_sheet_section.character_sheet_features.order(:order_index).last.order_index + 1
|
||||
else
|
||||
self.order_index = 1
|
||||
|
|
|
@ -18,6 +18,13 @@ class CharacterSheetSection < ApplicationRecord
|
|||
|
||||
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
|
||||
character_sheet_features.minimum(:order_index) || 1
|
||||
end
|
||||
|
|
|
@ -9,4 +9,6 @@ class Template < ApplicationRecord
|
|||
validates :content, presence: true
|
||||
validates :klass, presence: true,
|
||||
length: { maximum: 200 }
|
||||
|
||||
scope :game_system, ->(game_system) { where(game_system:) }
|
||||
end
|
||||
|
|
|
@ -14,4 +14,8 @@
|
|||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<div id="<%= dom_id(character_sheet_section) %>_subsections" class="subsections">
|
||||
<%= render character_sheet_section.character_sheet_subsections %>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -6,6 +6,10 @@
|
|||
<%= f.text_field :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 %>
|
||||
<% end %>
|
||||
</section>
|
||||
|
|
|
@ -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" %>
|
||||
<%= turbo_stream.append(content_target) do %>
|
||||
<%= render @section %>
|
||||
|
|
|
@ -75,6 +75,8 @@ en:
|
|||
add_section: Add section
|
||||
new:
|
||||
create_section: Create section
|
||||
form:
|
||||
template: From template (leave blank for none)
|
||||
characters:
|
||||
index:
|
||||
my_characters: My characters
|
||||
|
|
Loading…
Reference in New Issue