fixture_dependencies 1.6.0 → 1.7.0

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: a28471e142b1c38f38fd7cdfcc5c3b45d86eb49c
4
- data.tar.gz: 2e5e9a34844cd0a6eae9b3e3e539f4007bc19ed8
3
+ metadata.gz: f8ad357a8dd82bac97d857dbff858e3e319b37a8
4
+ data.tar.gz: fdadc103e8677cd3cbfc4ea66469ec239162925d
5
5
  SHA512:
6
- metadata.gz: 67a1332884ebc8316a9b4f9e329cd854f6dd393537444928a1462112a505861faa1998e197868f31be479dfdf05aa75479d20952a83bc4bcd99b833a0a231dd1
7
- data.tar.gz: cce165d367a71e9fbe5a7b5de4879812cccd4b27602e951595b386c1c359906c2375ca6335c7854bc6e8973e015263c59bd802353eb148bcb823b45053a648dc
6
+ metadata.gz: 94338b1ff1c00c1da084ab47ebe717d0623a6ba2c762b104626d1da58cb92f0d35c3188f9423492f877f2a568577ab3819aca330d809e96ac48365e402f01b06
7
+ data.tar.gz: 00230fb715dbec6733402c0ec3400324f3d872fdcc1966d0611bce385e122f0d65b77a4326343547fc4fd2520ecf06c82b3eec0214d49b39a6cb4b07ad765314
data/README.md CHANGED
@@ -187,6 +187,33 @@ the loosest coupling possible. Here's an example:
187
187
  Don't worry about loading the same fixture twice, if a fixture is already
188
188
  loaded, it won't attempt to load it again.
189
189
 
190
+ ## Loading attributes only
191
+
192
+ You can load only the attributes of fixtures, without saving them with
193
+ load\_attributes. This is useful for occasions where you want to mutate
194
+ attributes without having to create lots of fixtures or want to test
195
+ code that is run before or after the database transaction (validations,
196
+ model hooks).
197
+
198
+ ```
199
+ # load_attributes responds like load, but without saving the record
200
+ fruit = load_attributes(:fruit__banana)
201
+ # test the behaviour before saving the record
202
+ fruit.save
203
+ # test the behaviour after saving the record
204
+ ```
205
+
206
+ You can also use the build method for loading the attributes of a
207
+ single record, merging the attributes passed as options. This is useful
208
+ for testing changes in behaviour when mutating a single parameter:
209
+
210
+ ```
211
+ old_banana = build(:fruit__banana, :age=>'old')
212
+ fresh_banana = build(:fruit__banana, :age=>'new')
213
+ old_banana.must_be :rotten?
214
+ new_banana.wont_be :rotten?
215
+ ```
216
+
190
217
  ## one_to_many/many_to_many/has_many/has_and_belongs_to_many assocations
191
218
 
192
219
  Here's an example of using has_one (logon_information), has_many (assets), and
@@ -31,6 +31,26 @@ class FixtureDependencies
31
31
  # This will load the data from the yaml files for each argument whose model
32
32
  # is not already in the fixture hash.
33
33
  def self.load(*records)
34
+ load_with_options(records)
35
+ end
36
+
37
+ # Load the attributes for the record arguments. This method responds
38
+ # to the same interface as 'load', the difference being that has_many
39
+ # associations are not loaded.
40
+ def self.load_attributes(*records)
41
+ load_with_options(records, :attributes_only=>true)
42
+ end
43
+
44
+ # Loads the attribute for a single record, merging optional attributes.
45
+ def self.build(record, attributes = {})
46
+ obj = FixtureDependencies.load_attributes([record])
47
+
48
+ attributes.each { |key, value| obj.send("#{key}=", value) }
49
+
50
+ obj
51
+ end
52
+
53
+ def self.load_with_options(records, opts = {})
34
54
  ret = records.map do |record|
35
55
  if record.is_a?(Hash)
