Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions app/models/concerns/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,32 @@ def notify_bots
BotUser.enqueue_event(event, self.project_media.team_id, self) unless self.project_media.nil?
end

def field_max_length(field, default)
CheckConfig.get(:"article_max_#{field}_length", default, :integer)
end

def article_get_validation_length_fields
fields = { title: 800, url: 600 }
extra = self.is_a?(FactCheck) ? { summary: 3956 } : { description: 3956 }
fields.merge(extra)
end

def max_fields_length
article_get_validation_length_fields.each do |field, default|
max = field_max_length(field, default)
length = self[field]&.length || 0
errors.add(field, I18n.t(:"errors.messages.article_character_limit_reached")) if length > max
end
end

def truncate_fields_for_bot_user
article_get_validation_length_fields.each do |field, default|
max = field_max_length(field, default)
length = self[field]&.length || 0
self[field] = self[field][0...max] if length > max
end
end

protected

def index_in_elasticsearch(pm_id, data)
Expand Down
3 changes: 2 additions & 1 deletion app/models/explainer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ class Explainer < ApplicationRecord
has_many :project_medias, through: :explainer_items

before_validation :set_team, :set_language
before_validation :truncate_fields_for_bot_user, if: proc { |fc| fc.user.is_a?(BotUser) }
validates_format_of :url, with: URI.regexp, allow_blank: true, allow_nil: true
validates_presence_of :team, :title, :description
validate :language_in_allowed_values
validate :language_in_allowed_values, :max_fields_length

after_save :update_paragraphs_in_alegre
after_update :detach_explainer_if_trashed
Expand Down
3 changes: 2 additions & 1 deletion app/models/fact_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ class FactCheck < ApplicationRecord
before_validation :set_imported, on: :create
before_validation :set_claim_description, on: :create, unless: proc { |fc| fc.claim_description.present? }
before_validation :set_original_claim_for_published_articles, on: :create, if: proc { |fc| fc.publish_report && fc.set_original_claim.blank? }
before_validation :truncate_fields_for_bot_user, if: proc { |fc| fc.user.is_a?(BotUser) }

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, :rating_in_allowed_values
validate :language_in_allowed_values, :title_or_summary_exists, :rating_in_allowed_values, :max_fields_length

before_save :clean_fact_check_tags
after_save :update_report, unless: proc { |fc| fc.skip_report_update || !DynamicAnnotation::AnnotationType.where(annotation_type: 'report_design').exists? || fc.project_media.blank? }
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ en:
invalid_feed_saved_search_list_type: The saved search list type is invalid.
platform_allowed_values_error: 'cannot be of type %{type}, allowed types: %{allowed_types}'
restrict_registration_to_invited_users_only: 'Looks like you don’t have an invitation yet. Please request one from your workspace.'
article_character_limit_reached: Character Limit Reached.
activerecord:
models:
link: Link
Expand Down
24 changes: 24 additions & 0 deletions test/models/explainer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ def teardown
end
end

test "should validate max length" do
stub_configs({ 'article_max_title_length' => 20, 'article_max_url_length' => 30, 'article_max_description_length' => 20 }) do
assert_difference 'Explainer.count' do
create_explainer title: random_string(5), description: random_string(12), url: nil
end
assert_no_difference 'Explainer.count' do
assert_raises ActiveRecord::RecordInvalid do
create_explainer title: random_string(15), description: random_string(25), url: random_url
end
assert_raises ActiveRecord::RecordInvalid do
create_explainer title: random_string(25), description: random_string(20), url: random_url
end
end
team = create_team
bot_user = create_bot_user team: team, team_author_id: team.id
ex = nil
assert_difference 'Explainer.count' do
ex = create_explainer team: team, user: bot_user, title: random_string(30), description: random_string(30), url: nil
end
assert_equal 20, ex.title.length
assert_equal 20, ex.description.length
end
end

test "should belong to user and team" do
u = create_user
t = create_team
Expand Down
28 changes: 28 additions & 0 deletions test/models/fact_check_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,34 @@ def setup
end
end

test "should validate max length" do
stub_configs({ 'article_max_title_length' => 20, 'article_max_url_length' => 30, 'article_max_summary_length' => 20 }) do
assert_difference 'FactCheck.count' do
create_fact_check claim_description: create_claim_description, title: random_string(5), summary: random_string(12), url: nil
end
assert_no_difference 'FactCheck.count' do
assert_raises ActiveRecord::RecordInvalid do
create_fact_check claim_description: create_claim_description, title: random_string(15), summary: random_string(25), url: random_url
end
assert_raises ActiveRecord::RecordInvalid do
create_fact_check claim_description: create_claim_description, title: random_string(25), summary: random_string(15), url: random_url
end
end
# should pass for autmation import (user is a Bot)
team = create_team
pm = create_project_media team: team
bot_user = create_bot_user team: team, team_author_id: team.id
fc = nil
with_current_user_and_team(bot_user, team) do
assert_difference 'FactCheck.count' do
fc = create_fact_check claim_description: create_claim_description(project_media: pm), title: random_string(30), summary: random_string(30), url: random_url
end
end
assert_equal 20, fc.title.length
assert_equal 20, fc.summary.length
end
end

test "should create many fact-checks without signature" do
assert_difference 'FactCheck.count', 2 do
create_fact_check signature: nil, claim_description: create_claim_description
Expand Down
Loading