2023-05-31 23:19:20 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
class TemplatesController < ApplicationController
|
2023-09-17 00:45:57 +03:00
|
|
|
load_and_authorize_resource :template
|
|
|
|
|
|
2023-05-31 23:19:20 +03:00
|
|
|
def show
|
2024-02-03 22:37:17 +02:00
|
|
|
submissions = @template.submissions.accessible_by(current_ability)
|
2023-12-30 00:37:03 +02:00
|
|
|
submissions = submissions.active if @template.archived_at.blank?
|
2025-06-05 13:16:55 +03:00
|
|
|
submissions = Submissions.search(current_user, submissions, params[:q], search_values: true)
|
2024-12-13 19:45:37 +02:00
|
|
|
submissions = Submissions::Filter.call(submissions, current_user, params.except(:status))
|
2023-06-28 00:22:34 +03:00
|
|
|
|
2023-10-19 20:19:52 +03:00
|
|
|
@base_submissions = submissions
|
|
|
|
|
|
2024-12-13 19:45:37 +02:00
|
|
|
submissions = Submissions::Filter.filter_by_status(submissions, params)
|
2024-11-20 22:49:06 +02:00
|
|
|
|
2024-11-23 01:45:10 +02:00
|
|
|
submissions = if params[:completed_at_from].present? || params[:completed_at_to].present?
|
|
|
|
|
submissions.order(Submitter.arel_table[:completed_at].maximum.desc)
|
|
|
|
|
else
|
|
|
|
|
submissions.order(id: :desc)
|
|
|
|
|
end
|
2023-10-19 20:19:52 +03:00
|
|
|
|
2025-05-28 22:48:15 +03:00
|
|
|
@pagy, @submissions = pagy_auto(submissions.preload(:template_accesses, submitters: :start_form_submission_events))
|
2023-06-29 02:04:17 +03:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
|
redirect_to root_path
|
2023-05-31 23:19:20 +03:00
|
|
|
end
|
|
|
|
|
|
2026-01-07 19:30:18 +02:00
|
|
|
def new; end
|
2023-05-31 23:19:20 +03:00
|
|
|
|
2023-06-28 00:22:34 +03:00
|
|
|
def edit
|
2023-09-17 00:45:57 +03:00
|
|
|
ActiveRecord::Associations::Preloader.new(
|
|
|
|
|
records: [@template],
|
2024-07-16 01:40:38 +03:00
|
|
|
associations: [schema_documents: [:blob, { preview_images_attachments: :blob }]]
|
2023-09-17 00:45:57 +03:00
|
|
|
).call
|
2023-06-18 16:26:51 +00:00
|
|
|
|
2023-10-22 01:54:20 +03:00
|
|
|
@template_data =
|
|
|
|
|
@template.as_json.merge(
|
|
|
|
|
documents: @template.schema_documents.as_json(
|
2026-02-12 09:22:08 +02:00
|
|
|
methods: %i[metadata signed_key],
|
2023-10-22 01:54:20 +03:00
|
|
|
include: { preview_images: { methods: %i[url metadata filename] } }
|
|
|
|
|
)
|
|
|
|
|
).to_json
|
|
|
|
|
|
2023-06-28 00:22:34 +03:00
|
|
|
render :edit, layout: 'plain'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create
|
2026-01-07 19:30:18 +02:00
|
|
|
@template.author = current_user
|
|
|
|
|
@template.folder = TemplateFolders.find_or_create_by_name(current_user, params[:folder_name])
|
|
|
|
|
@template.account = current_account
|
2024-02-03 22:37:17 +02:00
|
|
|
|
2025-08-26 12:29:51 +03:00
|
|
|
Templates.maybe_assign_access(@template)
|
|
|
|
|
|
2023-05-31 23:19:20 +03:00
|
|
|
if @template.save
|
2025-06-05 13:16:55 +03:00
|
|
|
SearchEntries.enqueue_reindex(@template)
|
|
|
|
|
|
2025-07-04 21:11:08 +03:00
|
|
|
WebhookUrls.enqueue_events(@template, 'template.created')
|
2024-05-12 11:37:33 +03:00
|
|
|
|
2026-01-07 19:30:18 +02:00
|
|
|
redirect_to(edit_template_path(@template))
|
2023-05-31 23:19:20 +03:00
|
|
|
else
|
2025-08-14 09:25:35 +03:00
|
|
|
render turbo_stream: turbo_stream.replace(:modal, template: 'templates/new'), status: :unprocessable_content
|
2023-05-31 23:19:20 +03:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-03-23 23:16:40 +02:00
|
|
|
def update
|
2025-06-05 13:16:55 +03:00
|
|
|
@template.assign_attributes(template_params)
|
|
|
|
|
|
|
|
|
|
is_name_changed = @template.name_changed?
|
|
|
|
|
|
|
|
|
|
@template.save!
|
|
|
|
|
|
|
|
|
|
SearchEntries.enqueue_reindex(@template) if is_name_changed
|
2024-03-23 23:16:40 +02:00
|
|
|
|
2025-07-04 21:11:08 +03:00
|
|
|
WebhookUrls.enqueue_events(@template, 'template.updated')
|
2024-05-12 11:37:33 +03:00
|
|
|
|
2024-03-23 23:16:40 +02:00
|
|
|
head :ok
|
|
|
|
|
end
|
|
|
|
|
|
2023-05-31 23:19:20 +03:00
|
|
|
def destroy
|
2023-10-29 18:51:58 +02:00
|
|
|
notice =
|
2024-12-13 13:31:54 +02:00
|
|
|
if params[:permanently].in?(['true', true])
|
2023-10-29 18:51:58 +02:00
|
|
|
@template.destroy!
|
2023-05-31 23:19:20 +03:00
|
|
|
|
2024-09-18 19:47:47 +03:00
|
|
|
I18n.t('template_has_been_removed')
|
2023-10-29 18:51:58 +02:00
|
|
|
else
|
2023-12-30 00:37:03 +02:00
|
|
|
@template.update!(archived_at: Time.current)
|
2023-10-29 18:51:58 +02:00
|
|
|
|
2026-03-18 08:46:19 +02:00
|
|
|
WebhookUrls.enqueue_events(@template, 'template.archived')
|
|
|
|
|
|
2024-09-18 19:47:47 +03:00
|
|
|
I18n.t('template_has_been_archived')
|
2023-10-29 18:51:58 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
redirect_back(fallback_location: root_path, notice:)
|
2023-05-31 23:19:20 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2024-03-23 23:16:40 +02:00
|
|
|
def template_params
|
|
|
|
|
params.require(:template).permit(
|
|
|
|
|
:name,
|
2026-02-12 09:22:08 +02:00
|
|
|
{ schema: [[:attachment_uuid, :google_drive_file_id, :name, :dynamic,
|
2025-10-09 16:43:11 +03:00
|
|
|
{ conditions: [%i[field_uuid value action operation]] }]],
|
2026-02-20 12:27:39 +02:00
|
|
|
submitters: [%i[name uuid is_requester linked_to_uuid invite_via_field_uuid
|
|
|
|
|
invite_by_uuid optional_invite_by_uuid email order]],
|
2026-02-12 09:22:08 +02:00
|
|
|
variables_schema: {},
|
2024-03-23 23:16:40 +02:00
|
|
|
fields: [[:uuid, :submitter_uuid, :name, :type,
|
|
|
|
|
:required, :readonly, :default_value,
|
2025-07-27 10:27:12 +03:00
|
|
|
:title, :description, :prefillable,
|
2024-03-23 23:16:40 +02:00
|
|
|
{ preferences: {},
|
2025-08-23 20:11:10 +03:00
|
|
|
default_value: [],
|
2024-11-27 21:47:53 +02:00
|
|
|
conditions: [%i[field_uuid value action operation]],
|
2024-03-23 23:16:40 +02:00
|
|
|
options: [%i[value uuid]],
|
2025-08-20 18:19:23 +03:00
|
|
|
validation: %i[message pattern min max step],
|
2026-02-12 09:22:08 +02:00
|
|
|
areas: [%i[uuid x y w h cell_w attachment_uuid option_uuid page]] }]] }
|
2024-03-23 23:16:40 +02:00
|
|
|
)
|
|
|
|
|
end
|
2023-05-31 23:19:20 +03:00
|
|
|
end
|