Scaffold background jobs settings

This commit is contained in:
Eugene Burmakin 2024-07-09 20:28:59 +02:00
parent 54446f1c3c
commit b648c18f9d
15 changed files with 247 additions and 7 deletions

File diff suppressed because one or more lines are too long

View file

@ -13,6 +13,12 @@ class ApplicationController < ActionController::Base
@unread_notifications ||= Notification.where(user: current_user).unread
end
def authenticate_first_user!
return if current_user == User.first
redirect_to root_path, notice: 'You are not authorized to perform this action.', status: :unauthorized
end
def authenticate_api_key
return head :unauthorized unless current_api_user

View file

@ -0,0 +1,26 @@
# frozen_string_literal: true
class Settings::BackgroundJobsController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_first_user!
def index
@queues = Sidekiq::Queue.all
end
def show; end
def new
end
def edit; end
def create
end
def update
end
def destroy
end
end

View file

@ -23,10 +23,4 @@ class Settings::UsersController < ApplicationController
def user_params
params.require(:user).permit(:email)
end
def authenticate_first_user!
return if current_user == User.first
redirect_to settings_users_url, notice: 'You are not authorized to perform this action.', status: :unauthorized
end
end

View file

@ -109,4 +109,8 @@ module ApplicationHelper
base_title = 'Dawarich'
page_title.empty? ? base_title : "#{page_title} | #{base_title}"
end
def active_tab?(link_path)
'tab-active' if current_page?(link_path)
end
end

View file

@ -0,0 +1,2 @@
module Settings::BackgroundJobsHelper
end

View file

@ -0,0 +1,2 @@
<div id="<%= dom_id background_job %>">
</div>

View file

@ -0,0 +1,17 @@
<%= form_with(model: settings_background_job, class: "contents") do |form| %>
<% if settings_background_job.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<h2><%= pluralize(settings_background_job.errors.count, "error") %> prohibited this settings_background_job from being saved:</h2>
<ul>
<% settings_background_job.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="inline">
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
</div>
<% end %>

View file

@ -0,0 +1,8 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">Editing background job</h1>
<%= render "form", settings_background_job: @settings_background_job %>
<%= link_to "Show this background job", @settings_background_job, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to background jobs", settings_background_jobs_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View file

@ -0,0 +1,18 @@
<% content_for :title, "Background jobs" %>
<div class="w-full">
<div class="flex justify-between items-center">
<h1 class="font-bold text-4xl">Background jobs</h1>
</div>
<div id="settings_background_jobs" class="min-w-full">
<% @queues.each do |queue| %>
<div class="card shadow-2xl bg-base-100 p-5 my-5">
<h2 class="text-2xl font-bold"><%= queue.name %></h2>
<div class="flex justify-between items-center">
<p class="text-lg">Jobs in queue: <%= queue.size %></p>
<a href="<%= 'settings_background_jobs_clear_queue_path(queue)' %>" class="btn btn-primary">Clear queue</a>
</div>
</div>
<% end %>
</div>
</div>

View file

@ -0,0 +1,7 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">New background job</h1>
<%= render "form", settings_background_job: @settings_background_job %>
<%= link_to "Back to background jobs", settings_background_jobs_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View file

@ -0,0 +1,15 @@
<div class="mx-auto md:w-2/3 w-full flex">
<div class="mx-auto">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<%= render @settings_background_job %>
<%= link_to "Edit this background job", edit_settings_background_job_path(@settings_background_job), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to background jobs", settings_background_jobs_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<div class="inline-block ml-2">
<%= button_to "Destroy this background job", @settings_background_job, method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
</div>
</div>
</div>

View file

@ -1,6 +1,11 @@
<% content_for :title, 'Settings' %>
<div class="min-h-content bg-base-200 w-full">
<div role="tablist" class="tabs tabs-lifted tabs-lg">
<%= link_to 'Main', settings_path, role: 'tab', class: "tab #{active_tab?(settings_path)}" %>
<%= link_to 'Background Jobs', settings_background_jobs_path, role: 'tab', class: "tab #{active_tab?(settings_background_jobs_path)}" %>
</div>
<div class="flex flex-col lg:flex-row w-full my-10 space-x-4">
<div class="card flex-shrink-0 w-full max-w-sm shadow-2xl bg-base-100 px-5 py-5 mx-5">
<h2 class="text-2xl font-bold">Edit your Dawarich settings!</h1>

