seed_helper 1.7.0 → 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/Gemfile +6 -1
- data/README.md +12 -2
- data/lib/seed_helper.rb +14 -3
- data/lib/seed_helper/version.rb +1 -1
- data/spec/lib/{seed_formatter_spec.rb → seed_helper/output_formatter_spec.rb} +4 -4
- data/spec/lib/seed_helper_spec.rb +53 -0
- data/spec/spec_helper.rb +30 -2
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f1942680d76a807e344ad2e7f847c0bb08b82b73
|
4
|
+
data.tar.gz: 3359c7b204678197cd1e2c73d38b0817a8b0aaf1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0ac9998fd0a2f994b0bf45ded6e9d1d1713afca81b0e395f0f4b9a6347edec69dd973369c60bee290cd6f577e1e15e1c88a8008bedf0ea16e6aed9ec66c49a9
|
7
|
+
data.tar.gz: 2fdae7db2b14e0ee7626bc9fdbfdbab0ce5119fe58f2eda94a7bcc6d524515d3a96088cf8db654ad81ad665f3c1056dd8867b8c986801564922f4405229cb186
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.2.2
|
data/Gemfile
CHANGED
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.
|
29
|
+
role = SeedHelper.find_or_create_resource(Role, {name: role_name})
|
30
30
|
|
31
31
|
end
|
32
32
|
|
@@ -53,6 +53,16 @@ SeedHelper.create_seed_task(:create_users, [:create_roles]) do
|
|
53
53
|
end
|
54
54
|
```
|
55
55
|
|
56
|
+
By default, the `.find_or_create_resource` method uses the hash you provide it to find a User with those attributes. It can then determine whether or not it needs to create a new User.
|
57
|
+
|
58
|
+
This becomes problematic when we have a lot of fields, since we don't want to try and identify a User by some of those fields - password, for example.
|
59
|
+
|
60
|
+
In that case, we can pass through a third argument of all those additional fields:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
SeedHelper.find_or_create_resource(User, {email: email}, {password: "password", role: role_name})
|
64
|
+
```
|
65
|
+
|
56
66
|
## Example: Using FactoryGirl
|
57
67
|
|
58
68
|
If you don't want to provide specific attributes, you can use the `.bulk_create` function to do so. EG:
|
@@ -93,4 +103,4 @@ SeedHelper.create_seed_task(:second_task_name, [:task_name])
|
|
93
103
|
# No dependencies (other than environment) but with a custom title. This will show up as
|
94
104
|
# description of task in `rake -T` and will be printed to screen when task runs
|
95
105
|
SeedHelper.create_seed_task(:third_task_name, [], "My sick name")
|
96
|
-
```
|
106
|
+
```
|
data/lib/seed_helper.rb
CHANGED
@@ -7,11 +7,22 @@ class SeedHelper
|
|
7
7
|
extend SeedHelper::OutputFormatter
|
8
8
|
extend SeedHelper::RakeHelper
|
9
9
|
|
10
|
-
|
11
|
-
|
10
|
+
# If a resource_class with matching identifiable_attributes exists, return that user and present a
|
11
|
+
# resource_already_exists message.
|
12
|
+
#
|
13
|
+
# Otherwise, create a new instance of resource_class using the identifiable_attributes and
|
14
|
+
# additional_attributes.
|
15
|
+
#
|
16
|
+
# @param [Class] resource_class: The class to create the resource in. EG: User
|
17
|
+
# @param [Hash] identifiable_attributes: A hash of attributes and values that can be used to
|
18
|
+
# identify the given resource. EG: {email: "jordan@example.com"}
|
19
|
+
# @param [Hash] additional_attributes: A hash of attributes and values that don't identify
|
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={})
|
22
|
+
if resource = find_resource(resource_class, identifiable_attributes)
|
12
23
|
resource_already_exists(resource)
|
13
24
|
else
|
14
|
-
resource = resource_class.new(
|
25
|
+
resource = resource_class.new(identifiable_attributes.merge(additional_attributes))
|
15
26
|
create_resource(resource)
|
16
27
|
end
|
17
28
|
return resource
|
data/lib/seed_helper/version.rb
CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe SeedHelper do
|
4
4
|
|
5
5
|
describe '#output' do
|
6
|
-
subject { SeedHelper.output
|
6
|
+
subject { SeedHelper.output(message, options) }
|
7
7
|
let(:message) { "message" }
|
8
8
|
let(:options) { {} }
|
9
9
|
|
@@ -38,7 +38,7 @@ describe SeedHelper do
|
|
38
38
|
end
|
39
39
|
|
40
40
|
describe '#message' do
|
41
|
-
subject { SeedHelper.message
|
41
|
+
subject { SeedHelper.message(message, options) }
|
42
42
|
let(:message) { "message" }
|
43
43
|
let(:options) { {:prefix => prefix, :color => color} }
|
44
44
|
let(:prefix) { nil }
|
@@ -72,7 +72,7 @@ describe SeedHelper do
|
|
72
72
|
end
|
73
73
|
|
74
74
|
describe '#success' do
|
75
|
-
subject { SeedHelper.success
|
75
|
+
subject { SeedHelper.success(message, options) }
|
76
76
|
let(:message) { "message" }
|
77
77
|
let(:options) { {:prefix => prefix, :color => color} }
|
78
78
|
let(:prefix) { nil }
|
@@ -106,7 +106,7 @@ describe SeedHelper do
|
|
106
106
|
end
|
107
107
|
|
108
108
|
describe '#error' do
|
109
|
-
subject { SeedHelper.error
|
109
|
+
subject { SeedHelper.error(message, options) }
|
110
110
|
let(:message) { "message" }
|
111
111
|
let(:options) { {:prefix => prefix, :color => color} }
|
112
112
|
let(:prefix) { nil }
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SeedHelper do
|
4
|
+
|
5
|
+
describe '.find_or_create_resource' do
|
6
|
+
subject { SeedHelper.find_or_create_resource(User, {email: email}, additional_args) }
|
7
|
+
let(:email) { "jordan@example.com" }
|
8
|
+
|
9
|
+
context "without providing additional args" do
|
10
|
+
let(:additional_args) { {} }
|
11
|
+
|
12
|
+
context "when resource already exists" do
|
13
|
+
let!(:user) { User.create(email: email) }
|
14
|
+
it { should eq(user) }
|
15
|
+
end
|
16
|
+
|
17
|
+
context "when resource doesn't exist" do
|
18
|
+
it "creates a new Resource" do
|
19
|
+
expect(User.count).to eq(0)
|
20
|
+
new_user = subject
|
21
|
+
expect(User.count).to eq(1)
|
22
|
+
expect(new_user).to be_kind_of(User)
|
23
|
+
expect(new_user.email).to eq(email)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "providing additional args" do
|
29
|
+
let(:additional_args) { {name: "Name"} }
|
30
|
+
|
31
|
+
context "when a matching resource exists" do
|
32
|
+
let!(:user) { User.create(email: email, name: "Other name") }
|
33
|
+
|
34
|
+
it "only matches on identifiable args" do
|
35
|
+
expect(subject).to eq(user)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "when no resource exists" do
|
40
|
+
it "creates a new item with the identifiable attributes and the additional attributes" do
|
41
|
+
expect(User.count).to eq(0)
|
42
|
+
new_user = subject
|
43
|
+
expect(User.count).to eq(1)
|
44
|
+
expect(new_user).to be_kind_of(User)
|
45
|
+
expect(new_user.email).to eq(email)
|
46
|
+
expect(new_user.name).to eq("Name")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,4 +1,32 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
require 'bundler
|
2
|
+
require 'bundler'
|
3
|
+
Bundler.require
|
4
|
+
require 'rails/all'
|
3
5
|
|
4
|
-
require 'seed_helper'
|
6
|
+
require 'seed_helper'
|
7
|
+
|
8
|
+
# Create an in-memory database to do some basic tests with a real model
|
9
|
+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
|
10
|
+
|
11
|
+
class User < ActiveRecord::Base
|
12
|
+
def to_s
|
13
|
+
email
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
ActiveRecord::Schema.define do
|
18
|
+
self.verbose = false
|
19
|
+
|
20
|
+
create_table :users, force: true do |t|
|
21
|
+
t.string :email
|
22
|
+
t.string :name
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
RSpec.configure do |config|
|
28
|
+
|
29
|
+
# DIY Database Cleaner
|
30
|
+
config.before(:each) { User.delete_all }
|
31
|
+
|
32
|
+
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.
|
4
|
+
version: 1.8.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:
|
11
|
+
date: 2016-01-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|
@@ -62,7 +62,8 @@ files:
|
|
62
62
|
- lib/seed_helper/version.rb
|
63
63
|
- license.txt
|
64
64
|
- seed_helper.gemspec
|
65
|
-
- spec/lib/
|
65
|
+
- spec/lib/seed_helper/output_formatter_spec.rb
|
66
|
+
- spec/lib/seed_helper_spec.rb
|
66
67
|
- spec/spec_helper.rb
|
67
68
|
homepage: https://github.com/jordanmaguire/seed_helper
|
68
69
|
licenses:
|
@@ -84,10 +85,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
85
|
version: '0'
|
85
86
|
requirements: []
|
86
87
|
rubyforge_project:
|
87
|
-
rubygems_version: 2.
|
88
|
+
rubygems_version: 2.4.8
|
88
89
|
signing_key:
|
89
90
|
specification_version: 4
|
90
91
|
summary: Make seeding data easier in Rails projects
|
91
92
|
test_files:
|
92
|
-
- spec/lib/
|
93
|
+
- spec/lib/seed_helper/output_formatter_spec.rb
|
94
|
+
- spec/lib/seed_helper_spec.rb
|
93
95
|
- spec/spec_helper.rb
|