Skip to content

Commit

Permalink
small bug fix
Browse files Browse the repository at this point in the history
when we have an array with one item uniq! returns nil
- which was causing an issue in project_media tag creation
– and I think was misleading in fact_check clean_tags
  • Loading branch information
vasconsaurus committed Sep 27, 2024
1 parent e2734b9 commit f9c0cca
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/models/annotations/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def self.create_project_media_tags(project_media_id, tags_json)

if !project_media.nil?
tags = JSON.parse(tags_json)
clean_tags = tags.map { |tag| tag.strip.gsub(/^#/, '') }.uniq!
clean_tags = tags.map { |tag| tag.strip.gsub(/^#/, '') }.uniq
clean_tags.each { |tag| Tag.create! annotated: project_media, tag: tag.strip, skip_check_ability: true }
else
error = StandardError.new("[ProjectMedia] Exception creating project media's tags in background. Project media is nil.")
Expand Down
2 changes: 1 addition & 1 deletion app/models/fact_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def self.get_exported_data(query, team)

def clean_tags
return if self.tags.blank?
self.tags.map! { |tag| tag.strip.gsub(/^#/, '') }.uniq!
self.tags = self.tags.map! { |tag| tag.strip.gsub(/^#/, '') }.uniq
end

private
Expand Down
14 changes: 14 additions & 0 deletions test/models/fact_check_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -603,5 +603,19 @@ def setup
fc = create_fact_check claim_description: cd, tags: ['one', 'one', '#one']

assert_equal 1, fc.tags.count
assert_equal 'one', fc.tags.first
end

test "should add existing tag to a new fact check" do
pm = create_project_media
cd = create_claim_description(project_media: pm)
create_fact_check claim_description: cd, tags: ['one']

pm2 = create_project_media
cd2 = create_claim_description(project_media: pm2)
fc2 = create_fact_check claim_description: cd2, tags: ['#one']

assert_equal 1, fc2.tags.count
assert_equal 'one', fc2.tags.first
end
end
14 changes: 14 additions & 0 deletions test/models/tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,18 @@ def setup

assert_equal 1, pm.reload.annotations('tag').count
end

test ":create_project_media_tags should be able to add an existing tag to a new project media" do
Sidekiq::Testing.fake!

team = create_team
project = create_project team: team
pm = create_project_media project: project
Tag.create_project_media_tags(pm.id, ['one'].to_json)

pm2 = create_project_media project: project
Tag.create_project_media_tags(pm2.id, ['#one'].to_json)

assert_equal 1, pm2.reload.annotations('tag').count
end
end

0 comments on commit f9c0cca

Please sign in to comment.