Skip to content

Commit

Permalink
CV2-5051: Item history updates (#1989)
Browse files Browse the repository at this point in the history
* CV2-5051: log version for ExplainerItem

* CV2-5051: log FactCheck update and add meta for ClaimDescription logs

* CV2-5051: migrate FactCheck logs to a new item

* CV2-5051: fix tests

* CV2-5051: exclude logs related to add/remove fact-checks whem migrate fact-check logs
  • Loading branch information
melsawy authored Aug 15, 2024
1 parent 11faa3b commit 110506e
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 22 deletions.
24 changes: 24 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 @@ -42,6 +45,10 @@ def project_media_was
ProjectMedia.find_by_id(self.project_media_id_before_last_save)
end

def version_metadata(_changes)
{ fact_check: self.fact_check&.title }.to_json
end

private

def set_team
Expand Down Expand Up @@ -89,4 +96,21 @@ def replace_media
old_pm.replace_by(new_pm)
end
end

def migrate_claim_and_fact_check_logs
# Migrate ClaimDescription logs
cd_versions = Version.from_partition(self.team_id).where(item_type: 'ClaimDescription', item_id: self.id)
# Exclude the one related to add/remove based on object_changes field.
cd_versions = cd_versions.reject do |v|
oc = begin JSON.parse(v.object_changes) rescue {} end
oc.length == 1 && oc.keys.include?('project_media_id')
end
Version.from_partition(self.team_id).where(id: cd_versions.map(&:id)).update_all(associated_id: self.project_media_id)
fc_id = self.fact_check&.id
unless fc_id.nil?
# Migrate FactCheck logs and exclude create event
Version.from_partition(self.team_id).where(item_type: 'FactCheck', item_id: fc_id)
.where.not(event: 'create').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: 1 addition & 1 deletion app/models/explainer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Explainer < ApplicationRecord
belongs_to :team

has_annotations
has_many :explainer_items
has_many :explainer_items, dependent: :destroy
has_many :project_medias, through: :explainer_items

before_validation :set_team
Expand Down
6 changes: 6 additions & 0 deletions app/models/explainer_item.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Join model
class ExplainerItem < ApplicationRecord
has_paper_trail on: [:create, :destroy], ignore: [:updated_at, :created_at], if: proc { |_x| User.current.present? }, versions: { class_name: 'Version' }

belongs_to :explainer
belongs_to :project_media

validates_presence_of :explainer, :project_media
validate :same_team

def version_metadata(_changes)
{ explainer_title: self.explainer.title }.to_json
end

private

def same_team
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
11 changes: 9 additions & 2 deletions app/models/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,16 @@ def get_associated
when 'create_assignment', 'destroy_assignment'
self.get_associated_from_assignment
when 'create_claimdescription', 'update_claimdescription'
['ProjectMedia', self.item.project_media_id]
when 'create_factcheck'
pm_id = self.item.project_media_id
if pm_id.nil?
changes = self.get_object_changes
pm_id = changes['project_media_id'][0] if changes.has_key?('project_media_id') && changes['project_media_id'][1].nil?
end
['ProjectMedia', pm_id]
when 'create_factcheck', 'update_factcheck'
['ProjectMedia', self.item.claim_description.project_media_id]
when 'create_explaineritem', 'destroy_explaineritem'
['ProjectMedia', self.item.project_media_id]
end
end

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 2, 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 4, v_count
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions test/models/explainer_item_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ def teardown
assert_not ExplainerItem.new(project_media: pm2, explainer: e1).valid?
end

test "should have versions" do
with_versioning do
u = create_user
t = create_team
create_team_user team: t, user: u, role: 'admin'
e = create_explainer team: t
pm = create_project_media team: t
with_current_user_and_team(u, t) do
assert_difference 'PaperTrail::Version.count', 1 do
pm.explainers << e
end
ei = ExplainerItem.where(project_media_id: pm.id, explainer_id: e.id).last
assert_equal 1, ei.versions.count
assert_difference 'PaperTrail::Version.count', 1 do
ei.destroy
end
end
end
end

test "should have permission to create explainer item" do
t1 = create_team
u1 = create_user
Expand Down
15 changes: 0 additions & 15 deletions test/models/explainer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,6 @@ def setup
end
end

test "should have versions" do
with_versioning do
u = create_user
t = create_team
create_team_user team: t, user: u, role: 'admin'
with_current_user_and_team(u, t) do
ex = nil
assert_difference 'PaperTrail::Version.count', 1 do
ex = create_explainer user: u, team: t
end
assert_equal 1, ex.versions.count
end
end
end

test "should not create explainer without user or team" do
assert_no_difference 'Explainer.count' do
assert_raises ActiveRecord::RecordInvalid do
Expand Down

0 comments on commit 110506e

Please sign in to comment.