Skip to content

Commit

Permalink
[WIP] Ticket CV2-6158: Finished implementation, only missing rake task
Browse files Browse the repository at this point in the history
  • Loading branch information
caiosba committed Feb 14, 2025
1 parent e624ade commit 6d9929d
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 14 deletions.
6 changes: 0 additions & 6 deletions app/graph/types/explainer_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 0 additions & 6 deletions app/graph/types/fact_check_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions app/models/concerns/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/models/explainer.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20250214142854_add_author_to_articles.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 8 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
8 changes: 8 additions & 0 deletions test/models/explainer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions test/models/fact_check_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6d9929d

Please sign in to comment.