From 6d9929d87d5ca21bbf50a11c2b6237e41e22538b Mon Sep 17 00:00:00 2001 From: Caio <117518+caiosba@users.noreply.github.com> Date: Fri, 14 Feb 2025 12:04:16 -0300 Subject: [PATCH] [WIP] Ticket CV2-6158: Finished implementation, only missing rake task --- app/graph/types/explainer_type.rb | 6 ------ app/graph/types/fact_check_type.rb | 6 ------ app/models/concerns/article.rb | 6 ++++++ app/models/explainer.rb | 2 ++ db/migrate/20250214142854_add_author_to_articles.rb | 7 +++++++ db/schema.rb | 10 ++++++++-- test/models/explainer_test.rb | 8 ++++++++ test/models/fact_check_test.rb | 8 ++++++++ 8 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 db/migrate/20250214142854_add_author_to_articles.rb diff --git a/app/graph/types/explainer_type.rb b/app/graph/types/explainer_type.rb index ddcf18d52..5f6220847 100644 --- a/app/graph/types/explainer_type.rb +++ b/app/graph/types/explainer_type.rb @@ -14,11 +14,5 @@ class ExplainerType < DefaultObject field :team, PublicTeamType, null: true field :tags, [GraphQL::Types::String, null: true], null: true field :trashed, GraphQL::Types::Boolean, null: true - field :author, UserType, null: true - - # FIXME: Return actual article creator - def author - object.user - end end diff --git a/app/graph/types/fact_check_type.rb b/app/graph/types/fact_check_type.rb index ab303d0b9..92e265f59 100644 --- a/app/graph/types/fact_check_type.rb +++ b/app/graph/types/fact_check_type.rb @@ -15,11 +15,5 @@ class FactCheckType < DefaultObject field :imported, GraphQL::Types::Boolean, null: true field :report_status, GraphQL::Types::String, null: true field :trashed, GraphQL::Types::Boolean, null: true - field :author, UserType, null: true - - # FIXME: Return actual article creator - def author - object.user - end end diff --git a/app/models/concerns/article.rb b/app/models/concerns/article.rb index 0e42f206f..e31925d0c 100644 --- a/app/models/concerns/article.rb +++ b/app/models/concerns/article.rb @@ -7,7 +7,9 @@ module Article include CheckElasticSearch belongs_to :user + belongs_to :author, class_name: 'User', foreign_key: 'author_id' + before_validation :set_author, on: :create before_validation :set_user validates_presence_of :user @@ -21,6 +23,10 @@ def text_fields # Implement it in the child class end + def set_author + self.author = User.current unless User.current.nil? + end + def set_user self.user = User.current unless User.current.nil? end diff --git a/app/models/explainer.rb b/app/models/explainer.rb index 46f7e6977..769477b42 100644 --- a/app/models/explainer.rb +++ b/app/models/explainer.rb @@ -1,6 +1,8 @@ class Explainer < ApplicationRecord include Article + has_paper_trail on: [:create, :update], ignore: [:updated_at, :created_at], if: proc { |_x| User.current.present? }, versions: { class_name: 'Version' } + belongs_to :team has_annotations diff --git a/db/migrate/20250214142854_add_author_to_articles.rb b/db/migrate/20250214142854_add_author_to_articles.rb new file mode 100644 index 000000000..07dd2f222 --- /dev/null +++ b/db/migrate/20250214142854_add_author_to_articles.rb @@ -0,0 +1,7 @@ +class AddAuthorToArticles < ActiveRecord::Migration[6.1] + def change + add_reference :fact_checks, :author, index: true + add_reference :explainers, :author, index: true + add_reference :claim_descriptions, :author, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 9909e84e8..4ae62c213 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2025_01_24_155814) do +ActiveRecord::Schema.define(version: 2025_02_14_142854) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -242,6 +242,8 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.bigint "team_id" + t.bigint "author_id" + t.index ["author_id"], name: "index_claim_descriptions_on_author_id" t.index ["project_media_id"], name: "index_claim_descriptions_on_project_media_id", unique: true t.index ["team_id"], name: "index_claim_descriptions_on_team_id" t.index ["user_id"], name: "index_claim_descriptions_on_user_id" @@ -346,7 +348,9 @@ t.datetime "updated_at", precision: 6, null: false t.string "tags", default: [], array: true t.boolean "trashed", default: false + t.bigint "author_id" t.index "date_trunc('day'::text, created_at)", name: "explainer_created_at_day" + t.index ["author_id"], name: "index_explainers_on_author_id" t.index ["created_at"], name: "index_explainers_on_created_at" t.index ["tags"], name: "index_explainers_on_tags", using: :gin t.index ["team_id"], name: "index_explainers_on_team_id" @@ -369,7 +373,9 @@ t.string "rating" t.boolean "imported", default: false t.boolean "trashed", default: false + t.bigint "author_id" t.index "date_trunc('day'::text, created_at)", name: "fact_check_created_at_day" + t.index ["author_id"], name: "index_fact_checks_on_author_id" t.index ["claim_description_id"], name: "index_fact_checks_on_claim_description_id", unique: true t.index ["created_at"], name: "index_fact_checks_on_created_at" t.index ["imported"], name: "index_fact_checks_on_imported" @@ -1008,6 +1014,6 @@ add_foreign_key "requests", "feeds" create_trigger :enforce_relationships, sql_definition: <<-SQL - CREATE TRIGGER enforce_relationships BEFORE INSERT ON public.relationships FOR EACH ROW EXECUTE FUNCTION validate_relationships() + CREATE TRIGGER enforce_relationships BEFORE INSERT ON public.relationships FOR EACH ROW EXECUTE PROCEDURE validate_relationships() SQL end diff --git a/test/models/explainer_test.rb b/test/models/explainer_test.rb index 7c40dce9d..6a1e1d4da 100644 --- a/test/models/explainer_test.rb +++ b/test/models/explainer_test.rb @@ -168,4 +168,12 @@ def teardown ex = create_explainer language: nil assert_equal 'en', ex.reload.language end + + test "should set author" do + u = create_user is_admin: true + User.current = u + ex = create_explainer + User.current = nil + assert_equal u, ex.author + end end diff --git a/test/models/fact_check_test.rb b/test/models/fact_check_test.rb index 5e5ad7413..af7eb0bda 100644 --- a/test/models/fact_check_test.rb +++ b/test/models/fact_check_test.rb @@ -733,4 +733,12 @@ def setup fc = create_fact_check assert_kind_of TiplineSearchResult, fc.as_tipline_search_result end + + test "should set author" do + u = create_user is_admin: true + User.current = u + fc = create_fact_check + User.current = nil + assert_equal u, fc.author + end end