seed_helper 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/DEVELOPER_README.md +23 -0
- data/Gemfile +6 -0
- data/README.md +64 -0
- data/Rakefile +1 -0
- data/lib/seed_helper.rb +26 -0
- data/lib/seed_helper/output_formatter.rb +55 -0
- data/lib/seed_helper/rake_helper.rb +19 -0
- data/lib/seed_helper/version.rb +3 -0
- data/license.txt +13 -0
- data/seed_helper.gemspec +21 -0
- data/spec/lib/seed_formatter_spec.rb +148 -0
- data/spec/spec_helper.rb +4 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7deef5a48d06c4551b328b949d99b0fc5a8517be
|
4
|
+
data.tar.gz: 5e3f65dd5a77ebbcef723ac4a8b0b6edcfcb7217
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8db99b64e111739b3d187885e9409ab55a62cc363a98772032e694537559da4d589dd8613cc12cb281a4ab2b525883c8d4f3d0a041aa9a89f1b22ab23c0ac093
|
7
|
+
data.tar.gz: c94008a4757b8f224f74f4af19b3bf7c3193abfc9b525fdd4dc551721d86c6065344a17f44692fcecf846db50c229d0800a791658f6a1d6c699a0ce79139e099
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
seed_helper
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.0
|
data/DEVELOPER_README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# SeedHelper
|
2
|
+
|
3
|
+
This is a README for working on SeedHelper
|
4
|
+
|
5
|
+
## Installing
|
6
|
+
|
7
|
+
```
|
8
|
+
gem build seed_helper.gemspec
|
9
|
+
gem install ./seed_helper-2.0.0.gem
|
10
|
+
```
|
11
|
+
|
12
|
+
## Running
|
13
|
+
|
14
|
+
Get into `irb`.
|
15
|
+
|
16
|
+
Run
|
17
|
+
|
18
|
+
```
|
19
|
+
require 'seed_helper'
|
20
|
+
include SeedHelper
|
21
|
+
```
|
22
|
+
|
23
|
+
Now you can use the functions in seed_helper.rb
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# SeedHelper
|
2
|
+
|
3
|
+
SeedHelper is a small gem I created while I was working on various projects with terrible Seed files.
|
4
|
+
|
5
|
+
## Purpose
|
6
|
+
|
7
|
+
The goal of SeedHelper is to provide some commonly required functionality to make creating Seed files easier.
|
8
|
+
|
9
|
+
## Dependencies
|
10
|
+
|
11
|
+
SeedHelper uses the [colored](https://github.com/defunkt/colored) gem to present output.
|
12
|
+
|
13
|
+
## Example: Seed implementation
|
14
|
+
|
15
|
+
I use rake tasks to create my seed data. That way you can easily run individual tasks for seeding specific pieces of data.
|
16
|
+
|
17
|
+
Using SeedHelper, a seed task might look like:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
# in lib/tasks/seeds/create_roles.rake
|
21
|
+
|
22
|
+
create_seed_task(:create_roles) do
|
23
|
+
|
24
|
+
["Admin", "Regular"].each do |role_name|
|
25
|
+
|
26
|
+
# Will print out a red message if Role fails to save
|
27
|
+
# Will print out a green message is Role succesfully creates
|
28
|
+
# Will print out a cyan message if Role already exists
|
29
|
+
role = create_resource(Role, {name: role_name})
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
# in lib/tasks/seeds/create_users.rake
|
36
|
+
|
37
|
+
# Specify a dependency on roles, so that running this task will first
|
38
|
+
# run the create_roles task
|
39
|
+
create_seed_task(:create_users, [:create_roles]) do
|
40
|
+
|
41
|
+
[
|
42
|
+
["admin@example.com", "Admin"]
|
43
|
+
].each do |email, role_name|
|
44
|
+
|
45
|
+
role = Role.find_by(name: role_name)
|
46
|
+
admin = create_resource(User, {email: email})
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
Keep in mind that the create_resource requires at least Rails 4.0.2 to run as it uses the `find_by` method.
|
54
|
+
|
55
|
+
## Example: Output
|
56
|
+
|
57
|
+
SeedHelper provides multiple methods for showing output that can be used outside of the `create_resource` method:
|
58
|
+
|
59
|
+
- `message` A general purpose message, generally used as a header. White by default.
|
60
|
+
- `success` Indicates a seed function was successful. Green by default.
|
61
|
+
- `error` Indicates a seed function has failed. Red by default.
|
62
|
+
- `resource_already_exists` Indicates that the data already exists in the database.
|
63
|
+
- `special_message` Show a purple multiline message. I use this to show logins for seed users.
|
64
|
+
- `print_new_line` Just to add a single space to separate groups of functionality.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/lib/seed_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'colored'
|
2
|
+
require 'seed_helper/version'
|
3
|
+
require 'seed_helper/output_formatter'
|
4
|
+
require 'seed_helper/rake_helper'
|
5
|
+
|
6
|
+
module SeedHelper
|
7
|
+
include SeedHelper::OutputFormatter
|
8
|
+
include SeedHelper::RakeHelper
|
9
|
+
|
10
|
+
def create_resource(resource_class, attributes)
|
11
|
+
if resource = resource_class.find_by(attributes)
|
12
|
+
resource_already_exists(resource)
|
13
|
+
else
|
14
|
+
resource = resource_class.new(attributes)
|
15
|
+
if resource.save
|
16
|
+
message = "#{resource} successfully created"
|
17
|
+
success(message)
|
18
|
+
else
|
19
|
+
message = "#{resource} failed to create. Errors: #{resource.errors.full_messages}"
|
20
|
+
error(message)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
return resource
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module SeedHelper::OutputFormatter
|
2
|
+
|
3
|
+
# Outputs a message with a set of given options
|
4
|
+
#
|
5
|
+
# @param [String] message: The message to format
|
6
|
+
# @param [Hash] options: A hash of options to apply to the string
|
7
|
+
# @option options [String] prefix: A prefix for the message. EG: "--> message"
|
8
|
+
# @option options [String] color: A Symbol representing the color from the Colored gem. See: Colored.colors
|
9
|
+
# @option options [String] suffix: A String suffix for the message. EG: "message !!!"
|
10
|
+
#
|
11
|
+
# @example Print out an error message
|
12
|
+
# SeedHelper.output "Some error", {:prefix => "!!! ", :color => :red}
|
13
|
+
# # outputs "!!! Some error" in red text
|
14
|
+
def output(message, options = {})
|
15
|
+
options[:color] ||= :white
|
16
|
+
$stdout.puts "#{options[:prefix]}#{message}#{options[:suffix]}".send(options[:color])
|
17
|
+
end
|
18
|
+
|
19
|
+
def message(message, options = {})
|
20
|
+
options[:prefix] ||= "*** "
|
21
|
+
options[:color] ||= :white
|
22
|
+
output message, options
|
23
|
+
end
|
24
|
+
|
25
|
+
def success(message, options = {})
|
26
|
+
options[:prefix] ||= " + "
|
27
|
+
options[:color] ||= :green
|
28
|
+
output message, options
|
29
|
+
end
|
30
|
+
|
31
|
+
def error(message, options = {})
|
32
|
+
options[:prefix] ||= " - "
|
33
|
+
options[:color] ||= :red
|
34
|
+
output message, options
|
35
|
+
end
|
36
|
+
|
37
|
+
def resource_already_exists(resource)
|
38
|
+
message = "#{resource} already exists"
|
39
|
+
|
40
|
+
options = {}
|
41
|
+
options[:prefix] ||= " > "
|
42
|
+
options[:color] ||= :cyan
|
43
|
+
output(message, options)
|
44
|
+
end
|
45
|
+
|
46
|
+
def special_message(*lines)
|
47
|
+
$stdout.puts ""
|
48
|
+
$stdout.puts lines.join("\n ").magenta
|
49
|
+
end
|
50
|
+
|
51
|
+
def print_new_line
|
52
|
+
$stdout.puts ""
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module SeedHelper::RakeHelper
|
2
|
+
|
3
|
+
def self.create_seed_task(task_name, dependencies=[], &task)
|
4
|
+
namespace :db do
|
5
|
+
namespace :seed do
|
6
|
+
desc "Creating #{task_name.to_s.humanize}"
|
7
|
+
task task_name => dependencies.append(:environment) do
|
8
|
+
message task_name.to_s.humanize
|
9
|
+
|
10
|
+
task.call
|
11
|
+
|
12
|
+
# Print a new line between each set of output for clarity
|
13
|
+
puts ""
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/license.txt
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
2
|
+
Version 2, December 2004
|
3
|
+
|
4
|
+
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
|
5
|
+
|
6
|
+
Everyone is permitted to copy and distribute verbatim or modified
|
7
|
+
copies of this license document, and changing it is allowed as long
|
8
|
+
as the name is changed.
|
9
|
+
|
10
|
+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
11
|
+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
12
|
+
|
13
|
+
0. You just DO WHAT THE FUCK YOU WANT TO.
|
data/seed_helper.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "seed_helper/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "seed_helper"
|
7
|
+
s.version = SeedHelper::VERSION
|
8
|
+
s.authors = ["Jordan Maguire"]
|
9
|
+
s.email = ["jordan@thefrontiergroup.com.au"]
|
10
|
+
s.homepage = "https://github.com/jordanmaguire/seed_helper"
|
11
|
+
s.summary = "Make seeding data easier in Rails projects"
|
12
|
+
s.description = "Make seeding data easier in Rails projects"
|
13
|
+
s.licenses = ['WTFPL']
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency 'colored'
|
21
|
+
end
|
@@ -0,0 +1,148 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class TestClass
|
4
|
+
include SeedHelper
|
5
|
+
end
|
6
|
+
|
7
|
+
describe SeedHelper do
|
8
|
+
|
9
|
+
let(:tc) { TestClass.new }
|
10
|
+
|
11
|
+
describe '#output' do
|
12
|
+
subject { tc.output message, options }
|
13
|
+
let(:message) { "message" }
|
14
|
+
let(:options) { {} }
|
15
|
+
|
16
|
+
context 'options[:prefix] is provided' do
|
17
|
+
let(:options) { {:prefix => "a prefix "} }
|
18
|
+
it "prefixes the message with options[:prefix]" do
|
19
|
+
$stdout.should_receive(:puts).with(/a prefix message/)
|
20
|
+
subject
|
21
|
+
end
|
22
|
+
end
|
23
|
+
context 'options[:suffix] is provided' do
|
24
|
+
let(:options) { {:suffix => " a suffix"} }
|
25
|
+
it "suffixes the message with options[:suffix]" do
|
26
|
+
$stdout.should_receive(:puts).with(/message a suffix/)
|
27
|
+
subject
|
28
|
+
end
|
29
|
+
end
|
30
|
+
context 'options[:color] is provided' do
|
31
|
+
let(:options) { {:color => :blue} }
|
32
|
+
it "prints the message with the provided color" do
|
33
|
+
$stdout.should_receive(:puts).with("\e[34mmessage\e[0m")
|
34
|
+
subject
|
35
|
+
end
|
36
|
+
end
|
37
|
+
context 'options[:color] is not provided' do
|
38
|
+
let(:options) { {:color => nil} }
|
39
|
+
it "prints the message in white by default" do
|
40
|
+
$stdout.should_receive(:puts).with("\e[37mmessage\e[0m")
|
41
|
+
subject
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#message' do
|
47
|
+
subject { tc.message message, options }
|
48
|
+
let(:message) { "message" }
|
49
|
+
let(:options) { {:prefix => prefix, :color => color} }
|
50
|
+
let(:prefix) { nil }
|
51
|
+
let(:color) { nil }
|
52
|
+
context 'options[:prefix] is provided' do
|
53
|
+
let(:prefix) { "a prefix " }
|
54
|
+
it 'passes options[:prefix] to output' do
|
55
|
+
tc.should_receive(:output).with(anything, hash_including(:prefix => prefix))
|
56
|
+
subject
|
57
|
+
end
|
58
|
+
end
|
59
|
+
context 'options[:prefix] is not provided' do
|
60
|
+
it 'passes a default prefix through to output' do
|
61
|
+
tc.should_receive(:output).with(anything, hash_including(:prefix => "*** "))
|
62
|
+
subject
|
63
|
+
end
|
64
|
+
end
|
65
|
+
context 'options[:color] is provided' do
|
66
|
+
let(:color) { :blue }
|
67
|
+
it 'passes options[:color] to output' do
|
68
|
+
tc.should_receive(:output).with(anything, hash_including(:color => color))
|
69
|
+
subject
|
70
|
+
end
|
71
|
+
end
|
72
|
+
context 'options[:color] is not provided' do
|
73
|
+
it 'passes a default color through to output' do
|
74
|
+
tc.should_receive(:output).with(anything, hash_including(:color => :white))
|
75
|
+
subject
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe '#success' do
|
81
|
+
subject { tc.success message, options }
|
82
|
+
let(:message) { "message" }
|
83
|
+
let(:options) { {:prefix => prefix, :color => color} }
|
84
|
+
let(:prefix) { nil }
|
85
|
+
let(:color) { nil }
|
86
|
+
context 'options[:prefix] is provided' do
|
87
|
+
let(:prefix) { "a prefix " }
|
88
|
+
it 'passes options[:prefix] to output' do
|
89
|
+
tc.should_receive(:output).with(anything, hash_including(:prefix => prefix))
|
90
|
+
subject
|
91
|
+
end
|
92
|
+
end
|
93
|
+
context 'options[:prefix] is not provided' do
|
94
|
+
it 'passes a default prefix through to output' do
|
95
|
+
tc.should_receive(:output).with(anything, hash_including(:prefix => " + "))
|
96
|
+
subject
|
97
|
+
end
|
98
|
+
end
|
99
|
+
context 'options[:color] is provided' do
|
100
|
+
let(:color) { :blue }
|
101
|
+
it 'passes options[:color] to output' do
|
102
|
+
tc.should_receive(:output).with(anything, hash_including(:color => color))
|
103
|
+
subject
|
104
|
+
end
|
105
|
+
end
|
106
|
+
context 'options[:color] is not provided' do
|
107
|
+
it 'passes a default color through to output' do
|
108
|
+
tc.should_receive(:output).with(anything, hash_including(:color => :green))
|
109
|
+
subject
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe '#error' do
|
115
|
+
subject { tc.error message, options }
|
116
|
+
let(:message) { "message" }
|
117
|
+
let(:options) { {:prefix => prefix, :color => color} }
|
118
|
+
let(:prefix) { nil }
|
119
|
+
let(:color) { nil }
|
120
|
+
context 'options[:prefix] is provided' do
|
121
|
+
let(:prefix) { "a prefix " }
|
122
|
+
it 'passes options[:prefix] to output' do
|
123
|
+
tc.should_receive(:output).with(anything, hash_including(:prefix => prefix))
|
124
|
+
subject
|
125
|
+
end
|
126
|
+
end
|
127
|
+
context 'options[:prefix] is not provided' do
|
128
|
+
it 'passes a default prefix through to output' do
|
129
|
+
tc.should_receive(:output).with(anything, hash_including(:prefix => " - "))
|
130
|
+
subject
|
131
|
+
end
|
132
|
+
end
|
133
|
+
context 'options[:color] is provided' do
|
134
|
+
let(:color) { :blue }
|
135
|
+
it 'passes options[:color] to output' do
|
136
|
+
tc.should_receive(:output).with(anything, hash_including(:color => color))
|
137
|
+
subject
|
138
|
+
end
|
139
|
+
end
|
140
|
+
context 'options[:color] is not provided' do
|
141
|
+
it 'passes a default color through to output' do
|
142
|
+
tc.should_receive(:output).with(anything, hash_including(:color => :red))
|
143
|
+
subject
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: seed_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jordan Maguire
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: colored
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Make seeding data easier in Rails projects
|
28
|
+
email:
|
29
|
+
- jordan@thefrontiergroup.com.au
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- ".rspec"
|
36
|
+
- ".ruby-gemset"
|
37
|
+
- ".ruby-version"
|
38
|
+
- DEVELOPER_README.md
|
39
|
+
- Gemfile
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/seed_helper.rb
|
43
|
+
- lib/seed_helper/output_formatter.rb
|
44
|
+
- lib/seed_helper/rake_helper.rb
|
45
|
+
- lib/seed_helper/version.rb
|
46
|
+
- license.txt
|
47
|
+
- seed_helper.gemspec
|
48
|
+
- spec/lib/seed_formatter_spec.rb
|
49
|
+
- spec/spec_helper.rb
|
50
|
+
homepage: https://github.com/jordanmaguire/seed_helper
|
51
|
+
licenses:
|
52
|
+
- WTFPL
|
53
|
+
metadata: {}
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 2.2.2
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: Make seeding data easier in Rails projects
|
74
|
+
test_files:
|
75
|
+
- spec/lib/seed_formatter_spec.rb
|
76
|
+
- spec/spec_helper.rb
|