Skip to content

Commit

Permalink
CV2-4901 fact check article list not displaying rating (#1952)
Browse files Browse the repository at this point in the history
* CV2-4901: Sync status value with fact-check rating

* CV2-4901: apply PR comment

* CV2-4901: fix tests
  • Loading branch information
melsawy authored Jul 15, 2024
1 parent 7a5d3df commit aa51c00
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
21 changes: 20 additions & 1 deletion app/models/fact_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ class FactCheck < ApplicationRecord
validates_presence_of :claim_description
validates_uniqueness_of :claim_description_id
validates_format_of :url, with: URI.regexp, allow_blank: true, allow_nil: true
validate :language_in_allowed_values, :title_or_summary_exists
validate :language_in_allowed_values, :title_or_summary_exists, :rating_in_allowed_values

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? }

def text_fields
['fact_check_title', 'fact_check_summary']
Expand Down Expand Up @@ -46,6 +47,14 @@ def language_in_allowed_values
errors.add(:language, I18n.t(:"errors.messages.invalid_article_language_value")) unless allowed_languages.include?(self.language)
end

def rating_in_allowed_values
unless self.rating.blank?
team = self.claim_description.team
allowed_statuses = team.verification_statuses('media', nil)['statuses'].collect{|s| s[:id]}
errors.add(:rating, I18n.t(:workflow_status_is_not_valid, status: self.rating, valid: allowed_statuses.join(', '))) unless allowed_statuses.include?(self.rating)
end
end

def title_or_summary_exists
errors.add(:base, I18n.t(:"errors.messages.fact_check_empty_title_and_summary")) if self.title.blank? && self.summary.blank?
end
Expand Down Expand Up @@ -88,6 +97,16 @@ def update_report
reports.save!
end

def update_item_status
pm = self.project_media
s = pm&.last_status_obj
unless s.nil?
s.skip_check_ability = true
s.status = self.rating
s.save!
end
end

def article_elasticsearch_data(action = 'create_or_update')
return if self.disable_es_callbacks || RequestStore.store[:disable_es_callbacks]
data = action == 'destroy' ? {
Expand Down
41 changes: 41 additions & 0 deletions test/models/fact_check_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,41 @@ def setup
assert_not_empty fc.reload.title
end

test "should validate rating" do
assert_no_difference 'FactCheck.count' do
assert_raises ActiveRecord::RecordInvalid do
create_fact_check rating: 'invalid_status'
end
end
assert_difference 'FactCheck.count' do
create_fact_check rating: 'verified'
end
# Validate custom status
t = create_team
value = {
label: 'Status',
default: 'stop',
active: 'done',
statuses: [
{ id: 'stop', label: 'Stopped', completed: '', description: 'Not started yet', style: { backgroundColor: '#a00' } },
{ id: 'done', label: 'Done!', completed: '', description: 'Nothing left to be done here', style: { backgroundColor: '#fc3' } }
]
}
t.send :set_media_verification_statuses, value
t.save!
pm = create_project_media team: t
cd = create_claim_description project_media: pm
assert_no_difference 'FactCheck.count' do
assert_raises ActiveRecord::RecordInvalid do
create_fact_check claim_description: cd, rating: 'invalid_status'
end
end
allowed_statuses = t.reload.verification_statuses('media', nil)['statuses'].collect{|s| s[:id]}
assert_difference 'FactCheck.count' do
create_fact_check claim_description: cd, rating: 'stop'
end
end

test "should create many fact-checks without signature" do
assert_difference 'FactCheck.count', 2 do
create_fact_check signature: nil
Expand Down Expand Up @@ -391,6 +426,7 @@ def setup
fc = cd.fact_check
fc.title = 'Foo Bar'
fc.save!
fc = fc.reload
assert_equal u.id, fc.publisher_id
assert_equal 'published', fc.report_status
assert_equal 'verified', fc.rating
Expand Down Expand Up @@ -428,6 +464,11 @@ def setup
assert_empty t.filtered_fact_checks(filters).map(&:id)
filters = { text: 'Foo' }
assert_equal [fc.id], t.filtered_fact_checks(filters).map(&:id)
# Update item status based on factcheck rating
fc.rating = 'verified'
fc.save!
s = pm.reload.last_verification_status_obj
assert_equal 'verified', s.status
end
end
end
Expand Down

0 comments on commit aa51c00

Please sign in to comment.