Skip to content

Commit

Permalink
extract tag cleaning logic to a concern
Browse files Browse the repository at this point in the history
  • Loading branch information
vasconsaurus committed Sep 30, 2024
1 parent 73a4266 commit fdf6970
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/models/annotations/tag.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Tag < ApplicationRecord
include AnnotationBase
extend TagHelpers

# "tag" is a reference to a TagText object
field :tag, GraphQL::Types::Int, presence: true
Expand Down Expand Up @@ -96,8 +97,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.each { |tag| Tag.create! annotated: project_media, tag: tag.strip, skip_check_ability: true }
clean_tags(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.")
CheckSentry.notify(error, project_media_id: project_media_id)
Expand Down
9 changes: 9 additions & 0 deletions app/models/concerns/tag_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'active_support/concern'

module TagHelpers
extend ActiveSupport::Concern

def clean_tags(tags)
tags.map { |tag| tag.strip.gsub(/^#/, '') }.uniq
end
end
7 changes: 4 additions & 3 deletions app/models/fact_check.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class FactCheck < ApplicationRecord
include Article
include TagHelpers

has_paper_trail on: [:create, :update], ignore: [:updated_at, :created_at, :rating, :report_status], if: proc { |_x| User.current.present? }, versions: { class_name: 'Version' }

Expand All @@ -18,7 +19,7 @@ class FactCheck < ApplicationRecord
validates_format_of :url, with: URI.regexp, allow_blank: true, allow_nil: true
validate :language_in_allowed_values, :title_or_summary_exists, :rating_in_allowed_values

before_save :clean_tags
before_save :clean_fact_check_tags
after_save :update_report, unless: proc { |fc| fc.skip_report_update || !DynamicAnnotation::AnnotationType.where(annotation_type: 'report_design').exists? || fc.project_media.blank? }
after_save :update_item_status, if: proc { |fc| fc.saved_change_to_rating? }
after_update :detach_claim_if_trashed
Expand Down Expand Up @@ -57,9 +58,9 @@ def self.get_exported_data(query, team)
data
end

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

private
Expand Down

0 comments on commit fdf6970

Please sign in to comment.