sprig 0.1.1 → 0.1.2

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: 243e9a9198023fecad0dbbbd51c7a7d6e3269572
4
- data.tar.gz: 6f59f121d41be507cede08fde695874fa98416f2
3
+ metadata.gz: 45e7ad607a25ae6b5ddd621ba63d2dc51d97ecf9
4
+ data.tar.gz: b156960667081a75408bab2b2f5363a89b320d35
5
5
  SHA512:
6
- metadata.gz: 7654b318cbabfa4b542849e3245a33205ecb6370eda6ac1dec799b39c0b12935c76328456616aeafe4e8bcbd940573a630cc150bf8b610bedd8d0dab2cdd1bbb
7
- data.tar.gz: 6ec8691e22fa991d01ad88382bbd90620e4e114d215cff3b21f76e0f3d1bcdcf75b3c98122612094f3a7200c412d1f1dba4bd9b65bed6b29696a26da9d8ffa85
6
+ metadata.gz: 69ab1c4161a19017678b2e03bbd6af6edb62ef498a56cc0d7c8e16b6097e9b72e66890f17605786eaa78bbbc9eb264e9034075728de8275e12f02ff2a28a112c
7
+ data.tar.gz: 231648356a91e8f181161a6c9077161d4eb1d2e178ad1e04c96290029f11fdbb9b9a1f7459f774db6ce143cc2a23514d99faa3a8da65a419e347652adde26b1e
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- #Sprig
1
+ ![Sprig](http://i.imgur.com/XCu3iVO.png)
2
2
 
3
3
  [![Code Climate](https://codeclimate.com/github/vigetlabs/sprig.png)](https://codeclimate.com/github/vigetlabs/sprig) [![Build Status](https://travis-ci.org/vigetlabs/sprig.png?branch=master)](https://travis-ci.org/vigetlabs/sprig) [![Gem Version](https://badge.fury.io/rb/sprig.png)](http://badge.fury.io/rb/sprig)
4
4
 
@@ -6,6 +6,11 @@ Seed Rails application by convention, not configuration.
6
6
 
7
7
  Provides support for common files types: *csv*, *yaml*, and *json*. Extensible for the rest!
8
8
 
9
+ Learn more about Sprig and view documentation at [http://vigetlabs.github.io/sprig/](http://vigetlabs.github.io/sprig/).
10
+
11
+ ## Installation
12
+
13
+ Use `rails generate sprig:install` to create environment-specific seed directories.
9
14
 
10
15
  ##The Sprig Directive
11
16
 
@@ -21,7 +26,6 @@ sprig [User, Post, Comment]
21
26
 
22
27
  This directive tells Sprig to go find your datafiles for the `User`, `Post`, and `Comment` seed resources, build records from the data entries, and insert them into the database. Sprig will automatically detect known datafile types like `.yml`, `.json`, or `.csv` within your environment-specific seed directory.
23
28
 
24
-
25
29
  ##Environment
26
30
 
27
31
  Seed files are unique to the environment in which your Rails application is running. Within `db/seeds` create an environment-specific directory (i.e. `/development` for your 'development' environment).
@@ -81,6 +85,8 @@ records:
81
85
  body: "Yaml Comment body"
82
86
  ```
83
87
 
88
+ **Note: For namespaced or STI classes, you'll need to include the namespace with the class name in the seed file name. For example `Users::HeadManager` would need to be `users_head_managers.yml`**
89
+
84
90
  ### Special Options
85
91
 
86
92
  These are provided in a `options:` key for *yaml* and *json* files.
@@ -100,6 +106,19 @@ options:
100
106
  find_existing_by: ['title', 'user_id']
101
107
  ```
102
108
 
109
+ ### Computed Values
110
+
111
+ It's common to want seed values that are dynamic. Sprig supports an ERB style syntax for computing seed attributes.
112
+
113
+ ```yaml
114
+ # posts.yml
115
+
116
+ records:
117
+ - sprig_id: 1
118
+ body: "Yaml Post body"
119
+ published_at: "<%= 1.week.ago %>"
120
+ ```
121
+
103
122
  ##Custom Sources and Parsers
104
123
 
105
124
  If all your data is in `.wat` files, fear not. You can tell Sprig where to look for your data, and point it toward a custom parser class for turning your data into records. The example below tells Sprig to read `User` seed data from a Google Spreadsheet, and parse it accordingly.
@@ -0,0 +1,31 @@
1
+ require 'rails/generators/base'
2
+
3
+ module Sprig
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ argument :arg_envs, :type => :array, :optional => true
7
+
8
+ desc "Install Sprig seed directories"
9
+
10
+ def create_enviroment_directories
11
+ empty_directory 'db/seeds'
12
+
13
+ envs.each { |env| empty_directory "db/seeds/#{env}" }
14
+ end
15
+
16
+ private
17
+
18
+ def envs
19
+ arg_envs ? arg_envs : default_envs
20
+ end
21
+
22
+ def default_envs
23
+ env_configs.map { |p| File.basename(p, File.extname(p)) }
24
+ end
25
+
26
+ def env_configs
27
+ Dir[Rails.root.join('config/environments', '*.rb')]
28
+ end
29
+ end
30
+ end
31
+ end
@@ -10,20 +10,28 @@ module Sprig
10
10
  autoload :Parser, 'sprig/parser'
11
11
  autoload :Helpers, 'sprig/helpers'
12
12
  autoload :Planter, 'sprig/planter'
13
- autoload :SprigLogger, 'sprig/sprig_logger'
14
- autoload :SprigRecordStore, 'sprig/sprig_record_store'
13
+ autoload :ProcessNotifier, 'sprig/process_notifier'
14
+ autoload :Logging, 'sprig/logging'
15
+ autoload :NullRecord, 'sprig/null_record'
16
+ autoload :SprigRecordStore, 'sprig/sprig_record_store'
15
17
  autoload :Data, 'sprig/data'
16
18
  autoload :Seed, 'sprig/seed'
17
19
 
18
- def self.configuration
19
- @@configuration ||= Sprig::Configuration.new
20
- end
20
+ class << self
21
+ def configuration
22
+ @@configuration ||= Sprig::Configuration.new
23
+ end
21
24
 
22
- def self.configure
23
- yield configuration
24
- end
25
+ def configure
26
+ yield configuration
27
+ end
28
+
29
+ def reset_configuration
30
+ @@configuration = nil
31
+ end
25
32
 
26
- def self.reset_configuration
27
- @@configuration = nil
33
+ def logger
34
+ configuration.logger
35
+ end
28
36
  end
29
37
  end
@@ -1,12 +1,16 @@
1
1
  module Sprig
2
2
  class Configuration
3
3
 
4
- attr_writer :directory
4
+ attr_writer :directory, :logger
5
5
 
6
6
  def directory
7
7
  Rails.root.join(@directory || default_directory, Rails.env)
8
8
  end
9
9
 
10
+ def logger
11
+ @logger ||= Logger.new($stdout)
12
+ end
13
+
10
14
  private
11
15
 
12
16
  def default_directory
@@ -24,7 +24,7 @@ module Sprig
24
24
  end
25
25
 
26
26
  def datasource
27
- @datasource ||= Source.new(klass.to_s.tableize, options)
27
+ @datasource ||= Source.new(klass.to_s.tableize.gsub("/", "_"), options)
28
28
  end
29
29
 
30
30
  private
@@ -16,6 +16,8 @@ module Sprig
16
16
 
17
17
  def sprig_record(klass, seed_id)
18
18
  SprigRecordStore.instance.get(klass, seed_id)
19
+ rescue SprigRecordStore::RecordNotFoundError => error
20
+ NullRecord.new(error)
19
21
  end
20
22
 
21
23
  def sprig_file(relative_path)
@@ -0,0 +1,35 @@
1
+ module Sprig
2
+ module Logging
3
+ LOG_LEVELS = {
4
+ debug: :blue,
5
+ info: :green,
6
+ warn: :orange,
7
+ error: :red
8
+ }
9
+
10
+ LOG_COLORS = {
11
+ blue: 34,
12
+ green: 32,
13
+ orange: 33,
14
+ red: 31
15
+ }
16
+
17
+ LOG_LEVELS.each do |level, color|
18
+ define_method("log_#{level}") do |message|
19
+ Sprig.logger.send(level, send(color, message.to_s))
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def colorize(message, color_code)
26
+ "\e[#{color_code}m#{message}\e[0m"
27
+ end
28
+
29
+ LOG_COLORS.each do |name, color_code|
30
+ define_method(name) do |message|
31
+ colorize(message, color_code)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,13 @@
1
+ module Sprig
2
+ class NullRecord
3
+ include Logging
4
+
5
+ def initialize(error)
6
+ log_error "#{error} (Substituted with NullRecord)"
7
+ end
8
+
9
+ def method_missing(*)
10
+ nil
11
+ end
12
+ end
13
+ end
@@ -9,7 +9,7 @@ module Sprig
9
9
  plant(seed)
10
10
  end
11
11
 
12
- logger.log_summary
12
+ notifier.finished
13
13
  end
14
14
 
15
15
  private
@@ -20,19 +20,19 @@ module Sprig
20
20
  DependencySorter.new(seeds).sorted_items
21
21
  end
22
22
 
23
- def logger
24
- @logger ||= SprigLogger.new
23
+ def notifier
24
+ @notifier ||= ProcessNotifier.new
25
25
  end
26
26
 
27
27
  def plant(seed)
28
- logger.processing
28
+ notifier.in_progress
29
29
  seed.before_save
30
30
 
31
31
  if seed.save_record
32
32
  seed.save_to_store
33
- logger.log_success(seed)
33
+ notifier.success(seed)
34
34
  else
35
- logger.log_error(seed)
35
+ notifier.error(seed)
36
36
  end
37
37
  end
38
38
  end
@@ -0,0 +1,59 @@
1
+ require 'active_support/inflector'
2
+
3
+ module Sprig
4
+ class ProcessNotifier
5
+ include Logging
6
+
7
+ def initialize
8
+ @success_count = 0
9
+ @error_count = 0
10
+ @errors = []
11
+ end
12
+
13
+ def success(seed)
14
+ log_info seed.success_log_text
15
+ @success_count += 1
16
+ end
17
+
18
+ def error(seed)
19
+ @errors << seed.record
20
+ log_error seed.error_log_text
21
+ log_error seed.record
22
+ log_error seed.record.errors.messages
23
+ @error_count += 1
24
+ end
25
+
26
+ def finished
27
+ log_debug 'Seeding complete.'
28
+
29
+ if @success_count > 0
30
+ log_info success_summary
31
+ else
32
+ log_error success_summary
33
+ end
34
+
35
+ if @error_count > 0
36
+ log_error error_summary
37
+
38
+ @errors.each do |error|
39
+ log_error error
40
+ log_error "#{error.errors.messages}\n"
41
+ end
42
+ end
43
+ end
44
+
45
+ def in_progress
46
+ log_debug "Planting those seeds...\r"
47
+ end
48
+
49
+ private
50
+
51
+ def success_summary
52
+ "#{@success_count} #{'seed'.pluralize(@success_count)} successfully planted."
53
+ end
54
+
55
+ def error_summary
56
+ "#{@error_count} #{'seed'.pluralize(@error_count)} couldn't be planted:"
57
+ end
58
+ end
59
+ end
@@ -4,17 +4,17 @@ module Sprig
4
4
  def self.new_from_directive(directive)
5
5
  raise ArgumentError, 'Must provide a Directive' unless directive.is_a? Directive
6
6
 
7
- klass = directive.klass
8
- datasource = directive.datasource
9
- options = directive.options
10
-
11
- new(klass, datasource, options)
7
+ new(
8
+ directive.klass,
9
+ directive.datasource,
10
+ directive.options
11
+ )
12
12
  end
13
13
 
14
14
  def initialize(klass, datasource, options)
15
- self.klass = klass
16
- self.datasource = datasource
17
- self.initial_options = options
15
+ self.klass = klass
16
+ self.datasource = datasource
17
+ self.initial_options = options
18
18
  end
19
19
 
20
20
  def add_seeds_to_hopper(hopper)
@@ -30,6 +30,7 @@ module Sprig
30
30
  def klass=(klass)
31
31
  raise ArgumentError, 'Must provide a Class as first argument' unless klass.is_a? Class
32
32
 
33
+ klass.reset_column_information
33
34
  @klass = klass
34
35
  end
35
36
 
@@ -11,7 +11,7 @@ module Sprig
11
11
  end
12
12
 
13
13
  def get(klass, sprig_id)
14
- records_of_klass(klass)[sprig_id.to_s] || record_not_found
14
+ records_of_klass(klass)[sprig_id.to_s] || record_not_found(klass, sprig_id)
15
15
  end
16
16
 
17
17
  private
@@ -24,7 +24,7 @@ module Sprig
24
24
  @records ||= {}
25
25
  end
26
26
 
27
- def record_not_found
27
+ def record_not_found(klass, sprig_id)
28
28
  raise(RecordNotFoundError, "Record for class #{klass} and sprig_id #{sprig_id} could not be found.")
29
29
  end
30
30
  end
@@ -1,3 +1,3 @@
1
1
  module Sprig
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
Binary file
@@ -0,0 +1 @@
1
+ # Rails environment config
@@ -0,0 +1 @@
1
+ # Rails environment config
@@ -0,0 +1 @@
1
+ # Rails environment config
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ validates :first_name, :last_name, :presence => true
3
+ end
@@ -0,0 +1,6 @@
1
+ # users.yml
2
+
3
+ records:
4
+ - sprig_id: 1
5
+ first_name: "Clark"
6
+ # intentionally leaving out last_name so record fails to create
@@ -0,0 +1,7 @@
1
+ # posts_missing_dependency.yml
2
+
3
+ records:
4
+ - sprig_id: 1
5
+ title: 'Yaml title'
6
+ content: 'Yaml content'
7
+ user_id: "<%= sprig_record(User, 1).id %>" # This sprig record did not save.
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+ require 'generators/sprig/install_generator'
3
+
4
+ describe Sprig::Generators::InstallGenerator do
5
+ destination File.expand_path("../../tmp", __FILE__)
6
+
7
+ before do
8
+ stub_rails_root
9
+ prepare_destination
10
+ run_generator
11
+ end
12
+
13
+ it "creates empty db/seeds directory" do
14
+ assert_directory "db/seeds"
15
+ end
16
+
17
+ it "creates empty seed environment directories" do
18
+ [
19
+ :development,
20
+ :test,
21
+ :production
22
+ ].each do |env|
23
+ assert_directory "db/seeds/#{env}"
24
+ end
25
+ end
26
+ end
27
+
28
+
29
+ # Generator arguments are set on a class basis. We need to open
30
+ # a new describe block to make these examples work.
31
+
32
+ describe Sprig::Generators::InstallGenerator do
33
+ context "with arguments" do
34
+ destination File.expand_path("../../tmp", __FILE__)
35
+ arguments %w(development test integration)
36
+
37
+ before do
38
+ stub_rails_root
39
+ prepare_destination
40
+ run_generator
41
+ end
42
+
43
+ it "creates empty seed directories from arguments" do
44
+ [
45
+ :development,
46
+ :test,
47
+ :integration
48
+ ].each do |env|
49
+ assert_directory "db/seeds/#{env}"
50
+ end
51
+
52
+ [
53
+ :production
54
+ ].each do |env|
55
+ assert_no_directory "db/seeds/#{env}"
56
+ end
57
+ end
58
+ end
59
+ end
@@ -7,7 +7,6 @@ describe Sprig::Configuration do
7
7
  end
8
8
 
9
9
  describe "#directory" do
10
-
11
10
  it "returns db/seeds/:env by default" do
12
11
  subject.directory.to_path.should == '~/db/seeds/development'
13
12
  end
@@ -18,4 +17,20 @@ describe Sprig::Configuration do
18
17
  subject.directory.to_path.should == '~/seed_files/development'
19
18
  end
20
19
  end
20
+
21
+ describe "#logger" do
22
+ it "returns an stdout logger by default" do
23
+ logger = double('Logger')
24
+ Logger.stub(:new).with($stdout).and_return(logger)
25
+
26
+ subject.logger.should == logger
27
+ end
28
+
29
+ it "returns a custom logger" do
30
+ logger = double('Logger')
31
+ subject.logger = logger
32
+
33
+ subject.logger.should == logger
34
+ end
35
+ end
21
36
  end
@@ -2,6 +2,11 @@ require 'spec_helper'
2
2
 
3
3
  describe Sprig::Directive do
4
4
 
5
+ module Users
6
+ class Admin < User
7
+ end
8
+ end
9
+
5
10
  describe "#klass" do
6
11
  context "given a class" do
7
12
  subject { described_class.new(Post) }
@@ -9,6 +14,12 @@ describe Sprig::Directive do
9
14
  its(:klass) { should == Post }
10
15
  end
11
16
 
17
+ context "given a class within a module" do
18
+ subject { described_class.new(Users::Admin) }
19
+
20
+ its(:klass) { should == Users::Admin }
21
+ end
22
+
12
23
  context "given options with a class" do
13
24
  subject { described_class.new(:class => Post) }
14
25
 
@@ -47,14 +58,26 @@ describe Sprig::Directive do
47
58
  describe "#datasource" do
48
59
  let(:datasource) { double('datasource') }
49
60
 
50
- subject { described_class.new(:class => Post, :source => 'source') }
61
+ context "with a class" do
62
+ subject { described_class.new(:class => Post, :source => 'source') }
51
63
 
52
- before do
53
- Sprig::Source.stub(:new).with('posts', { :source => 'source' }).and_return(datasource)
64
+ before do
65
+ Sprig::Source.stub(:new).with('posts', { :source => 'source' }).and_return(datasource)
66
+ end
67
+
68
+ it "returns a sprig data source" do
69
+ subject.datasource.should == datasource
70
+ end
54
71
  end
55
72
 
56
- it "returns a sprig data source" do
57
- subject.datasource.should == datasource
73
+ context "with a class within a module" do
74
+ subject { described_class.new(Users::Admin) }
75
+
76
+ it "passes the correct path to Source" do
77
+ Sprig::Source.should_receive(:new).with("users_admins", {})
78
+
79
+ subject.datasource
80
+ end
58
81
  end
59
82
  end
60
83
  end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sprig::NullRecord do
4
+ let(:error_msg) { 'Something bad happened.' }
5
+ subject { described_class.new(error_msg) }
6
+
7
+ describe "#new" do
8
+ it "logs an error upon initialization" do
9
+ log_should_receive(:error, with: "#{error_msg} (Substituted with NullRecord)")
10
+
11
+ subject
12
+ end
13
+ end
14
+
15
+ describe "#method_missing" do
16
+ it "returns nil for undefined method calls" do
17
+ subject.enhance_your_calm.should == nil
18
+ end
19
+ end
20
+
21
+ it_behaves_like "a logging entity" do
22
+ subject! { described_class.new(error_msg) }
23
+ end
24
+ end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sprig::ProcessNotifier do
4
+ it_behaves_like "a logging entity" do
5
+ subject { described_class.new }
6
+ end
7
+
8
+ describe "#success" do
9
+ let(:seed) { double('Seed', success_log_text: 'I am a teapot.') }
10
+
11
+ it "logs the seed's success message" do
12
+ log_should_receive(:info, with: 'I am a teapot.')
13
+
14
+ subject.success(seed)
15
+ end
16
+ end
17
+
18
+ describe "#error" do
19
+ let(:errors) { double('Errors', messages: 'error messages') }
20
+ let(:seed_record) { double('Record', to_s: 'Seed Record', errors: errors) }
21
+ let(:seed) { double('Seed', error_log_text: 'I am a teapot.', record: seed_record) }
22
+
23
+ it "logs the seed's error message and error details" do
24
+ log_should_receive(:error, with: 'I am a teapot.').ordered
25
+ log_should_receive(:error, with: 'Seed Record').ordered
26
+ log_should_receive(:error, with: 'error messages').ordered
27
+
28
+ subject.error(seed)
29
+ end
30
+ end
31
+
32
+ describe "#finished" do
33
+ it "logs a complete message" do
34
+ log_should_receive(:debug, with: 'Seeding complete.')
35
+
36
+ subject.finished
37
+ end
38
+
39
+ context "when records are saved successfully" do
40
+ let(:seed) { double('Seed', success_log_text: 'I am a teapot.') }
41
+
42
+ before do
43
+ subject.success(seed)
44
+ end
45
+
46
+ it "logs a summery of successful saves" do
47
+ log_should_receive(:info, with: '1 seed successfully planted.')
48
+
49
+ subject.finished
50
+ end
51
+ end
52
+
53
+ context "when no records are saved successfully" do
54
+ it "logs a summery of successful saves" do
55
+ log_should_receive(:error, with: '0 seeds successfully planted.')
56
+
57
+ subject.finished
58
+ end
59
+ end
60
+
61
+ context "when there is an error saving a record" do
62
+ let(:errors) { double('Errors', messages: 'error messages') }
63
+ let(:seed_record) { double('Record', to_s: 'Seed Record', errors: errors) }
64
+ let(:seed) { double('Seed', error_log_text: 'I am a teapot.', record: seed_record) }
65
+
66
+ before do
67
+ subject.error(seed)
68
+ end
69
+
70
+ it "logs a summary of errors" do
71
+ log_should_receive(:error, with: '0 seeds successfully planted.').ordered
72
+ log_should_receive(:error, with: "1 seed couldn't be planted:").ordered
73
+ log_should_receive(:error, with: 'Seed Record').ordered
74
+ log_should_receive(:error, with: "error messages\n").ordered
75
+
76
+ subject.finished
77
+ end
78
+ end
79
+ end
80
+
81
+ describe "#in_progress" do
82
+ it "logs an in-progress message" do
83
+ log_should_receive(:debug, with: "Planting those seeds...\r")
84
+
85
+ subject.in_progress
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sprig do
4
+ let(:configuration) { double('Configuration') }
5
+
6
+ before do
7
+ Sprig::Configuration.stub(:new).and_return(configuration)
8
+ end
9
+
10
+ describe ".configuration" do
11
+ context "when there is not yet a Configuration instance" do
12
+ it "returns a new Configuration instance" do
13
+ described_class.configuration.should == configuration
14
+ end
15
+ end
16
+
17
+ context "when there is an existing Configuration instance" do
18
+ before do
19
+ described_class.configuration
20
+ end
21
+
22
+ it "returns the existing Configuration instance" do
23
+ new_configuration = double('Configuration')
24
+ Sprig::Configuration.stub(:new).and_return(new_configuration)
25
+
26
+ described_class.configuration.should == configuration
27
+ end
28
+ end
29
+ end
30
+
31
+ describe ".reset_configuration" do
32
+ before do
33
+ described_class.configuration
34
+ end
35
+
36
+ it "clears the existing configuration" do
37
+ described_class.reset_configuration
38
+ new_configuration = double('Configuration')
39
+ Sprig::Configuration.stub(:new).and_return(new_configuration)
40
+
41
+ described_class.configuration.should == new_configuration
42
+ end
43
+ end
44
+
45
+ describe ".configure" do
46
+ it "yields the configuration" do
47
+ described_class.configure do |config|
48
+ config.should == configuration
49
+ end
50
+ end
51
+ end
52
+ end
@@ -6,13 +6,18 @@ require "database_cleaner"
6
6
  require "webmock"
7
7
  require "vcr"
8
8
  require "pry"
9
+ require "generator_spec"
9
10
 
10
11
  require "sprig"
11
12
  include Sprig::Helpers
12
13
 
13
14
  Dir[File.dirname(__FILE__) + '/fixtures/models/*.rb'].each {|file| require file }
15
+ Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each {|file| require file}
14
16
 
15
17
  RSpec.configure do |c|
18
+ c.include ColoredText
19
+ c.include LoggerMock
20
+
16
21
  c.before(:suite) do
17
22
  DatabaseCleaner.strategy = :transaction
18
23
  DatabaseCleaner.clean_with(:truncation)
@@ -38,11 +43,11 @@ end
38
43
  # Database
39
44
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "spec/db/activerecord.db")
40
45
 
41
- #User.connection.execute "DROP TABLE IF EXISTS users;"
42
- #User.connection.execute "CREATE TABLE users (id INTEGER PRIMARY KEY , first_name VARCHAR(255), last_name VARCHAR(255));"
46
+ User.connection.execute "DROP TABLE IF EXISTS users;"
47
+ User.connection.execute "CREATE TABLE users (id INTEGER PRIMARY KEY , first_name VARCHAR(255), last_name VARCHAR(255), type VARCHAR(255));"
43
48
 
44
49
  Post.connection.execute "DROP TABLE IF EXISTS posts;"
45
- Post.connection.execute "CREATE TABLE posts (id INTEGER PRIMARY KEY , title VARCHAR(255), content VARCHAR(255), published BOOLEAN);"
50
+ Post.connection.execute "CREATE TABLE posts (id INTEGER PRIMARY KEY , title VARCHAR(255), content VARCHAR(255), published BOOLEAN , user_id INTEGER);"
46
51
 
47
52
  Comment.connection.execute "DROP TABLE IF EXISTS comments;"
48
53
  Comment.connection.execute "CREATE TABLE comments (id INTEGER PRIMARY KEY , post_id INTEGER, body VARCHAR(255));"
@@ -112,6 +112,27 @@ describe "Seeding an application" do
112
112
  end
113
113
  end
114
114
 
115
+ context "with a relationship to a record that didn't save" do
116
+ around do |example|
117
+ load_seeds('invalid_users.yml', 'posts_missing_record.yml', &example)
118
+ end
119
+
120
+ it "does not error, but carries on with the seeding" do
121
+ expect {
122
+ sprig [
123
+ {
124
+ :class => Post,
125
+ :source => open('spec/fixtures/seeds/test/posts_missing_record.yml')
126
+ },
127
+ {
128
+ :class => User,
129
+ :source => open('spec/fixtures/seeds/test/invalid_users.yml')
130
+ }
131
+ ]
132
+ }.to_not raise_error
133
+ end
134
+ end
135
+
115
136
  context "with multiple files for a class" do
116
137
  around do |example|
117
138
  load_seeds('posts.yml', 'legacy_posts.yml', &example)
@@ -0,0 +1,17 @@
1
+ module ColoredText
2
+ def blue_text(text)
3
+ "\e[34m#{text}\e[0m"
4
+ end
5
+
6
+ def green_text(text)
7
+ "\e[32m#{text}\e[0m"
8
+ end
9
+
10
+ def orange_text(text)
11
+ "\e[33m#{text}\e[0m"
12
+ end
13
+
14
+ def red_text(text)
15
+ "\e[31m#{text}\e[0m"
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ module LoggerMock
2
+ def log_should_receive(level, options)
3
+ Sprig.logger.should_receive(level).with(send("log_#{level}_text", options.fetch(:with)))
4
+ end
5
+
6
+ def log_debug_text(text)
7
+ blue_text(text)
8
+ end
9
+
10
+ def log_info_text(text)
11
+ green_text(text)
12
+ end
13
+
14
+ def log_warn_text(text)
15
+ orange_text(text)
16
+ end
17
+
18
+ def log_error_text(text)
19
+ red_text(text)
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ shared_examples_for "a logging entity" do
2
+ let(:message) { 'A log message.' }
3
+
4
+ [
5
+ :debug,
6
+ :info,
7
+ :warn,
8
+ :error
9
+ ].each do |level|
10
+ it "can log a #{level}" do
11
+ log_should_receive(level, with: message)
12
+
13
+ subject.send("log_#{level}", message)
14
+ end
15
+ end
16
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sprig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lawson Kurtz
@@ -9,104 +9,118 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-28 00:00:00.000000000 Z
12
+ date: 2014-05-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ~>
18
+ - - "~>"
19
19
  - !ruby/object:Gem::Version
20
20
  version: '3.1'
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ~>
25
+ - - "~>"
26
26
  - !ruby/object:Gem::Version
27
27
  version: '3.1'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: sqlite3
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - ~>
32
+ - - "~>"
33
33
  - !ruby/object:Gem::Version
34
34
  version: 1.3.8
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ~>
39
+ - - "~>"
40
40
  - !ruby/object:Gem::Version
41
41
  version: 1.3.8
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: rspec
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ~>
46
+ - - "~>"
47
47
  - !ruby/object:Gem::Version
48
48
  version: 2.14.0
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ~>
53
+ - - "~>"
54
54
  - !ruby/object:Gem::Version
55
55
  version: 2.14.0
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: database_cleaner
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ~>
60
+ - - "~>"
61
61
  - !ruby/object:Gem::Version
62
62
  version: 1.2.0
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - ~>
67
+ - - "~>"
68
68
  - !ruby/object:Gem::Version
69
69
  version: 1.2.0
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: webmock
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - ~>
74
+ - - "~>"
75
75
  - !ruby/object:Gem::Version
76
76
  version: 1.15.0
77
77
  type: :development
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
- - - ~>
81
+ - - "~>"
82
82
  - !ruby/object:Gem::Version
83
83
  version: 1.15.0
84
84
  - !ruby/object:Gem::Dependency
85
85
  name: vcr
86
86
  requirement: !ruby/object:Gem::Requirement
87
87
  requirements:
88
- - - ~>
88
+ - - "~>"
89
89
  - !ruby/object:Gem::Version
90
90
  version: 2.8.0
91
91
  type: :development
92
92
  prerelease: false
93
93
  version_requirements: !ruby/object:Gem::Requirement
94
94
  requirements:
95
- - - ~>
95
+ - - "~>"
96
96
  - !ruby/object:Gem::Version
97
97
  version: 2.8.0
98
98
  - !ruby/object:Gem::Dependency
99
99
  name: pry
100
100
  requirement: !ruby/object:Gem::Requirement
101
101
  requirements:
102
- - - '>='
102
+ - - ">="
103
103
  - !ruby/object:Gem::Version
104
104
  version: '0'
105
105
  type: :development
106
106
  prerelease: false
107
107
  version_requirements: !ruby/object:Gem::Requirement
108
108
  requirements:
109
- - - '>='
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: generator_spec
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
110
124
  - !ruby/object:Gem::Version
111
125
  version: '0'
112
126
  description: Sprig is a library for managing interconnected, environment-specific
@@ -121,6 +135,7 @@ files:
121
135
  - MIT-LICENSE
122
136
  - README.md
123
137
  - Rakefile
138
+ - lib/generators/sprig/install_generator.rb
124
139
  - lib/sprig.rb
125
140
  - lib/sprig/configuration.rb
126
141
  - lib/sprig/data.rb
@@ -130,6 +145,8 @@ files:
130
145
  - lib/sprig/directive.rb
131
146
  - lib/sprig/directive_list.rb
132
147
  - lib/sprig/helpers.rb
148
+ - lib/sprig/logging.rb
149
+ - lib/sprig/null_record.rb
133
150
  - lib/sprig/parser.rb
134
151
  - lib/sprig/parser/base.rb
135
152
  - lib/sprig/parser/csv.rb
@@ -137,6 +154,7 @@ files:
137
154
  - lib/sprig/parser/json.rb
138
155
  - lib/sprig/parser/yml.rb
139
156
  - lib/sprig/planter.rb
157
+ - lib/sprig/process_notifier.rb
140
158
  - lib/sprig/seed.rb
141
159
  - lib/sprig/seed/attribute.rb
142
160
  - lib/sprig/seed/attribute_collection.rb
@@ -144,16 +162,20 @@ files:
144
162
  - lib/sprig/seed/factory.rb
145
163
  - lib/sprig/seed/record.rb
146
164
  - lib/sprig/source.rb
147
- - lib/sprig/sprig_logger.rb
148
165
  - lib/sprig/sprig_record_store.rb
149
166
  - lib/sprig/version.rb
150
167
  - spec/db/activerecord.db
151
168
  - spec/feature/configurations_spec.rb
152
169
  - spec/fixtures/cassettes/google_spreadsheet_json_posts.yml
170
+ - spec/fixtures/config/environments/development.rb
171
+ - spec/fixtures/config/environments/production.rb
172
+ - spec/fixtures/config/environments/test.rb
153
173
  - spec/fixtures/models/comment.rb
154
174
  - spec/fixtures/models/post.rb
175
+ - spec/fixtures/models/user.rb
155
176
  - spec/fixtures/seeds/staging/posts.yml
156
177
  - spec/fixtures/seeds/test/comments.yml
178
+ - spec/fixtures/seeds/test/invalid_users.yml
157
179
  - spec/fixtures/seeds/test/legacy_posts.yml
158
180
  - spec/fixtures/seeds/test/posts.csv
159
181
  - spec/fixtures/seeds/test/posts.json
@@ -161,11 +183,19 @@ files:
161
183
  - spec/fixtures/seeds/test/posts_find_existing_by_multiple.yml
162
184
  - spec/fixtures/seeds/test/posts_find_existing_by_single.yml
163
185
  - spec/fixtures/seeds/test/posts_missing_dependency.yml
186
+ - spec/fixtures/seeds/test/posts_missing_record.yml
187
+ - spec/lib/generators/sprig/install_generator_spec.rb
164
188
  - spec/lib/sprig/configuration_spec.rb
165
189
  - spec/lib/sprig/directive_list_spec.rb
166
190
  - spec/lib/sprig/directive_spec.rb
191
+ - spec/lib/sprig/null_record_spec.rb
192
+ - spec/lib/sprig/process_notifier_spec.rb
193
+ - spec/lib/sprig_spec.rb
167
194
  - spec/spec_helper.rb
168
195
  - spec/sprig_spec.rb
196
+ - spec/support/helpers/colored_text.rb
197
+ - spec/support/helpers/logger_mock.rb
198
+ - spec/support/shared_examples/a_logging_entity.rb
169
199
  homepage: http://www.github.com/vigetlabs/sprig
170
200
  licenses:
171
201
  - MIT
@@ -176,17 +206,17 @@ require_paths:
176
206
  - lib
177
207
  required_ruby_version: !ruby/object:Gem::Requirement
178
208
  requirements:
179
- - - '>='
209
+ - - ">="
180
210
  - !ruby/object:Gem::Version
181
211
  version: '0'
182
212
  required_rubygems_version: !ruby/object:Gem::Requirement
183
213
  requirements:
184
- - - '>='
214
+ - - ">="
185
215
  - !ruby/object:Gem::Version
186
216
  version: '0'
187
217
  requirements: []
188
218
  rubyforge_project:
189
- rubygems_version: 2.2.1
219
+ rubygems_version: 2.2.2
190
220
  signing_key:
191
221
  specification_version: 4
192
222
  summary: Relational, environment-specific seeding for Rails apps.
@@ -194,10 +224,15 @@ test_files:
194
224
  - spec/db/activerecord.db
195
225
  - spec/feature/configurations_spec.rb
196
226
  - spec/fixtures/cassettes/google_spreadsheet_json_posts.yml
227
+ - spec/fixtures/config/environments/development.rb
228
+ - spec/fixtures/config/environments/production.rb
229
+ - spec/fixtures/config/environments/test.rb
197
230
  - spec/fixtures/models/comment.rb
198
231
  - spec/fixtures/models/post.rb
232
+ - spec/fixtures/models/user.rb
199
233
  - spec/fixtures/seeds/staging/posts.yml
200
234
  - spec/fixtures/seeds/test/comments.yml
235
+ - spec/fixtures/seeds/test/invalid_users.yml
201
236
  - spec/fixtures/seeds/test/legacy_posts.yml
202
237
  - spec/fixtures/seeds/test/posts.csv
203
238
  - spec/fixtures/seeds/test/posts.json
@@ -205,9 +240,16 @@ test_files:
205
240
  - spec/fixtures/seeds/test/posts_find_existing_by_multiple.yml
206
241
  - spec/fixtures/seeds/test/posts_find_existing_by_single.yml
207
242
  - spec/fixtures/seeds/test/posts_missing_dependency.yml
243
+ - spec/fixtures/seeds/test/posts_missing_record.yml
244
+ - spec/lib/generators/sprig/install_generator_spec.rb
208
245
  - spec/lib/sprig/configuration_spec.rb
209
246
  - spec/lib/sprig/directive_list_spec.rb
210
247
  - spec/lib/sprig/directive_spec.rb
248
+ - spec/lib/sprig/null_record_spec.rb
249
+ - spec/lib/sprig/process_notifier_spec.rb
250
+ - spec/lib/sprig_spec.rb
211
251
  - spec/spec_helper.rb
212
252
  - spec/sprig_spec.rb
213
- has_rdoc:
253
+ - spec/support/helpers/colored_text.rb
254
+ - spec/support/helpers/logger_mock.rb
255
+ - spec/support/shared_examples/a_logging_entity.rb
@@ -1,68 +0,0 @@
1
- require 'active_support/inflector'
2
-
3
- module Sprig
4
- class SprigLogger
5
- def initialize
6
- @success_count = 0
7
- @error_count = 0
8
- @errors = []
9
- end
10
-
11
- def log_success(seed)
12
- message = seed.success_log_text
13
- puts green(message)
14
- @success_count += 1
15
- end
16
-
17
- def log_error(seed)
18
- message = seed.error_log_text
19
- @errors << seed.record
20
- puts red(message)
21
- @error_count += 1
22
- end
23
-
24
- def log_summary
25
- puts 'Seeding complete.'
26
-
27
- if @success_count > 0
28
- puts green(success_summary)
29
- else
30
- puts red(success_summary)
31
- end
32
-
33
- if @error_count > 0
34
- puts red(error_summary)
35
-
36
- @errors.each do |error|
37
- puts red("#{error}\n#{error.errors.messages}\n\n")
38
- end
39
- end
40
- end
41
-
42
- def processing
43
- print "Planting those seeds...\r"
44
- end
45
-
46
- private
47
-
48
- def colorize(message, color_code)
49
- "\e[#{color_code}m#{message}\e[0m"
50
- end
51
-
52
- def red(message)
53
- colorize(message, 31)
54
- end
55
-
56
- def green(message)
57
- colorize(message, 32)
58
- end
59
-
60
- def success_summary
61
- "#{@success_count} #{'seed'.pluralize(@success_count)} successfully planted."
62
- end
63
-
64
- def error_summary
65
- "#{@error_count} #{'seed'.pluralize(@error_count)} couldn't be planted:"
66
- end
67
- end
68
- end