seed_helper 1.9.0 → 1.11.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: dfc6dbc5105c39c42b3fefac804b1283552ffb41
4
- data.tar.gz: 1858088eedc41671ab90953fa829be840b282461
3
+ metadata.gz: 048e17c475ef146eb29e46267161a5cd96578edd
4
+ data.tar.gz: 611c97c09747d53bcb84b688ad288d993ddaa4d7
5
5
  SHA512:
6
- metadata.gz: 30014d393b676042af0efea31f307c6cf69534cef51c57de00b7efa6d137f5b1753343eaaede91b699f9c84c3d60f8da799de0df79104324dc2b68c82b5b4e32
7
- data.tar.gz: bc0f3de3e9d77e8bf4f8dd7260836395ac5ec2033ae6b895d4fef59e8037e92d411bc839a9b2eb79d9e24a241742937709baa9322689142a9efa7c31b39c295f
6
+ metadata.gz: 80438d1fa54715f4c2808e01ebda9f004c641182f169e0c75d6035b20d34412e489ff6b16ff0019035f51b3d2d9d0484786334ec37ee107baa9e4ad5f8c9aca6
7
+ data.tar.gz: 4172124ba98660806a1b9eb11d589a479f6c0d99d88ce19c21f352bf1f1d7047410eaa7e5dc2376fa9ab809a0d5a4975f1c5e2814162f51f65ef08040018f80f
data/README.md CHANGED
@@ -68,9 +68,8 @@ SeedHelper.find_or_create_resource(User, {email: email}, {password: "password",
68
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
69
 
70
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")
71
+ SeedHelper.find_or_create_resource(User, {email: "admin@example.com"}, {password: "Alligator8"}) do |attributes|
72
+ FactoryGirl.build(:user, :admin, :with_profile_picture, attributes)
74
73
  end
75
74
  ```
76
75
 
@@ -91,6 +90,15 @@ SeedHelper.bulk_create(MyClass) do
91
90
  end
92
91
  ```
93
92
 
93
+ If you want to bulk create some associated objects for a given object, you can pass a hash of attributes to scope the `.bulk_create` by:
94
+
95
+ ```ruby
96
+ Client.all.each do |client|
97
+ SeedHelper.bulk_create(User, {client_id: client.id}) do |client_attributes|
98
+ FactoryGirl.create_list(:user, 5, client_attributes)
99
+ end
100
+ end
101
+
94
102
  ## Example: Output
95
103
 
96
104
  SeedHelper provides multiple methods for showing output that can be used outside of the `create_resource` method:
data/lib/seed_helper.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  require 'colorize'
2
2
  require 'seed_helper/version'
3
+ require 'seed_helper/bulk_create'
3
4
  require 'seed_helper/output_formatter'
4
5
  require 'seed_helper/rake_helper'
5
6
 
6
7
  class SeedHelper
8
+ extend SeedHelper::BulkCreate
7
9
  extend SeedHelper::OutputFormatter
8
10
  extend SeedHelper::RakeHelper
9
11
 
@@ -23,7 +25,7 @@ class SeedHelper
23
25
  resource_already_exists(resource)
24
26
  else
25
27
  if constructor.present?
26
- resource = constructor.call
28
+ resource = constructor.call(identifiable_attributes.merge(additional_attributes))
27
29
  else
28
30
  resource = resource_class.new(identifiable_attributes.merge(additional_attributes))
29
31
  end
@@ -43,21 +45,6 @@ class SeedHelper
43
45
  did_save
44
46
  end
45
47
 
46
- def self.bulk_create(klass, &creation_block)
47
- klass_plural = klass.name.pluralize
48
-
49
- if klass.any?
50
- resource_already_exists(klass_plural)
51
- else
52
- begin
53
- creation_block.call
54
- success("Created #{klass_plural}")
55
- rescue
56
- error("Failed to create #{klass_plural}: #{$!}")
57
- end
58
- end
59
- end
60
-
61
48
  private
62
49
 
63
50
  def self.find_resource(resource_class, attributes)
@@ -0,0 +1,42 @@
1
+ module SeedHelper::BulkCreate
2
+
3
+ # Allow an easy means of using something like FactoryGirl.create_list to seed a number
4
+ # of records.
5
+ #
6
+ # If any instances of resource_class with matching identifiable_attributes exists, present a
7
+ # resource_already_exists message.
8
+ #
9
+ # Otherwise, run the provided creation_block.
10
+ #
11
+ # @param [Class] resource_class: The class to create the resource in. EG: User
12
+ # @param [Hash] identifiable_attributes: A hash of attributes and values that can be used to
13
+ # identify the given resource. EG: {email: "jordan@example.com"}
14
+ # @params [Proc] creation_block: A block that will create some objects
15
+ def bulk_create(klass, identifiable_attributes={}, &creation_block)
16
+ message_identifier = bulk_create_message_identifier(klass, identifiable_attributes)
17
+
18
+ if klass.where(identifiable_attributes).exists?
19
+ resource_already_exists(message_identifier)
20
+ else
21
+ begin
22
+ creation_block.call(identifiable_attributes)
23
+ success("Created #{message_identifier}")
24
+ rescue
25
+ error("Failed to create #{message_identifier}: #{$!}")
26
+ end
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def bulk_create_message_identifier(klass, identifiable_attributes)
33
+ klass_plural = klass.name.pluralize
34
+
35
+ if identifiable_attributes.any?
36
+ "#{klass_plural} (#{identifiable_attributes})"
37
+ else
38
+ klass_plural
39
+ end
40
+ end
41
+
42
+ end
@@ -1,3 +1,3 @@
1
1
  class SeedHelper
2
- VERSION = "1.9.0"
2
+ VERSION = "1.11.0"
3
3
  end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe SeedHelper::BulkCreate do
4
+
5
+ describe '#bulk_create' do
6
+ subject { SeedHelper.bulk_create(User, identifiable_attributes) { User.create(email: email)} }
7
+
8
+ let(:email) { "jordan@example.com" }
9
+
10
+ context "with identifiable_attributes" do
11
+ let(:identifiable_attributes) { {email: email} }
12
+
13
+ context "when a User with those attributes already exists" do
14
+ before { User.create!(email: email) }
15
+
16
+ it "sends an 'already exists' message" do
17
+ expect(SeedHelper).to receive(:resource_already_exists).with("Users ({:email=>\"jordan@example.com\"})")
18
+ subject
19
+ end
20
+ end
21
+
22
+ context "when no User with those attributes already exists" do
23
+ before { User.create!(email: "other@example.com") }
24
+
25
+ it "runs the given block" do
26
+ expect { subject }.to change { User.count }.by(1)
27
+ end
28
+
29
+ it "prints a success message" do
30
+ expect(SeedHelper).to receive(:success).with("Created Users ({:email=>\"jordan@example.com\"})")
31
+ subject
32
+ end
33
+ end
34
+ end
35
+
36
+ context "with no identifiable_attributes" do
37
+ let(:identifiable_attributes) { {} }
38
+
39
+ context "when a User already exists" do
40
+ before { User.create! }
41
+
42
+ it "sends an 'already exists' message" do
43
+ expect(SeedHelper).to receive(:resource_already_exists).with("Users")
44
+ subject
45
+ end
46
+ end
47
+
48
+ context "when no Users exist" do
49
+ it "runs the given block" do
50
+ expect { subject }.to change { User.count }.to(1)
51
+ end
52
+
53
+ it "prints a success message" do
54
+ expect(SeedHelper).to receive(:success).with("Created Users")
55
+ subject
56
+ end
57
+ end
58
+ end
59
+
60
+ end
61
+
62
+ end
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.9.0
4
+ version: 1.11.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-03-15 00:00:00.000000000 Z
11
+ date: 2016-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -57,11 +57,13 @@ files:
57
57
  - README.md
58
58
  - Rakefile
59
59
  - lib/seed_helper.rb
60
+ - lib/seed_helper/bulk_create.rb
60
61
  - lib/seed_helper/output_formatter.rb
61
62
  - lib/seed_helper/rake_helper.rb
62
63
  - lib/seed_helper/version.rb
63
64
  - license.txt
64
65
  - seed_helper.gemspec
66
+ - spec/lib/seed_helper/bulk_create_spec.rb
65
67
  - spec/lib/seed_helper/output_formatter_spec.rb
66
68
  - spec/lib/seed_helper_spec.rb
67
69
  - spec/spec_helper.rb
@@ -90,6 +92,7 @@ signing_key:
90
92
  specification_version: 4
91
93
  summary: Make seeding data easier in Rails projects
92
94
  test_files:
95
+ - spec/lib/seed_helper/bulk_create_spec.rb
93
96
  - spec/lib/seed_helper/output_formatter_spec.rb
94
97
  - spec/lib/seed_helper_spec.rb
95
98
  - spec/spec_helper.rb