diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index cd8d228c..191f1dc8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -17,6 +17,7 @@ class ApplicationController < ActionController::Base helper_method :button_title, :current_account, + :true_ability, :form_link_host, :svg_icon @@ -102,6 +103,10 @@ class ApplicationController < ActionController::Base current_user&.account end + def true_ability + @true_ability ||= Ability.new(true_user) + end + def maybe_redirect_to_setup redirect_to setup_index_path unless User.exists? end diff --git a/app/controllers/templates_clone_controller.rb b/app/controllers/templates_clone_controller.rb new file mode 100644 index 00000000..802a97e0 --- /dev/null +++ b/app/controllers/templates_clone_controller.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +class TemplatesCloneController < ApplicationController + load_and_authorize_resource :template, instance_name: :base_template + + def new + authorize!(:create, Template) + + @template = Template.new(name: "#{@base_template.name} (#{I18n.t('clone')})") + end + + def create + ActiveRecord::Associations::Preloader.new( + records: [@base_template], + associations: [schema_documents: :preview_images_attachments] + ).call + + @template = Templates::Clone.call(@base_template, author: current_user, + name: params.dig(:template, :name), + folder_name: params[:folder_name]) + + authorize!(:create, @template) + + if params[:account_id].present? && true_ability.authorize!(:manage, Account.find(params[:account_id])) + @template.account_id = params[:account_id] + @template.author = true_user if true_user.account_id == @template.account_id + @template.folder = @template.account.default_template_folder if @template.account_id != current_account.id + else + @template.account = current_account + end + + Templates.maybe_assign_access(@template) + + if @template.save + Templates::CloneAttachments.call(template: @template, original_template: @base_template) + + SearchEntries.enqueue_reindex(@template) + + WebhookUrls.enqueue_events(@template, 'template.created') + + maybe_redirect_to_template(@template) + else + render turbo_stream: turbo_stream.replace(:modal, partial: 'templates_clone/form'), status: :unprocessable_content + end + end + + 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: I18n.t('template_has_been_cloned')) + end + end +end diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index c96360d9..39b45044 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -3,8 +3,6 @@ class TemplatesController < ApplicationController load_and_authorize_resource :template - before_action :load_base_template, only: %i[new create] - def show submissions = @template.submissions.accessible_by(current_ability) submissions = submissions.active if @template.archived_at.blank? @@ -26,9 +24,7 @@ class TemplatesController < ApplicationController redirect_to root_path end - def new - @template.name = "#{@base_template.name} (#{I18n.t('clone')})" if @base_template - end + def new; end def edit ActiveRecord::Associations::Preloader.new( @@ -48,37 +44,18 @@ class TemplatesController < ApplicationController end def create - if @base_template - ActiveRecord::Associations::Preloader.new( - records: [@base_template], - associations: [schema_documents: :preview_images_attachments] - ).call - - @template = Templates::Clone.call(@base_template, author: current_user, - name: params.dig(:template, :name), - folder_name: params[:folder_name]) - else - @template.author = current_user - @template.folder = TemplateFolders.find_or_create_by_name(current_user, params[:folder_name]) - end - - if params[:account_id].present? && authorized_clone_account_id?(params[:account_id]) - @template.account_id = params[:account_id] - @template.folder = @template.account.default_template_folder if @template.account_id != current_account.id - else - @template.account = current_account - end + @template.author = current_user + @template.folder = TemplateFolders.find_or_create_by_name(current_user, params[:folder_name]) + @template.account = current_account Templates.maybe_assign_access(@template) if @template.save - Templates::CloneAttachments.call(template: @template, original_template: @base_template) if @base_template - SearchEntries.enqueue_reindex(@template) WebhookUrls.enqueue_events(@template, 'template.created') - maybe_redirect_to_template(@template) + redirect_to(edit_template_path(@template)) else render turbo_stream: turbo_stream.replace(:modal, template: 'templates/new'), status: :unprocessable_content end @@ -132,23 +109,4 @@ class TemplatesController < ApplicationController areas: [%i[x y w h cell_w attachment_uuid option_uuid page]] }]] } ) end - - def authorized_clone_account_id?(account_id) - true_user.account_id.to_s == account_id.to_s || - true_user.account.linked_accounts.accessible_by(current_ability).exists?(id: account_id) - end - - 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: I18n.t('template_has_been_cloned')) - end - end - - def load_base_template - return if params[:base_template_id].blank? - - @base_template = Template.accessible_by(current_ability).find_by(id: params[:base_template_id]) - end end diff --git a/app/views/templates/_file_form.html.erb b/app/views/templates/_file_form.html.erb index d0c59485..eaedc346 100644 --- a/app/views/templates/_file_form.html.erb +++ b/app/views/templates/_file_form.html.erb @@ -1,12 +1,4 @@ <%= form_for @template, data: { turbo_frame: :_top }, html: { autocomplete: :off } do |f| %> - <% if @base_template %> - <%= hidden_field_tag :base_template_id, @base_template.id %> - <% end %> - <% if @base_template && (can?(:manage, :tenants) || true_user != current_user) && true_user.account.linked_accounts.active.accessible_by(current_ability).exists? %> -