mongoid-rspec 1.5.6 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -9,6 +9,18 @@ RSpec matchers for Mongoid 3.x.
9
9
 
10
10
  For Mongoid 2.x, use [mongoid-rspec 1.4.5](http://rubygems.org/gems/mongoid-rspec/versions/1.4.5)
11
11
 
12
+ Installation
13
+ -
14
+ Add to your Gemfile
15
+
16
+ gem 'mongoid-rspec'
17
+
18
+ Drop in existing or dedicated support file in spec/support (spec/support/mongoid.rb)
19
+
20
+ RSpec.configure do |configuration|
21
+ configuration.include Mongoid::Matchers
22
+ end
23
+
12
24
  Association Matchers
13
25
  -
14
26
  describe User do
@@ -104,6 +116,17 @@ Validation Matchers
104
116
  it { should custom_validate(:ssn).with_validator(SsnValidator) }
105
117
  end
106
118
 
119
+ Accepts Nested Attributes Matcher
120
+ -
121
+ describe User do
122
+ it { should accept_nested_attributes_for(:articles) }
123
+ it { should accept_nested_attributes_for(:comments) }
124
+ end
125
+
126
+ describe Article do
127
+ it { should accept_nested_attributes_for(:permalink) }
128
+ end
129
+
107
130
  Index Matcher
108
131
  -
109
132
  describe Article do
@@ -143,18 +166,6 @@ Others
143
166
  it { should_not have_field(:number_of_comments).of_type(Integer).with_default_value_of(1) }
144
167
  end
145
168
 
146
- Use
147
- -
148
- add in Gemfile
149
-
150
- gem 'mongoid-rspec'
151
-
152
- drop in existing or dedicated support file in spec/support (spec/support/mongoid.rb)
153
-
154
- RSpec.configure do |configuration|
155
- configuration.include Mongoid::Matchers
156
- end
157
-
158
169
  Acknowledgement
159
170
  -
160
171
  Thanks to [Durran Jordan](https://github.com/durran) for providing the changes necessary to make
@@ -0,0 +1,65 @@
1
+ module Mongoid
2
+ module Matchers # :nodoc:
3
+
4
+ # Ensures that the model can accept nested attributes for the specified
5
+ # association.
6
+ #
7
+ # Example:
8
+ # it { should accept_nested_attributes_for(:articles) }
9
+ #
10
+ def accept_nested_attributes_for(attribute)
11
+ AcceptNestedAttributesForMatcher.new(attribute)
12
+ end
13
+
14
+ class AcceptNestedAttributesForMatcher
15
+
16
+ def initialize(attribute)
17
+ @attribute = attribute.to_s
18
+ @options = {}
19
+ end
20
+
21
+ def matches?(subject)
22
+ @subject = subject
23
+ match?
24
+ end
25
+
26
+ def failure_message
27
+ "Expected #{expectation} (#{@problem})"
28
+ end
29
+
30
+ def negative_failure_message
31
+ "Did not expect #{expectation}"
32
+ end
33
+
34
+ def description
35
+ description = "accepts_nested_attributes_for :#{@attribute}"
36
+ end
37
+
38
+ protected
39
+ def match?
40
+ exists?
41
+ end
42
+
43
+ def exists?
44
+ if config
45
+ true
46
+ else
47
+ @problem = 'is not declared'
48
+ false
49
+ end
50
+ end
51
+
52
+ def config
53
+ model_class.nested_attributes["#{@attribute}_attributes"]
54
+ end
55
+
56
+ def model_class
57
+ @subject.class
58
+ end
59
+
60
+ def expectation
61
+ "#{model_class.name} to accept nested attributes for #{@attribute}"
62
+ end
63
+ end
64
+ end
65
+ end
@@ -109,15 +109,11 @@ module Mongoid
109
109
  end
110
110
 
111
111
  def actual_min
112
- @validator.options[:minimum] || ((@validator.options[:in] || @validator.options[:within]).try(&:min)).tap do |x|
113
- puts "minimum: #{x}, #{@validator.options}"
114
- end
112
+ @validator.options[:minimum] || ((@validator.options[:in] || @validator.options[:within]).try(&:min))
115
113
  end
116
114
 
117
115
  def actual_max
118
- @validator.options[:maximum] || ((@validator.options[:in] || @validator.options[:within]).try(&:max)).tap do |x|
119
- puts "maximum: #{x}"
120
- end
116
+ @validator.options[:maximum] || ((@validator.options[:in] || @validator.options[:within]).try(&:max))
121
117
  end
122
118
  end
123
119
 
data/lib/mongoid-rspec.rb CHANGED
@@ -8,6 +8,7 @@ require 'matchers/associations'
8
8
  require 'matchers/collections'
9
9
  require 'matchers/indexes'
10
10
  require 'matchers/allow_mass_assignment'
11
+ require 'matchers/accept_nested_attributes'
11
12
  require 'matchers/validations'
12
13
  require 'matchers/validations/associated'
13
14
  require 'matchers/validations/confirmation_of'
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Rspec
3
- VERSION = "1.5.6"
3
+ VERSION = "1.6.0"
4
4
  end
5
5
  end
@@ -23,4 +23,6 @@ class Article
23
23
  index({ title: 1 }, { unique: true, background: true })
24
24
  index({ published: 1 })
25
25
  index({ 'permalink._id' => 1 })
26
+
27
+ accepts_nested_attributes_for :permalink
26
28
  end
data/spec/models/user.rb CHANGED
@@ -30,6 +30,8 @@ class User
30
30
  attr_accessible :login, :email, :age, :password
31
31
  attr_accessible :role, :as => :admin
32
32
 
33
+ accepts_nested_attributes_for :articles, :comments
34
+
33
35
  def admin?
34
36
  false
35
37
  end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe "AcceptsNestedAttributes" do
4
+ describe User do
5
+ it { should accept_nested_attributes_for(:articles) }
6
+ it { should accept_nested_attributes_for(:comments) }
7
+ end
8
+
9
+ describe Article do
10
+ it { should accept_nested_attributes_for(:permalink) }
11
+ end
12
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.6
4
+ version: 1.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-13 00:00:00.000000000 Z
12
+ date: 2013-01-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -75,6 +75,7 @@ files:
75
75
  - LICENSE
76
76
  - README.md
77
77
  - Rakefile
78
+ - lib/matchers/accept_nested_attributes.rb
78
79
  - lib/matchers/allow_mass_assignment.rb
79
80
  - lib/matchers/associations.rb
80
81
  - lib/matchers/collections.rb
@@ -106,6 +107,7 @@ files:
106
107
  - spec/models/site.rb
107
108
  - spec/models/user.rb
108
109
  - spec/spec_helper.rb
110
+ - spec/unit/accept_nested_attributes_spec.rb
109
111
  - spec/unit/allow_mass_assignment_spec.rb
110
112
  - spec/unit/associations_spec.rb
111
113
  - spec/unit/collections_spec.rb
@@ -133,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
135
  version: '0'
134
136
  requirements: []
135
137
  rubyforge_project: mongoid-rspec
136
- rubygems_version: 1.8.23
138
+ rubygems_version: 1.8.24
137
139
  signing_key:
138
140
  specification_version: 3
139
141
  summary: RSpec matchers for Mongoid
@@ -149,6 +151,7 @@ test_files:
149
151
  - spec/models/site.rb
150
152
  - spec/models/user.rb
151
153
  - spec/spec_helper.rb
154
+ - spec/unit/accept_nested_attributes_spec.rb
152
155
  - spec/unit/allow_mass_assignment_spec.rb
153
156
  - spec/unit/associations_spec.rb
154
157
  - spec/unit/collections_spec.rb
@@ -156,4 +159,3 @@ test_files:
156
159
  - spec/unit/indexes_spec.rb
157
160
  - spec/unit/validations_spec.rb
158
161
  - spec/validators/ssn_validator.rb
159
- has_rdoc: