mongoid-embedded-errors 2.0.1 → 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +17 -0
- data/.rspec +3 -0
- data/.rubocop.yml +77 -0
- data/Appraisals +15 -0
- data/Gemfile +7 -3
- data/Gemfile.lock +152 -28
- data/Guardfile +42 -0
- data/README.md +4 -5
- data/Rakefile +9 -7
- data/bin/_guard-core +17 -0
- data/bin/appraisal +17 -0
- data/bin/guard +17 -0
- data/bin/rake +17 -0
- data/bin/rspec +17 -0
- data/gemfiles/mongoid_3.gemfile +11 -0
- data/gemfiles/mongoid_3.gemfile.lock +98 -0
- data/gemfiles/mongoid_4.gemfile +11 -0
- data/gemfiles/mongoid_4.gemfile.lock +110 -0
- data/gemfiles/mongoid_5.gemfile +11 -0
- data/gemfiles/mongoid_5.gemfile.lock +106 -0
- data/gemfiles/mongoid_6.gemfile +11 -0
- data/gemfiles/mongoid_6.gemfile.lock +100 -0
- data/lib/mongoid-embedded-errors.rb +2 -41
- data/lib/mongoid/embedded_errors.rb +36 -0
- data/lib/mongoid/embedded_errors/embedded_in.rb +10 -0
- data/lib/mongoid/embedded_errors/version.rb +4 -0
- data/mongoid-embedded-errors.gemspec +13 -11
- data/spec/lib/mongoid/embedded_errors_spec.rb +38 -0
- data/spec/spec_helper.rb +34 -9
- data/spec/support/models/annotation.rb +9 -0
- data/spec/support/models/article.rb +15 -0
- data/spec/support/models/page.rb +11 -0
- data/spec/support/models/section.rb +11 -0
- data/spec/support/mongoid.yml +13 -0
- metadata +51 -20
- data/lib/mongoid-embedded-errors/embedded_in.rb +0 -16
- data/lib/mongoid-embedded-errors/version.rb +0 -5
- data/spec/config.yml +0 -6
- data/spec/embedded_errors_spec.rb +0 -41
- data/spec/support/models.rb +0 -49
@@ -1,41 +1,2 @@
|
|
1
|
-
|
2
|
-
require
|
3
|
-
require "mongoid-embedded-errors/embedded_in"
|
4
|
-
|
5
|
-
module Mongoid
|
6
|
-
module EmbeddedErrors
|
7
|
-
|
8
|
-
def self.included(klass)
|
9
|
-
# make sure that the alias only happens once:
|
10
|
-
unless klass.instance_methods.include?(:errors_without_embedded_errors)
|
11
|
-
klass.alias_method_chain(:errors, :embedded_errors)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def errors_with_embedded_errors
|
16
|
-
errs = errors_without_embedded_errors
|
17
|
-
self.embedded_relations.each do |name, metadata|
|
18
|
-
# name is something like pages or sections
|
19
|
-
# if there is an 'is invalid' message for the relation then let's work it:
|
20
|
-
if errs[name]
|
21
|
-
# first delete the unless 'is invalid' error for the relation
|
22
|
-
errs.delete(name.to_sym)
|
23
|
-
# next, loop through each of the relations (pages, sections, etc...)
|
24
|
-
[self.send(name)].flatten.reject(&:nil?).each_with_index do |rel, i|
|
25
|
-
# get each of their individual message and add them to the parent's errors:
|
26
|
-
if rel.errors.any?
|
27
|
-
rel.errors.messages.each do |k, v|
|
28
|
-
key = (rel.metadata.relation == Mongoid::Relations::Embedded::Many ? "#{name}[#{i}].#{k}" : "#{name}.#{k}").to_sym
|
29
|
-
errs.delete(key)
|
30
|
-
errs[key] = v
|
31
|
-
errs[key].flatten!
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
return errs
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
41
|
-
end
|
1
|
+
# rubocop:disable Style/FileName
|
2
|
+
require 'mongoid/embedded_errors'
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
require 'mongoid/embedded_errors/version'
|
3
|
+
require 'mongoid/embedded_errors/embedded_in'
|
4
|
+
|
5
|
+
module Mongoid::EmbeddedErrors
|
6
|
+
def self.included(klass)
|
7
|
+
# make sure that the alias only happens once:
|
8
|
+
unless klass.instance_methods.include?(:errors_without_embedded_errors)
|
9
|
+
klass.alias_method_chain(:errors, :embedded_errors)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def errors_with_embedded_errors
|
14
|
+
errors_without_embedded_errors.tap do |errs|
|
15
|
+
embedded_relations.each do |name, metadata|
|
16
|
+
# name is something like pages or sections
|
17
|
+
# if there is an 'is invalid' message for the relation then let's work it:
|
18
|
+
next unless Array(public_send(name)).any? { |doc| doc.errors.any? }
|
19
|
+
# first delete the unless 'is invalid' error for the relation
|
20
|
+
errs[name].delete 'is invalid'
|
21
|
+
errs.delete name.to_sym if errs[name].empty?
|
22
|
+
# next, loop through each of the relations (pages, sections, etc...)
|
23
|
+
[send(name)].flatten.reject(&:nil?).each_with_index do |rel, i|
|
24
|
+
# get each of their individual message and add them to the parent's errors:
|
25
|
+
next unless rel.errors.any?
|
26
|
+
rel.errors.messages.each do |k, v|
|
27
|
+
key = (metadata.relation == Mongoid::Relations::Embedded::Many ? "#{name}[#{i}].#{k}" : "#{name}.#{k}").to_sym
|
28
|
+
errs.delete(key)
|
29
|
+
errs[key] = v
|
30
|
+
errs[key].flatten!
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'mongoid/relations/embedded/in'
|
2
|
+
|
3
|
+
module Mongoid::Relations::Macros::ClassMethods
|
4
|
+
alias embedded_in_without_embedded_errors embedded_in
|
5
|
+
def embedded_in(*args)
|
6
|
+
relation = embedded_in_without_embedded_errors(*args)
|
7
|
+
send(:include, Mongoid::EmbeddedErrors)
|
8
|
+
relation
|
9
|
+
end
|
10
|
+
end
|
@@ -1,21 +1,23 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'mongoid
|
4
|
+
require 'mongoid/embedded_errors/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
|
-
gem.name =
|
7
|
+
gem.name = 'mongoid-embedded-errors'
|
8
8
|
gem.version = Mongoid::EmbeddedErrors::VERSION
|
9
|
-
gem.authors = [
|
10
|
-
gem.email = [
|
11
|
-
gem.description =
|
12
|
-
|
13
|
-
gem.
|
9
|
+
gem.authors = ['Mark Bates', 'Kristijan Novoselić']
|
10
|
+
gem.email = ['mark@markbates.com', 'kristijan@glooko.com']
|
11
|
+
gem.description = 'Easily bubble up errors from embedded '\
|
12
|
+
'documents in Mongoid.'
|
13
|
+
gem.summary = 'Easily bubble up errors from embedded '\
|
14
|
+
'documents in Mongoid.'
|
15
|
+
gem.homepage = ''
|
14
16
|
|
15
|
-
gem.files = `git ls-files`.split(
|
16
|
-
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
18
|
+
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
17
19
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
-
gem.require_paths = [
|
20
|
+
gem.require_paths = ['lib']
|
19
21
|
|
20
|
-
gem.add_dependency(
|
22
|
+
gem.add_dependency('mongoid', '>=3.0')
|
21
23
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
RSpec.describe Mongoid::EmbeddedErrors do
|
2
|
+
describe '#errors_with_embedded_errors' do
|
3
|
+
subject(:article) { Article.new name: 'Test', summary: '-', pages: pages }
|
4
|
+
before { |spec| article.validate unless spec.metadata[:do_not_validate] }
|
5
|
+
|
6
|
+
context 'when article does not have any pages associated' do
|
7
|
+
let(:pages) { [] }
|
8
|
+
|
9
|
+
it { is_expected.not_to be_valid }
|
10
|
+
it "returns `can't be blank` error for pages" do
|
11
|
+
expect(article.errors[:pages]).to include "can't be blank"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
context 'when article has one or more invalid pages' do
|
15
|
+
let(:pages) { [Page.new] }
|
16
|
+
|
17
|
+
it { is_expected.not_to be_valid }
|
18
|
+
it 'does not have any errors under `:pages` key' do
|
19
|
+
expect(article.errors[:pages]).to be_empty
|
20
|
+
end
|
21
|
+
it 'returns all errors for `pages[0]` object' do
|
22
|
+
expect(article.errors[:'pages[0].title']).to include "can't be blank"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
context 'when all pages in article are valid' do
|
26
|
+
let(:pages) { [Page.new(title: 'First page')] }
|
27
|
+
|
28
|
+
it { is_expected.to be_valid }
|
29
|
+
end
|
30
|
+
context 'when embedded document has not been validated', :do_not_validate do
|
31
|
+
let(:pages) { [Page.new] }
|
32
|
+
|
33
|
+
it 'does not trigger validations' do
|
34
|
+
expect(article.errors).to be_empty
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,18 +1,43 @@
|
|
1
1
|
require 'bundler/setup'
|
2
|
-
|
3
|
-
require 'mongoid-embedded-errors' # and any other gems you need
|
4
|
-
|
2
|
+
require 'mongoid-embedded-errors'
|
5
3
|
require 'database_cleaner'
|
6
4
|
|
7
|
-
|
8
|
-
|
5
|
+
current_path = File.dirname(__FILE__)
|
6
|
+
SPEC_MODELS_PATH = File.join(current_path, 'support/**/*.rb').freeze
|
7
|
+
Dir[SPEC_MODELS_PATH].each { |f| require f }
|
9
8
|
|
10
|
-
|
9
|
+
Mongoid.load! File.join(current_path, 'support/mongoid.yml'), :test
|
11
10
|
|
12
11
|
RSpec.configure do |config|
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
config.filter_run :focus
|
14
|
+
config.filter_run_excluding :skip
|
13
15
|
|
14
|
-
config.
|
15
|
-
|
16
|
+
config.expect_with :rspec do |expectations|
|
17
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
18
|
+
expectations.syntax = :expect
|
16
19
|
end
|
17
20
|
|
18
|
-
|
21
|
+
config.mock_with :rspec do |mocks|
|
22
|
+
mocks.verify_partial_doubles = true
|
23
|
+
end
|
24
|
+
config.disable_monkey_patching!
|
25
|
+
|
26
|
+
config.before(:suite) do
|
27
|
+
DatabaseCleaner.strategy = :truncation
|
28
|
+
end
|
29
|
+
|
30
|
+
config.around(:each) do |example|
|
31
|
+
DatabaseCleaner.cleaning { example.run }
|
32
|
+
end
|
33
|
+
|
34
|
+
config.before(:each) do
|
35
|
+
# Need to manually reload spec models for mutant to work as expected
|
36
|
+
if ENV['MUTANT']
|
37
|
+
Dir[SPEC_MODELS_PATH].each do |filename|
|
38
|
+
Object.send(:remove_const, File.basename(filename, '.rb').capitalize)
|
39
|
+
load filename
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Article
|
2
|
+
include Mongoid::Document
|
3
|
+
include Mongoid::Timestamps
|
4
|
+
include Mongoid::EmbeddedErrors
|
5
|
+
|
6
|
+
embeds_many :pages
|
7
|
+
embeds_one :annotation
|
8
|
+
|
9
|
+
field :name, type: String
|
10
|
+
field :summary, type: String
|
11
|
+
|
12
|
+
validates :name, presence: true
|
13
|
+
validates :summary, presence: true
|
14
|
+
validates :pages, presence: true
|
15
|
+
end
|
metadata
CHANGED
@@ -1,50 +1,79 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-embedded-errors
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Bates
|
8
|
+
- Kristijan Novoselić
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2016-09-30 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: mongoid
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
16
17
|
requirements:
|
17
|
-
- -
|
18
|
+
- - ">="
|
18
19
|
- !ruby/object:Gem::Version
|
19
|
-
version: 3.0
|
20
|
+
version: '3.0'
|
20
21
|
type: :runtime
|
21
22
|
prerelease: false
|
22
23
|
version_requirements: !ruby/object:Gem::Requirement
|
23
24
|
requirements:
|
24
|
-
- -
|
25
|
+
- - ">="
|
25
26
|
- !ruby/object:Gem::Version
|
26
|
-
version: 3.0
|
27
|
+
version: '3.0'
|
27
28
|
description: Easily bubble up errors from embedded documents in Mongoid.
|
28
29
|
email:
|
29
30
|
- mark@markbates.com
|
30
|
-
|
31
|
+
- kristijan@glooko.com
|
32
|
+
executables:
|
33
|
+
- _guard-core
|
34
|
+
- appraisal
|
35
|
+
- guard
|
36
|
+
- rake
|
37
|
+
- rspec
|
31
38
|
extensions: []
|
32
39
|
extra_rdoc_files: []
|
33
40
|
files:
|
34
|
-
- .
|
41
|
+
- ".codeclimate.yml"
|
42
|
+
- ".gitignore"
|
43
|
+
- ".rspec"
|
44
|
+
- ".rubocop.yml"
|
45
|
+
- Appraisals
|
35
46
|
- Gemfile
|
36
47
|
- Gemfile.lock
|
48
|
+
- Guardfile
|
37
49
|
- LICENSE.txt
|
38
50
|
- README.md
|
39
51
|
- Rakefile
|
52
|
+
- bin/_guard-core
|
53
|
+
- bin/appraisal
|
54
|
+
- bin/guard
|
55
|
+
- bin/rake
|
56
|
+
- bin/rspec
|
57
|
+
- gemfiles/mongoid_3.gemfile
|
58
|
+
- gemfiles/mongoid_3.gemfile.lock
|
59
|
+
- gemfiles/mongoid_4.gemfile
|
60
|
+
- gemfiles/mongoid_4.gemfile.lock
|
61
|
+
- gemfiles/mongoid_5.gemfile
|
62
|
+
- gemfiles/mongoid_5.gemfile.lock
|
63
|
+
- gemfiles/mongoid_6.gemfile
|
64
|
+
- gemfiles/mongoid_6.gemfile.lock
|
40
65
|
- lib/mongoid-embedded-errors.rb
|
41
|
-
- lib/mongoid
|
42
|
-
- lib/mongoid
|
66
|
+
- lib/mongoid/embedded_errors.rb
|
67
|
+
- lib/mongoid/embedded_errors/embedded_in.rb
|
68
|
+
- lib/mongoid/embedded_errors/version.rb
|
43
69
|
- mongoid-embedded-errors.gemspec
|
44
|
-
- spec/
|
45
|
-
- spec/embedded_errors_spec.rb
|
70
|
+
- spec/lib/mongoid/embedded_errors_spec.rb
|
46
71
|
- spec/spec_helper.rb
|
47
|
-
- spec/support/models.rb
|
72
|
+
- spec/support/models/annotation.rb
|
73
|
+
- spec/support/models/article.rb
|
74
|
+
- spec/support/models/page.rb
|
75
|
+
- spec/support/models/section.rb
|
76
|
+
- spec/support/mongoid.yml
|
48
77
|
homepage: ''
|
49
78
|
licenses: []
|
50
79
|
metadata: {}
|
@@ -54,23 +83,25 @@ require_paths:
|
|
54
83
|
- lib
|
55
84
|
required_ruby_version: !ruby/object:Gem::Requirement
|
56
85
|
requirements:
|
57
|
-
- -
|
86
|
+
- - ">="
|
58
87
|
- !ruby/object:Gem::Version
|
59
88
|
version: '0'
|
60
89
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
90
|
requirements:
|
62
|
-
- -
|
91
|
+
- - ">="
|
63
92
|
- !ruby/object:Gem::Version
|
64
93
|
version: '0'
|
65
94
|
requirements: []
|
66
95
|
rubyforge_project:
|
67
|
-
rubygems_version: 2.
|
96
|
+
rubygems_version: 2.4.5.1
|
68
97
|
signing_key:
|
69
98
|
specification_version: 4
|
70
99
|
summary: Easily bubble up errors from embedded documents in Mongoid.
|
71
100
|
test_files:
|
72
|
-
- spec/
|
73
|
-
- spec/embedded_errors_spec.rb
|
101
|
+
- spec/lib/mongoid/embedded_errors_spec.rb
|
74
102
|
- spec/spec_helper.rb
|
75
|
-
- spec/support/models.rb
|
76
|
-
|
103
|
+
- spec/support/models/annotation.rb
|
104
|
+
- spec/support/models/article.rb
|
105
|
+
- spec/support/models/page.rb
|
106
|
+
- spec/support/models/section.rb
|
107
|
+
- spec/support/mongoid.yml
|
@@ -1,16 +0,0 @@
|
|
1
|
-
require "mongoid/relations/embedded/in"
|
2
|
-
|
3
|
-
module Mongoid
|
4
|
-
module Relations
|
5
|
-
module Macros
|
6
|
-
module ClassMethods
|
7
|
-
alias :embedded_in_without_embedded_errors :embedded_in
|
8
|
-
def embedded_in(*args)
|
9
|
-
relation = embedded_in_without_embedded_errors(*args)
|
10
|
-
self.send(:include, Mongoid::EmbeddedErrors)
|
11
|
-
return relation
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|