diff --git a/app/controllers/counters_controller.rb b/app/controllers/counters_controller.rb index 2f8cb73..239e9c8 100644 --- a/app/controllers/counters_controller.rb +++ b/app/controllers/counters_controller.rb @@ -11,6 +11,7 @@ class CountersController < ApplicationController def create @counter = @section.counters.new(counter_params) + @editable = true unless @counter.save render :new, status: :unprocessable_entity end diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index e8fb0ff..f08d448 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -11,6 +11,7 @@ class StatsController < ApplicationController def create @stat = @section.stats.new(stat_params) + @editable = true unless @stat.save render :new, status: :unprocessable_entity end diff --git a/app/controllers/text_fields_controller.rb b/app/controllers/text_fields_controller.rb new file mode 100644 index 0000000..d56d024 --- /dev/null +++ b/app/controllers/text_fields_controller.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +class TextFieldsController < ApplicationController + before_action :set_section, only: [ :new, :create ] + before_action :set_character, only: [ :new, :create ] + before_action :set_text_field, only: [ :update, :destroy ] + + def new + @text_field = @section.text_fields.new + end + + def create + @text_field = @section.text_fields.new(text_field_params) + @editable = true + unless @text_field.save + render :new, status: :unprocessable_entity + end + end + + def update + @text_field.update(text_field_params) + end + + def destroy + @id = helpers.dom_id(@text_field) + @text_field.destroy + end + + private + def set_character + @character = @section.character + end + + def set_section + @section = Current.user.character_sheet_sections.find(params[:character_sheet_section_id]) + end + + def set_text_field + @text_field = Current.user.text_fields.find(params[:id]) + end + + def text_field_params + params.require(:text_field).permit( + :name, + :content, + :character_sheet_section_id, + ) + end +end diff --git a/app/models/user.rb b/app/models/user.rb index fec6849..bd8766f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -12,6 +12,7 @@ class User < ApplicationRecord has_many :counters, through: :character_sheet_sections has_many :stats, through: :character_sheet_sections has_many :tables, through: :players + has_many :text_fields, through: :character_sheet_sections has_rich_text :profile has_one_attached :avatar do |attachable| attachable.variant :standard, resize_to_limit: [ 100, 100 ], preprocessed: true diff --git a/app/views/character_sheet_sections/_character_sheet_section.html.erb b/app/views/character_sheet_sections/_character_sheet_section.html.erb index eb6e28c..7164b4a 100644 --- a/app/views/character_sheet_sections/_character_sheet_section.html.erb +++ b/app/views/character_sheet_sections/_character_sheet_section.html.erb @@ -10,6 +10,9 @@
<%= render character_sheet_section.counters %>
+
+ <%= render character_sheet_section.text_fields %> +
<%= render character_sheet_section.character_sheet_subsections %>
diff --git a/app/views/character_sheet_sections/_edit_links.html.erb b/app/views/character_sheet_sections/_edit_links.html.erb index 1d3b43a..df661e4 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_counter"), new_character_sheet_section_counter_path(section), data: { turbo_stream: true }, class: "add-counter" %> + <%= link_to t(".add_text_field"), + new_character_sheet_section_text_field_path(section), data: { turbo_stream: true }, class: "add-text-field" %> <% 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/counters/create.turbo_stream.erb b/app/views/counters/create.turbo_stream.erb index 68cd0ea..aa41d2e 100644 --- a/app/views/counters/create.turbo_stream.erb +++ b/app/views/counters/create.turbo_stream.erb @@ -1,11 +1,11 @@ -<%= content_target = "#{dom_id(@section)}_counters" %> +<% content_target = "#{dom_id(@section)}_counters" %> <%= turbo_stream.append(content_target) do %> <%= render @counter %> <% end %> -<%= form_target = "#{dom_id(@section)}_add_section" %> +<% form_target = "#{dom_id(@section)}_add_section" %> <%= turbo_stream.replace(form_target) do %> -
+
<%= render partial: "character_sheet_sections/edit_links", locals: { section: @section, parent: @section.parent_section, id: nil} %>
<% end %> diff --git a/app/views/stats/create.turbo_stream.erb b/app/views/stats/create.turbo_stream.erb index f9d4f97..09ba523 100644 --- a/app/views/stats/create.turbo_stream.erb +++ b/app/views/stats/create.turbo_stream.erb @@ -5,7 +5,7 @@ <%= form_target = "#{dom_id(@section)}_add_section" %> <%= turbo_stream.replace(form_target) do %> -
+
<%= render partial: "character_sheet_sections/edit_links", locals: { section: @section, parent: @section.parent_section, id: nil} %>
<% end %> diff --git a/app/views/text_fields/_form.html.erb b/app/views/text_fields/_form.html.erb new file mode 100644 index 0000000..13f0bd4 --- /dev/null +++ b/app/views/text_fields/_form.html.erb @@ -0,0 +1,11 @@ +
+ <%= form_with model: @text_field, url: character_sheet_section_text_fields_path(@section) do |f| %> + <%= f.hidden_field :character_sheet_section_id, value: @section.id %> + + <%= f.label :name %> + <%= f.text_field :name %> + <%= display_form_errors(@section, :name) %> + + <%= f.submit button_text %> + <% end %> +
diff --git a/app/views/text_fields/_text_field.html.erb b/app/views/text_fields/_text_field.html.erb new file mode 100644 index 0000000..07b8f7b --- /dev/null +++ b/app/views/text_fields/_text_field.html.erb @@ -0,0 +1,17 @@ +
+ <% if @editable %> + <%= link_to t(".delete"), text_field, + data: { turbo_method: :delete, turbo_confirm: t(".confirm_delete", name: text_field.name) } %> + <% end %> + +
<%= text_field.name %>
+ <% if @editable %> + <%= form_with model: text_field, class: "text-field-form" do |f| %> + <%= f.rich_text_area :content %> + <%= f.submit t(".save") %> + <% end %> + <% else %> +

<%= text_field.content %>

+ <% end %> +
+ diff --git a/app/views/text_fields/create.turbo_stream.erb b/app/views/text_fields/create.turbo_stream.erb new file mode 100644 index 0000000..280f6e3 --- /dev/null +++ b/app/views/text_fields/create.turbo_stream.erb @@ -0,0 +1,11 @@ +<% content_target = "#{dom_id(@section)}_text_fields" %> +<%= turbo_stream.append(content_target) do %> + <%= render @text_field %> +<% end %> + +<% form_target = "#{dom_id(@section)}_add_section" %> +<%= turbo_stream.replace(form_target) do %> +
+ <%= render partial: "character_sheet_sections/edit_links", locals: { section: @section, parent: @section.parent_section, id: nil} %> +
+<% end %> diff --git a/app/views/text_fields/destroy.turbo_stream.erb b/app/views/text_fields/destroy.turbo_stream.erb new file mode 100644 index 0000000..f50ff4a --- /dev/null +++ b/app/views/text_fields/destroy.turbo_stream.erb @@ -0,0 +1 @@ +<%= turbo_stream.remove @id %> diff --git a/app/views/text_fields/new.turbo_stream.erb b/app/views/text_fields/new.turbo_stream.erb new file mode 100644 index 0000000..26b1b29 --- /dev/null +++ b/app/views/text_fields/new.turbo_stream.erb @@ -0,0 +1,6 @@ +<% target = "#{dom_id(@text_field.character_sheet_section)}_add_section" %> +<%= turbo_stream.replace(target) do %> +
+ <%= render partial: "form", locals: { button_text: t(".create_text_field") } %> +
+<% end %> diff --git a/app/views/text_fields/update.turbo_stream.erb b/app/views/text_fields/update.turbo_stream.erb new file mode 100644 index 0000000..aa37d0a --- /dev/null +++ b/app/views/text_fields/update.turbo_stream.erb @@ -0,0 +1,3 @@ +<%= turbo_stream.replace dom_id(@text_field) do %> + <%= render @text_field %> +<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index ee6c55b..af97242 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -65,6 +65,7 @@ en: confirm_delete: Are you sure you want to delete this section? add_stat: Add stat add_counter: Add counter + add_text_field: Add text field index: edit: Edit character sheet stop_editing: Stop editing @@ -213,6 +214,13 @@ en: destroy: success: Deleted table “%{name}”. error: Failed to delete table. + text_fields: + text_field: + delete: Delete text field + confirm_delete: Are you sure you want to delete %{name}? + save: Save + new: + create_text_field: Create text field users: validations: email_format: "must be a valid email address" diff --git a/config/routes.rb b/config/routes.rb index cbda471..9de6188 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,6 +18,7 @@ Rails.application.routes.draw do resources :character_sheet_sections, only: [ :destroy ] do resources :counters, only: [ :new, :create ] resources :stats, only: [ :new, :create ] + resources :text_fields, only: [ :new, :create ] end resources :characters do resources :character_sheet_sections, only: [ :index, :new, :create ] @@ -33,6 +34,7 @@ Rails.application.routes.draw do resources :events, only: [ :index ] resources :table_invites, only: [ :new, :create ] end + resources :text_fields, only: [ :update, :destroy ] resources :admin, only: [ :index ] namespace :admin do diff --git a/todo.md b/todo.md index 913dd2d..da68a07 100644 --- a/todo.md +++ b/todo.md @@ -1,9 +1,12 @@ +- don't roll when no roll set +- set orders on sheets +- easily edit text fields without entering edit mode +- add roll command to counters (e.g. luck) - test for rolls controller & system tests -- table notifications for rolls -- roll on click +- Show individual dice on dice roll +- customise trix bar - Sheets - Lists - - Text fields - Weapons/spells - Character avatars - default avatars