Compare commits

...

3 Commits

Author SHA1 Message Date
Trevor Vallender 9fe186a08d Styling and information improvements 2024-06-05 18:49:34 +01:00
Trevor Vallender 9684dd39dd Add CRUD actions for characters 2024-06-05 18:46:27 +01:00
Trevor Vallender 2aed4cdfc2 Add Character model 2024-06-05 16:00:02 +01:00
27 changed files with 400 additions and 3 deletions

View File

@ -0,0 +1,31 @@
.character {
background-color: var(--inset-bg-color);
border-radius: var(--border-radius);
padding: 1em;
h5 {
font-size: 1.1em;
margin: 0;
}
h5 a:link, h5 a:visited {
color: inherit;
text-decoration: none;
}
> ul {
padding: 0;
li {
list-style-type: none;
display: inline;
color: var(--secondary-text-color);
font-size: .8em;
&:after {
content: " • ";
}
&:last-child:after {
content: "";
}
}
}
}

View File

@ -5,6 +5,8 @@
--header-text-color: #fff; --header-text-color: #fff;
--header-height: 200px; --header-height: 200px;
--secondary-text-color: #777;
--inset-bg-color: #eee; --inset-bg-color: #eee;
--border-radius: .5em; --border-radius: .5em;

View File

@ -0,0 +1,62 @@
# frozen_string_literal: true
class CharactersController < ApplicationController
before_action :set_character, only: [ :show, :edit, :update, :destroy ]
def index
@characters = Current.user.characters
end
def new
@character = Current.user.characters.new
end
def create
@character = Current.user.characters.new(character_params)
if @character.save
redirect_to @character, notice: t(".success")
else
flash.now[:alert] = t(".error")
render :new, status: :unprocessable_entity
end
end
def show
end
def edit
end
def update
if @character.update(character_params)
redirect_to @character, notice: t(".success", name: @character.name)
else
flash.now[:alert] = t(".error")
render :edit, status: :unprocessable_entity
end
end
def destroy
name = @character.name
if @character.destroy
redirect_to characters_path, notice: t(".success", name:)
else
flash[:alert] = t(".error")
redirect_to characters_path
end
end
private
def set_character
@character = Current.user.characters.find(params[:id])
end
def character_params
params.require(:character).permit(
:name,
:game_system_id,
:table_id,
)
end
end

10
app/models/character.rb Normal file
View File

@ -0,0 +1,10 @@
# frozen_string_literal: true
class Character < ApplicationRecord
belongs_to :table, optional: true
belongs_to :game_system
belongs_to :user
validates :name, presence: true,
length: { maximum: 200 }
end

View File

@ -1,6 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
class GameSystem < ApplicationRecord class GameSystem < ApplicationRecord
has_many :characters, dependent: :restrict_with_error
has_many :tables, dependent: :restrict_with_error
validates :name, presence: true, validates :name, presence: true,
uniqueness: true, uniqueness: true,
length: { maximum: 100 } length: { maximum: 100 }

View File

@ -3,6 +3,7 @@
class Table < ApplicationRecord class Table < ApplicationRecord
belongs_to :owner, class_name: "User" belongs_to :owner, class_name: "User"
belongs_to :game_system belongs_to :game_system
has_many :characters, dependent: :nullify
has_many :players, dependent: :destroy has_many :players, dependent: :destroy
has_many :table_invites, dependent: :destroy has_many :table_invites, dependent: :destroy
has_many :users, through: :players has_many :users, through: :players

View File

@ -5,6 +5,7 @@ class User < ApplicationRecord
deletable_attachments :avatar deletable_attachments :avatar
has_and_belongs_to_many :site_roles has_and_belongs_to_many :site_roles
has_many :characters, dependent: :destroy
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 :tables, through: :players has_many :tables, through: :players

View File

@ -0,0 +1,7 @@
<div id=<%= dom_id(character) %> class="character">
<h5><%= link_to character.name, character %></h5>
<ul>
<li><%= character.game_system.name %></li>
<li><%= character.user.username %></li>
</ul>
</div>

View File

