fixture_dependencies 1.5.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +58 -0
  3. data/lib/fixture_dependencies.rb +22 -1
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8f97d8604529a86b8c656e191bdb5a3c7ee1cbcd
4
- data.tar.gz: 344a6f88b868604949ecd69f1cc616fa0fc92772
3
+ metadata.gz: a28471e142b1c38f38fd7cdfcc5c3b45d86eb49c
4
+ data.tar.gz: 2e5e9a34844cd0a6eae9b3e3e539f4007bc19ed8
5
5
  SHA512:
6
- metadata.gz: 9afe16c93a4dde823b5929a3e1437e90a42654b35246f95dcb21e4c01ed0f9c986d3d40e558a30ee0b31154d7ab793e9671fedb99febf5213ae1170055d1fb6d
7
- data.tar.gz: 2412abac028e52a3dd52678eb539902eac6a7d28909b56433f98a107e41f0577a9adfa0a47301870f5dedf6817f4b560725c5d680c69cde2b9e0fa8f497ea36d
6
+ metadata.gz: 67a1332884ebc8316a9b4f9e329cd854f6dd393537444928a1462112a505861faa1998e197868f31be479dfdf05aa75479d20952a83bc4bcd99b833a0a231dd1
7
+ data.tar.gz: cce165d367a71e9fbe5a7b5de4879812cccd4b27602e951595b386c1c359906c2375ca6335c7854bc6e8973e015263c59bd802353eb148bcb823b45053a648dc
data/README.md CHANGED
@@ -221,6 +221,64 @@ fixtures. That doesn't work with fixture dependencies, as there is no
221
221
  associated model. Instead, you use a has_and_belongs_to_many association name
222
222
  in the the appropriate model fixtures (see above).
223
223
 
224
+ ## belongs_to/many_to_one polymorphic fixtures
225
+
226
+ ActiveRecord supports polymorphic associations by default. With Sequel, this
227
+ is made via the `sequel_polymorphic` gem.
228
+
229
+ Here the mapping in Rails:
230
+
231
+ ```
232
+ class Animal < ActiveRecord::Base
233
+ has_many :fruits, as: :eater
234
+ end
235
+ class Fruit < ActiveRcord::Base
236
+ belongs_to :eater, polymorphic: true
237
+ end
238
+ ```
239
+
240
+ And here on Sequel:
241
+
242
+ ```
243
+ require 'sequel_polymorphic'
244
+ class Animal < Sequel::Model
245
+ plugin :polymorphic
246
+ ony_to_many :fruits, as: :eater
247
+ end
248
+ class Fruit < Sequel::Model
249
+ plugin :polymorphic
250
+ many_to_one :eater, polymorphic: true
251
+ end
252
+ ```
253
+
254
+ In both cases, the fixtures looks like:
255
+
256
+ `animals.yml`:
257
+
258
+ ```
259
+ george:
260
+ id: 1
261
+ name: George
262
+ ```
263
+
264
+ `fruits.yml`:
265
+
266
+ ```
267
+ apple:
268
+ id: 1
269
+ name: Apple
270
+ eater: george (Animal)
271
+ ```
272
+
273
+ In your test, use something like this:
274
+
275
+ ```
276
+ apple = load(:fruit__apple)
277
+ apple.eater.name.must_equal "George"
278
+ ```
279
+
280
+ fixture_dependencies will set the `eater` association in `Fruit` instance `george` instance of `Animal`.
281
+
224
282
  ## Cyclic dependencies
225
283
 
226
284
  fixture_dependencies handles almost all cyclic dependencies. It handles all
@@ -121,7 +121,7 @@ class << FixtureDependencies
121
121
  raise TypeError, 'not ActiveRecord or Sequel model'
122
122
  end
123
123
  end
124
-
124
+
125
125
  # Return the class associated with the given model_name. By default, the
126
126
  # class name is automagically derived from the model name, however this
127
127
  # can be overridden by <tt>FixtureDependencies.class_map[:name] =
@@ -186,6 +186,12 @@ class << FixtureDependencies
186
186
  next if attr.is_a?(Array)
187
187
  if reflection = model_method(:reflection, mtype, model, attr.to_sym)
188
188
  if [:belongs_to, :many_to_one].include?(model_method(:reflection_type, mtype, reflection))
189
+
190
+ if polymorphic_association?(value)
191
+ value, polymorphic_class = polymorphic_association(value)
192
+ reflection[:class_name] = polymorphic_class
193
+ end
194
+
189
195
  dep_name = "#{model_method(:reflection_class, mtype, reflection).name.underscore}__#{value}".to_sym
190
196
  if dep_name == record
191
197
  # Self referential record, use primary key
@@ -278,4 +284,19 @@ class << FixtureDependencies
278
284
  fpk.map{|v| attributes[v]}
279
285
  end
280
286
  end
287
+
288
+ # Polymorphic when value has the class indication
289
+ # Example: john (Account)
290
+ # => true
291
+ def polymorphic_association?(value)
292
+ polymorphic_association(value).size == 2
293
+ end
294
+
295
+ # Extract association id and association_class
296
+ # Example: addressable: john (Account)
297
+ # => ["john", "Account"]
298
+ def polymorphic_association(value)
299
+ value.to_s.scan(/(.*)\s\((.*)\)/).flatten
300
+ end
301
+
281
302
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixture_dependencies
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Evans
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-28 00:00:00.000000000 Z
11
+ date: 2015-08-31 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: code@jeremyevans.net
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  version: '0'
52
52
  requirements: []
53
53
  rubyforge_project:
54
- rubygems_version: 2.4.5
54
+ rubygems_version: 2.4.5.1
55
55
  signing_key:
56
56
  specification_version: 4
57
57
  summary: Sequel/ActiveRecord fixture loader that handles dependency graphs