Compare commits

..

No commits in common. "a0271f0b69d98bdc00d760f61cb83f4e5e2304ce" and "810ebcbed3c3a49dded14286a9524d43951b3a51" have entirely different histories.

27 changed files with 208 additions and 58 deletions

View File

@ -50,14 +50,14 @@
} }
} }
.stats { .stats, .counters {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
gap: .2em; gap: .2em;
justify-content: center; justify-content: center;
} }
.stat, .text-field { .stat, .counter, .text-field {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
border-radius: var(--border-radius); border-radius: var(--border-radius);
@ -89,7 +89,6 @@
max-width: 20em; max-width: 20em;
overflow-y: scroll; overflow-y: scroll;
font-size: .8em; font-size: .8em;
align-content: center;
} }
input[type=submit] { input[type=submit] {
width: 100%; width: 100%;
@ -97,9 +96,6 @@
height: auto; height: auto;
padding: auto; padding: auto;
} }
a:link, a:visited {
text-decoration: none;
}
} }
.feature-edit-buttons { .feature-edit-buttons {

View File

@ -41,26 +41,13 @@ form, fieldset {
} }
} }
form.stat-form { form.stat-form, form.counter-form {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
} }
.feature-box trix-toolbar { .feature-box form.stat-form {
display: block;
}
.feature-box form.stat-form, .feature-box form.text-field-form {
display: block; display: block;
margin: 1em auto; margin: 1em auto;
width: fit-content; width: fit-content;
} }
.feature-box form.text-field-form {
width: 100%;
.trix-content, .trix-editor {
width: 100%;
max-width: 100%;
margin-bottom: 1em;
}
}

View File

@ -113,7 +113,7 @@ hr {
z-index: 5000; z-index: 5000;
} }
#modal > div { #modal div {
position: absolute; position: absolute;
top: 50%; top: 50%;
left: 50%; left: 50%;
@ -133,10 +133,6 @@ hr {
text-align: center; text-align: center;
} }
.feature-box trix-editor {
grid-column: 1/3;
}
.nf { .nf {
color: var(--text-color); color: var(--text-color);
} }
@ -147,3 +143,8 @@ hr {
cursor: pointer; cursor: pointer;
box-shadow: 0 0 5px var(--shadow-color); box-shadow: 0 0 5px var(--shadow-color);
} }
.roll-button::before {
content: "YO";
background-color: red;
}

View File

@ -0,0 +1,52 @@
# frozen_string_literal: true
class CountersController < ApplicationController
before_action :set_section, only: [ :new, :create ]
before_action :set_character, only: [ :new, :create ]
before_action :set_counter, only: [ :update, :destroy ]
def new
@counter = @section.counters.new
@counter.build_character_sheet_feature
end
def create
@counter = @section.counters.new(counter_params)
@editable = true
unless @counter.save
render :new, status: :unprocessable_entity
end
end
def update
@counter.update(counter_params)
end
def destroy
@id = helpers.dom_id(@counter)
@counter.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_counter
@counter = Current.user.counters.find(params[:id])
end
def counter_params
params.require(:counter).permit(
:name,
:value,
character_sheet_feature_attributes: [
:id, :featurable_id, :featurable_type, :character_sheet_section_id, :_destroy,
],
)
end
end

View File

@ -3,7 +3,7 @@
class TextFieldsController < ApplicationController class TextFieldsController < ApplicationController
before_action :set_section, only: [ :new, :create ] before_action :set_section, only: [ :new, :create ]
before_action :set_character, only: [ :new, :create ] before_action :set_character, only: [ :new, :create ]
before_action :set_text_field, only: [ :show, :update, :destroy ] before_action :set_text_field, only: [ :update, :destroy ]
def new def new
@text_field = @section.text_fields.new @text_field = @section.text_fields.new
@ -18,9 +18,6 @@ class TextFieldsController < ApplicationController
end end
end end
def show
end
def update def update
@text_field.update(text_field_params) @text_field.update(text_field_params)
end end