@ -0,0 +1,17 @@
<%# locals: (button_text:) -%>
<%= form_with model: @character do |f| %>
<%= f.label :name %>
<%= f.text_field :name%>
<%= display_form_errors(@character, :name) %>
<%= f.label :game_system_id %>
<%= f.collection_select :game_system_id, GameSystem.all, :id, :name, required: true, include_blank: " " %>
<%= display_form_errors(@character, :game_system) %>
<%= f.label :table_id %>
<%= f.collection_select :table_id, Current.user.tables, :id, :name, required: true, include_blank: " " %>
<%= display_form_errors(@character, :table) %>
<%= f.submit button_text %>
<% end %>

View File

@ -0,0 +1,7 @@
<% content_for :title, t(".edit_character", name: @character.name) %>
<h2><%= t(".edit_character", name: @character.name) %></h2>
<%= link_to t(".delete_character", name: @character.name), character_path(@character),
data: { turbo_method: :delete, turbo_confirm: t(".delete_character_confirmation", name: @character.name) } %>
<%= render partial: "form", locals: { button_text: t(".update") } %>

View File

@ -0,0 +1,11 @@
<% content_for :title, t(".my_characters") %>
<h2><%= t(".my_characters") %></h2>
<%= link_to t(".add_character"), new_character_path %>
<% if @characters.any? %>
<%= render @characters %>
<% else %>
<p><%= t(".no_characters") %></p>
<% end %>

View File

@ -0,0 +1,5 @@
<% content_for :title, t(".new_character") %>
<h2><%= t(".new_character") %></h2>
<%= render partial: "form", locals: { button_text: t(".create") } %>

View File

@ -0,0 +1,16 @@
<% content_for :title, @character.name %>
<h2><%= @character.name %></h2>
<%= link_to t(".edit"), edit_character_path(@character) %>
<dl>
<dt><%= t(".owner") %></dt>
<dd><%= link_to @character.user.username, @character.user %></dd>
<dt><%= t(".game_system") %></dt>
<dd><%= @character.game_system.name %></dd>
<% if @character.table && @character.table.users.include?(Current.user) %>
<dt><%= t(".table") %></dt>
<dd><%= link_to @character.table.name, @character.table %></dd>
<% end %>
</dl>

View File

@ -18,6 +18,7 @@
<nav> <nav>
<ul> <ul>
<% if logged_in? %> <% if logged_in? %>
<li><%= link_to t(".characters"), characters_path %></li>
<li><%= link_to t(".tables"), tables_path %></li> <li><%= link_to t(".tables"), tables_path %></li>
<li><%= link_to t(".profile"), user_path(Current.user) %></li> <li><%= link_to t(".profile"), user_path(Current.user) %></li>
<li><%= link_to t("log_out"), logout_path, data: {turbo_method: :delete} %></li> <li><%= link_to t("log_out"), logout_path, data: {turbo_method: :delete} %></li>

View File

@ -12,6 +12,24 @@
<dd><%= @table.owner.username %></dd> <dd><%= @table.owner.username %></dd>
</dl> </dl>
<h4><%= t(".characters") %></h4>
<% if @table.characters.any? %>
<ul><%= render @table.characters %></ul>
<% else %>
<p><%= t(".no_characters") %></p>
<% end %>
<h4><%= t(".users") %></h4>
<% if @table.users.any? %>
<ul>
<% @table.users.each do |user| %>
<li><%= link_to user.username, user %></li>
<% end %>
</ul>
<% else %>
<p><%= t(".no_users") %></p>
<% end %>
<% if @table_invites.any? %> <% if @table_invites.any? %>
<h4>Pending invites</h4> <h4>Pending invites</h4>
<ul> <ul>

40
config/brakeman.ignore Normal file
View File

