Skip to content

Commit

Permalink
書籍と著者のリレーションシップを定義。
Browse files Browse the repository at this point in the history
  • Loading branch information
mikoto2000 committed Sep 6, 2024
1 parent 2827c6a commit 326a87e
Show file tree
Hide file tree
Showing 31 changed files with 769 additions and 6 deletions.
65 changes: 65 additions & 0 deletions app/controllers/authors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class AuthorsController < ApplicationController
include Pagy::Backend
before_action :set_author, only: %i[show edit update destroy]

# GET /authors
def index
@authors = Author
.all
@q = @authors.ransack(params[:q])
@q.sorts = "id asc" if @q.sorts.empty?
@pagy, @authors = pagy(@q.result, page: params[:page], items: params[:items])
end

# GET /authors/1
def show
end

# GET /authors/new
def new
@author = Author.new
end

# GET /authors/1/edit
def edit
end

# POST /authors
def create
@author = Author.new(author_params)

if @author.save
redirect_to @author, notice: t("controller.create.success", model: Author.model_name.human)
else
render :new, status: :unprocessable_entity
end
end

# PATCH/PUT /authors/1
def update
if @author.update(author_params)
redirect_to @author, notice: t("controller.edit.success", model: Author.model_name.human)
else
render :edit, status: :unprocessable_entity
end
end

# DELETE /authors/1
def destroy
@author.destroy!
redirect_to authors_url, notice: t("controller.destroy.success", model: Author.model_name.human)
end

private

# Use callbacks to share common setup or constraints between actions.
def set_author
@author = Author
.find(params[:id])
end

# Only allow a list of trusted parameters through.
def author_params
params.require(:author).permit(:name)
end
end
2 changes: 1 addition & 1 deletion app/controllers/book_masters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ def set_book_master

# Only allow a list of trusted parameters through.
def book_master_params
params.require(:book_master).permit(:isbn, :title, :publication_date, :ndc_category_id)
params.require(:book_master).permit(:isbn, :title, :publication_date, :ndc_category_id, {author_id: []})

Check failure on line 64 in app/controllers/book_masters_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideHashLiteralBraces: Space inside { missing.

Check failure on line 64 in app/controllers/book_masters_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideHashLiteralBraces: Space inside } missing.
end
end
3 changes: 3 additions & 0 deletions app/helpers/authors_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module AuthorsHelper
include Pagy::Frontend
end
8 changes: 8 additions & 0 deletions app/models/author.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Author < ApplicationRecord
def self.ransackable_attributes(_auth_object = nil)
%w[name id created_at updated_at]
end

has_many :book_author_relationship

Check failure on line 6 in app/models/author.rb

View workflow job for this annotation

GitHub Actions / lint

Rails/HasManyOrHasOneDependent: Specify a `:dependent` option.
has_many :book_masters, through: :book_author_relationship
end
7 changes: 7 additions & 0 deletions app/models/book_author_relationship.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class BookAuthorRelationship < ApplicationRecord
def self.ransackable_attributes(_auth_object = nil)
%w[book_master_id author_id id created_at updated_at]
end
belongs_to :book_master
belongs_to :author
end
7 changes: 6 additions & 1 deletion app/models/book_master.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
class BookMaster < ApplicationRecord
def self.ransackable_attributes(_auth_object = nil)
%w[isbn title publication_date ndc_category_id id created_at updated_at]
%w[isbn title publication_date ndc_category_id author_id id created_at updated_at]
end
def self.ransackable_associations(auth_object = nil)

Check failure on line 5 in app/models/book_master.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/EmptyLineBetweenDefs: Expected 1 empty line between method definitions; found 0.

Check failure on line 5 in app/models/book_master.rb

View workflow job for this annotation

GitHub Actions / lint

Lint/UnusedMethodArgument: Unused method argument - `auth_object`. If it's necessary, use `_` or `_auth_object` as an argument name to indicate that it won't be used. If it's unnecessary, remove it. You can also write as `ransackable_associations(*)` if you want the method to accept any arguments but don't care about them.
["authors", "book_author_relationship", "ndc_category"]

Check failure on line 6 in app/models/book_master.rb

View workflow job for this annotation

GitHub Actions / lint

Style/WordArray: Use `%w` or `%W` for an array of words.
end
belongs_to :ndc_category
has_many :book_author_relationship

Check failure on line 9 in app/models/book_master.rb

View workflow job for this annotation

GitHub Actions / lint

