association_accessors 1.3.0

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.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/Appraisals +25 -0
  4. data/Gemfile +6 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +254 -0
  7. data/Rakefile +27 -0
  8. data/association_accessors.gemspec +39 -0
  9. data/bin/test +21 -0
  10. data/gemfiles/activerecord_4.1.gemfile +7 -0
  11. data/gemfiles/activerecord_4.1.gemfile.lock +67 -0
  12. data/gemfiles/activerecord_4.2.gemfile +7 -0
  13. data/gemfiles/activerecord_4.2.gemfile.lock +67 -0
  14. data/gemfiles/activerecord_5.0.gemfile +7 -0
  15. data/gemfiles/activerecord_5.0.gemfile.lock +63 -0
  16. data/gemfiles/activerecord_5.1.gemfile +7 -0
  17. data/gemfiles/activerecord_5.1.gemfile.lock +63 -0
  18. data/gemfiles/activerecord_5.2.gemfile +7 -0
  19. data/gemfiles/activerecord_5.2.gemfile.lock +63 -0
  20. data/gemfiles/rspec_2.gemfile +7 -0
  21. data/gemfiles/rspec_2.gemfile.lock +58 -0
  22. data/lib/association_accessors.rb +30 -0
  23. data/lib/association_accessors/collection_association.rb +35 -0
  24. data/lib/association_accessors/singular_association.rb +19 -0
  25. data/lib/association_accessors/test/matcher.rb +60 -0
  26. data/lib/association_accessors/version.rb +3 -0
  27. data/spec/association_accessors_spec.rb +5 -0
  28. data/spec/dummy.rb +99 -0
  29. data/spec/matcher_spec.rb +56 -0
  30. data/spec/spec_helper.rb +23 -0
  31. data/spec/with_association/belongs_to_spec.rb +33 -0
  32. data/spec/with_association/has_and_belongs_to_many_spec.rb +45 -0
  33. data/spec/with_association/has_many_spec.rb +45 -0
  34. data/spec/with_association/has_many_through_spec.rb +27 -0
  35. data/spec/with_association/has_one_spec.rb +33 -0
  36. data/spec/with_association/has_one_through_spec.rb +43 -0
  37. data/spec/with_association/polymorphic/belongs_to_spec.rb +38 -0
  38. data/spec/with_association/polymorphic/has_many_spec.rb +44 -0
  39. data/spec/with_association/polymorphic/has_one_spec.rb +33 -0
  40. data/spec/with_association/with_custom_name_spec.rb +33 -0
  41. data/spec/with_attribute_spec.rb +47 -0
  42. metadata +203 -0