@ -0,0 +1,40 @@
{
"ignored_warnings": [
{
"warning_type": "Dynamic Render Path",
"warning_code": 15,
"fingerprint": "42478896b621fa561e9866e0bc1339d9864fea72109e83c02b194967272202cc",
"check_name": "Render",
"message": "Render path contains parameter value",
"file": "app/views/tables/show.html.erb",
"line": 17,
"link": "https://brakemanscanner.org/docs/warning_types/dynamic_render_path/",
"code": "render(action => Current.user.tables.find(params[:id]).characters, {})",
"render_path": [
{
"type": "controller",
"class": "TablesController",
"method": "show",
"line": 12,
"file": "app/controllers/tables_controller.rb",
"rendered": {
"name": "tables/show",
"file": "app/views/tables/show.html.erb"
}
}
],
"location": {
"type": "template",
"template": "tables/show"
},
"user_input": "params[:id]",
"confidence": "Weak",
"cwe_id": [
22
],
"note": ""
}
],
"updated": "2024-06-05 18:49:27 +0100",
"brakeman_version": "6.1.2"
}

View File

@ -15,6 +15,7 @@ en:
game_systems: Game Systems game_systems: Game Systems
jobs: Jobs jobs: Jobs
application: application:
characters: Characters
profile: Profile profile: Profile
tables: Tables tables: Tables
mailer: mailer:
@ -54,6 +55,33 @@ en:
destroy: destroy:
success: Successfully deleted “%{name}”. success: Successfully deleted “%{name}”.
error: “%{name}” could not be deleted. error: “%{name}” could not be deleted.
characters:
index:
my_characters: My characters
add_character: Create new character
no_characters: You do not have any characters yet
new:
new_character: Create a character
create: Create character
create:
success: “%{name}” has been created.
error: Your character could not be created.
show:
edit: Edit character
owner: Player
game_system: Game system
table: Table
edit:
edit_character: Edit %{name}
update: Update character
delete_character: Delete “%{name}”
delete_character_confirmation: Are you sure you want to delete “%{name}”?
update:
success: Updated “%{name}”
error: Could not update your character
destroy:
success: Deleted “%{name}”
error: Could not delete your character
password_resets: password_resets:
new: new:
reset_password: Reset your password reset_password: Reset your password
@ -79,7 +107,6 @@ en:
update: update:
success: Your password has been updated success: Your password has been updated
error: Failed to update password error: Failed to update password
sessions: sessions:
create: create:
success: "Hello, %{name}!" success: "Hello, %{name}!"
@ -134,6 +161,10 @@ en:
game_system: Game system game_system: Game system
owner: Owner owner: Owner
invite_user: Invite a new player invite_user: Invite a new player
no_characters: This table doesnt have any characters yet
no_users: This table doesnt have any players yet
users: Players
characters: Characters
new: new:
new_table: New table new_table: New table
create_table: Create table create_table: Create table

View File

@ -15,6 +15,7 @@ Rails.application.routes.draw do
resources :password_resets, only: [ :new, :create, :edit, :update ] resources :password_resets, only: [ :new, :create, :edit, :update ]
resources :sessions, only: [ :new, :create, :destroy ] resources :sessions, only: [ :new, :create, :destroy ]
resources :characters
resources :table_invites, only: [ :index, :edit, :update ] resources :table_invites, only: [ :index, :edit, :update ]
resources :tables do resources :tables do
resources :table_invites, only: [ :new, :create ] resources :table_invites, only: [ :new, :create ]

View File

@ -0,0 +1,16 @@
# frozen_string_literal: true
class CreateCharacters < ActiveRecord::Migration[7.1]
def change
create_table :characters do |t|
t.string :name, null: false
t.belongs_to :table, null: true, foreign_key: true
t.belongs_to :game_system, null: false, foreign_key: true
t.belongs_to :user, null: false, foreign_key: true
t.timestamps
end
add_check_constraint :characters, "length(name) <= 200", name: "chk_character_name_max_length"
end
end