36
56
  record.map do |k, vals|
@@ -43,14 +63,14 @@ class FixtureDependencies
43
63
  end.flatten.compact.map do |record|
44
64
  model_name, name = split_name(record)
45
65
  if name
46
- use(record.to_sym)
66
+ use(record.to_sym, opts)
47
67
  else
48
68
  model_name = model_name.singularize
49
69
  unless loaded[model_name.to_sym]
50
70
  puts "loading #{model_name}.yml" if verbose > 0
51
71
  load_yaml(model_name)
52
72
  end
53
- fixtures[model_name.to_sym].keys.map{|name| use(:"#{model_name}__#{name}")}
73
+ fixtures[model_name.to_sym].keys.map{|name| use(:"#{model_name}__#{name}", opts)}
54
74
  end
55
75
  end
56
76
  ret.length == 1 ? ret[0] : ret
@@ -142,7 +162,7 @@ class << FixtureDependencies
142
162
  # the database, return it. Will check the yaml file for fixtures if no
143
163
  # fixtures yet exist for the model. If the fixture isn't in the fixture
144
164
  # hash, raise an error.
145
- def use(record, loading = [], procs = {})
165
+ def use(record, opts = {}, loading = [], procs = {})
146
166
  spaces = " " * loading.length
147
167
  puts "#{spaces}using #{record}" if verbose > 0
148
168
  puts "#{spaces}load stack:#{loading.inspect}" if verbose > 1
@@ -218,7 +238,7 @@ class << FixtureDependencies
218
238
  else
219
239
  # Regular assocation, load it
220
240
  puts "#{spaces}#{record}.#{attr}: belongs_to:#{dep_name}" if verbose > 1
221
- use(dep_name, loading, procs)
241
+ use(dep_name, {}, loading, procs)
222
242
  value = get(dep_name)
223
243
  end
224
244
  else
@@ -230,6 +250,8 @@ class << FixtureDependencies
230
250
  obj.send("#{attr}=", value)
231
251
  end
232
252
 
253
+ return obj if opts[:attributes_only]
254
+
233
255
  puts "#{spaces}saving #{record}" if verbose > 1
234
256
 
235
257
  model_method(:model_save, mtype, obj)
@@ -260,7 +282,7 @@ class << FixtureDependencies
260
282
  else
261
283
  # Regular association, add it
262
284
  puts "#{spaces}#{record}.#{attr}: #{rtype}:#{dep_name}" if verbose > 1
263
- model_method(:add_associated_object, mtype, reflection, attr, obj, use(dep_name, loading, procs))
285
+ model_method(:add_associated_object, mtype, reflection, attr, obj, use(dep_name, {}, loading, procs))
264
286
  end
265
287
  end
266
288
  end
@@ -30,4 +30,12 @@ example_group.class_eval do
30
30
  def load(*args)
31
31
  FixtureDependencies.load(*args)
32
32
  end
33
+
34
+ def load_attributes(*args)
35
+ FixtureDependencies.load_attributes(*args)
36
+ end
37
+
38
+ def build(*args)
39
+ FixtureDependencies.build(*args)
40
+ end
33
41
  end
@@ -9,6 +9,14 @@ module Test
9
9
  def load(*fixture)
10
10
  FixtureDependencies.load(*fixture)
11
11
  end
12
+
13
+ def load_attributes(*args)
14
+ FixtureDependencies.load_attributes(*args)
15
+ end
16
+
17
+ def build(*args)
18
+ FixtureDependencies.build(*args)
19
+ end
12
20
  end
13
21
  end
14
22
  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.6.0
4
+ version: 1.7.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-08-31 00:00:00.000000000 Z
11
+ date: 2016-02-26 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.1
54
+ rubygems_version: 2.5.1
55
55
  signing_key:
56
56
  specification_version: 4
57
57
  summary: Sequel/ActiveRecord fixture loader that handles dependency graphs