Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions app/controllers/documents_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding: utf-8
class DocumentsController < ApplicationController
before_action :set_document, only: %i[ show edit update destroy ]
before_action :get_form_data, only: %i[ new edit ]
protect_from_forgery :except => [:api_markdown]

# GET /documents or /documents.json
Expand All @@ -26,9 +27,6 @@ def show
# GET /documents/new
def new
@document = Document.new
@users = User.where(active: true)
@projects = Project.all
@tags = Tag.all
# TODO: バージョン7.1以降では datetime_field に include_seconds オプションが導入されるため,以下の秒を0にする記述は不要になる
@document.start_at = DateTime.current.change(sec: 0)
@document.end_at = DateTime.current.change(sec: 0)
Expand All @@ -41,23 +39,22 @@ def new

# GET /documents/1/edit
def edit
@users = User.where(active: true)
@projects = Project.all
@tags = Tag.all
end

# POST /documents or /documents.json
def create
@document = current_user.documents.build(document_params)
parse_tag_names(params[:tag_names]) if params[:tag_names]

if @document.save #XXX: save! => save
if @document.save
respond_to do |format|
format.html { redirect_to @document, notice: "文書を追加しました" }
format.json { render :show, status: :created, location: @document }
end
else
respond_to do |format|
flash.now[:danger] = '文書の作成に失敗しました.'
get_form_data
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @document.errors, status: :unprocessable_entity }
end
Expand All @@ -66,15 +63,28 @@ def create

# PATCH/PUT /documents/1 or /documents/1.json
def update
parse_tag_names(params[:tag_names]) if params[:tag_names]
if @document.update(document_params)
flash[:success] = "文書を更新しました"
redirect_to @document
else
respond_to do |format|
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @document.errors, status: :unprocessable_entity }
end
@document.transaction do
parse_tag_names(params[:tag_names]) if params[:tag_names]
@document.update!(document_params)
end

respond_to do |format|
format.html { redirect_to @document, notice: '文書を更新しました' }
format.json { render :show, status: :ok }
end
rescue ActiveRecord::StaleObjectError
flash.now[:danger] = 'この文書は他のユーザーによって更新されました.'
get_form_data
respond_to do |format|
format.html { render :edit, status: :conflict }
format.json { render json: { error: flash[:error] }, status: :conflict }
end
rescue
flash.now[:danger] = '文書の更新に失敗しました.'
get_form_data
respond_to do |format|
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @document.errors, status: :unprocessable_entity }
end
end

Expand All @@ -101,7 +111,7 @@ def set_document

# Only allow a list of trusted parameters through.
def document_params
params.require(:document).permit(:creator_id, :content, :description, :project_id, :start_at, :end_at, :location, :text,)
params.require(:document).permit(:creator_id, :content, :description, :project_id, :start_at, :end_at, :location, :text, :lock_version)
end

def parse_tag_names(tag_names)
Expand Down Expand Up @@ -133,4 +143,10 @@ def build_sort_query_with_default(param)
"start_at DESC"
end
end

def get_form_data
@users = User.where(active: true)
@projects = Project.all
@tags = Tag.all
end
end
39 changes: 26 additions & 13 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def create
@task = current_user.tasks.build(task_params)
parse_tag_names(params[:tag_names]) if params[:tag_names]

if @task.save!
if @task.save
matched = task_params[:description].match(/\[AI([0-9]+)\]/)
if matched != nil
ActionItem.find(matched[1]).update(task_url: tasks_path + "/" + @task.id.to_s)
Expand All @@ -59,6 +59,8 @@ def create
end
else
respond_to do |format|
flash.now[:danger] = 'タスクの作成に失敗しました.'
get_form_data
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
Expand All @@ -67,17 +69,28 @@ def create

# PATCH/PUT /tasks/1 or /tasks/1.json
def update
parse_tag_names(params[:tag_names]) if params[:tag_names]
if @task.update(task_params)
respond_to do |format|
format.html { redirect_to @task, notice: "タスクを更新しました." }
format.json { render :show, status: :ok, location: @task }
end
else
respond_to do |format|
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
@task.transaction do
parse_tag_names(params[:tag_names]) if params[:tag_names]
@task.update!(task_params)
end

respond_to do |format|
format.html { redirect_to @task, notice: "タスクを更新しました." }
format.json { render :show, status: :ok, location: @task }
end
rescue ActiveRecord::StaleObjectError
flash.now[:danger] = 'このタスクは他のユーザーによって更新されました.'
get_form_data
respond_to do |format|
format.html { render :edit, status: :conflict }
format.json { render json: { error: flash[:error] }, status: :conflict }
end
rescue
flash.now[:danger] = 'タスクの更新に失敗しました.'
get_form_data
respond_to do |format|
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @document.errors, status: :unprocessable_entity }
end
end

Expand Down Expand Up @@ -106,7 +119,7 @@ def get_form_data

# Only allow a list of trusted parameters through.
def task_params
params.require(:task).permit(:assigner_id, :due_at, :content, :description, :project_id, :task_state_id)
params.require(:task).permit(:assigner_id, :due_at, :content, :description, :project_id, :task_state_id, :lock_version)
end

def parse_tag_names(tag_names)
Expand Down
2 changes: 2 additions & 0 deletions app/views/documents/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
</div>
<% end %>

<%= form.hidden_field :lock_version %>

<div class="mb-4">
<%= form.label :content, "文書名", class: "form-label fw-bold mb-2" %>
<%= form.text_field :content, class: "form-control", placeholder: "文書名を入力してください", required: true %>
Expand Down
2 changes: 2 additions & 0 deletions app/views/tasks/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
</div>
<% end %>

<%= form.hidden_field :lock_version %>

<div class="mb-4">
<%= form.label :assigner_id, "担当者", class: "form-label fw-bold" %>
<%= form.select :assigner_id, @users.map { |u| [u.screen_name, u.id] }, {}, class: "form-select" %>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20260219050805_add_lock_version_to_documents.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddLockVersionToDocuments < ActiveRecord::Migration[7.2]
def change
add_column :documents, :lock_version, :integer
end
end
5 changes: 5 additions & 0 deletions db/migrate/20260225143559_add_lock_version_to_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddLockVersionToTasks < ActiveRecord::Migration[7.2]
def change
add_column :tasks, :lock_version, :integer
end
end
4 changes: 3 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.