Add CRUD actions for characters
This commit is contained in:
parent
2aed4cdfc2
commit
9684dd39dd
|
@ -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
|
|
@ -0,0 +1,4 @@
|
|||
<div id=<%= dom_id(character) %>>
|
||||
<h6><%= link_to character.name, character %></h6>
|
||||
<p><%= character.game_system.name %></p>
|
||||
</div>
|
|
@ -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 %>
|
|
@ -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") } %>
|
|
@ -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 %>
|
|
@ -0,0 +1,5 @@
|
|||
<% content_for :title, t(".new_character") %>
|
||||
|
||||
<h2><%= t(".new_character") %></h2>
|
||||
|
||||
<%= render partial: "form", locals: { button_text: t(".create") } %>
|
|
@ -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>
|
|
@ -18,6 +18,7 @@
|
|||
<nav>
|
||||
<ul>
|
||||
<% if logged_in? %>
|
||||
<li><%= link_to t(".characters"), characters_path %></li>
|
||||
<li><%= link_to t(".tables"), tables_path %></li>
|
||||
<li><%= link_to t(".profile"), user_path(Current.user) %></li>
|
||||
<li><%= link_to t("log_out"), logout_path, data: {turbo_method: :delete} %></li>
|
||||
|
|
|
@ -15,6 +15,7 @@ en:
|
|||
game_systems: Game Systems
|
||||
jobs: Jobs
|
||||
application:
|
||||
characters: Characters
|
||||
profile: Profile
|
||||
tables: Tables
|
||||
mailer:
|
||||
|
@ -54,6 +55,33 @@ en:
|
|||
destroy:
|
||||
success: Successfully deleted “%{name}”.
|
||||
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:
|
||||
new:
|
||||
reset_password: Reset your password
|
||||
|
@ -79,7 +107,6 @@ en:
|
|||
update:
|
||||
success: Your password has been updated
|
||||
error: Failed to update password
|
||||
|
||||
sessions:
|
||||
create:
|
||||
success: "Hello, %{name}!"
|
||||
|
|
|
@ -15,6 +15,7 @@ Rails.application.routes.draw do
|
|||
resources :password_resets, only: [ :new, :create, :edit, :update ]
|
||||
resources :sessions, only: [ :new, :create, :destroy ]
|
||||
|
||||
resources :characters
|
||||
resources :table_invites, only: [ :index, :edit, :update ]
|
||||
resources :tables do
|
||||
resources :table_invites, only: [ :new, :create ]
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue