Skip to content

Commit f140f21

Browse files
authored
Fix TranslatedEtiquetteValidator to use Decidim.enable_etiquette_validator setting (decidim#15092)
1 parent abb3e77 commit f140f21

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

decidim-core/app/validators/translated_etiquette_validator.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# validates :my_i18n_field, translated_etiquette: true
77
class TranslatedEtiquetteValidator < EtiquetteValidator
88
def validate_each(record, attribute, _value)
9+
return unless Decidim.enable_etiquette_validator
10+
911
translated_attr = "#{attribute}_#{default_locale_for(record)}".gsub("-", "__")
1012
translated_value = record.send(translated_attr)
1113
return if translated_value.blank?
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
describe TranslatedEtiquetteValidator do
6+
subject { validatable.new(body:, current_organization:) }
7+
8+
let(:validatable) do
9+
Class.new do
10+
def self.model_name
11+
ActiveModel::Name.new(self, nil, "Validatable")
12+
end
13+
14+
def self.translatable_fields(*fields)
15+
@translatable_fields = fields
16+
end
17+
18+
include Decidim::AttributeObject::Model
19+
include ActiveModel::Validations
20+
21+
attribute :body
22+
attribute :current_organization
23+
24+
translatable_fields :body
25+
26+
validates :body, translated_etiquette: true
27+
28+
# Add accessor for translated fields
29+
def body_en
30+
body[:en]
31+
end
32+
end
33+
end
34+
35+
let(:current_organization) { create(:organization, default_locale: :en) }
36+
37+
let(:body) { { en: "A SCREAMING BODY WITH TOO MANY CAPS" } }
38+
39+
context "when Decidim.enable_etiquette_validator is false" do
40+
before do
41+
allow(Decidim).to receive(:enable_etiquette_validator).and_return(false)
42+
end
43+
44+
it "skips validation for all translatable fields" do
45+
expect(subject).to be_valid
46+
end
47+
end
48+
49+
context "when Decidim.enable_etiquette_validator is true" do
50+
before do
51+
allow(Decidim).to receive(:enable_etiquette_validator).and_return(true)
52+
end
53+
54+
context "with invalid content" do
55+
it "performs validation on translatable fields" do
56+
expect(subject).not_to be_valid
57+
expect(subject.errors[:body_en]).not_to be_empty
58+
end
59+
end
60+
61+
context "with valid content" do
62+
let(:body) { { en: "This is a reasonable body with proper capitalization" } }
63+
64+
it "allows valid content" do
65+
expect(subject).to be_valid
66+
end
67+
end
68+
end
69+
end

0 commit comments

Comments
 (0)