Rails/HasManyOrHasOneDependent: Specify a `:dependent` option.
has_many :authors, through: :book_author_relationship
end
42 changes: 42 additions & 0 deletions app/views/authors/_author.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<div id="<%= dom_id author %>">
<table class="table table-hover">
<tr>
<td>
<strong>Id:</strong>
</td>
<td>
<%= author.id %>
</td>
</tr>
<tr>
<td>
<strong>
<%= Author.human_attribute_name(:name) %>
</strong>
</td>
<td>
<%= author.name %>
</td>
</tr>
<tr>
<td>
<strong>
<%= Author.human_attribute_name(:created_at) %>
</strong>
</td>
<td>
<%= author.created_at && l(author.created_at) %>
</td>
</tr>
<tr>
<td>
<strong>
<%= Author.human_attribute_name(:updated_at) %>
</strong>
</td>
<td>
<%= author.updated_at && l(author.updated_at) %>
</td>
</tr>
</table>
</div>
2 changes: 2 additions & 0 deletions app/views/authors/_author.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! author, :id, :name, :created_at, :updated_at
json.url author_url(author, format: :json)
45 changes: 45 additions & 0 deletions app/views/authors/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<%= form_with(model: author, html: { class: %w[form-control form-control-sm] }) do |form| -%>
<%- if author.errors.any? -%>
<div style="color: red">
<h2><%= t("errors.template.header", model: Author.model_name.human, count: author.errors.count) %></h2>

<ul>
<% author.errors.each do |error| %>
<li><%= error.full_message %></li>
<%- end -%>
</ul>
</div>
<%- end -%>

<%- if author.persisted? -%>
<div>
<%= form.label :id, style: "display: block" %>
<div class="ps-2">
<%= form.number_field :id, class: %w[form-control form-control-sm], disabled: true %>
</div>
</div>
<%- end -%>
<div>
<%= form.label :name, style: "display: block" %>
<div class="ps-2">
<%= form.text_field :name, class: %w[form-control form-control-sm] %>
</div>
</div>
<%- if author.persisted? -%>
<div>
<%= form.label :created_at, style: "display: block" %>
<div class="ps-2">
<%= form.datetime_field :created_at, class: %w[form-control form-control-sm], disabled: true %>
</div>
</div>
<div>
<%= form.label :updated_at, style: "display: block" %>
<div class="ps-2">
<%= form.datetime_field :updated_at, class: %w[form-control form-control-sm], disabled: true %>
</div>
</div>
<%- end -%>
<div>
<%= form.submit class: %w[btn btn-primary] %>
</div>
<% end %>
9 changes: 9 additions & 0 deletions app/views/authors/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1><%= t("view.edit.title", model: Author.model_name.human) %></h1>
<%= render "form", author: @author %>

<br>

<div>
<%= link_to t("view.edit.move_to_show", model: Author.model_name.human), @author %> |
<%= link_to t("view.edit.move_to_index", model: Author.model_name.human.pluralize), authors_path %>
</div>
111 changes: 111 additions & 0 deletions app/views/authors/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<div style="display: flex;flex-direction: column;height: 100vh;">

<header class="main-content-header">
<p style="color: green"><%= notice %></p>

<h1><%= Author.model_name.human %><%= link_to "", new_author_path, class: %w[bi bi-plus-circle-fill ms-2] %></h1>

<div class="accordion">
<div class="accordion-item">
<h2 class="accordion-header" id="search-form-header">
<button
class="accordion-button collapsed"
type="button"
data-bs-toggle="collapse"
data-bs-target="#search-form"
aria-expanded="false"
aria-controls="search-form"><%= t("view.index.search_condition") %></button>
</h2>
<div id="search-form" class="panel panel-default accordion-collapse collapse" aria-labelledby="headingOne">
<%= search_form_for @q, html: { class: %w[form-control form-control-sm], data: { turbo_frame: "list" } } do |f| %>
<div>
<%= f.label :id_eq %>
<div class="ps-2">
<%= f.number_field :id_eq, class: %w[form-control form-control-sm] %>
</div>
</div>
<div>
<%= f.label :name_cont %>
<div class="ps-2">
<%= f.search_field :name_cont, class: %w[form-control form-control-sm] %>
</div>
</div>
<div>
<%= f.label :created_at %>
<div class="ps-2">
<%= f.datetime_field :created_at_gteq, class: %w[form-control form-control-sm] %>
<%= f.datetime_field :created_at_lteq_end_of_minute, class: %w[form-control form-control-sm] %>
</div>
</div>
<div>
<%= f.label :updated_at %>
<div class="ps-2">
<%= f.datetime_field :updated_at_gteq, class: %w[form-control form-control-sm] %>
<%= f.datetime_field :updated_at_lteq_end_of_minute, class: %w[form-control form-control-sm] %>
</div>
</div>
<%= f.hidden_field :limit, id: "f-limit", value: params[:limit] || Pagy::DEFAULT[:limit] %>
<%= f.submit class: %w[btn btn-primary] %>
<input
type="reset"
class="btn btn-primary"
onclick="clear_form(arguments[0], this)"
value="<%= t("helpers.submit.clear") %>">
<%- end -%>
</div>
</div>
</header>