View File

@ -8,6 +8,8 @@ class CharacterSheetSection < ApplicationRecord
foreign_key: :parent_section_id, foreign_key: :parent_section_id,
dependent: :destroy dependent: :destroy
has_many :character_sheet_features has_many :character_sheet_features
has_many :counters, dependent: :destroy, through: :character_sheet_features,
source: :featurable, source_type: "Counter"
has_many :stats, dependent: :destroy, through: :character_sheet_features, has_many :stats, dependent: :destroy, through: :character_sheet_features,
source: :featurable, source_type: "Stat" source: :featurable, source_type: "Stat"
has_many :text_fields, dependent: :destroy, through: :character_sheet_features, has_many :text_fields, dependent: :destroy, through: :character_sheet_features,

12
app/models/counter.rb Normal file
View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
class Counter < ApplicationRecord
has_one :character_sheet_feature, as: :featurable, dependent: :destroy
has_one :character_sheet_section, through: :character_sheet_feature
accepts_nested_attributes_for :character_sheet_feature, allow_destroy: true
validates :name, presence: true,
length: { maximum: 100 }
validates :value, presence: true,
numericality: true
end

View File

@ -3,7 +3,6 @@
class TextField < ApplicationRecord class TextField < ApplicationRecord
has_one :character_sheet_feature, as: :featurable, dependent: :destroy has_one :character_sheet_feature, as: :featurable, dependent: :destroy
has_one :character_sheet_section, through: :character_sheet_feature has_one :character_sheet_section, through: :character_sheet_feature
has_one :character, through: :character_sheet_section
accepts_nested_attributes_for :character_sheet_feature, allow_destroy: true accepts_nested_attributes_for :character_sheet_feature, allow_destroy: true
has_rich_text :content has_rich_text :content

View File

@ -10,6 +10,7 @@ class User < ApplicationRecord
has_many :character_sheet_features, through: :character_sheet_sections has_many :character_sheet_features, through: :character_sheet_sections
has_many :owned_tables, foreign_key: :owner_id, class_name: "Table" has_many :owned_tables, foreign_key: :owner_id, class_name: "Table"
has_many :players, dependent: :destroy has_many :players, dependent: :destroy
has_many :counters, through: :character_sheet_sections
has_many :stats, through: :character_sheet_sections has_many :stats, through: :character_sheet_sections
has_many :tables, through: :players has_many :tables, through: :players
has_many :text_fields, through: :character_sheet_sections has_many :text_fields, through: :character_sheet_sections

View File

@ -5,6 +5,8 @@
<% if section.present? %> <% if section.present? %>
<%= link_to t(".add_stat"), <%= link_to t(".add_stat"),
new_character_sheet_section_stat_path(section), data: { turbo_stream: true }, class: "add-stat" %> 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"), <%= link_to t(".add_text_field"),
new_character_sheet_section_text_field_path(section), data: { turbo_stream: true }, class: "add-text-field" %> new_character_sheet_section_text_field_path(section), data: { turbo_stream: true }, class: "add-text-field" %>
<% end %> <% end %>

View File

@ -0,0 +1,17 @@
<div class="counter" id="<%= dom_id(counter) %>">
<% if @editable %>
<div class="feature-edit-buttons">
<%= icon_link_to("cod-triangle_left", reorder_down_character_sheet_feature_path(counter),
data: { turbo_method: :patch }) %>
<%= icon_link_to("md-delete", counter,
data: { turbo_method: :delete, turbo_confirm: t(".confirm_delete", name: counter.name) }) %>
<%= icon_link_to("cod-triangle_right", reorder_up_character_sheet_feature_path(counter),
data: { turbo_method: :patch }) %>
</div>
<% end %>
<h6><%= counter.name %></h6>
<%= form_with model: counter, class: "counter-form",
data: { controller: "auto-update", auto_update_update_url_value: counter_path(counter) } do |f| %>
<%= f.number_field :value %>
<% end %>
</div>

View File

@ -0,0 +1,16 @@
<section class="inset">
<%= form_with model: @counter, url: character_sheet_section_counters_path(@section) do |f| %>
<% if @counter.new_record? %>
<%= f.fields_for :character_sheet_feature do |ff| %>
<%= ff.hidden_field :featurable_id, value: @counter.id %>
<%= ff.hidden_field :character_sheet_section_id, value: @section.id %>
<% end %>
<% end %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= display_form_errors(@section, :name) %>
<%= f.submit button_text %>
<% end %>
</section>

View File

@ -0,0 +1,11 @@
<% content_target = "#{dom_id(@section)}_features" %>
<%= turbo_stream.append(content_target) do %>
<%= render @counter %>
<% end %>
<% form_target = "#{dom_id(@section)}_add_section" %>
<%= turbo_stream.replace(form_target) do %>
<div id="<%= dom_id(@section) %>_add_section">
<%= render partial: "character_sheet_sections/edit_links", locals: { section: @section, parent: @section.parent_section, id: nil} %>
</div>
<% end %>

View File

@ -0,0 +1 @@
<%= turbo_stream.remove @id %>

View File

@ -0,0 +1,6 @@
<% target = "#{dom_id(@section)}_add_section" %>
<%= turbo_stream.replace(target) do %>
<div id="<%= target %>">
<%= render partial: "form", locals: { button_text: t(".create_counter") } %>
</div>
<% end %>

View File

@ -0,0 +1,3 @@
<%= turbo_stream.replace dom_id(@counter) do %>
<%= render @counter %>
<% end %>

View File

@ -17,9 +17,7 @@
<%= f.submit t(".save") %> <%= f.submit t(".save") %>
<% end %> <% end %>
<% else %> <% else %>
<%= link_to text_field, data: { turbo_frame: :modal } do %>
<%= text_field.content %> <%= text_field.content %>
<% end %> <% end %>
<% end %>
</div> </div>

View File

@ -1,13 +0,0 @@
<%= turbo_frame_tag :modal do %>
<div class="feature-box text-field">
<%= icon_link_to "fa-close",
table_character_character_sheet_sections_path(@text_field.character.table, @text_field.character),
class: "icon-link" %>
<h2><%= @text_field.name %></h2>
<%= form_with model: @text_field, class: "text-field-form",
data: { controller: "dice-roll modal-closer", action: "modal-closer#closeModal" } do |f| %>
<%= f.rich_text_area :content %>
<%= f.submit t(".save") %>
<% end %>
</div>
<% end %>

View File

@ -64,6 +64,7 @@ en:
delete_section: Delete %{name} delete_section: Delete %{name}
confirm_delete: Are you sure you want to delete this section? confirm_delete: Are you sure you want to delete this section?
add_stat: Add stat add_stat: Add stat
add_counter: Add counter
add_text_field: Add text field add_text_field: Add text field
index: index:
edit: Edit character sheet edit: Edit character sheet
@ -101,6 +102,12 @@ en:
destroy: destroy:
success: Deleted “%{name}” success: Deleted “%{name}”
error: Could not delete your character error: Could not delete your character
counters:
counter:
delete: Delete counter
confirm_delete: Are you sure you want to delete %{name}?
new:
create_counter: Create counter
password_resets: password_resets:
new: new:
reset_password: Reset your password reset_password: Reset your password
@ -217,8 +224,6 @@ en:
delete: Delete text field delete: Delete text field
confirm_delete: Are you sure you want to delete %{name}? confirm_delete: Are you sure you want to delete %{name}?
save: Save save: Save
show:
save: Save
new: new:
create_text_field: Create text field create_text_field: Create text field
users: users:

View File

