active_record_ignored_attributes 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Readme.md +19 -1
- data/lib/active_record_ignored_attributes/has_attribute_values.rb +9 -0
- data/lib/active_record_ignored_attributes/matchers/have_attribute_values.rb +15 -0
- data/lib/active_record_ignored_attributes/matchers.rb +1 -0
- data/lib/active_record_ignored_attributes/version.rb +1 -1
- data/lib/active_record_ignored_attributes.rb +3 -1
- data/spec/has_attribute_values_spec.rb +24 -0
- data/spec/matchers/have_attribute_values_spec.rb +38 -0
- data/spec/{matchers_spec.rb → matchers/same_as_spec.rb} +2 -2
- metadata +7 -3
data/Readme.md
CHANGED
@@ -44,6 +44,16 @@ Using `same_as?`:
|
|
44
44
|
b = Address.new(address: 'Nowhere Road')
|
45
45
|
a.same_as?(b) # => false
|
46
46
|
|
47
|
+
Using `has_attribute_values?`:
|
48
|
+
|
49
|
+
a = Address.new(address: 'A St.', city: "Don't care")
|
50
|
+
a.has_attribute_values?(address: 'A St.') # => true
|
51
|
+
a.has_attribute_values?(address: 'A St.', city: 'Different') # => false
|
52
|
+
|
53
|
+
b = Address.new(address: 'B St.', city: "Don't care")
|
54
|
+
b.has_attribute_values?(address: 'A St.') # => false
|
55
|
+
b.has_attribute_values?({}) # => true
|
56
|
+
|
47
57
|
|
48
58
|
Installing
|
49
59
|
==========
|
@@ -128,7 +138,7 @@ or:
|
|
128
138
|
RSpec
|
129
139
|
=======
|
130
140
|
|
131
|
-
This gem comes with a `be_same_as` matcher for RSpec.
|
141
|
+
This gem comes with a `be_same_as` and `have_attribute_values` matcher for RSpec.
|
132
142
|
|
133
143
|
Add this to your spec_helper.rb:
|
134
144
|
|
@@ -152,7 +162,15 @@ and it will lovingly do a diff for you and only show you the attributes in each
|
|
152
162
|
expected: #<Address address: "Nowhere Road">
|
153
163
|
got: #<Address address: "B St.">
|
154
164
|
|
165
|
+
Or use `should have_attribute_values` whenever it's more convenient to specify the expected attributes with a hash instead of building a new model instance:
|
166
|
+
|
167
|
+
a = Address.new( name: 'A', address: 'The Same Address', city: "Don't care")
|
168
|
+
a.should have_attribute_values name: 'A', address: 'A Slightly Different Address'
|
169
|
+
|
170
|
+
will fail with:
|
155
171
|
|
172
|
+
expected: {:name=>"A", :address=>"A Slightly Different Address"}
|
173
|
+
got: {:name=>"A", :address=>"The Same Address"}
|
156
174
|
|
157
175
|
Motivation
|
158
176
|
==========
|
@@ -0,0 +1,9 @@
|
|
1
|
+
#require 'active_support/core_ext/hash/keys'
|
2
|
+
|
3
|
+
module ActiveRecordIgnoredAttributes::HasAttributeValues
|
4
|
+
def has_attribute_values?(expected)
|
5
|
+
self.attributes.slice(*expected.stringify_keys.keys) == expected.stringify_keys
|
6
|
+
end
|
7
|
+
alias_method :has_attributes_hash?, :has_attribute_values?
|
8
|
+
end
|
9
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
RSpec::Matchers.define :have_attribute_values do |expected|
|
2
|
+
match do |actual|
|
3
|
+
actual.has_attribute_values?(expected)
|
4
|
+
end
|
5
|
+
|
6
|
+
failure_message_for_should do |actual|
|
7
|
+
%(expected: #{expected.symbolize_keys.inspect}\n) +
|
8
|
+
%( got: #{actual.attributes.slice(*expected.stringify_keys.keys).symbolize_keys.inspect})
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message_for_should_not do |actual|
|
12
|
+
%(expected #{actual.inspect}\n) +
|
13
|
+
%(not to have attribute values #{expected.symbolize_keys.inspect})
|
14
|
+
end
|
15
|
+
end
|
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'active_record'
|
2
2
|
require "active_record_ignored_attributes/version"
|
3
|
-
require "active_record_ignored_attributes/inspect"
|
4
3
|
require "active_record_ignored_attributes/same_attributes_as"
|
4
|
+
require "active_record_ignored_attributes/has_attribute_values"
|
5
|
+
require "active_record_ignored_attributes/inspect"
|
5
6
|
|
6
7
|
module ActiveRecordIgnoredAttributes
|
7
8
|
extend ActiveSupport::Concern
|
@@ -22,5 +23,6 @@ end
|
|
22
23
|
ActiveRecord::Base.class_eval do
|
23
24
|
include ActiveRecordIgnoredAttributes
|
24
25
|
include ActiveRecordIgnoredAttributes::SameAttributesAs
|
26
|
+
include ActiveRecordIgnoredAttributes::HasAttributeValues
|
25
27
|
include ActiveRecordIgnoredAttributes::Inspect
|
26
28
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Address do
|
4
|
+
describe 'has_attribute_values?' do
|
5
|
+
let(:address) { Address.new }
|
6
|
+
|
7
|
+
it 'should return true if passed {}' do
|
8
|
+
a = Address.new( name: 'A', address: 'The Same Address', city: "Don't care")
|
9
|
+
a.has_attribute_values?({}).should be_true
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should return true if the given attributes have the given values' do
|
13
|
+
a = Address.new( name: 'A', address: 'The Same Address', city: "Don't care")
|
14
|
+
a.has_attribute_values?(name: 'A', address: 'The Same Address').should be_true
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should return false if any of the given attributes have different values than given' do
|
18
|
+
a = Address.new( name: 'A', address: 'The Same Address', city: "Don't care")
|
19
|
+
a.has_attribute_values?(name: 'A', address: 'The Same Address', city: "I do care after all").should be_false
|
20
|
+
a.has_attribute_values?(name: 'B', address: 'The Same Address').should be_false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'pathname'; __DIR__ = Pathname.new(__FILE__).dirname
|
2
|
+
require __DIR__ + "../spec_helper"
|
3
|
+
require 'active_support/core_ext/string/strip'
|
4
|
+
|
5
|
+
describe "have_attribute_values" do
|
6
|
+
it "delegates to has_attribute_values?" do
|
7
|
+
object, other = Object.new, Object.new
|
8
|
+
mock(object).has_attribute_values?(other) { true }
|
9
|
+
object.should have_attribute_values(other)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'matches when it should match' do
|
13
|
+
object = Address.new( name: 'A', address: 'The Same Address', city: "Don't care")
|
14
|
+
expect do
|
15
|
+
(object.should have_attribute_values name: 'A', address: 'The Same Address').should be_true
|
16
|
+
end.to_not raise_error
|
17
|
+
end
|
18
|
+
|
19
|
+
it "reports a nice failure message for should" do
|
20
|
+
object = Address.new( name: 'A', address: 'The Same Address', city: "Don't care")
|
21
|
+
expect do
|
22
|
+
object.should have_attribute_values name: 'A', address: 'A Slightly Different Address'
|
23
|
+
end.to raise_error(<<-End.strip_heredoc.chomp)
|
24
|
+
expected: {:name=>"A", :address=>"A Slightly Different Address"}
|
25
|
+
got: {:name=>"A", :address=>"The Same Address"}
|
26
|
+
End
|
27
|
+
end
|
28
|
+
|
29
|
+
it "reports a nice failure message for should_not" do
|
30
|
+
object = Address.new( name: 'A', address: 'The Same Address', city: "Don't care")
|
31
|
+
expect do
|
32
|
+
object.should_not have_attribute_values name: 'A', address: 'The Same Address'
|
33
|
+
end.to raise_error(<<-End.strip_heredoc.chomp)
|
34
|
+
expected {Address id: nil, name: "A", address: "The Same Address", city: "Don't care", state: nil, postal_code: nil, country: nil}
|
35
|
+
not to have attribute values {:name=>"A", :address=>"The Same Address"}
|
36
|
+
End
|
37
|
+
end
|
38
|
+
end
|
@@ -12,7 +12,7 @@ describe "be_same_as" do
|
|
12
12
|
object, other = Address.new(address: 'A Street'), Address.new(address: 'B Street')
|
13
13
|
expect do
|
14
14
|
object.should be_same_as(other)
|
15
|
-
end.
|
15
|
+
end.to raise_error(<<-End.strip_heredoc.chomp)
|
16
16
|
expected: #<Address address: "B Street">
|
17
17
|
got: #<Address address: "A Street">
|
18
18
|
End
|
@@ -22,7 +22,7 @@ describe "be_same_as" do
|
|
22
22
|
object, other = Address.new(address: 'A Street'), Address.new(address: 'A Street')
|
23
23
|
expect do
|
24
24
|
object.should_not be_same_as(other)
|
25
|
-
end.
|
25
|
+
end.to raise_error(<<-End.strip_heredoc.chomp)
|
26
26
|
expected: {Address id: nil, name: nil, address: "A Street", city: nil, state: nil, postal_code: nil, country: nil}
|
27
27
|
to not be same_as: {Address id: nil, name: nil, address: "A Street", city: nil, state: nil, postal_code: nil, country: nil}
|
28
28
|
End
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: active_record_ignored_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Tyler Rick
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-09-07 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -119,14 +119,18 @@ files:
|
|
119
119
|
- active_record_ignored_attributes.gemspec
|
120
120
|
- lib/active_record_ignored_attributes.rb
|
121
121
|
- lib/active_record_ignored_attributes/comparison.rb
|
122
|
+
- lib/active_record_ignored_attributes/has_attribute_values.rb
|
122
123
|
- lib/active_record_ignored_attributes/inspect.rb
|
123
124
|
- lib/active_record_ignored_attributes/matchers.rb
|
124
125
|
- lib/active_record_ignored_attributes/matchers/be_same_as.rb
|
126
|
+
- lib/active_record_ignored_attributes/matchers/have_attribute_values.rb
|
125
127
|
- lib/active_record_ignored_attributes/same_attributes_as.rb
|
126
128
|
- lib/active_record_ignored_attributes/version.rb
|
127
129
|
- spec/attributes_spec.rb
|
130
|
+
- spec/has_attribute_values_spec.rb
|
128
131
|
- spec/inspect_spec.rb
|
129
|
-
- spec/
|
132
|
+
- spec/matchers/have_attribute_values_spec.rb
|
133
|
+
- spec/matchers/same_as_spec.rb
|
130
134
|
- spec/same_as_spec.rb
|
131
135
|
- spec/spec_helper.rb
|
132
136
|
- spec/support/database.mysql2.yml
|