View file

@ -9,6 +9,7 @@ Rails.application.routes.draw do
resources :settings, only: :index
namespace :settings do
resources :background_jobs
resources :users, only: :create
end

View file

@ -0,0 +1,135 @@
require 'rails_helper'
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to test the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
RSpec.describe "/settings/background_jobs", type: :request do
# This should return the minimal set of attributes required to create a valid
# Settings::BackgroundJob. As you add validations to Settings::BackgroundJob, be sure to
# adjust the attributes here as well.
let(:valid_attributes) {
skip("Add a hash of attributes valid for your model")
}
let(:invalid_attributes) {
skip("Add a hash of attributes invalid for your model")
}
describe "GET /index" do
it "renders a successful response" do
Settings::BackgroundJob.create! valid_attributes
get settings_background_jobs_url
expect(response).to be_successful
end
end
describe "GET /show" do
it "renders a successful response" do
background_job = Settings::BackgroundJob.create! valid_attributes
get settings_background_job_url(background_job)
expect(response).to be_successful
end
end
describe "GET /new" do
it "renders a successful response" do
get new_settings_background_job_url
expect(response).to be_successful
end
end
describe "GET /edit" do
it "renders a successful response" do
background_job = Settings::BackgroundJob.create! valid_attributes
get edit_settings_background_job_url(background_job)
expect(response).to be_successful
end
end
describe "POST /create" do
context "with valid parameters" do
it "creates a new Settings::BackgroundJob" do
expect {
post settings_background_jobs_url, params: { settings_background_job: valid_attributes }
}.to change(Settings::BackgroundJob, :count).by(1)
end
it "redirects to the created settings_background_job" do
post settings_background_jobs_url, params: { settings_background_job: valid_attributes }
expect(response).to redirect_to(settings_background_job_url(Settings::BackgroundJob.last))
end
end
context "with invalid parameters" do
it "does not create a new Settings::BackgroundJob" do
expect {
post settings_background_jobs_url, params: { settings_background_job: invalid_attributes }
}.to change(Settings::BackgroundJob, :count).by(0)
end
it "renders a response with 422 status (i.e. to display the 'new' template)" do
post settings_background_jobs_url, params: { settings_background_job: invalid_attributes }
expect(response).to have_http_status(:unprocessable_entity)
end
end
end
describe "PATCH /update" do
context "with valid parameters" do
let(:new_attributes) {
skip("Add a hash of attributes valid for your model")
}
it "updates the requested settings_background_job" do
background_job = Settings::BackgroundJob.create! valid_attributes
patch settings_background_job_url(background_job), params: { settings_background_job: new_attributes }
background_job.reload
skip("Add assertions for updated state")
end
it "redirects to the settings_background_job" do
background_job = Settings::BackgroundJob.create! valid_attributes
patch settings_background_job_url(background_job), params: { settings_background_job: new_attributes }
background_job.reload
expect(response).to redirect_to(settings_background_job_url(background_job))
end
end
context "with invalid parameters" do
it "renders a response with 422 status (i.e. to display the 'edit' template)" do
background_job = Settings::BackgroundJob.create! valid_attributes
patch settings_background_job_url(background_job), params: { settings_background_job: invalid_attributes }
expect(response).to have_http_status(:unprocessable_entity)
end
end
end
describe "DELETE /destroy" do
it "destroys the requested settings_background_job" do
background_job = Settings::BackgroundJob.create! valid_attributes
expect {
delete settings_background_job_url(background_job)
}.to change(Settings::BackgroundJob, :count).by(-1)
end
it "redirects to the settings_background_jobs list" do
background_job = Settings::BackgroundJob.create! valid_attributes
delete settings_background_job_url(background_job)
expect(response).to redirect_to(settings_background_jobs_url)
end
end
end