1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-18 07:52:23 +01:00

(feat) soft destroy machines/spaces

This commit is contained in:
Sylvain 2022-11-22 14:17:25 +01:00
parent 03f3819255
commit 509dd0eb5f
11 changed files with 61 additions and 6 deletions

View File

@ -1,5 +1,6 @@
# Changelog Fab-manager
- Soft destroy of spaces and machines
- Fix a bug: in upgrade script, the error "the input device is not a TTY" is thrown when migrating the database
- Fix a bug: broken display of machines pages
- Fix a bug: some automated tests were randomly failing because ElasticSearch was not synced

View File

@ -12,6 +12,8 @@ class API::MachinesController < API::ApiController
def show
@machine = Machine.includes(:machine_files, :projects).friendly.find(params[:id])
head :not_found if @machine.deleted_at
end
def create
@ -35,7 +37,11 @@ class API::MachinesController < API::ApiController
def destroy
authorize @machine
@machine.destroy
if @machine.destroyable?
@machine.destroy
else
@machine.soft_destroy!
end
head :no_content
end

View File

@ -6,11 +6,13 @@ class API::SpacesController < API::ApiController
respond_to :json
def index
@spaces = Space.includes(:space_image)
@spaces = Space.includes(:space_image).where(deleted_at: nil)
end
def show
@space = Space.includes(:space_files, :projects).friendly.find(params[:id])
head :not_found if @space.deleted_at
end
def create
@ -36,7 +38,11 @@ class API::SpacesController < API::ApiController
def destroy
@space = get_space
authorize @space
@space.destroy
if @space.destroyable?
@space.destroy
else
@space.soft_destroy!
end
head :no_content
end

View File

@ -82,6 +82,10 @@ class Machine < ApplicationRecord
reservations.empty?
end
def soft_destroy!
update(deleted_at: DateTime.current)
end
def packs?(user)
prepaid_packs.where(group_id: user.group_id)
.where(disabled: [false, nil])

View File

@ -66,6 +66,10 @@ class Space < ApplicationRecord
reservations.empty?
end
def soft_destroy!
update(deleted_at: DateTime.current)
end
private
def update_gateway_product

View File

@ -11,6 +11,6 @@ class MachinePolicy < ApplicationPolicy
end
def destroy?
user.admin? and record.destroyable?
user.admin?
end
end

View File

@ -1,3 +1,6 @@
# frozen_string_literal: true
# Check the access policies for API::SpacesController
class SpacePolicy < ApplicationPolicy
def create?
user.admin?
@ -8,6 +11,6 @@ class SpacePolicy < ApplicationPolicy
end
def destroy?
user.admin? and record.destroyable?
user.admin?
end
end

View File

@ -9,6 +9,9 @@ class MachineService
else
Machine.includes(:machine_image, :plans).order(sort_by)
end
# do not include soft destroyed
machines = machines.where(deleted_at: nil)
if filters[:disabled].present?
state = filters[:disabled] == 'false' ? [nil, false] : true
machines = machines.where(disabled: state)

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
# Allow soft destroy of machines.
# Machines with existing reservation cannot be destroyed because we need them for rebuilding invoices, statistics, etc.
# This attribute allows to make a "soft destroy" of a Machine, marking it as destroyed so it doesn't appear anymore in
# the interface (as if it was destroyed) but still lives in the database so we can use it to build data.
class AddDeletedAtToMachine < ActiveRecord::Migration[5.2]
def change
add_column :machines, :deleted_at, :datetime
add_index :machines, :deleted_at
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
# Allow soft destroy of spaces.
# Spaces with existing reservation cannot be destroyed because we need them for rebuilding invoices, statistics, etc.
# This attribute allows to make a "soft destroy" of a Space, marking it as destroyed so it doesn't appear anymore in
# the interface (as if it was destroyed) but still lives in the database so we can use it to build data.
class AddDeletedAtToSpace < ActiveRecord::Migration[5.2]
def change
add_column :spaces, :deleted_at, :datetime
add_index :spaces, :deleted_at
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_10_03_133019) do
ActiveRecord::Schema.define(version: 2022_11_22_123605) do
# These are extensions that must be enabled in order to support this database
enable_extension "fuzzystrmatch"
@ -358,6 +358,8 @@ ActiveRecord::Schema.define(version: 2022_10_03_133019) do
t.datetime "updated_at"
t.string "slug"
t.boolean "disabled"
t.datetime "deleted_at"
t.index ["deleted_at"], name: "index_machines_on_deleted_at"
t.index ["slug"], name: "index_machines_on_slug", unique: true
end
@ -878,6 +880,8 @@ ActiveRecord::Schema.define(version: 2022_10_03_133019) do
t.datetime "updated_at", null: false
t.text "characteristics"
t.boolean "disabled"
t.datetime "deleted_at"
t.index ["deleted_at"], name: "index_spaces_on_deleted_at"
end
create_table "spaces_availabilities", id: :serial, force: :cascade do |t|