Skip to content

Commit

Permalink
Duplicate certificate templates
Browse files Browse the repository at this point in the history
  • Loading branch information
mpugach committed Dec 31, 2023
1 parent b43835c commit 358a975
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 1 deletion.
36 changes: 36 additions & 0 deletions app/controllers/copy_certificate_templates_controller.rb
@@ -0,0 +1,36 @@
class CopyCertificateTemplatesController < HtmlRespondableController
def create
orig_template = CertificateTemplate.find(params[:certificate_template_id])
new_template = CertificateTemplate.new(orig_template.attributes.slice('title', 'institution_id', 'program_type'))

authorize new_template

new_template.title = "Copy of #{new_template.title}"
new_template.file = orig_template.file

ActiveRecord::Base.transaction do
new_template.save

orig_template.certificate_template_entries.each do |entry|
duplicate_object(entry, new_template.id)
end

orig_template.certificate_template_images.each do |image|
duplicate_object(image, new_template.id)
end
end

respond_with(
new_template,
location: -> { edit_certificate_template_path(new_template) }
)
end

private

def duplicate_object(related_object, new_template_id)
dup = related_object.dup
dup.certificate_template_id = new_template_id
dup.save
end
end
4 changes: 4 additions & 0 deletions app/helpers/concerns/link_tos.rb
Expand Up @@ -13,6 +13,10 @@ def link_to_show_person_or_name(person, short: false)
end
end

def link_to_copy(condition, path)
link_to_action(condition, path, 'success', t('links.copy'), 'duplicate', method: :post)
end

def link_to_new(condition, path)
link_to_action(condition, path, 'success', t('links.new'), 'file')
end
Expand Down
2 changes: 2 additions & 0 deletions app/views/certificate_templates/_nav_links.html.haml
Expand Up @@ -3,6 +3,8 @@

= link_to_new policy(certificate_template).new?, new_certificate_template_path

= link_to_copy current_person.can_act?('certificate_template:new'), certificate_template_copy_path(certificate_template)

= link_to_index policy(CertificateTemplate).index?, certificate_templates_path

= link_to_destroy policy(certificate_template).destroy?, certificate_template
2 changes: 2 additions & 0 deletions app/views/certificate_templates/index.html.haml
Expand Up @@ -31,6 +31,8 @@

- if policy(certificate_template).tap { |p| break p.edit? || p.destroy? }
%td.col-xs-2.col-sm-4.col-md-3.text-right
= link_to_copy current_person.can_act?('certificate_template:new'),
certificate_template_copy_path(certificate_template)
= link_to_edit policy(certificate_template).edit?,
edit_certificate_template_path(certificate_template)
= link_to_destroy policy(certificate_template).destroy?, certificate_template
1 change: 1 addition & 0 deletions config/locales/general.ru.yml
Expand Up @@ -45,6 +45,7 @@ ru:
links:
apply_to_program: 'Подать заявление'
back: 'Назад'
copy: 'Копировать'
crop_photo: 'Обрезать фотографию'
delete: 'Удалить'
edit_profile: 'Редактировать профиль'
Expand Down
1 change: 1 addition & 0 deletions config/locales/general.uk.yml
Expand Up @@ -45,6 +45,7 @@ uk:
links:
apply_to_program: 'Подати заяву'
back: 'Назад'
copy: 'Копівати'
crop_photo: 'Обрізати фотографію'
delete: 'Видалити'
edit_profile: 'Редагувати профіль'
Expand Down
5 changes: 4 additions & 1 deletion config/routes.rb
Expand Up @@ -33,10 +33,13 @@
resources :study_applications, only: %i[create destroy]
resources :answers, only: %i[update edit]
resources :certificate_imports, only: %i[new create]
resources :certificate_templates, only: %i[new create edit update index destroy]
resources :certificate_template_fonts, only: %i[new create edit update index destroy]
resources :signatures, only: %i[new create edit update index destroy]

resources :certificate_templates, only: %i[new create edit update index destroy] do
resource :copy_certificate_templates, only: %i[create], as: :copy
end

resources :people do
get '/journal', action: :journal

Expand Down

0 comments on commit 358a975

Please sign in to comment.