CRUD actions for tables

This commit is contained in:
Trevor Vallender 2024-05-28 20:16:43 +01:00
parent 7fadc2d90b
commit 3ce9081996
13 changed files with 207 additions and 2 deletions

View File

@ -6,6 +6,7 @@ body {
font-size: 1.1em;
margin: 0 auto;
max-width: 80em;
padding: 1em;
}
main {

View File

@ -1,6 +1,61 @@
# frozen_string_literal: true
class TablesController < ApplicationController
before_action :set_table, only: [ :show, :edit, :update, :destroy ]
def index
@tables = Current.user.tables
end
def show
end
def new
@table = Table.new
end
def create
@table = Current.user.tables.new(table_params)
@table.slug = helpers.generate_slug_for(model: Table, string: @table.name)
if @table.save
redirect_to @table, notice: t(".success", name: @table.name)
else
flash.now[:alert] = t(".error")
render :new, status: :unprocessable_entity
end
end
def edit
end
def update
if @table.update(table_params)
redirect_to @table, notice: t(".success", name: @table.name)
else
flash.now[:alert] = t(".error")
render :edit, status: :unprocessable_entity
end
end
def destroy
if @table.destroy
redirect_to tables_path, notice: t(".success", name: @table.name)
else
flash[:alert] = t(".error", name: @table.name)
redirect_to tables_path
end
end
private
def set_table
@table = Current.user.tables.find(params[:id])
end
def table_params
params.require(:table).permit(
:name,
:game_system_id,
)
end
end

View File

@ -1,4 +1,8 @@
# frozen_string_literal: true
module ApplicationHelper
def generate_slug_for(model:, string:)
# TODO: Need to ensure this is unique for that model
string.parameterize
end
end

View File

@ -2,6 +2,7 @@
class User < ApplicationRecord
has_and_belongs_to_many :site_roles
has_many :tables, foreign_key: :owner_id
has_secure_password
generates_token_for :password_reset, expires_in: 4.hours do

View File

@ -0,0 +1,11 @@
<%# locals: (table:, button_text:) -%>
<%= form_with model: @table do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :game_system_id %>
<%= f.collection_select :game_system_id, GameSystem.all, :id, :name %>
<%= f.submit button_text %>
<% end %>

View File

@ -0,0 +1,5 @@
<%# locals: (table:) -%>
<div id="<%= dom_id(table) %>" class="table">
<h4><%= link_to table.name, table %></h4>
</div>

View File

@ -0,0 +1,8 @@
<% content_for :title, t(".edit_table", name: @table.name) %>
<h2><%= t(".edit_table", name: @table.name) %></h2>
<%= link_to t(".delete_table", name: @table.name), table_path(@table),
data: { turbo_method: :delete, turbo_confirm: t(".delete_table_confirmation", name: @table.name) } %>
<%= render partial: "tables/form",
locals: { table: @table, button_text: t(".update_table") } %>

View File

@ -1 +1,10 @@
<p>Hello</p>
<% content_for :title, t(".tables") %>
<%= link_to t(".new_table"), new_table_path %>
<h2>Your tables</h2>
<% if @tables.any? %>
<%= render @tables %>
<% else %>
<p>You have no tables.</p>
<% end %>

View File

@ -0,0 +1,6 @@
<% content_for :title, t(".new_table") %>
<h2><%= t(".new_table") %></h2>
<%= render partial: "tables/form",
locals: { table: @table, button_text: t(".create_table") } %>

View File

@ -0,0 +1,12 @@
<% content_for :title, @table.name %>
<h2><%= @table.name %></h2>
<%= link_to t(".edit_table"), edit_table_path(@table) %>
<dl>
<dt><%= t(".game_system") %>:</dt>
<dd><%= @table.game_system.name %></dd>
<dt><%= t(".owner") %>:</dt>
<dd><%= @table.owner.username %></dd>
</dl>

View File

@ -56,6 +56,31 @@ en:
destroy:
log_out: Log out
success: "You have signed out."
tables:
index:
new_table: Create a table
tables: Tables
show:
edit_table: Edit table
game_system: Game system
owner: Owner
new:
new_table: New table
create_table: Create table
create:
success: Your table “%{name}” has been created.
error: Failed to create table
edit:
delete_table: Delete %{name}
delete_table_confirmation: Are you sure you want to delete %{name}?
edit_table: Edit %{name}
update_table: Update table
update:
success: Updated table “%{name}”.
error: Failed to update table.
destroy:
success: Deleted table “%{name}”.
error: Failed to delete table.
users:
validations:
email_format: "must be a valid email address"

View File

@ -12,7 +12,7 @@ Rails.application.routes.draw do
resources :account_verifications, only: [ :show ]
resources :sessions, only: [ :new, :create, :destroy ]
resources :tables, only: [ :index ]
resources :tables
resources :admin, only: [ :index ]
namespace :admin do

View File

@ -0,0 +1,68 @@
# frozen_string_literal: true
require "test_helper"
class TablesControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
sign_in users(:trevor)
get tables_url
assert_response :success
end
test "should get show" do
sign_in users(:trevor)
get table_url(tables(:table))
assert_response :success
end
test "should get new" do
sign_in users(:trevor)
get new_table_url
assert_response :success
end
test "should create table" do
sign_in users(:trevor)
assert_changes("Table.count", +1) do
post(tables_url, params: { table: table_params })
end
assert_redirected_to table_path(Table.last)
end
test "should alert to invalid table" do
sign_in users(:trevor)
assert_no_changes("Table.count") do
post(tables_url, params: { table: { name: "new_table" } })
end
assert_response :unprocessable_entity
end
test "should get edit" do
sign_in users(:trevor)
get edit_table_url(tables(:table))
assert_response :success
end
test "should update table" do
sign_in users(:trevor)
patch table_url(tables(:table)), params: { table: { name: "new name" } }
assert_redirected_to table_path(tables(:table))
end
test "should destroy table" do
sign_in users(:trevor)
assert_difference("Table.count", -1) do
delete table_url(tables(:table))
end
assert_redirected_to tables_url
end
private
def table_params
{
name: "A new table",
game_system_id: game_systems(:dnd).id,
}
end
end