diff --git a/app/models/concerns/project_media_creators.rb b/app/models/concerns/project_media_creators.rb index 0fb58650b..5c9898b7e 100644 --- a/app/models/concerns/project_media_creators.rb +++ b/app/models/concerns/project_media_creators.rb @@ -264,7 +264,7 @@ def create_claim_description_and_fact_check def create_tags if self.set_tags.is_a?(Array) - GenericWorker.perform_in(1.second, 'ProjectMedia', 'create_tags_in_background', project_media_id: self.id, tags_json: self.set_tags.to_json) + GenericWorker.perform_in(1.second, 'ProjectMedia', 'create_tags_in_background', project_media_id: self.id, tags_json: self.set_tags.to_json, user_id: self.user_id) end end end diff --git a/app/models/project_media.rb b/app/models/project_media.rb index a963cf438..24ef2186a 100644 --- a/app/models/project_media.rb +++ b/app/models/project_media.rb @@ -488,6 +488,7 @@ def add_nested_objects(ms) def self.create_tags_in_background(**params) params = params.with_indifferent_access project_media = ProjectMedia.find_by_id(params['project_media_id']) + unless project_media.nil? tags = JSON.parse(params['tags_json']) tags.each { |tag| Tag.create! annotated: project_media, tag: tag.strip, skip_check_ability: true } diff --git a/app/workers/generic_worker.rb b/app/workers/generic_worker.rb index c5f7c78eb..a1d1890a8 100644 --- a/app/workers/generic_worker.rb +++ b/app/workers/generic_worker.rb @@ -4,9 +4,14 @@ class GenericWorker def perform(klass_name, klass_method, *method_args) klass = klass_name.constantize - options = method_args.extract_options! + options = method_args.extract_options!.with_indifferent_access if options + if options.key?(:user_id) + user_id = options.delete(:user_id) + User.current = User.find_by_id(user_id) + end klass.public_send(klass_method, **options) + User.current = nil else klass.public_send(klass_method) end diff --git a/test/models/project_media_8_test.rb b/test/models/project_media_8_test.rb index 8dc8dfbea..c33e1060e 100644 --- a/test/models/project_media_8_test.rb +++ b/test/models/project_media_8_test.rb @@ -30,6 +30,7 @@ def setup t = create_team p = create_project team: t + assert_nothing_raised do create_project_media project: p, tags: ['one'] end @@ -40,6 +41,7 @@ def setup t = create_team p = create_project team: t + assert_nothing_raised do create_project_media project: p, tags: ['one', 'two', 'three'] end diff --git a/test/workers/generic_worker_test.rb b/test/workers/generic_worker_test.rb index 94faa232d..ad9ce8a21 100644 --- a/test/workers/generic_worker_test.rb +++ b/test/workers/generic_worker_test.rb @@ -14,7 +14,7 @@ def setup pm = create_project_media project: p assert_nothing_raised do - GenericWorker.perform_async('Tag', 'create!', annotated_type: 'ProjectMedia' , annotated_id: pm.id, tag: 'test_tag', skip_check_ability: true) + GenericWorker.perform_async('Tag', 'create!', annotated_type: 'ProjectMedia' , annotated_id: pm.id, tag: 'test_tag', skip_check_ability: true, user_id: pm.user_id) end end