Added project status and asynchronous initialization
This commit is contained in:
@@ -4,7 +4,7 @@ class ProjectsController < ApplicationController
|
||||
before_action :fetch_project!, only: %w[show edit update destroy]
|
||||
|
||||
def index
|
||||
@projects = Project.all
|
||||
@projects = available_projects
|
||||
end
|
||||
|
||||
def show; end
|
||||
@@ -16,7 +16,7 @@ class ProjectsController < ApplicationController
|
||||
def create
|
||||
@project = Project.new(project_params)
|
||||
if @project.save
|
||||
redirect_to @project
|
||||
redirect_to url_for(action: :index), notice: "Project #{@project.name} being created."
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
@@ -36,8 +36,12 @@ class ProjectsController < ApplicationController
|
||||
|
||||
private
|
||||
|
||||
def available_projects
|
||||
Project.ready
|
||||
end
|
||||
|
||||
def fetch_project!
|
||||
@project = Project.find_by!(code: params[:id])
|
||||
@project = available_projects.find_by!(code: params[:id])
|
||||
self.current_project = @project
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class ProjectPostInitJob < ApplicationJob
|
||||
queue_as :default
|
||||
|
||||
def perform(project_id)
|
||||
project = Project.preparing.find(project_id)
|
||||
|
||||
create_tasks_number_sequence(project)
|
||||
project.update!(status: :ready)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_tasks_number_sequence(project)
|
||||
Project.connection.execute "CREATE SEQUENCE IF NOT EXISTS #{project.tasks_number_sequence_name} AS INT UNSIGNED"
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,8 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Project < ApplicationRecord
|
||||
enum :status, %i[preparing ready archived].index_by(&:itself), default: :preparing
|
||||
|
||||
validates :name, :code, presence: true
|
||||
validates :code, exclusion: { in: %w[new] }, uniqueness: true, format: { with: /\A[a-z]{2,}\z/ }
|
||||
|
||||
@@ -10,7 +12,7 @@ class Project < ApplicationRecord
|
||||
|
||||
normalizes :code, with: ->(code) { code.strip.downcase.gsub(/\W+/, '') }
|
||||
|
||||
after_commit :create_tasks_number_sequence, on: :create
|
||||
after_commit :schedule_post_init_job, on: :create
|
||||
after_destroy_commit :drop_tasks_number_sequence
|
||||
|
||||
def to_param
|
||||
@@ -32,8 +34,8 @@ class Project < ApplicationRecord
|
||||
|
||||
private
|
||||
|
||||
def create_tasks_number_sequence
|
||||
self.class.connection.execute "CREATE SEQUENCE IF NOT EXISTS #{tasks_number_sequence_name} AS INT UNSIGNED"
|
||||
def schedule_post_init_job
|
||||
ProjectPostInitJob.perform_later id
|
||||
end
|
||||
|
||||
def drop_tasks_number_sequence
|
||||
|
||||
Reference in New Issue
Block a user