object_attorney 3.0.2 → 3.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c4d890c47532df6465b42cf292cce79ea7547269
4
- data.tar.gz: 53f487a5a4f3f6a8ef76e107fdb3ca5d3340af06
3
+ metadata.gz: 430a83b927b50895e29c3bb4cfdde9ac06fa01aa
4
+ data.tar.gz: 7bcdd02aa7efa81622b000ab85bfe9035d9bc154
5
5
  SHA512:
6
- metadata.gz: cc07794845c2f7370d4583e0ce75ea4918b4cf0de7644f69d35f2bb2c920b22e55989f311b5d5e76e35a004de6e38557caba229ef188c048afedb1540951d09b
7
- data.tar.gz: b948a71214feb47355f3aca6caafa9970a3888a6c079e6bc3b1e09124ca3b7d6c81b3e30699059a86285085603a292d216adb8a16f3056d88d1efa91c26f2b7e
6
+ metadata.gz: ce5de94f752f2a82a1716dd27438a7b3a1a19de46fcf32db532a29d5a4e6d95e1a1f8277a51e86af9f33a643a1e1e0c873941a36eac9e6ee9fbe5892a45cdeff
7
+ data.tar.gz: fffe49cdd524b284ef444b23bfef76d94698cf5239800884654ffaab49b0e6184e23457847155c7a2d50e8b600e5f1aec0073930b9f1b930634571aa2003daff
data/README.md CHANGED
@@ -4,6 +4,7 @@ This gem allows you to create use cases with ActiveModel validations and keep yo
4
4
  [![Code Climate](https://codeclimate.com/github/goncalvesjoao/object_attorney/badges/gpa.svg)](https://codeclimate.com/github/goncalvesjoao/object_attorney)
5
5
  [![Test Coverage](https://codeclimate.com/github/goncalvesjoao/object_attorney/badges/coverage.svg)](https://codeclimate.com/github/goncalvesjoao/object_attorney/coverage)
6
6
  [![Build Status](https://travis-ci.org/goncalvesjoao/object_attorney.svg?branch=master)](https://travis-ci.org/goncalvesjoao/object_attorney)
7
+ [![Gem Version](https://badge.fury.io/rb/object_attorney.svg)](https://badge.fury.io/rb/object_attorney)
7
8
 
8
9
  ## 1) Basic Usage
9
10
  ```ruby
@@ -4,7 +4,6 @@ require 'object_attorney/helpers'
4
4
  require 'object_attorney/class_methods'
5
5
 
6
6
  module ObjectAttorney
7
-
8
7
  def self.included(base_class)
9
8
  base_class.extend ClassMethods
10
9
  base_class.extend ActiveModel::Validations::HelperMethods
@@ -74,7 +73,6 @@ module ObjectAttorney
74
73
  allegation.founded_accusation(self, defendant)
75
74
  end.compact
76
75
  end
77
-
78
76
  end
79
77
 
80
78
  require 'object_attorney/base'
@@ -1,7 +1,5 @@
1
1
  module ObjectAttorney
2
-
3
2
  class Accusation
4
-
5
3
  def initialize(validation, attorney, defendant)
6
4
  @attorney = attorney
7
5
  @defendant = defendant
@@ -40,7 +38,5 @@ module ObjectAttorney
40
38
  def options
41
39
  @validation.options
42
40
  end
43
-
44
41
  end
45
-
46
42
  end
@@ -1,12 +1,25 @@
1
+ require 'object_attorney/validations/custom'
1
2
  require 'object_attorney/accusation'
2
3
 
3
4
  module ObjectAttorney
4
-
5
5
  class Allegation
6
+ VALIDATION_OVERWRITES = {
7
+ # ActiveModel::Validations::NumericalityValidator =>
8
+ # Validations::Numericality,
9
+ custom: Validations::Custom
10
+ }.freeze
11
+
12
+ attr_reader :validation
13
+
14
+ def initialize(validation_class, options, &block)
15
+ overwrite_class = VALIDATION_OVERWRITES[validation_class]
6
16
 
7
- def initialize(validation)
8
17
  # expected to be an ActiveModel::Validations::<Class> instance
9
- @validation = validation
18
+ @validation = (overwrite_class || validation_class).new(options, &block)
19
+ end
20
+
21
+ def attributes
22
+ validation.attributes
10
23
  end
11
24
 
12
25
  def founded_accusation(attorney, defendant)
@@ -14,7 +27,5 @@ module ObjectAttorney
14
27
 
15
28
  accusation.founded ? accusation : nil
16
29
  end
17
-
18
30
  end
19
-
20
31
  end
@@ -1,7 +1,5 @@
1
1
  module ObjectAttorney
2
-
3
2
  class Base
4
-
5
3
  include ObjectAttorney
6
4
 
7
5
  defend :defendant
@@ -11,7 +9,5 @@ module ObjectAttorney
11
9
  def initialize(defendant)
12
10
  @defendant = defendant
13
11
  end
14
-
15
12
  end
16
-
17
13
  end
@@ -1,10 +1,7 @@
1
- require 'object_attorney/validations/custom'
2
1
  require 'object_attorney/allegation'
3
2
 
4
3
  module ObjectAttorney
5
-
6
4
  module ClassMethods
7
-
8
5
  attr_writer :allegations, :defendant_options
9
6
 
10
7
  def defendant_options
@@ -20,20 +17,18 @@ module ObjectAttorney
20
17
  end
21
18
 
22
19
  def validate(*args, &block)
23
- allegations[nil] << Allegation.new(Validations::Custom.new(args, &block))
20
+ allegations[nil] << Allegation.new(:custom, args, &block)
24
21
  end
25
22
 
26
23
  def validates_with(*args, &block)
27
24
  options = args.extract_options!
28
25
 
29
26
  args.each do |validation_class|
30
- store_allegations_by_attribute validation_class.new(options, &block)
31
- end
32
- end
27
+ allegation = Allegation.new(validation_class, options, &block)
33
28
 
34
- def store_allegations_by_attribute(validation)
35
- validation.attributes.each do |attribute|
36
- allegations[attribute.to_sym] << Allegation.new(validation)
29
+ allegation.attributes.each do |attribute|
30
+ allegations[attribute.to_sym].push allegation
31
+ end
37
32
  end
38
33
  end
39
34
 
@@ -44,7 +39,5 @@ module ObjectAttorney
44
39
 
45
40
  super
46
41
  end
47
-
48
42
  end
49
-
50
43
  end
@@ -1,18 +1,21 @@
1
1
  module ObjectAttorney
2
-
3
2
  module Errors
4
3
  # ActiveModel::Errors told me to declare
5
4
  # the following methods for a minimal implementation
6
-
7
5
  module ClassMethods
8
6
  def human_attribute_name(attribute, _ = {})
9
7
  attribute
10
8
  end
11
9
 
12
- # Having doubts if this is really necessary
13
- # def lookup_ancestors
14
- # [self]
15
- # end
10
+ # Necessary for proper translations
11
+ def lookup_ancestors
12
+ [self]
13
+ end
14
+
15
+ # Necessary for proper translations
16
+ def i18n_scope
17
+ :activemodel
18
+ end
16
19
  end
17
20
 
18
21
  def self.included(base_class)
@@ -27,7 +30,5 @@ module ObjectAttorney
27
30
  def read_attribute_for_validation(attribute)
28
31
  send(attribute)
29
32
  end
30
-
31
33
  end
32
-
33
34
  end
@@ -1,9 +1,7 @@
1
1
  require 'object_attorney/errors'
2
2
 
3
3
  module ObjectAttorney
4
-
5
4
  module Helpers
6
-
7
5
  module_function
8
6
 
9
7
  def marked_for_destruction?(object)
@@ -31,9 +29,7 @@ module ObjectAttorney
31
29
  def extend_errors_if_necessary(object)
32
30
  return if object.respond_to?(:errors)
33
31
 
34
- object.class.class_eval { include Errors }
32
+ object.class.send(:include, Errors)
35
33
  end
36
-
37
34
  end
38
-
39
35
  end
@@ -1,8 +1,6 @@
1
1
  module ObjectAttorney
2
2
  module Validations
3
-
4
3
  class Custom
5
-
6
4
  attr_reader :options
7
5
 
8
6
  attr_writer :attorney
@@ -18,8 +16,6 @@ module ObjectAttorney
18
16
  Helpers.call_method!(@attorney, method, defendant)
19
17
  end
20
18
  end
21
-
22
19
  end
23
-
24
20
  end
25
21
  end
@@ -1,5 +1,3 @@
1
1
  module ObjectAttorney
2
-
3
- VERSION = '3.0.2'.freeze
4
-
2
+ VERSION = '3.0.3'.freeze
5
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: object_attorney
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.2
4
+ version: 3.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - João Gonçalves
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-25 00:00:00.000000000 Z
11
+ date: 2016-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -116,16 +116,7 @@ executables: []
116
116
  extensions: []
117
117
  extra_rdoc_files: []
118
118
  files:
119
- - ".gitignore"
120
- - ".rspec"
121
- - ".rubocop.yml"
122
- - ".ruby-version"
123
- - ".travis.yml"
124
- - Gemfile
125
- - Gemfile.lock
126
- - LICENSE.txt
127
119
  - README.md
128
- - Rakefile
129
120
  - lib/object_attorney.rb
130
121
  - lib/object_attorney/accusation.rb
131
122
  - lib/object_attorney/allegation.rb
@@ -135,13 +126,6 @@ files:
135
126
  - lib/object_attorney/helpers.rb
136
127
  - lib/object_attorney/validations/custom.rb
137
128
  - lib/object_attorney/version.rb
138
- - object_attorney.gemspec
139
- - spec/object_attorney/base_spec.rb
140
- - spec/object_attorney/custom_validation_spec.rb
141
- - spec/object_attorney/object_attorney_spec.rb
142
- - spec/spec_helper.rb
143
- - spec/support/post.rb
144
- - spec/support/user.rb
145
129
  homepage: https://github.com/streetbees/object_attorney
146
130
  licenses:
147
131
  - MIT
@@ -166,10 +150,4 @@ rubygems_version: 2.5.1
166
150
  signing_key:
167
151
  specification_version: 4
168
152
  summary: Allows you to keep your ActiveModel validations out of your objects
169
- test_files:
170
- - spec/object_attorney/base_spec.rb
171
- - spec/object_attorney/custom_validation_spec.rb
172
- - spec/object_attorney/object_attorney_spec.rb
173
- - spec/spec_helper.rb
174
- - spec/support/post.rb
175
- - spec/support/user.rb
153
+ test_files: []
data/.gitignore DELETED
@@ -1,50 +0,0 @@
1
- *.gem
2
- *.rbc
3
- /.config
4
- /coverage/
5
- /InstalledFiles
6
- /pkg/
7
- /spec/reports/
8
- /spec/examples.txt
9
- /test/tmp/
10
- /test/version_tmp/
11
- /tmp/
12
-
13
- ## Specific to RubyMotion:
14
- .dat*
15
- .repl_history
16
- build/
17
-
18
- ## Documentation cache and generated files:
19
- /.yardoc/
20
- /_yardoc/
21
- /doc/
22
- /rdoc/
23
-
24
- ## Environment normalization:
25
- /.bundle/
26
- /vendor/bundle
27
- /lib/bundler/man/
28
-
29
- # for a library or gem, you might want to ignore these files since the code is
30
- # intended to run in multiple environments; otherwise, check them in:
31
- # Gemfile.lock
32
- # .ruby-version
33
- .ruby-gemset
34
-
35
- # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
- .rvmrc
37
-
38
- *.bundle
39
- *.so
40
- *.o
41
- *.a
42
- mkmf.log
43
-
44
- .DS_Store
45
-
46
- # Jetbrains Idea files
47
- .idea/
48
- api.iml
49
-
50
- db/schema.rb
data/.rspec DELETED
@@ -1,4 +0,0 @@
1
- --tty
2
- --color
3
- --format documentation
4
- --format html -o "tmp/rspec_result.html"
data/.rubocop.yml DELETED
@@ -1,19 +0,0 @@
1
- Style/EmptyLinesAroundClassBody:
2
- Enabled: false
3
-
4
- Style/EmptyLinesAroundModuleBody:
5
- Enabled: false
6
-
7
- Style/FrozenStringLiteralComment:
8
- Enabled: false
9
-
10
- AllCops:
11
- DisplayCopNames: true
12
- Exclude:
13
- - 'spec/**/*'
14
- - 'Gemfile*'
15
- - '*.gemspec'
16
- - 'vendor/**/*'
17
-
18
- Documentation:
19
- Enabled: false
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 2.3.0
data/.travis.yml DELETED
@@ -1,4 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 1.9.3
4
- - 2.3.0
data/Gemfile DELETED
@@ -1,3 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
data/Gemfile.lock DELETED
@@ -1,77 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- object_attorney (3.0.1)
5
- activemodel (~> 3)
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- activemodel (3.2.22.2)
11
- activesupport (= 3.2.22.2)
12
- builder (~> 3.0.0)
13
- activesupport (3.2.22.2)
14
- i18n (~> 0.6, >= 0.6.4)
15
- multi_json (~> 1.0)
16
- ast (2.3.0)
17
- builder (3.0.4)
18
- codeclimate-test-reporter (0.4.8)
19
- simplecov (>= 0.7.1, < 1.0.0)
20
- coderay (1.1.1)
21
- diff-lcs (1.2.5)
22
- docile (1.1.5)
23
- i18n (0.7.0)
24
- json (1.8.3)
25
- method_source (0.8.2)
26
- multi_json (1.12.1)
27
- parser (2.3.1.2)
28
- ast (~> 2.2)
29
- powerpack (0.1.1)
30
- pry (0.10.3)
31
- coderay (~> 1.1.0)
32
- method_source (~> 0.8.1)
33
- slop (~> 3.4)
34
- rainbow (2.1.0)
35
- rake (11.2.2)
36
- rspec (3.4.0)
37
- rspec-core (~> 3.4.0)
38
- rspec-expectations (~> 3.4.0)
39
- rspec-mocks (~> 3.4.0)
40
- rspec-core (3.4.4)
41
- rspec-support (~> 3.4.0)
42
- rspec-expectations (3.4.0)
43
- diff-lcs (>= 1.2.0, < 2.0)
44
- rspec-support (~> 3.4.0)
45
- rspec-mocks (3.4.1)
46
- diff-lcs (>= 1.2.0, < 2.0)
47
- rspec-support (~> 3.4.0)
48
- rspec-support (3.4.1)
49
- rubocop (0.37.2)
50
- parser (>= 2.3.0.4, < 3.0)
51
- powerpack (~> 0.1)
52
- rainbow (>= 1.99.1, < 3.0)
53
- ruby-progressbar (~> 1.7)
54
- unicode-display_width (~> 0.3)
55
- ruby-progressbar (1.8.1)
56
- simplecov (0.11.2)
57
- docile (~> 1.1.0)
58
- json (~> 1.8)
59
- simplecov-html (~> 0.10.0)
60
- simplecov-html (0.10.0)
61
- slop (3.6.0)
62
- unicode-display_width (0.3.1)
63
-
64
- PLATFORMS
65
- ruby
66
-
67
- DEPENDENCIES
68
- codeclimate-test-reporter (= 0.4.8)
69
- object_attorney!
70
- pry (= 0.10.3)
71
- rake (= 11.2.2)
72
- rspec (= 3.4.0)
73
- rubocop (= 0.37.2)
74
- simplecov (= 0.11.2)
75
-
76
- BUNDLED WITH
77
- 1.12.5
data/LICENSE.txt DELETED
@@ -1,22 +0,0 @@
1
- Copyright (c) 2013 goncalvesjoao
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile DELETED
@@ -1,10 +0,0 @@
1
- require 'bundler/gem_tasks'
2
-
3
- require 'rspec/core/rake_task'
4
- require 'rubocop/rake_task'
5
-
6
- RSpec::Core::RakeTask.new
7
- RuboCop::RakeTask.new
8
-
9
- task(:default).clear
10
- task default: [:rubocop, :spec]
@@ -1,30 +0,0 @@
1
- lib = File.expand_path('../lib', __FILE__)
2
-
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
-
5
- require 'object_attorney/version'
6
-
7
- Gem::Specification.new do |gem|
8
- gem.name = 'object_attorney'
9
- gem.version = ObjectAttorney::VERSION
10
- gem.license = 'MIT'
11
- gem.authors = ['João Gonçalves']
12
- gem.email = ['goncalves.joao@gmail.com']
13
- gem.summary = 'Allows you to keep your ActiveModel validations out of your objects'
14
- gem.description = 'This gem allows you to create use cases with ActiveModel validations and keep your model clean'
15
- gem.homepage = 'https://github.com/streetbees/object_attorney'
16
-
17
- gem.files = `git ls-files`.split($/)
18
- gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
19
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
- gem.require_paths = ['lib']
21
-
22
- gem.add_development_dependency 'pry', '0.10.3'
23
- gem.add_development_dependency 'rake', '11.2.2'
24
- gem.add_development_dependency 'rspec', '3.4.0'
25
- gem.add_development_dependency 'rubocop', '0.37.2'
26
- gem.add_development_dependency 'simplecov', '0.11.2'
27
- gem.add_development_dependency 'codeclimate-test-reporter', '0.4.8'
28
-
29
- gem.add_dependency 'activemodel', '~> 3'
30
- end
@@ -1,21 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe ObjectAttorney::Base do
4
-
5
- context "Initializing an attorney with an object" do
6
- before do
7
- @user = User.new
8
-
9
- @user_validator = Class.new(ObjectAttorney::Base) do
10
- validates_presence_of :first_name
11
- end
12
-
13
- @user_validator.new(@user).valid?
14
- end
15
-
16
- it "should validate the object" do
17
- expect(@user.errors.messages).to eq({ first_name: ["can't be blank"] })
18
- end
19
- end
20
-
21
- end
@@ -1,60 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe ObjectAttorney do
4
-
5
- context "given an attorney with a custom validation" do
6
- before do
7
- @custom_validator = Struct.new(:user) do
8
- include ObjectAttorney
9
-
10
- defend :user
11
-
12
- validate :phone_number_country_code, if: :should_validate_country_code
13
-
14
- def phone_number_country_code(user)
15
- return if user.phone_number.split(' ')[0] == '123'
16
-
17
- user.errors.add(:phone_number, 'invalid country code')
18
- end
19
-
20
- def should_validate_country_code(user)
21
- !user.dont_validate
22
- end
23
- end
24
- end
25
-
26
- context "given a user with an invalid phone_number" do
27
- before do
28
- @user = User.new(phone_number: 'bad number')
29
- @custom_validator.new(@user).valid?
30
- end
31
-
32
- it "@user.errors should mention the bad phone_number error" do
33
- expect(@user.errors.added?(:phone_number, 'invalid country code')).to be true
34
- end
35
-
36
- context "and preventing the validation" do
37
- before do
38
- @user = User.new(phone_number: 'really bad number', dont_validate: true)
39
- @custom_validator.new(@user).valid?
40
- end
41
-
42
- it "@user.errors should be empty" do
43
- expect(@user.errors.empty?).to be true
44
- end
45
- end
46
- end
47
-
48
- context "given a user with a valid phone_number" do
49
- before do
50
- @user = User.new(phone_number: '123 123')
51
- @custom_validator.new(@user).valid?
52
- end
53
-
54
- it "@user.errors should be empty" do
55
- expect(@user.errors.empty?).to be true
56
- end
57
- end
58
- end
59
-
60
- end
@@ -1,228 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe ObjectAttorney do
4
-
5
- context "When the defendant is nil" do
6
- before do
7
- @user_validator = Struct.new(:user) do
8
- include ObjectAttorney
9
-
10
- defend :user
11
-
12
- validates_presence_of :first_name, unless: Proc.new { |user| user.dont_validate }
13
- end
14
- end
15
-
16
- it "#valid? should be true" do
17
- expect(@user_validator.new(nil).valid?).to be true
18
- end
19
- end
20
-
21
- context "When the defendant is empty" do
22
- before do
23
- @user = User.new
24
- @user.posts = []
25
-
26
- @user_validator = Struct.new(:user) do
27
- include ObjectAttorney
28
-
29
- defend :posts, in: :user
30
-
31
- validates_presence_of :first_name, unless: Proc.new { |user| user.dont_validate }
32
- end
33
- end
34
-
35
- it "#valid? should be true" do
36
- expect(@user_validator.new(@user).valid?).to be true
37
- end
38
- end
39
-
40
- context "When the defendant is not defined" do
41
- before do
42
- @user = User.new
43
-
44
- @user_validator = Struct.new(:user) do
45
- include ObjectAttorney
46
-
47
- defend :posts
48
-
49
- validates_presence_of :first_name, unless: Proc.new { |user| user.dont_validate }
50
- end
51
- end
52
-
53
- it "#valid? should raise an error" do
54
- expect { @user_validator.new(@user).valid? }.to raise_error NotImplementedError
55
- end
56
- end
57
-
58
- context "When the defendant is not defined on the parent" do
59
- before do
60
- @user = User.new
61
-
62
- @user_validator = Struct.new(:user) do
63
- include ObjectAttorney
64
-
65
- defend :comments, in: :user
66
-
67
- validates_presence_of :first_name, unless: Proc.new { |user| user.dont_validate }
68
- end
69
- end
70
-
71
- it "#valid? should raise an error" do
72
- expect { @user_validator.new(@user).valid? }.to raise_error NotImplementedError
73
- end
74
- end
75
-
76
- context "When the defendant is a single object" do
77
- before do
78
- @user = User.new
79
-
80
- @user_validator = Struct.new(:user) do
81
- include ObjectAttorney
82
-
83
- defend :user
84
-
85
- validates_presence_of :first_name, unless: Proc.new { |user| user.dont_validate }
86
- end
87
- end
88
-
89
- it "@user.errors should mention first_name" do
90
- expect(@user_validator.new(@user).invalid?).to be true
91
-
92
- expect(@user.errors.messages).to eq({ first_name: ["can't be blank"] })
93
- end
94
-
95
- context "and the unless validation is true" do
96
- before do
97
- @user = User.new(dont_validate: true)
98
- end
99
-
100
- it "@user.errors should be empty" do
101
- expect(@user_validator.new(@user).valid?).to be true
102
-
103
- expect(@user.errors.empty?).to be true
104
- end
105
- end
106
- end
107
-
108
- context "When the defendant is an array nested inside another object" do
109
- before do
110
- @user = User.new
111
- @user.posts = [Post.new, Post.new(_destroy: true), nil, Post.new(title: 'yada')]
112
-
113
- @user_validator = Struct.new(:user) do
114
- include ObjectAttorney
115
-
116
- defend :posts, in: :user
117
-
118
- validates_presence_of :title
119
- end
120
-
121
- @user_validator.new(@user).invalid?
122
- end
123
-
124
- it "@user.errors should mention that :posts are invalid" do
125
- expect(@user.errors.messages).to eq({ posts: ["is invalid"] })
126
- end
127
-
128
- it "only the first @user.posts should have errors" do
129
- expect(@user.posts[0].errors.messages).to eq({ title: ["can't be blank"] })
130
- expect(@user.posts[1].errors.empty?).to be true
131
- expect(@user.posts[2]).to be_nil
132
- expect(@user.posts[3].errors.empty?).to be true
133
- end
134
- end
135
-
136
- describe "inheritance" do
137
-
138
- context "when a use case inherits from another that has a dependant and a validation" do
139
- before do
140
- @user1 = User.new
141
- @user2 = User.new
142
- @user3 = User.new
143
- @user4 = User.new
144
-
145
- @user_validator1 = Class.new do
146
- include ObjectAttorney
147
-
148
- defend :user
149
-
150
- validates_presence_of :first_name
151
-
152
- attr_accessor :user
153
-
154
- def initialize(user)
155
- @user = user
156
- end
157
- end
158
-
159
- @user_validator2 = Class.new(@user_validator1) do
160
- validates_presence_of :phone_number
161
- end
162
-
163
- @user_validator3 = Class.new(@user_validator2) do
164
- defend :users
165
-
166
- attr_accessor :users
167
-
168
- def initialize(users)
169
- @users = users
170
- end
171
- end
172
-
173
- @user_validator1.new(@user1).valid?
174
- @user_validator2.new(@user2).valid?
175
- @user_validator3.new([@user3, @user4]).valid?
176
- end
177
-
178
- it "@user_validator1.defendant_options should mention :user" do
179
- expect(@user_validator1.defendant_options).to eq({ name: :user })
180
- end
181
-
182
- it "@user_validator2.defendant_options should mention :user" do
183
- expect(@user_validator2.defendant_options).to eq({ name: :user })
184
- end
185
-
186
- it "@user_validator3.defendant_options should mention :users" do
187
- expect(@user_validator3.defendant_options).to eq({ name: :users })
188
- end
189
-
190
- it "@user1.errors should ONLY mention first_name" do
191
- expect(@user1.errors.count).to be 1
192
-
193
- expect(@user1.errors.messages).to eq({
194
- first_name: ["can't be blank"]
195
- })
196
- end
197
-
198
- it "@user2.errors should mention first_name and phone_number" do
199
- expect(@user2.errors.messages).to eq({
200
- first_name: ["can't be blank"],
201
- phone_number: ["can't be blank"]
202
- })
203
-
204
- expect(@user2.errors.count).to be 2
205
- end
206
-
207
- it "@user3.errors should mention first_name and phone_number" do
208
- expect(@user3.errors.messages).to eq({
209
- first_name: ["can't be blank"],
210
- phone_number: ["can't be blank"]
211
- })
212
-
213
- expect(@user3.errors.count).to be 2
214
- end
215
-
216
- it "@user4.errors should mention first_name and phone_number" do
217
- expect(@user4.errors.messages).to eq({
218
- first_name: ["can't be blank"],
219
- phone_number: ["can't be blank"]
220
- })
221
-
222
- expect(@user4.errors.count).to be 2
223
- end
224
- end
225
-
226
- end
227
-
228
- end
data/spec/spec_helper.rb DELETED
@@ -1,25 +0,0 @@
1
- require "codeclimate-test-reporter"
2
-
3
- CodeClimate::TestReporter.start
4
-
5
- require "simplecov"
6
-
7
- SimpleCov.start do
8
- root("lib/")
9
- coverage_dir("../tmp/coverage/")
10
- end
11
-
12
- $: << File.expand_path('../', File.dirname(__FILE__))
13
-
14
- require 'pry'
15
- require 'object_attorney'
16
-
17
- Dir["./spec/**/support/**/*.rb"].each do |file|
18
- require file
19
- end
20
-
21
- RSpec.configure do |config|
22
- config.run_all_when_everything_filtered = true
23
-
24
- config.order = 'random'
25
- end
data/spec/support/post.rb DELETED
@@ -1,13 +0,0 @@
1
- class Post
2
-
3
- attr_accessor :title, :_destroy
4
-
5
- def initialize(attributes = {})
6
- (attributes || {}).each { |name, value| send("#{name}=", value) }
7
- end
8
-
9
- def marked_for_destruction?
10
- _destroy
11
- end
12
-
13
- end
data/spec/support/user.rb DELETED
@@ -1,9 +0,0 @@
1
- class User
2
-
3
- attr_accessor :first_name, :phone_number, :dont_validate, :posts
4
-
5
- def initialize(attributes = {})
6
- (attributes || {}).each { |name, value| send("#{name}=", value) }
7
- end
8
-
9
- end