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.
@@ -1,41 +1,2 @@
1
- require 'mongoid'
2
- require "mongoid-embedded-errors/version"
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
@@ -0,0 +1,4 @@
1
+ module Mongoid; end
2
+ module Mongoid::EmbeddedErrors
3
+ VERSION = '2.1.1'.freeze
4
+ 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-embedded-errors/version'
4
+ require 'mongoid/embedded_errors/version'
5
5
 
6
6
  Gem::Specification.new do |gem|
7
- gem.name = "mongoid-embedded-errors"
7
+ gem.name = 'mongoid-embedded-errors'
8
8
  gem.version = Mongoid::EmbeddedErrors::VERSION
9
- gem.authors = ["Mark Bates"]
10
- gem.email = ["mark@markbates.com"]
11
- gem.description = %q{Easily bubble up errors from embedded documents in Mongoid.}
12
- gem.summary = %q{Easily bubble up errors from embedded documents in Mongoid.}
13
- gem.homepage = ""
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 = ["lib"]
20
+ gem.require_paths = ['lib']
19
21
 
20
- gem.add_dependency("mongoid", ">=3.0.0")
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
@@ -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
- Mongoid.load!(File.join(File.dirname(__FILE__), "config.yml"), :test)
8
- require File.join(File.dirname(__FILE__), "support", "models")
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
- DatabaseCleaner[:mongoid].strategy = :truncation
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.before(:each) do
15
- DatabaseCleaner.clean
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
- end
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,9 @@
1
+ class Annotation
2
+ include Mongoid::Document
3
+
4
+ embedded_in :article, inverse_of: :annotation
5
+
6
+ field :text, type: String
7
+
8
+ validates :text, presence: true
9
+ 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
@@ -0,0 +1,11 @@
1
+ class Page
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps
4
+
5
+ embedded_in :article, inverse_of: :pages
6
+ embeds_many :sections
7
+
8
+ field :title, type: String
9
+
10
+ validates :title, presence: true
11
+ end
@@ -0,0 +1,11 @@
1
+ class Section
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps
4
+
5
+ embedded_in :page, inverse_of: :sections
6
+
7
+ field :header, type: String
8
+ field :body, type: String
9
+
10
+ validates :header, presence: true
11
+ end
@@ -0,0 +1,13 @@
1
+ test:
2
+ # mongoid 3 and 4
3
+ sessions:
4
+ default:
5
+ database: mongoid_embedded_errors_test
6
+ hosts:
7
+ - localhost:27017
8
+ # mongoid 5 and newer
9
+ clients:
10
+ default:
11
+ database: mongoid_embedded_errors_test
12
+ hosts:
13
+ - localhost:27017
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.0.1
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: 2013-07-18 00:00:00.000000000 Z
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.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.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
- executables: []
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
- - .gitignore
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-embedded-errors/embedded_in.rb
42
- - lib/mongoid-embedded-errors/version.rb
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/config.yml
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.0.3
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/config.yml
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
- has_rdoc:
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
@@ -1,5 +0,0 @@
1
- module Mongoid
2
- module EmbeddedErrors
3
- VERSION = "2.0.1"
4
- end
5
- end
@@ -1,6 +0,0 @@
1
- test:
2
- sessions:
3
- default:
4
- database: mongoid_embedded_errors_test
5
- hosts:
6
- - localhost:27017