18
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_05_132327) do ActiveRecord::Schema[7.1].define(version: 2024_06_05_143732) 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"
@ -52,6 +52,19 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_05_132327) do
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end end
create_table "characters", force: :cascade do |t|
t.string "name", null: false
t.bigint "table_id"
t.bigint "game_system_id", null: false
t.bigint "user_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["game_system_id"], name: "index_characters_on_game_system_id"
t.index ["table_id"], name: "index_characters_on_table_id"
t.index ["user_id"], name: "index_characters_on_user_id"
t.check_constraint "length(name::text) <= 200", name: "chk_character_name_max_length"
end
create_table "game_systems", force: :cascade do |t| create_table "game_systems", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
@ -232,6 +245,9 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_05_132327) do
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "characters", "game_systems"
add_foreign_key "characters", "tables"
add_foreign_key "characters", "users"
add_foreign_key "players", "tables" add_foreign_key "players", "tables"
add_foreign_key "players", "users" add_foreign_key "players", "users"
add_foreign_key "solid_queue_blocked_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade add_foreign_key "solid_queue_blocked_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade

View File

@ -0,0 +1,55 @@
# frozen_string_literal: true
require "test_helper"
class CharactersControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
sign_in users(:trevor)
get characters_url
assert_response :success
end
test "should get show" do
sign_in users(:trevor)
get character_url(characters(:nardren))
assert_response :success
end
test "should get new" do
sign_in users(:trevor)
get new_character_url
assert_response :success
end
test "should create character" do
sign_in users(:trevor)
assert_difference("Character.count") do
post characters_url, params: { character: {
name: "Acornia", game_system_id: game_systems(:dnd).id, table_id: tables(:dnd_table).id,
} }
end
assert_redirected_to character_url(Character.last)
end
test "should get edit" do
sign_in users(:trevor)
get edit_character_url(characters(:nardren))
assert_response :success
end
test "should update character" do
sign_in users(:trevor)
patch character_url(characters(:nardren)), params: { character: {
name: "Acornia",
} }
assert_redirected_to character_url(characters(:nardren))
end
test "should destroy character" do
sign_in users(:trevor)
assert_difference("Character.count", -1) do
delete character_url(characters(:nardren))
end
assert_redirected_to characters_url
end
end

5
test/fixtures/characters.yml vendored Normal file
View File

@ -0,0 +1,5 @@
nardren:
name: Nardren Rockseeker
table: dnd_table
game_system: dnd
user: trevor

View File

@ -3,3 +3,6 @@ troika:
dnd: dnd:
name: Dungeons & Dragons name: Dungeons & Dragons
dragonbane:
name: Dragonbane

View File

@ -16,3 +16,8 @@ gimlis_table:
<<: *DEFAULTS <<: *DEFAULTS
name: Gimli's Table name: Gimli's Table
owner: gimli owner: gimli
dnd_table:
<<: *DEFAULTS
name: The Dungeon Master's Table
game_system: dnd

View File

@ -59,7 +59,7 @@ class AdminGameSystemsIntegrationTest < ActionDispatch::IntegrationTest
test "destroy should destroy game system" do test "destroy should destroy game system" do
sign_in users(:admin) sign_in users(:admin)
assert_difference "GameSystem.count", -1 do assert_difference "GameSystem.count", -1 do
delete admin_game_system_path(game_systems(:dnd)) delete admin_game_system_path(game_systems(:dragonbane))
end end
assert_redirected_to admin_game_systems_path assert_redirected_to admin_game_systems_path
end end

View File

@ -0,0 +1,9 @@
# frozen_string_literal: true
require "test_helper"
class CharacterTest < ActiveSupport::TestCase
test "name must exist" do
assert_must_exist(characters(:nardren), :name)
end
end

View File

@ -6,4 +6,28 @@ class GameSystemTest < ActiveSupport::TestCase
test "name must exist" do test "name must exist" do
assert_must_exist(game_systems(:troika), :name) assert_must_exist(game_systems(:troika), :name)
end end
test "cannot delete games with characters" do
game = game_systems(:dnd)
game.tables.destroy_all
assert_no_difference("GameSystem.count") do
game_systems(:dnd).destroy
end
game.characters.destroy_all
assert_difference("GameSystem.count", -1) do
game_systems(:dnd).destroy
end
end
test "cannot delete games with tables" do
game = game_systems(:dnd)
game.characters.destroy_all
assert_no_difference("GameSystem.count") do
game_systems(:dnd).destroy
end
game.tables.destroy_all
assert_difference("GameSystem.count", -1) do
game_systems(:dnd).destroy
end
end
end end