@ -22,12 +22,14 @@ Rails.application.routes.draw do
end end
end end
resources :character_sheet_sections, only: [ :destroy ] do resources :character_sheet_sections, only: [ :destroy ] do
resources :counters, only: [ :new, :create ]
resources :stats, only: [ :new, :create ] resources :stats, only: [ :new, :create ]
resources :text_fields, only: [ :new, :create ] resources :text_fields, only: [ :new, :create ]
end end
resources :characters do resources :characters do
resources :character_sheet_sections, only: [ :index, :new, :create ] resources :character_sheet_sections, only: [ :index, :new, :create ]
end end
resources :counters, only: [ :update, :destroy ]
resources :stats, only: [ :show, :update, :destroy ] resources :stats, only: [ :show, :update, :destroy ]
resources :table_invites, only: [ :index, :edit, :update ] resources :table_invites, only: [ :index, :edit, :update ]
resources :tables do resources :tables do
@ -38,7 +40,7 @@ Rails.application.routes.draw do
resources :events, only: [ :index ] resources :events, only: [ :index ]
resources :table_invites, only: [ :new, :create ] resources :table_invites, only: [ :new, :create ]
end end
resources :text_fields, only: [ :show, :update, :destroy ] resources :text_fields, only: [ :update, :destroy ]
resources :admin, only: [ :index ] resources :admin, only: [ :index ]
namespace :admin do namespace :admin do

View File

@ -1,7 +0,0 @@
# frozen_string_literal: true
class DropCounters < ActiveRecord::Migration[7.1]
def change
drop_table :counters
end
end

10
db/schema.rb generated
View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_06_21_091420) do ActiveRecord::Schema[7.1].define(version: 2024_06_19_190424) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -88,6 +88,14 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_21_091420) do
t.check_constraint "length(name::text) <= 200", name: "chk_character_name_max_length" t.check_constraint "length(name::text) <= 200", name: "chk_character_name_max_length"
end end
create_table "counters", force: :cascade do |t|
t.string "name", null: false
t.integer "value", default: 0, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.check_constraint "length(name::text) <= 100", name: "chk_counter_name_max_length"
end
create_table "dice_rolls", force: :cascade do |t| create_table "dice_rolls", force: :cascade do |t|
t.string "rollable_type" t.string "rollable_type"
t.bigint "rollable_id" t.bigint "rollable_id"

View File

@ -0,0 +1,28 @@
# frozen_string_literal: true
require "test_helper"
class CountersControllerTest < ActionDispatch::IntegrationTest
test "should render new turbo stream" do
sign_in users(:trevor)
get new_character_sheet_section_counter_url(character_sheet_sections(:counters)), as: :turbo_stream
assert_response :success
end
test "should create counter" do
sign_in users(:trevor)
assert_difference "Counter.count", 1 do
post character_sheet_section_counters_url(character_sheet_sections(:counters)),
params: { counter: { name: "Ammo", character_sheet_section_id: character_sheet_sections(:counters).id } },
as: :turbo_stream
end
end
test "should delete counter" do
sign_in users(:trevor)
assert_difference "Counter.count", -1 do
delete counter_url(Counter.first), as: :turbo_stream
assert_response :success
end
end
end

View File

@ -7,6 +7,12 @@ stats-strength:
featurable: strength (Stat) featurable: strength (Stat)
order_index: 1 order_index: 1
counter:
<<: *DEFAULTS
character_sheet_section: counters
featurable: hp (Counter)
order_index: 2
text_field: text_field:
<<: *DEFAULTS <<: *DEFAULTS
character_sheet_section: info character_sheet_section: info

View File

@ -2,6 +2,10 @@ stats:
name: Stats name: Stats
character: nardren character: nardren
counters:
name: Status
character: nardren
subsection: subsection:
name: Subsection name: Subsection
character: nardren character: nardren

3
test/fixtures/counters.yml vendored Normal file
View File

@ -0,0 +1,3 @@
hp:
name: HP
value: 10

View File

@ -0,0 +1,13 @@
# frozen_string_literal: true
require "test_helper"
class CounterTest < ActiveSupport::TestCase
test "name must exist" do
assert_must_exist(counters(:hp), :name)
end
test "value must exist" do
assert_must_exist(counters(:hp), :value)
end
end