@@ -0,0 +1,43 @@
1
+ RSpec.describe 'for `has_one :through` association' do
2
+ let!(:author) { Author.create! }
3
+ let!(:book) { author.books.create! }
4
+ let!(:address) { author.create_address! }
5
+
6
+ it 'generates `#[association_name]_[attribute_name]` accessor.' do
7
+ expect(book).to respond_to :address_serial, :address_serial=
8
+ end
9
+
10
+ describe '`#[association_name]_[attribute_name]`' do
11
+ it 'returns the value of the `[attribute]` of the associated record if there is any.' do
12
+ expect { book.address = nil }.to change(book, :address_serial).from(address.serial).to nil
13
+ end
14
+ end
15
+
16
+ describe '`#[association_name]_[attribute_name]=(value)`' do
17
+ it 'sets the association value to nil if `value` is nil.' do
18
+ expect { book.address_serial = nil }.to change(book, :address).from(address).to nil
19
+ end
20
+
21
+ it 'raises ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection.', activerecord: [ '4' ] do
22
+ new_address = Address.create!
23
+ expect { book.address_serial = new_address.serial }
24
+ .to raise_exception(ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection,
25
+ %{Cannot modify association 'Book#address' because the source reflection class 'Address' is associated to 'Author' via :has_one.})
26
+ end
27
+
28
+ it 'raises ActiveRecord::HasOneThroughCantAssociateThroughHasOneOrManyReflection.', activerecord: [ '5' ] do
29
+ new_address = Address.create!
30
+ expect { book.address_serial = new_address.serial }
31
+ .to raise_exception(ActiveRecord::HasOneThroughCantAssociateThroughHasOneOrManyReflection,
32
+ %{Cannot modify association 'Book#address' because the source reflection class 'Address' is associated to 'Author' via :has_one.})
33
+ end
34
+
35
+ it 'raises ActiveRecord::RecordNotFound if record with `[attribute]` = `value` does not exist.', activerecord: [ '4.1' ] do
36
+ expect { book.address_serial = address.serial.next }.to raise_exception ActiveRecord::RecordNotFound
37
+ end
38
+
39
+ it 'raises ActiveRecord::RecordNotFound if record with `[attribute]` = `value` does not exist.', activerecord: [ '4.2', '5' ] do
40
+ expect { book.address_serial = address.serial.next }.to raise_exception ActiveRecord::RecordNotFound, "Couldn't find Address"
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,38 @@
1
+ RSpec.describe 'for polymorphic `belongs_to` association' do
2
+ let!(:author) { Author.create! }
3
+ let!(:image) { author.create_image! }
4
+
5
+ it 'generates `#[association_name]_[attribute_name]` accessor.' do
6
+ expect(image).to respond_to :imageable_serial, :imageable_serial=
7
+ end
8
+
9
+ describe '`#[association_name]_[attribute_name]`' do
10
+ it 'returns the value of the `[attribute]` of the associated record if there is any.' do
11
+ expect { image.imageable = nil }.to change(image, :imageable_serial).from(author.serial).to nil
12
+ end
13
+ end
14
+
15
+ describe '`#[association_name]_[attribute_name]=(value)`' do
16
+ it 'sets the association value to nil if `value` is nil.' do
17
+ expect { image.imageable_serial = nil }.to change(image, :imageable).from(author).to nil
18
+ end
19
+
20
+ it 'sets the association value to the record queried by its `[attribute]` having the value `value` if such record exists.' do
21
+ new_author = Author.create!
22
+ expect { image.imageable_serial = new_author.serial }.to change(image, :imageable).from(author).to new_author
23
+ end
24
+
25
+ it 'raises NoMethodError if `[association_name]_type` is not set.' do
26
+ image.imageable = nil
27
+ expect { image.imageable_serial = author.serial }.to raise_exception NoMethodError, %{undefined method `find_by!' for nil:NilClass}
28
+ end
29
+
30
+ it 'raises ActiveRecord::RecordNotFound if record with `[attribute]` = `value` does not exist.', activerecord: [ '4.1' ] do
31
+ expect { image.imageable_serial = author.serial.next }.to raise_exception ActiveRecord::RecordNotFound
32
+ end
33
+
34
+ it 'raises ActiveRecord::RecordNotFound if record with `[attribute]` = `value` does not exist.', activerecord: [ '4.2', '5' ] do
35
+ expect { image.imageable_serial = author.serial.next }.to raise_exception ActiveRecord::RecordNotFound, "Couldn't find Author"
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,44 @@
1
+ RSpec.describe 'for polymorphic `has_many` association' do
2
+ let!(:book) { Book.create! }
3
+ let!(:image_1) { book.images.create! }
4
+ let!(:image_2) { book.images.create! }
5
+
6
+ it 'generates `#[association_name.singularize]_[attribute_name.pluralize]` accessor.' do
7
+ expect(book).to respond_to :image_serials, :image_serials=
8
+ end
9
+
10
+ describe '`#[association_name.singularize]_[attribute_name.pluralize]`' do
11
+ it 'returns an array with the values of `[attribute]` of the associated records.' do
12
+ expect(book.image_serials).to match_array [ image_1.serial, image_2.serial ]
13
+ expect { book.images = [] }.to change(book, :image_serials).to []
14
+ end
15
+ end
16
+
17
+ describe '`#[association_name.singularize]_[attribute_name.pluralize]`=(values)' do
18
+ it 'sets the association value to the records queried by `[attribute]` = `value` if all exist.' do
19
+ expect { book.image_serials = [] }.to change { book.images.ids }.from([ image_1.id, image_2.id ]).to []
20
+ expect { book.image_serials = [ image_1.serial ] }.to change { book.images.ids }.from([]).to [ image_1.id ]
21
+ end
22
+
23
+ it 'raises ActiveRecord::RecordNotFound if any of the given `values` has no record.', activerecord: [ '4' ] do
24
+ expect { book.image_serials = [ image_1.serial, image_2.serial.next ] }
25
+ .to raise_exception(ActiveRecord::RecordNotFound,
26
+ %{Couldn't find all Images with 'id': ("#{image_1.serial}", "#{image_2.serial.next}")} +
27
+ %{ (found 1 results, but was looking for 2)})
28
+ end
29
+
30
+ it 'raises ActiveRecord::RecordNotFound if any of the given `values` has no record.', activerecord: [ '5.0', '5.1' ] do
31
+ expect { book.image_serials = [ image_1.serial, image_2.serial.next ] }
32
+ .to raise_exception(ActiveRecord::RecordNotFound,
33
+ %{Couldn't find all Images with 'serial': ("#{image_1.serial}", "#{image_2.serial.next}")} +
34
+ %{ (found 1 results, but was looking for 2)})
35
+ end
36
+
37
+ it 'raises ActiveRecord::RecordNotFound if any of the given `values` has no record.', activerecord: [ '5.2' ] do
38
+ expect { book.image_serials = [ image_1.serial, image_2.serial.next ] }
39
+ .to raise_exception(ActiveRecord::RecordNotFound,
40
+ %{Couldn't find all Images with 'serial': ("#{image_1.serial}", "#{image_2.serial.next}")} +
41
+ %{ (found 1 results, but was looking for 2). Couldn't find Image with serial "#{image_2.serial.next}".})
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,33 @@
1
+ RSpec.describe 'for polymorphic `has_one` association' do
2
+ let!(:author) { Author.create! }
3
+ let!(:image) { author.create_image! }
4
+
5
+ it 'generates `#[association_name]_[attribute_name]` accessor.' do
6
+ expect(author).to respond_to :image_serial, :image_serial=
7
+ end
8
+
9
+ describe '`#[association_name]_[attribute_name]`' do
10
+ it 'returns the value of the `[attribute]` of the associated record if there is any.' do
11
+ expect { author.image = nil }.to change(author, :image_serial).from(image.serial).to nil
12
+ end
13
+ end
14
+
15
+ describe '`#[association_name]_[attribute_name]=(value)`' do
16
+ it 'sets the association value to nil if `value` is nil.' do
17
+ expect { author.image_serial = nil }.to change(author, :image).from(image).to nil
18
+ end
19
+
20
+ it 'sets the association value to the record queried by its `[attribute]` having the value `value` if such record exists.' do
21
+ new_image = Image.create!
22
+ expect { author.image_serial = new_image.serial }.to change(author, :image).from(image).to new_image
23
+ end
24
+
25
+ it 'raises ActiveRecord::RecordNotFound if record with `[attribute]` = `value` does not exist.', activerecord: [ '4.1' ] do
26
+ expect { author.image_serial = image.serial.next }.to raise_exception ActiveRecord::RecordNotFound
27
+ end
28
+
29
+ it 'raises ActiveRecord::RecordNotFound if record with `[attribute]` = `value` does not exist.', activerecord: [ '4.2', '5' ] do
30
+ expect { author.image_serial = image.serial.next }.to raise_exception ActiveRecord::RecordNotFound, "Couldn't find Image"
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ RSpec.describe 'for association with custom name' do
2
+ let!(:author) { Author.create! }
3
+ let!(:book) { Book.create! author: author }
4
+
5
+ it 'generates `#[association_name]_[attribute_name]` accessor.' do
6
+ expect(book).to respond_to :writer_serial, :writer_serial=
7
+ end
8
+
9
+ describe '`#[association_name]_[attribute_name]`' do
10
+ it 'returns the value of the `[attribute]` of the associated record if there is any.' do
11
+ expect { book.author = nil }.to change(book, :writer_serial).from(author.serial).to nil
12
+ end
13
+ end
14
+
15
+ describe '`#[association_name]_[attribute_name]=(value)`' do
16
+ it 'sets the association value to nil if `value` is nil.' do
17
+ expect { book.writer_serial = nil }.to change(book, :author).from(author).to nil
18
+ end
19
+
20
+ it 'sets the association value to the record queried by its `[attribute]` having the value `value` if such record exists.' do
21
+ new_author = Author.create!
22
+ expect { book.writer_serial = new_author.serial }.to change(book, :author).from(author).to new_author
23
+ end
24
+
25
+ it 'raises ActiveRecord::RecordNotFound if record with `[attribute]` = `value` does not exist.', activerecord: [ '4.1' ] do
26
+ expect { book.writer_serial = author.serial.next }.to raise_exception ActiveRecord::RecordNotFound
27
+ end
28
+
29
+ it 'raises ActiveRecord::RecordNotFound if record with `[attribute]` = `value` does not exist.', activerecord: [ '4.2', '5' ] do
30
+ expect { book.writer_serial = author.serial.next }.to raise_exception ActiveRecord::RecordNotFound, "Couldn't find Author"
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,47 @@
1
+ RSpec.describe 'the `with_attribute:` key' do
2
+ context 'if default is not set' do
3
+ it 'cannot be nil.' do
4
+ expect { Book.association_accessor_for :chapters }
5
+ .to raise_exception ArgumentError, 'missing keyword: with_attribute'
6
+ end
7
+
8
+ it 'can be a string.' do
9
+ expect(Book.new).not_to respond_to :chapter_serials, :chapter_serials=
10
+ expect { Book.association_accessor_for :chapters, with_attribute: 'serial' }.not_to raise_exception
11
+ expect(Book.new).to respond_to :chapter_serials, :chapter_serials=
12
+ end
13
+
14
+ it 'can be a symbol.' do
15
+ expect(Book.new).not_to respond_to :chapter_uuids, :chapter_uuids=
16
+ expect { Book.association_accessor_for :chapters, with_attribute: :uuid }.not_to raise_exception
17
+ expect(Book.new).to respond_to :chapter_uuids, :chapter_uuids=
18
+ end
19
+ end
20
+
21
+ context 'if default is set' do
22
+ before :all do
23
+ AssociationAccessors.default_attribute = :serial
24
+ end
25
+ after :all do
26
+ AssociationAccessors.default_attribute = nil
27
+ end
28
+
29
+ it 'can be nil, and default will be used.' do
30
+ expect(Address.new).not_to respond_to :author_serial, :author_serial=
31
+ expect { Address.association_accessor_for :author }.not_to raise_exception
32
+ expect(Address.new).to respond_to :author_serial, :author_serial=
33
+ end
34
+
35
+ it 'can be string to override default' do
36
+ expect(Address.new).not_to respond_to :author_uuid, :author_uuid=
37
+ expect { Address.association_accessor_for :author, with_attribute: 'uuid' }.not_to raise_exception
38
+ expect(Address.new).to respond_to :author_uuid, :author_uuid=
39
+ end
40
+
41
+ it 'can be symbol to override default' do
42
+ expect(Address.new).not_to respond_to :author_guuid, :author_guuid=
43
+ expect { Address.association_accessor_for :author, with_attribute: :guuid }.not_to raise_exception
44
+ expect(Address.new).to respond_to :author_guuid, :author_guuid=
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,203 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: association_accessors
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Szijjártó Nagy Misu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: appraisal
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.2'
97
+ description: Provides association accessors like `Company#user_serials` and `User#company_serial`
98
+ where `Company.has_many :users` and `User.belongs_to :company`.
99
+ email:
100
+ - szijjartonagy.misu@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".rspec"
106
+ - Appraisals
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - association_accessors.gemspec
112
+ - bin/test
113
+ - gemfiles/activerecord_4.1.gemfile
114
+ - gemfiles/activerecord_4.1.gemfile.lock
115
+ - gemfiles/activerecord_4.2.gemfile
116
+ - gemfiles/activerecord_4.2.gemfile.lock
117
+ - gemfiles/activerecord_5.0.gemfile
118
+ - gemfiles/activerecord_5.0.gemfile.lock
119
+ - gemfiles/activerecord_5.1.gemfile
120
+ - gemfiles/activerecord_5.1.gemfile.lock
121
+ - gemfiles/activerecord_5.2.gemfile
122
+ - gemfiles/activerecord_5.2.gemfile.lock
123
+ - gemfiles/rspec_2.gemfile
124
+ - gemfiles/rspec_2.gemfile.lock
125
+ - lib/association_accessors.rb
126
+ - lib/association_accessors/collection_association.rb
127
+ - lib/association_accessors/singular_association.rb
128
+ - lib/association_accessors/test/matcher.rb
129
+ - lib/association_accessors/version.rb
130
+ - spec/association_accessors_spec.rb
131
+ - spec/dummy.rb
132
+ - spec/matcher_spec.rb
133
+ - spec/spec_helper.rb
134
+ - spec/with_association/belongs_to_spec.rb
135
+ - spec/with_association/has_and_belongs_to_many_spec.rb
136
+ - spec/with_association/has_many_spec.rb
137
+ - spec/with_association/has_many_through_spec.rb
138
+ - spec/with_association/has_one_spec.rb
139
+ - spec/with_association/has_one_through_spec.rb
140
+ - spec/with_association/polymorphic/belongs_to_spec.rb
141
+ - spec/with_association/polymorphic/has_many_spec.rb
142
+ - spec/with_association/polymorphic/has_one_spec.rb
143
+ - spec/with_association/with_custom_name_spec.rb
144
+ - spec/with_attribute_spec.rb
145
+ homepage: https://github.com/SzNagyMisu/association_accessors
146
+ licenses:
147
+ - MIT
148
+ metadata:
149
+ homepage_uri: https://github.com/SzNagyMisu/association_accessors
150
+ source_code_uri: https://github.com/SzNagyMisu/association_accessors
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.7.6
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: Handling AR associations with other attributes than primary key
171
+ test_files:
172
+ - gemfiles/activerecord_4.1.gemfile
173
+ - gemfiles/activerecord_4.1.gemfile.lock
174
+ - gemfiles/activerecord_4.2.gemfile
175
+ - gemfiles/activerecord_4.2.gemfile.lock
176
+ - gemfiles/activerecord_5.0.gemfile
177
+ - gemfiles/activerecord_5.0.gemfile.lock
178
+ - gemfiles/activerecord_5.1.gemfile
179
+ - gemfiles/activerecord_5.1.gemfile.lock
180
+ - gemfiles/activerecord_5.2.gemfile
181
+ - gemfiles/activerecord_5.2.gemfile.lock
182
+ - gemfiles/rspec_2.gemfile
183
+ - gemfiles/rspec_2.gemfile.lock
184
+ - spec/association_accessors_spec.rb
185
+ - spec/dummy.rb
186
+ - spec/matcher_spec.rb
187
+ - spec/spec_helper.rb
188
+ - spec/with_association/belongs_to_spec.rb
189
+ - spec/with_association/has_and_belongs_to_many_spec.rb
190
+ - spec/with_association/has_many_spec.rb
191
+ - spec/with_association/has_many_through_spec.rb
192
+ - spec/with_association/has_one_spec.rb
193
+ - spec/with_association/has_one_through_spec.rb
194
+ - spec/with_association/polymorphic/belongs_to_spec.rb
195
+ - spec/with_association/polymorphic/has_many_spec.rb
196
+ - spec/with_association/polymorphic/has_one_spec.rb
197
+ - spec/with_association/with_custom_name_spec.rb
198
+ - spec/with_attribute_spec.rb
199
+ - Appraisals
200
+ - Gemfile
201
+ - Rakefile
202
+ - ".rspec"
203
+ - bin/test