From 5aa5fe0f5ab1b51d452200ac8045b53746dbcf57 Mon Sep 17 00:00:00 2001 From: insects Date: Wed, 12 Mar 2025 13:21:38 +0100 Subject: [PATCH] feat: support cloning instances --- Gemfile | 3 ++- Gemfile.lock | 3 +++ app/controllers/instance_controller.rb | 18 ++++++++++++++++++ app/views/instance/show.html.erb | 3 +++ config/routes.rb | 1 + 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index f3dc569..5bc68ac 100644 --- a/Gemfile +++ b/Gemfile @@ -11,6 +11,7 @@ gem "jbuilder" gem "tomlrb" gem "nanoid" gem "spicy-proton" +gem "deep_cloneable" gem "tzinfo-data", platforms: %i[ windows jruby ] @@ -26,6 +27,7 @@ group :development, :test do gem "debug", platforms: %i[ mri windows ], require: "debug/prelude" gem "brakeman", require: false gem "rubocop-rails-omakase", require: false + gem "dockerfile-rails", ">= 1.7" end group :development do @@ -37,4 +39,3 @@ group :test do gem "selenium-webdriver" end -gem "dockerfile-rails", ">= 1.7", :group => :development diff --git a/Gemfile.lock b/Gemfile.lock index d03e6d5..b1d0b70 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -101,6 +101,8 @@ GEM debug (1.10.0) irb (~> 1.10) reline (>= 0.3.8) + deep_cloneable (3.2.1) + activerecord (>= 3.1.0, < 9) dockerfile-rails (1.7.9) rails (>= 3.0.0) drb (2.2.1) @@ -342,6 +344,7 @@ DEPENDENCIES brakeman capybara debug + deep_cloneable dockerfile-rails (>= 1.7) importmap-rails jbuilder diff --git a/app/controllers/instance_controller.rb b/app/controllers/instance_controller.rb index 737abba..5cfa8d9 100644 --- a/app/controllers/instance_controller.rb +++ b/app/controllers/instance_controller.rb @@ -58,6 +58,20 @@ class InstanceController < ApplicationController end end + def clone + instance = clone_instance_params + i = Instance.find_by(public_id: instance) + new_inst = i.deep_clone(include: [ :fairies, :pops ]) + new_inst.public_id = Nanoid.generate(size: 6) + new_inst.password = Nanoid.generate(size: 4, alphabet: "0123456789") + new_inst.name = Spicy::Proton.pair(" ") + if new_inst.save + @id = new_inst.public_id + @password = new_inst.password + render "set_password" + end + end + def show @instance = Instance.includes(:pops, :fairies).find_by(public_id: show_instance_params) if @instance @@ -125,6 +139,10 @@ class InstanceController < ApplicationController params.expect(:info) end + def clone_instance_params + params.expect(:instance) + end + def show_instance_params params.expect(:public_id) end diff --git a/app/views/instance/show.html.erb b/app/views/instance/show.html.erb index 0e6da04..71bd166 100644 --- a/app/views/instance/show.html.erb +++ b/app/views/instance/show.html.erb @@ -50,3 +50,6 @@ <%= javascript_include_tag "list" %> +<%= form_with url: clone_instance_path(instance: @instance.public_id) do |f| %> + <%= f.submit "clone instance" %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index bc88fe6..d4f7a70 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ Rails.application.routes.draw do post "/new", to: "instance#create", as: :new_instance post "/new_from_fe", to: "instance#create_from_fe", as: :new_from_fe + post "/clone", to: "instance#clone", as: :clone_instance post "/pop", to: "instance#pop", as: :pop_in_instance post "/pop/adjust", to: "instance#adjust_pop", as: :adjust_pop post "/reset", to: "instance#reset", as: :reset_in_instance