diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 312f71cd..0f5ab590 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -52,7 +52,13 @@ class DashboardController < ApplicationController def filter_templates(templates) rel = templates.active.preload(:author).order(id: :desc) - rel = rel.where(folder_id: current_account.default_template_folder.id) if params[:q].blank? + + if params[:q].blank? + shared_template_ids = + TemplateSharing.where(account_id: [current_account.id, TemplateSharing::ALL_ID]).select(:template_id) + + rel = rel.where(folder_id: current_account.default_template_folder.id).or(rel.where(id: shared_template_ids)) + end Templates.search(rel, params[:q]) end diff --git a/app/controllers/submissions_controller.rb b/app/controllers/submissions_controller.rb index 9c6d6026..fcc7b90b 100644 --- a/app/controllers/submissions_controller.rb +++ b/app/controllers/submissions_controller.rb @@ -75,6 +75,6 @@ class SubmissionsController < ApplicationController end def load_template - @template = current_account.templates.find(params[:template_id]) + @template = Template.accessible_by(current_ability).find(params[:template_id]) end end diff --git a/app/controllers/template_sharings_testing_controller.rb b/app/controllers/template_sharings_testing_controller.rb new file mode 100644 index 00000000..c8f64696 --- /dev/null +++ b/app/controllers/template_sharings_testing_controller.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class TemplateSharingsTestingController < ApplicationController + load_and_authorize_resource :template, parent: true + + before_action do + authorize!(:manage, TemplateSharing.new(template: @template)) + end + + def create + testing_account = Accounts.find_or_create_testing_user(true_user.account).account + + if params[:value] == '1' + TemplateSharing.create!(ability: :manage, account: testing_account, template: @template) + else + TemplateSharing.find_by(template: @template, account: testing_account)&.destroy! + end + + head :ok + end +end diff --git a/app/controllers/templates_code_modal_controller.rb b/app/controllers/templates_code_modal_controller.rb new file mode 100644 index 00000000..bc2549ab --- /dev/null +++ b/app/controllers/templates_code_modal_controller.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class TemplatesCodeModalController < ApplicationController + load_and_authorize_resource :template + + def show; end +end diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index 05cb2bac..0123b248 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -6,7 +6,7 @@ class TemplatesController < ApplicationController before_action :load_base_template, only: %i[new create] def show - submissions = @template.submissions + submissions = @template.submissions.accessible_by(current_ability) submissions = submissions.active if @template.archived_at.blank? submissions = Submissions.search(submissions, params[:q]) @@ -47,11 +47,12 @@ class TemplatesController < ApplicationController name: params.dig(:template, :name), folder_name: params[:folder_name]) else - @template.account = current_account @template.author = current_user @template.folder = TemplateFolders.find_or_create_by_name(current_user, params[:folder_name]) end + @template.account = current_account + if @template.save Templates::CloneAttachments.call(template: @template, original_template: @base_template) if @base_template @@ -87,6 +88,6 @@ class TemplatesController < ApplicationController def load_base_template return if params[:base_template_id].blank? - @base_template = current_account.templates.find_by(id: params[:base_template_id]) + @base_template = Template.accessible_by(current_ability).find_by(id: params[:base_template_id]) end end diff --git a/app/controllers/templates_preview_controller.rb b/app/controllers/templates_preview_controller.rb new file mode 100644 index 00000000..1cac478a --- /dev/null +++ b/app/controllers/templates_preview_controller.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +class TemplatesPreviewController < ApplicationController + load_and_authorize_resource :template + + def show + ActiveRecord::Associations::Preloader.new( + records: [@template], + associations: [schema_documents: { preview_images_attachments: :blob }] + ).call + + @template_data = + @template.as_json.merge( + documents: @template.schema_documents.as_json( + methods: [:metadata], + include: { preview_images: { methods: %i[url metadata filename] } } + ) + ).to_json + + render :show, layout: 'plain' + end +end diff --git a/app/controllers/testing_accounts_controller.rb b/app/controllers/testing_accounts_controller.rb index 1bce910e..6c6cf3d1 100644 --- a/app/controllers/testing_accounts_controller.rb +++ b/app/controllers/testing_accounts_controller.rb @@ -7,7 +7,7 @@ class TestingAccountsController < ApplicationController authorize!(:manage, current_account) authorize!(:manage, current_user) - impersonate_user(Accounts.find_or_create_testing_user(current_account)) + impersonate_user(Accounts.find_or_create_testing_user(true_user.account)) redirect_back(fallback_location: root_path) end diff --git a/app/javascript/application.js b/app/javascript/application.js index c5ae487d..dc2df889 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -88,6 +88,7 @@ window.customElements.define('template-builder', class extends HTMLElement { backgroundColor: '#faf7f5', withPhone: this.dataset.withPhone === 'true', withLogo: this.dataset.withLogo !== 'false', + editable: this.dataset.editable !== 'false', withPayment: this.dataset.withPayment === 'true', currencies: (this.dataset.currencies || '').split(',').filter(Boolean), acceptFileTypes: this.dataset.acceptFileTypes, diff --git a/app/javascript/template_builder/builder.vue b/app/javascript/template_builder/builder.vue index 532f53a4..b0185da4 100644 --- a/app/javascript/template_builder/builder.vue +++ b/app/javascript/template_builder/builder.vue @@ -62,6 +62,7 @@ + + + diff --git a/app/models/template.rb b/app/models/template.rb index 7b3d7f1b..2e5be5a1 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -57,6 +57,7 @@ class Template < ApplicationRecord class_name: 'ActiveStorage::Attachment', dependent: :destroy, as: :record, inverse_of: :record has_many :submissions, dependent: :destroy + has_many :template_sharings, dependent: :destroy scope :active, -> { where(archived_at: nil) } scope :archived, -> { where.not(archived_at: nil) } diff --git a/app/models/template_sharing.rb b/app/models/template_sharing.rb new file mode 100644 index 00000000..b520fd2c --- /dev/null +++ b/app/models/template_sharing.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: template_sharings +# +# id :bigint not null, primary key +# ability :string not null +# created_at :datetime not null +# updated_at :datetime not null +# account_id :bigint not null +# template_id :bigint not null +# +# Indexes +# +# index_template_sharings_on_account_id_and_template_id (account_id,template_id) UNIQUE +# index_template_sharings_on_template_id (template_id) +# +# Foreign Keys +# +# fk_rails_... (template_id => templates.id) +# +class TemplateSharing < ApplicationRecord + ALL_ID = -1 + + belongs_to :template + belongs_to :account, optional: true +end diff --git a/app/views/icons/_code.html.erb b/app/views/icons/_code.html.erb new file mode 100644 index 00000000..c56b13ff --- /dev/null +++ b/app/views/icons/_code.html.erb @@ -0,0 +1,6 @@ + + + + + + diff --git a/app/views/submissions/_link_form.html.erb b/app/views/submissions/_link_form.html.erb index 01221296..47c2db7b 100644 --- a/app/views/submissions/_link_form.html.erb +++ b/app/views/submissions/_link_form.html.erb @@ -6,181 +6,7 @@
Or embed on your website
- - - -
-
- - <%= link_to 'Learn More', console_redirect_index_path(redir: "#{Docuseal::CONSOLE_URL}/embedding/form"), target: '_blank', data: { turbo: false }, class: 'btn btn-ghost text-gray-100 flex', rel: 'noopener' %> - - - - - -
<script src="<%= Docuseal::CDN_URL %>/js/form.js"></script>
-
-<docuseal-form data-src="<%= start_form_url(slug: template.slug) %>"></docuseal-form>
-
-
- -
- - + <%= render 'templates/embedding', template: %>
Close
diff --git a/app/views/templates/_embedding.html.erb b/app/views/templates/_embedding.html.erb new file mode 100644 index 00000000..af6df88c --- /dev/null +++ b/app/views/templates/_embedding.html.erb @@ -0,0 +1,175 @@ + + + +
+
+ + <%= link_to 'Learn More', console_redirect_index_path(redir: "#{Docuseal::CONSOLE_URL}/embedding/form"), target: '_blank', data: { turbo: false }, class: 'btn btn-ghost text-gray-100 flex', rel: 'noopener' %> + + + + + +
<script src="<%= Docuseal::CDN_URL %>/js/form.js"></script>
+
+<docuseal-form data-src="<%= start_form_url(slug: template.slug) %>"></docuseal-form>
+
+
+ +
+ + diff --git a/app/views/templates/_template.html.erb b/app/views/templates/_template.html.erb index 67905060..18543244 100644 --- a/app/views/templates/_template.html.erb +++ b/app/views/templates/_template.html.erb @@ -7,6 +7,9 @@

<%= svg_icon('user', class: 'w-4 h-4') %> <%= template.author.full_name.presence || template.author.email.to_s.sub(/\+\w+@/, '@') %> + <% if template.account_id != current_account.id %> + shared + <% end %>

@@ -24,7 +27,7 @@