Skip to content

Commit

Permalink
CV2-5051: migrate FactCheck logs to a new item
Browse files Browse the repository at this point in the history
  • Loading branch information
melsawy committed Aug 13, 2024
1 parent 5e932d9 commit f951e17
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
11 changes: 11 additions & 0 deletions app/models/claim_description.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class ClaimDescription < ApplicationRecord
include Article

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

before_validation :set_team, on: :create
belongs_to :project_media, optional: true
belongs_to :team
Expand All @@ -13,6 +15,7 @@ class ClaimDescription < ApplicationRecord
after_commit :update_fact_check, on: [:update]
after_update :update_report_status
after_update :replace_media
after_update :migrate_claim_and_fact_check_logs, if: proc { |cd| cd.saved_change_to_project_media_id? && !cd.project_media_id.nil? }

# To avoid GraphQL conflict with name `context`
alias_attribute :claim_context, :context
Expand Down Expand Up @@ -93,4 +96,12 @@ def replace_media
old_pm.replace_by(new_pm)
end
end

def migrate_claim_and_fact_check_logs
Version.from_partition(self.team_id).where(item_type: 'ClaimDescription', item_id: self.id).update_all(associated_id: self.project_media_id)
fc_id = self.fact_check&.id
unless fc_id.nil?
Version.from_partition(self.team_id).where(item_type: 'FactCheck', item_id: fc_id).update_all(associated_id: self.project_media_id)
end
end
end
2 changes: 0 additions & 2 deletions app/models/concerns/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ module Article
included do
include CheckElasticSearch

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

belongs_to :user

before_validation :set_user
Expand Down
2 changes: 2 additions & 0 deletions app/models/fact_check.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class FactCheck < ApplicationRecord
include Article

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

enum report_status: { unpublished: 0, published: 1, paused: 2 }

attr_accessor :skip_report_update, :publish_report
Expand Down
28 changes: 26 additions & 2 deletions test/models/claim_description_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,36 @@ def setup
t = create_team
create_team_user team: t, user: u, role: 'admin'
pm = create_project_media team: t
pm2 = create_project_media team: t
with_current_user_and_team(u, t) do
cd = nil
assert_difference 'PaperTrail::Version.count', 1 do
fc = nil
assert_difference 'PaperTrail::Version.count', 2 do
cd = create_claim_description project_media: pm, user: u
fc = create_fact_check claim_description: cd
end
assert_equal 1, cd.versions.count
cd.description = 'update description'
cd.save!
fc.title = 'update title'
fc.save!
# Remove FactCheck
cd.project_media_id = nil
cd.save!
assert_equal 3, cd.versions.count
assert_equal 2, fc.versions.count
v_count = Version.from_partition(t.id).where(associated_type: 'ProjectMedia', associated_id: pm.id, item_type: ['ClaimDescription', 'FactCheck']).count
assert_equal 5, v_count
# Add existing FactCheck to another media
cd.project_media_id = pm2.id
cd.save!
assert_equal 4, cd.versions.count
assert_equal 2, fc.versions.count
# Old item logs
v_count = Version.from_partition(t.id).where(associated_type: 'ProjectMedia', associated_id: pm.id, item_type: ['ClaimDescription', 'FactCheck']).count
assert_equal 0, v_count
# New item logs
v_count = Version.from_partition(t.id).where(associated_type: 'ProjectMedia', associated_id: pm2.id, item_type: ['ClaimDescription', 'FactCheck']).count
assert_equal 6, v_count
end
end
end
Expand Down
1 change: 0 additions & 1 deletion test/models/explainer_item_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def teardown
pm.explainers << e
end
ei = ExplainerItem.where(project_media_id: pm.id, explainer_id: e.id).last
assert_equal u.id, ei.user_id
assert_equal 1, ei.versions.count
assert_difference 'PaperTrail::Version.count', 1 do
ei.destroy
Expand Down

0 comments on commit f951e17

Please sign in to comment.