seed_helper 1.8.0 → 1.9.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f1942680d76a807e344ad2e7f847c0bb08b82b73
4
- data.tar.gz: 3359c7b204678197cd1e2c73d38b0817a8b0aaf1
3
+ metadata.gz: dfc6dbc5105c39c42b3fefac804b1283552ffb41
4
+ data.tar.gz: 1858088eedc41671ab90953fa829be840b282461
5
5
  SHA512:
6
- metadata.gz: e0ac9998fd0a2f994b0bf45ded6e9d1d1713afca81b0e395f0f4b9a6347edec69dd973369c60bee290cd6f577e1e15e1c88a8008bedf0ea16e6aed9ec66c49a9
7
- data.tar.gz: 2fdae7db2b14e0ee7626bc9fdbfdbab0ce5119fe58f2eda94a7bcc6d524515d3a96088cf8db654ad81ad665f3c1056dd8867b8c986801564922f4405229cb186
6
+ metadata.gz: 30014d393b676042af0efea31f307c6cf69534cef51c57de00b7efa6d137f5b1753343eaaede91b699f9c84c3d60f8da799de0df79104324dc2b68c82b5b4e32
7
+ data.tar.gz: bc0f3de3e9d77e8bf4f8dd7260836395ac5ec2033ae6b895d4fef59e8037e92d411bc839a9b2eb79d9e24a241742937709baa9322689142a9efa7c31b39c295f
data/README.md CHANGED
@@ -26,7 +26,7 @@ SeedHelper.create_seed_task(:create_roles) do
26
26
  # Will print out a red message if Role fails to save
27
27
  # Will print out a green message is Role succesfully creates
28
28
  # Will print out a cyan message if Role already exists
29
- role = SeedHelper.find_or_create_resource(Role, {name: role_name})
29
+ SeedHelper.find_or_create_resource(Role, {name: role_name})
30
30
 
31
31
  end
32
32
 
@@ -63,7 +63,22 @@ In that case, we can pass through a third argument of all those additional field
63
63
  SeedHelper.find_or_create_resource(User, {email: email}, {password: "password", role: role_name})
64
64
  ```
65
65
 
66
- ## Example: Using FactoryGirl
66
+ ## Example: Using FactoryGirl for a single object
67
+
68
+ Sometimes you'll want to create an object using FactoryGirl. You can pass a block to `.find_or_create_resource` to achieve this. EG:
69
+
70
+ ```ruby
71
+ email = "admin@example.com"
72
+ SeedHelper.find_or_create_resource(User, {email: email}) do
73
+ FactoryGirl.build(:user, :admin, :with_profile_picture, email: email, password: "Alligator8")
74
+ end
75
+ ```
76
+
77
+ This will return a User that matches {email: "admin@example.com"} (and print out a message saying the object already exists), or it will use the FG call to create a new object (and return a success message if the object returned is valid).
78
+
79
+ NB: You should return an unpersisted object in this block.
80
+
81
+ ## Example: Using FactoryGirl for a collection of objects
67
82
 
68
83
  If you don't want to provide specific attributes, you can use the `.bulk_create` function to do so. EG:
69
84
 
data/lib/seed_helper.rb CHANGED
@@ -18,11 +18,15 @@ class SeedHelper
18
18
  # identify the given resource. EG: {email: "jordan@example.com"}
19
19
  # @param [Hash] additional_attributes: A hash of attributes and values that don't identify
20
20
  # the given resource, but should be assigned to the new resource.
21
- def self.find_or_create_resource(resource_class, identifiable_attributes, additional_attributes={})
21
+ def self.find_or_create_resource(resource_class, identifiable_attributes, additional_attributes={}, &constructor)
22
22
  if resource = find_resource(resource_class, identifiable_attributes)
23
23
  resource_already_exists(resource)
24
24
  else
25
- resource = resource_class.new(identifiable_attributes.merge(additional_attributes))
25
+ if constructor.present?
26
+ resource = constructor.call
27
+ else
28
+ resource = resource_class.new(identifiable_attributes.merge(additional_attributes))
29
+ end
26
30
  create_resource(resource)
27
31
  end
28
32
  return resource
@@ -1,3 +1,3 @@
1
1
  class SeedHelper
2
- VERSION = "1.8.0"
2
+ VERSION = "1.9.0"
3
3
  end
@@ -6,6 +6,15 @@ describe SeedHelper do
6
6
  subject { SeedHelper.find_or_create_resource(User, {email: email}, additional_args) }
7
7
  let(:email) { "jordan@example.com" }
8
8
 
9
+ it "can build the object via a block" do
10
+ new_user = SeedHelper.find_or_create_resource(User, {email: email}) do
11
+ User.new(email: "jordan.rules@example.com")
12
+ end
13
+
14
+ expect(new_user).to be_kind_of(User)
15
+ expect(new_user.email).to eq("jordan.rules@example.com")
16
+ end
17
+
9
18
  context "without providing additional args" do
10
19
  let(:additional_args) { {} }
11
20
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seed_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.0
4
+ version: 1.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Maguire
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-21 00:00:00.000000000 Z
11
+ date: 2016-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize