deferring 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 425c85b43ca58b47075b81fd98800f7ca2cb530e
4
- data.tar.gz: 17649b71b066711330887c6a1170c2070baca519
3
+ metadata.gz: 0807e9de82ca4e5593921580b87eaa83bc6ed3b5
4
+ data.tar.gz: cba881489e3cbb1e43a6b87e617eb4a28a3f83ef
5
5
  SHA512:
6
- metadata.gz: 4be333a99bf094965edf5f1636f84d039525d873844d4ba62ca75645676893d73ee1106948acd12b3f6302d231e441a323464663f2927e1b02a90fa1bc5bc709
7
- data.tar.gz: 2c5d2de74c766c08956095a851a4f7405596327a5bc98e2ce9ad9e8b0d5b7b98f1b3040ec412423afe729e6f000c123293d36abe3e7a663bd5446d2c61c6bdef
6
+ metadata.gz: 3250b185fc57e85c66a1b25b84ad111a1d98b31a76fa434e6587c16a52551d0cc9b43a2ea2abd4b7f963ad723edfe8cbb1807e12e10010d5f37865b8e9d1b198
7
+ data.tar.gz: 137c400ee0f8270e69951d5e49c6c61ee5849285983c8121a0dd1fa2aba9692a84d340bd063719445630ed92144268c4ab1fd782b4777306bed47af8618da1cb
@@ -175,8 +175,10 @@ module Deferring
175
175
  define_method :"#{association_name.singularize}_ids=" do |ids|
176
176
  find_or_create_deferred_association(association_name, listeners, inverse_association_name)
177
177
 
178
+ ids ||= []
178
179
  klass = self.class.reflect_on_association(:"#{association_name}").klass
179
180
  objects = klass.find(ids.reject(&:blank?))
181
+
180
182
  send(:"deferred_#{association_name}").objects = objects
181
183
  end
182
184
 
@@ -192,6 +194,7 @@ module Deferring
192
194
  attr_accessor :"#{association_name}_checked"
193
195
  # collection_singular_checked=
194
196
  define_method(:"#{association_name}_checked=") do |ids|
197
+ ids ||= ''
195
198
  send(:"#{association_name.singularize}_ids=", ids.split(','))
196
199
  end
197
200
 
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module Deferring
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
@@ -101,14 +101,65 @@ RSpec.describe 'deferred has_and_belongs_to_many associations' do
101
101
  expect{ bob.save }.to change{ Person.where(name: 'Bob').first.teams.size }.from(0).to(1)
102
102
  end
103
103
 
104
- describe '#collection_checked=' do
104
+ it 'unlinks all records when assigning empty array' do
105
+ bob.team_ids = [dba.id, operations.id]
106
+ bob.save
107
+
108
+ bob.team_ids = []
109
+ expect(bob.teams.length).to eq(0)
110
+
111
+ expect{ bob.save }.to change{ Person.where(name: 'Bob').first.teams.size }.from(2).to(0)
112
+ end
113
+
114
+ it 'unlinks all records when assigning nil' do
115
+ bob.team_ids = [dba.id, operations.id]
116
+ bob.save
117
+
118
+ bob.team_ids = nil
119
+ expect(bob.teams.length).to eq(0)
120
+
121
+ expect{ bob.save }.to change{ Person.where(name: 'Bob').first.teams.size }.from(2).to(0)
122
+ end
123
+ end
124
+
125
+ describe '#collection_checked=' do
126
+
127
+ it 'set associated records' do
128
+ bob.teams_checked = "#{dba.id},#{operations.id}"
129
+ expect{ bob.save }.to change{ Person.where(name: 'Bob').first.teams.size }.from(0).to(2)
130
+ end
105
131
 
106
- it 'set associated records' do
107
- bob.teams_checked = [dba.id, operations.id]
132
+ it 'replace existing records when assigning a new set of ids of records' do
133
+ bob.team_ids = [dba.id, operations.id]
134
+ bob.save
135
+
136
+ # Replace DBA and Operations by Support.
137
+ bob.teams_checked = "#{support.id}"
138
+
139
+ expect{ bob.save }.to change{ Person.where(name: 'Bob').first.teams.size }.from(2).to(1)
140
+ expect(bob.teams.first).to eq(support)
141
+ end
142
+
143
+ it 'unlinks all records when assigning empty string' do
144
+ bob.team_ids = [dba.id, operations.id]
145
+ bob.save
146
+
147
+ # Unlinks DBA and Operations.
148
+ bob.teams_checked = ""
149
+ expect(bob.teams.length).to eq(0)
150
+
151
+ expect{ bob.save }.to change{ Person.where(name: 'Bob').first.teams.size }.from(2).to(0)
152
+ end
153
+
154
+ it 'unlinks all records when assigning nil' do
155
+ bob.team_ids = [dba.id, operations.id]
156
+ bob.save
108
157
 
109
- expect{ bob.save }.to change{ Person.where(name: 'Bob').first.teams.size }.from(0).to(2)
110
- end
158
+ # Unlinks DBA and Operations.
159
+ bob.teams_checked = nil
160
+ expect(bob.teams.length).to eq(0)
111
161
 
162
+ expect{ bob.save }.to change{ Person.where(name: 'Bob').first.teams.size }.from(2).to(0)
112
163
  end
113
164
 
114
165
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deferring
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robin Roestenburg
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-04 00:00:00.000000000 Z
11
+ date: 2014-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord