make the validation module act like the ActiveModel one a little bit more

did committed Jun 20, 2017
commit 0cf606c56f52dac2587e5890ada904256c6a7c40
Showing 21 changed files with 95 additions and 24 deletions
config/locales/de.yml +6 -1
@@ @@ -16,6 +16,11 @@ de:
errors:
messages:
blank: "leer"
+ taken: ist bereits vergeben
+ confirmation: stimmt nicht mit %{attribute} überein
+ too_short:
+ one: ist zu kurz (weniger als 1 Zeichen)
+ other: ist zu kurz (weniger als %{count} Zeichen)
pagination:
previous: "« Zurück"
@@ @@ -154,4 +159,4 @@ de:
skip_last_comma: true
words_connector: ", "
two_words_connector: " und "
- last_word_connector: " und "
\ No newline at end of file
+ last_word_connector: " und "
config/locales/en.yml +5 -1
@@ @@ -27,7 +27,11 @@ en:
session_assign: "Syntax Error in 'session_assign' - Valid syntax: session_assign [var] = [source]"
messages:
blank: "can't be blank"
- unique: "must be unique"
+ taken: "must be unique"
+ confirmation: doesn't match %{attribute}
+ too_short:
+ one: is too short (minimum is 1 character)
+ other: is too short (minimum is %{count} characters)
pagination:
previous: "« Previous"
config/locales/es.yml +6 -1
@@ @@ -16,6 +16,11 @@ es:
errors:
messages:
blank: "no puede estar en blanco"
+ taken: ya está en uso
+ confirmation: no coincide
+ too_short:
+ one: "es demasiado corto (1 carácter mínimo)"
+ other: "es demasiado corto (%{count} caracteres mínimo)"
pagination:
previous: "« Anterior"
@@ @@ -130,4 +135,4 @@ es:
skip_last_comma: true
words_connector: ", "
two_words_connector: " y "
- last_word_connector: " y "
\ No newline at end of file
+ last_word_connector: " y "
config/locales/et.yml +3 -0
@@ @@ -16,6 +16,9 @@ et:
errors:
messages:
blank: "tühi"
+ taken: on juba võetud
+ confirmation: ei vasta kinnitusele
+ too_short: on liiga lühike (miinimum on %{count} tähemärki)
pagination:
previous: "« Eelmine"
config/locales/fr.yml +5 -1
@@ @@ -17,7 +17,11 @@ fr:
errors:
messages:
blank: "doit être rempli(e)"
- unique: "doit être unique"
+ taken: n'est pas disponible
+ confirmation: "ne concorde pas avec %{attribute}"
+ too_short:
+ one: est trop court (au moins un caractère)
+ other: est trop court (au moins %{count} caractères)
pagination:
previous: "« Précédent"
config/locales/it.yml +6 -1
@@ @@ -16,6 +16,11 @@ it:
errors:
messages:
blank: "non può essere lasciato in bianco"
+ taken: è già presente
+ confirmation: non coincide con %{attribute}
+ too_short:
+ one: è troppo corto (il minimo è 1 carattere)
+ other: è troppo corto (il minimo è %{count} caratteri)
pagination:
previous: "« Precedente"
@@ @@ -152,4 +157,4 @@ it:
skip_last_comma: true
words_connector: ", "
two_words_connector: " e "
- last_word_connector: " e "
\ No newline at end of file
+ last_word_connector: " e "
config/locales/nb.yml +4 -1
@@ @@ -20,6 +20,9 @@ nb:
errors:
messages:
blank: "kan ikke være blank"
+ taken: er allerede i bruk
+ confirmation: er ikke lik %{attribute}
+ too_short: er for kort (minimum %{count} tegn)
pagination:
previous: "« Previous"
@@ @@ -188,4 +191,4 @@ nb:
percentage:
format:
- delimiter: ""
\ No newline at end of file
+ delimiter: ""
config/locales/nl.yml +5 -0
@@ @@ -14,6 +14,11 @@ nl:
errors:
messages:
blank: "mag niet leeg zijn"
+ taken: is al in gebruik
+ confirmation: komt niet overeen met %{attribute}
+ too_short:
+ one: is te kort (minimaal %{count} teken)
+ other: is te kort (minimaal %{count} tekens)
number:
format:
config/locales/pl.yml +7 -0
@@ @@ -16,6 +16,13 @@ pl:
errors:
messages:
blank: "nie może być puste"
+ taken: zostało już zajęte
+ confirmation: nie zgadza się z polem %{attribute}
+ too_short:
+ few: jest za krótkie (przynajmniej %{count} znaki)
+ many: jest za krótkie (przynajmniej %{count} znaków)
+ one: jest za krótkie (przynajmniej jeden znak)
+ other: jest za krótkie (przynajmniej %{count} znaków)
date:
formats:
config/locales/pt-BR.yml +5 -0
@@ @@ -16,6 +16,11 @@ pt-BR:
errors:
messages:
blank: "não pode ficar em branco"
+ taken: já está em uso
+ confirmation: não é igual a %{attribute}
+ too_short:
+ one: 'é muito curto (mínimo: 1 caracter)'
+ other: 'é muito curto (mínimo: %{count} caracteres)'
date:
formats:
config/locales/ru.yml +8 -1
@@ @@ -15,7 +15,14 @@ ru:
errors:
messages:
- blank: "can not be blank"
+ blank: не может быть пустым
+ taken: уже существует
+ confirmation: не совпадает со значением поля %{attribute}
+ too_short:
+ few: недостаточной длины (не может быть меньше %{count} символов)
+ many: недостаточной длины (не может быть меньше %{count} символов)
+ one: недостаточной длины (не может быть меньше %{count} символа)
+ other: недостаточной длины (не может быть меньше %{count} символа)
pagination:
previous: "« Предыдущая"
locomotive/steam/entities/content_entry.rb b/lib/locomotive/steam/entities/content_entry.rb +4 -0
@@ @@ -64,6 +64,10 @@ module Locomotive::Steam
self[content_type.label_field_name]
end
+ def _label_of(name)
+ content_type.field_label_of(name)
+ end
+
def localized_attributes
@localized_attributes.tap do |hash|
if hash && hash.has_key?(content_type.label_field_name.to_sym)
locomotive/steam/entities/content_type.rb b/lib/locomotive/steam/entities/content_type.rb +4 -0
@@ @@ -52,6 +52,10 @@ module Locomotive::Steam
(self[:label_field_name] || fields.first.name).to_sym
end
+ def field_label_of(name)
+ fields_by_name[name].label.downcase
+ end
+
def order_by
name = self[:order_by] == 'manually' ? '_position' : self[:order_by]
locomotive/steam/models/concerns/validation.rb b/lib/locomotive/steam/models/concerns/validation.rb +7 -8
@@ @@ -36,16 +36,15 @@ module Locomotive
add(attribute, :blank) if value.blank?
end
- def add(attribute, message)
- (@messages[attribute] ||= []) << generate_message(message)
+ def add(attribute, message, options = {})
+ (@messages[attribute] ||= []) << generate_message(message, options)
end
- def generate_message(message)
- case message
- when :blank, :unique then I18n.t(message, scope: 'errors.messages')
- else
- message
- end
+ def generate_message(message, options = {})
+ I18n.t(message, {
+ scope: 'errors.messages',
+ default: message
+ }.merge(options))
end
end
locomotive/steam/services/auth_service.rb b/lib/locomotive/steam/services/auth_service.rb +2 -2
@@ @@ -142,11 +142,11 @@ EMAIL
confirmation = self["#{name}_confirmation"]
if password.to_s.size < Locomotive::Steam::AuthService::MIN_PASSWORD_LENGTH
- self.errors.add(name, :password_too_short)
+ self.errors.add(name, :too_short, count: Locomotive::Steam::AuthService::MIN_PASSWORD_LENGTH)
end
if !password.blank? && password != confirmation
- self.errors.add(name, :password_different_from_confirmation)
+ self.errors.add("#{name}_confirmation", :confirmation, attribute: self._label_of(name))
end
set_password(password) if self.errors.empty?
locomotive/steam/services/content_entry_service.rb b/lib/locomotive/steam/services/content_entry_service.rb +1 -1
@@ @@ -133,7 +133,7 @@ module Locomotive
# fields marked as unique
content_type_repository.look_for_unique_fields(entry.content_type).each do |name, _|
if _repository.exists?(name => entry.send(name))
- entry.errors.add(name, :unique)
+ entry.errors.add(name, :taken)
end
end
spec/fixtures/default/app/views/pages/account/sign_up.liquid +4 -1
@@ @@ -34,12 +34,15 @@ handle: sign_up
<div class="form-group">
<label for="auth-password">Password</label>
{% if auth_entry.errors.password.size > 0 %}
- <span class="inline-error">{{ 'auth_' | append: auth_entry.errors.password.first | translate }}</span>
+ <span class="inline-error">{{ auth_entry.errors.password.first }}</span>
{% endif %}
<input type="password" name="auth_entry[password]" class="form-control" id="auth-password" placeholder="Password">
</div>
<div class="form-group">
<label for="auth-password">Password confirmation</label>
+ {% if auth_entry.errors.password_confirmation.size > 0 %}
+ <span class="inline-error">{{ auth_entry.errors.password_confirmation.first }}</span>
+ {% endif %}
<input type="password" name="auth_entry[password_confirmation]" class="form-control" id="auth-password" placeholder="Password confirmation">
</div>
<button type="submit" class="btn btn-default">Create account</button>
spec/integration/server/auth_spec.rb +1 -1
@@ @@ -63,7 +63,7 @@ describe 'Authentication' do
sign_up(params)
expect(last_response.status).to eq 200
expect(last_response.body).to include '/account/sign-up'
- expect(last_response.body).to include "Your password doesn't match the confirmation"
+ expect(last_response.body).to include "doesn't match password"
end
end
spec/unit/entities/content_type_spec.rb +7 -0
@@ @@ -30,6 +30,13 @@ describe Locomotive::Steam::ContentType do
end
+ describe '#field_label_of' do
+
+ subject { content_type.field_label_of(:title) }
+ it { is_expected.to eq 'title' }
+
+ end
+
describe '#localized_names' do
subject { content_type.localized_names }
spec/unit/services/auth_service_spec.rb +4 -3
@@ @@ -121,18 +121,19 @@ describe Locomotive::Steam::AuthService do
it 'returns false' do
is_expected.to eq false
- expect(content_entry.errors[:password]).to eq([:password_too_short])
+ expect(content_entry.errors[:password]).to eq(['is too short (minimum is 6 characters)'])
end
end
context "the password doesn't match the confirmation" do
- let(:attributes) { { password: 'easyone', password_confirmation: 'oneeasy' } }
+ let(:type) { instance_double('ContentType', slug: 'accounts', label_field_name: :title, fields: repository, fields_by_name: {}, field_label_of: 'password') }
+ let(:attributes) { { password: 'easyone', password_confirmation: 'oneeasy' } }
it 'returns false' do
is_expected.to eq false
- expect(content_entry.errors[:password]).to eq([:password_different_from_confirmation])
+ expect(content_entry.errors[:password_confirmation]).to eq(["doesn't match password"])
end
end
spec/unit/services/content_entry_service_spec.rb +1 -1
@@ @@ -48,7 +48,7 @@ describe Locomotive::Steam::ContentEntryService do
before do
allow(entry_repository).to receive(:exists?).with(title: 'Hello world').and_return(true)
- expect(entry.errors).to receive(:add).with(:title, :unique).and_return(true)
+ expect(entry.errors).to receive(:add).with(:title, :taken).and_return(true)
end
it { is_expected.to eq false }