<main style="flex-grow: 1;flex-shrink: 1;overflow-y: auto;">
<%= turbo_frame_tag "list" do %>
<div id="authors" class="text-nowrap">
<table class="table table-hover text-nowrap">
<thead class="table-light sticky-top">
<th><%= sort_link(@q, :id, default_order: :asc, class: "d-block") %></th>
<th><%= sort_link(@q, :name, default_order: :asc, class: "d-block") %></th>
<th><%= sort_link(@q, :created_at, default_order: :asc, class: "d-block") %></th>
<th><%= sort_link(@q, :updated_at, default_order: :asc, class: "d-block") %></th>
<th><%= t("view.index.operation") %></th>
</thead>
<tbody>
<%- @authors.each do |author| %>
<tr
tabindex="0"
role="button"
onkeypress="handleEnterKeypressListIten(arguments[0], this, '<%= url_target(nil, author).to_s %>')"
onclick="handleClickListItem(arguments[0], this, '<%= url_target(nil, author).to_s %>')">
<td><%= author.id %></td>
<td><%= author.name %></td>
<td><%= author.created_at && l(author.created_at) %></td>
<td><%= author.updated_at && l(author.updated_at) %></td>
<td>
<%= button_to(
t("helpers.submit.delete"),
author,
method: :delete,
tabindex: 0,
class: %w[btn btn-primary],
onclick: "handleDeleteListItem(arguments[0], this)",
data: { turbo_confirm: t("helpers.dialog.delete", id: author.id) }
) %>
</td>
</tr>
<%- end %>
</tbody>
</table>
</div>
<div>
<%== pagy_bootstrap_nav(@pagy) %>
<%== pagy_limit_selector_js(@pagy, item_name: "Author".pluralize(@pagy.count), id: "pagy-limit-selector") %>
<%= link_to(
t("view.index.update_item_per_page", model: Author.model_name.human),
"javascript:void(0);",
onClick: "updateItemPerPage(arguments[0],
this)"
) %>
</div>
<% end %>
</main>
</div>
1 change: 1 addition & 0 deletions app/views/authors/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @authors, partial: "authors/author", as: :author
8 changes: 8 additions & 0 deletions app/views/authors/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h1><%= t("view.new.title", model: Author.model_name.human) %></h1>
<%= render "form", author: @author %>

<br>

<div>
<%= link_to t("view.new.move_to_index", model: Author.model_name.human.pluralize), authors_path %>
</div>
16 changes: 16 additions & 0 deletions app/views/authors/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<p style="color: green"><%= notice %></p>

<h1><%= Author.model_name.human %></h1>

<%= render @author %>

<div>
<%= link_to t("view.show.move_to_update", model: Author.model_name.human), edit_author_path(@author) %> |
<%= link_to t("view.show.move_to_index", model: Author.model_name.human.pluralize), authors_path %>

<%= button_to t("helpers.submit.delete"),
@author,
method: :delete,
class: %w[btn btn-primary],
data: { turbo_confirm: t("helpers.dialog.delete", id: @author.id) } %>
</div>
1 change: 1 addition & 0 deletions app/views/authors/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "authors/author", author: @author
12 changes: 12 additions & 0 deletions app/views/book_masters/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@
) %>
</div>
</div>
<div>
<%= form.label :author %>
<div class="ps-2">
<%= form.collection_select(
:author_ids,
Author.all,
:id,
:name,
{ multiple: true }
) %>
</div>
</div>
<%- if book_master.persisted? -%>
<div>
<%= form.label :created_at, style: "display: block" %>
Expand Down
Loading

0 comments on commit 326a87e

Please sign in to comment.