diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index 0123b248..41c8c7d4 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -51,12 +51,17 @@ class TemplatesController < ApplicationController @template.folder = TemplateFolders.find_or_create_by_name(current_user, params[:folder_name]) end - @template.account = current_account + if params[:account_id].present? && current_account.linked_accounts.exists?(id: params[:account_id]) + @template.account_id = params[:account_id] + @template.folder = @template.account.default_template_folder + else + @template.account = current_account + end if @template.save Templates::CloneAttachments.call(template: @template, original_template: @base_template) if @base_template - redirect_to edit_template_path(@template) + maybe_redirect_to_template(@template) else render turbo_stream: turbo_stream.replace(:modal, template: 'templates/new'), status: :unprocessable_entity end @@ -81,6 +86,14 @@ class TemplatesController < ApplicationController private + def maybe_redirect_to_template(template) + if template.account == current_account + redirect_to(edit_template_path(@template)) + else + redirect_back(fallback_location: root_path, notice: 'Template has been clonned') + end + end + def template_params params.require(:template).permit(:name) end diff --git a/app/views/templates/new.html.erb b/app/views/templates/new.html.erb index 0187f2d2..d65222b2 100644 --- a/app/views/templates/new.html.erb +++ b/app/views/templates/new.html.erb @@ -12,11 +12,16 @@ - + Change Folder + <% if @base_template && can?(:manage, :tenants) && current_account.account_linked_accounts.exists?(account_type: :linked) %> +
+ <%= select_tag :account_id, options_for_select([current_account, *current_account.linked_accounts.where.not(id: current_account.testing_accounts)].map { |e| [e.name, e.id] }, current_account.id), required: true, class: 'base-select' %> +
+ <% end %>
<%= f.button button_title(title: @base_template ? 'Submit' : 'Create', disabled_with: 'Creating'), class: 'base-button' %>