typo in an error message (validation) + embed the errors of a content entry into its JSON representation + remove duplicated code

did committed Oct 19, 2015
commit 70ccac70a756c6e25d9d5d3724b608637cc79504
Showing 7 changed files with 20 additions and 29 deletions
config/locales/en.yml +1 -1
@@ @@ -26,7 +26,7 @@ en:
paginate: "Syntax Error in 'paginate' - Valid syntax: paginate <collection> by <number>"
session_assign: "Syntax Error in 'session_assign' - Valid syntax: session_assign [var] = [source]"
messages:
- blank: "can't not be blank"
+ blank: "can't be blank"
unique: "must be unique"
pagination:
locomotive/steam/entities/content_entry.rb b/lib/locomotive/steam/entities/content_entry.rb +3 -0
@@ @@ -85,6 +85,9 @@ module Locomotive::Steam
_attributes.inject({}) do |hash, name|
hash[name.to_s] = send(name)
hash
+ end.tap do |hash|
+ # errors?
+ hash['errors'] = self.errors.to_hash.stringify_keys unless self.errors.empty?
end
end
locomotive/steam/models/concerns/validation.rb b/lib/locomotive/steam/models/concerns/validation.rb +1 -1
@@ @@ -22,7 +22,7 @@ module Locomotive
attr_accessor :messages
- def_delegators :@messages, :[], :clear, :empty?, :each, :size
+ def_delegators :@messages, :[], :clear, :empty?, :each, :size, :to_hash
alias_method :blank?, :empty?
locomotive/steam/services/entry_submission_service.rb b/lib/locomotive/steam/services/entry_submission_service.rb +1 -14
@@ @@ -34,20 +34,7 @@ module Locomotive
def to_json(entry)
return nil if entry.nil?
- # default values
- hash = { _slug: entry._slug, content_type_slug: entry.content_type_slug }
-
- # dynamic attributes
- entry.content_type.fields.all.each do |field|
- next if %w(belongs_to has_many many_to_many).include?(field.type.to_s)
-
- hash[field.name] = entry.send(field.name)
- end
-
- # errors
- hash[:errors] = entry.errors.messages unless entry.errors.empty?
-
- hash.to_json
+ entry.to_json
end
private
spec/integration/server/contact_form_spec.rb +5 -5
@@ @@ -49,9 +49,9 @@ describe 'ContactForm' do
subject { entry['errors'] }
it 'lists all the errors' do
- expect(subject['name']).to eq ["can't not be blank"]
- expect(subject['email']).to eq ["can't not be blank"]
- expect(subject['email']).to eq ["can't not be blank"]
+ expect(subject['name']).to eq ["can't be blank"]
+ expect(subject['email']).to eq ["can't be blank"]
+ expect(subject['email']).to eq ["can't be blank"]
end
end
@@ @@ -79,7 +79,7 @@ describe 'ContactForm' do
end
it 'displays errors' do
- expect(response.body.to_s).to include "can't not be blank"
+ expect(response.body.to_s).to include "can't be blank"
end
context 'redirects outside the site' do
@@ @@ -130,7 +130,7 @@ describe 'ContactForm' do
end
it 'displays errors' do
- expect(response.body.to_s).to include "can't not be blank"
+ expect(response.body.to_s).to include "can't be blank"
end
end
spec/unit/entities/content_entry_spec.rb +1 -1
@@ @@ -26,7 +26,7 @@ describe Locomotive::Steam::ContentEntry do
let(:attributes) { {} }
it { is_expected.to eq false }
- it { subject; expect(content_entry.errors[:title]).to eq(["can't not be blank"]) }
+ it { subject; expect(content_entry.errors[:title]).to eq(["can't be blank"]) }
it { subject; expect(content_entry.errors.empty?).to eq false }
end
spec/unit/services/entry_submission_service_spec.rb +8 -7
@@ @@ -48,19 +48,20 @@ describe Locomotive::Steam::EntrySubmissionService do
context 'existing content entry' do
- let(:errors) { {} }
- let(:fields) { [instance_double('TitleField', name: :title, type: :string)] }
- let(:type) { instance_double('Articles') }
- let(:entry) { instance_double('DecoratedEntry', _slug: 'hello-world', title: 'Hello world', content_type: type, content_type_slug: :articles, errors: errors) }
+ let(:field) { instance_double('TitleField', name: :title, type: :string) }
+ let(:fields) { [field] }
+ let(:type) { instance_double('Articles', slug: 'articles', label_field_name: :title, fields_by_name: { title: field }, persisted_field_names: [:title]) }
+ let(:entry) { Locomotive::Steam::ContentEntry.new(_slug: 'hello-world', title: 'Hello world', content_type: type) }
before { allow(type).to receive(:fields).and_return(instance_double('FieldRepository', all: fields)) }
- it { is_expected.to eq '{"_slug":"hello-world","content_type_slug":"articles","title":"Hello world"}' }
+ it { is_expected.to match %r{{"_id":null,"_slug":"hello-world","_label":"Hello world","_visible":true,"_position":0,"content_type_slug":"articles","created_at":"[^\"]+","updated_at":"[^\"]+","title":"Hello world"}} }
context 'with errors' do
- let(:errors) { instance_double('Errors', empty?: false, messages: { title: ["can't be blank"] }) }
- it { is_expected.to eq '{"_slug":"hello-world","content_type_slug":"articles","title":"Hello world","errors":{"title":["can\'t be blank"]}}' }
+ before { entry.errors.add(:title, "can't be blank") }
+
+ it { is_expected.to match %r{,\"errors\":\{\"title\":\[\"can't be blank\"\]\}} }
end