Skip to content

Commit

Permalink
Pass User.current to the GenericWorker
Browse files Browse the repository at this point in the history
We log the revisions for tag creation in case User.current exists
otherwise no logs for this action
  • Loading branch information
vasconsaurus committed Sep 5, 2024
1 parent 5e43fb6 commit 61f78c1
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/models/concerns/project_media_creators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions app/models/project_media.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
7 changes: 6 additions & 1 deletion app/workers/generic_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/models/project_media_8_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/workers/generic_worker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 61f78c1

Please sign in to comment.