diff --git a/app/controllers/competitions/competition_modules_controller.rb b/app/controllers/competitions/competition_modules_controller.rb index da9873c43..c4692af70 100644 --- a/app/controllers/competitions/competition_modules_controller.rb +++ b/app/controllers/competitions/competition_modules_controller.rb @@ -15,7 +15,17 @@ class Competitions::CompetitionModulesController < Competitions::BaseController md = current_module.competition_module_md_content || current_module.build_competition_module_md_content md.name = params[:md_name] md.content = params[:md_content] - md.save! + + ActiveRecord::Base.transaction do + md.save! + + attachment_ids = Array.wrap(params[:attachment_ids]).map(&:to_i) + old_attachment_ids = md.attachments.pluck(:id) + + destroy_ids = old_attachment_ids - attachment_ids + md.attachments.where(id: destroy_ids).delete_all + Attachment.where(id: attachment_ids - old_attachment_ids).update_all(container: md) + end render_ok end diff --git a/app/models/searchable/dependents/user.rb b/app/models/searchable/dependents/user.rb index 103131ed6..f6dcaa430 100644 --- a/app/models/searchable/dependents/user.rb +++ b/app/models/searchable/dependents/user.rb @@ -10,13 +10,13 @@ module Searchable::Dependents::User def check_searchable_dependents if firstname_previously_changed? || lastname_previously_changed? || user_extension.school_id_previously_changed? # reindex shixun - created_shixuns.each{ |shixun| shixun.reindex } + created_shixuns.each(&:reindex) # reindex course manage_courses.each(&:reindex) # reindex subject - created_subjects.each { |subject| subject.reindex } + created_subjects.each(&:reindex) end end end \ No newline at end of file diff --git a/app/views/competitions/competition_modules/show.json.jbuilder b/app/views/competitions/competition_modules/show.json.jbuilder index d47742cf0..69cdcc544 100644 --- a/app/views/competitions/competition_modules/show.json.jbuilder +++ b/app/views/competitions/competition_modules/show.json.jbuilder @@ -5,4 +5,7 @@ if md.present? json.md_name md.name json.md_content md.content json.created_at md.created_at.strftime('%Y-%m-%d %H:%M:%S') + json.attachments do + json.array! md.attachments, partial: 'attachments/attachment_simple', as: :attachment + end end \ No newline at end of file