sprig-reap 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 34c7b63d6313c6df641d97b2af567e75ce3bc4bd
4
- data.tar.gz: 261e9297a9f6c52eda8d1185a26bd2c27df83a6e
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MGFkOTJiZmQ2OTk3Nzg1YjczOGExODI4MTQwMDk5Mzc4M2YzZGJjZA==
5
+ data.tar.gz: !binary |-
6
+ MDYzMGYzYTZiY2IzMjJmZDU5MzczODMyZTVhYThjM2VlZmQ3ODEyMA==
5
7
  SHA512:
6
- metadata.gz: c78eec13c257dabf3aa1d0c3bcb6b4c20eaf2eb07fcbbaa24f64428af9fbc70a2fd40631310f78192ef94ff68e668014a3dac036fdacf8e7364e49dd02a382b2
7
- data.tar.gz: d85206a1bbd94e3c22452a87ed56746a402bf3104ec1d8489cdeab8f7d69b2b70e24055b60fb53ba13950d4af04d306bc0aebf5d81a3fcc93886224db88acb29
8
+ metadata.gz: !binary |-
9
+ YWUxZjgyYWYyODg2ZWQ1NWY3MjcxMjNjYzY3ZThhMDAwNDU4NzZkZmMxNDM1
10
+ YTlkZjFmYzAwZDNkZGFiYjM5MTdjNzgzZmNlNzhhY2NkMGM5YjE1ZWM3ZjJm
11
+ ZDYzNzA1MTcyMmYxMGNlM2FmMGRjMzcxNTk0YjFiMWU4N2E1NmU=
12
+ data.tar.gz: !binary |-
13
+ MDE1OWMyMTQ0MjI0YjVkNzBiNmViYWZiYzQwMWE2YjUyN2FjM2ViMDJmNWEw
14
+ ZDcwYTIwZDdmYmFlOGE1MjA3YWJhZGI3MmY5MzFjOWI1YjQxYTFjY2VkMmRk
15
+ MThjNjU3ZTMyMmRjMjM4OTdkNjIyMWJlMzEwODc3NmQ1ZGY0MTE=
data/README.md CHANGED
@@ -49,13 +49,14 @@ Sprig.reap(target_env: 'integration')
49
49
 
50
50
  #### Model List
51
51
  If you only want to `reap` a subset of your models, you may provide a list of models
52
- (`ActiveRecord::Base.subclasses`-only) you want seed files for:
52
+ (`ActiveRecord::Base.subclasses`-only) or `ActiveRecord::Relations` (for pulling records based on
53
+ scope):
53
54
  ```
54
55
  # Rake Task
55
- rake db:seed:reap MODELS=User,Post
56
+ rake db:seed:reap MODELS=User,Post.published
56
57
 
57
58
  # Rails Console
58
- Sprig.reap(models: [User, Post])
59
+ Sprig.reap(models: [User, Post.published])
59
60
  ```
60
61
 
61
62
  #### Ignored Attributes
@@ -84,10 +85,10 @@ Sprig.reap(omit_empty_attrs: true)
84
85
  You're free to take or leave as many options as you'd like:
85
86
  ```
86
87
  # Rake Task
87
- rake db:seed:reap TARGET_ENV=integration MODELS=User,Post IGNORED_ATTRS=created_at,updated_at OMIT_EMPTY_ATTRS=true
88
+ rake db:seed:reap TARGET_ENV=integration MODELS=User,Post.published IGNORED_ATTRS=created_at,updated_at OMIT_EMPTY_ATTRS=true
88
89
 
89
90
  # Rails Console
90
- Sprig.reap(target_env: 'integration', models: [User, Post], ignored_attrs: [:created_at, :updated_at], omit_empty_attrs: true)
91
+ Sprig.reap(target_env: 'integration', models: [User, Post.published], ignored_attrs: [:created_at, :updated_at], omit_empty_attrs: true)
91
92
  ```
92
93
 
93
94
  ### Adding to Existing Seed Files (`.yaml` only)
data/lib/sprig/reap.rb CHANGED
@@ -3,6 +3,7 @@ require "sprig/reap/railtie"
3
3
 
4
4
  module Sprig::Reap
5
5
  autoload :TsortableHash, 'sprig/reap/tsortable_hash'
6
+ autoload :Inputs, 'sprig/reap/inputs'
6
7
  autoload :Configuration, 'sprig/reap/configuration'
7
8
  autoload :Model, 'sprig/reap/model'
8
9
  autoload :Association, 'sprig/reap/association'
@@ -20,7 +21,7 @@ module Sprig::Reap
20
21
 
21
22
  configure do |config|
22
23
  config.target_env = options[:target_env] || options['TARGET_ENV']
23
- config.classes = options[:models] || options['MODELS']
24
+ config.models = options[:models] || options['MODELS']
24
25
  config.ignored_attrs = options[:ignored_attrs] || options['IGNORED_ATTRS']
25
26
  config.omit_empty_attrs = options[:omit_empty_attrs] || options['OMIT_EMPTY_ATTRS']
26
27
  end
@@ -41,7 +42,7 @@ module Sprig::Reap
41
42
  cattr_reader :configuration
42
43
 
43
44
  delegate :target_env,
44
- :classes,
45
+ :models,
45
46
  :ignored_attrs,
46
47
  :logger,
47
48
  :omit_empty_attrs,
@@ -2,31 +2,27 @@ module Sprig::Reap
2
2
  class Configuration
3
3
 
4
4
  def target_env
5
- @target_env ||= Rails.env
5
+ @target_env ||= Sprig::Reap::Inputs::Environment.default
6
6
  end
7
7
 
8
- def target_env=(given_env)
9
- parse_valid_env_from given_env do |target_environment|
10
- @target_env = target_environment
11
- end
8
+ def target_env=(input)
9
+ @target_env = Sprig::Reap::Inputs::Environment.parse(input)
12
10
  end
13
11
 
14
- def classes
15
- @classes ||= valid_classes
12
+ def models
13
+ @classes ||= Sprig::Reap::Inputs::Model.default
16
14
  end
17
15
 
18
- def classes=(given_classes)
19
- parse_valid_classes_from given_classes do |classes|
20
- @classes = classes
21
- end
16
+ def models=(input)
17
+ @classes ||= Sprig::Reap::Inputs::Model.parse(input)
22
18
  end
23
19
 
24
20
  def ignored_attrs
25
- @ignored_attrs ||= []
21
+ @ignored_attrs ||= Sprig::Reap::Inputs::IgnoredAttrs.default
26
22
  end
27
23
 
28
24
  def ignored_attrs=(input)
29
- @ignored_attrs = parse_ignored_attrs_from(input)
25
+ @ignored_attrs = Sprig::Reap::Inputs::IgnoredAttrs.parse(input)
30
26
  end
31
27
 
32
28
  def logger
@@ -40,58 +36,5 @@ module Sprig::Reap
40
36
  def omit_empty_attrs=(input)
41
37
  @omit_empty_attrs = true if String(input).strip.downcase == 'true'
42
38
  end
43
-
44
- private
45
-
46
- def valid_classes
47
- @valid_classes ||= begin
48
- Rails.application.eager_load!
49
- ActiveRecord::Base.subclasses
50
- end
51
- end
52
-
53
- def parse_valid_env_from(input)
54
- return if input.nil?
55
- target_environment = input.to_s.strip.downcase
56
- create_seeds_folder(target_environment)
57
- yield target_environment
58
- end
59
-
60
- def create_seeds_folder(target_env)
61
- folder = Rails.root.join('db', 'seeds', target_env)
62
- FileUtils.mkdir_p(folder) unless File.directory? folder
63
- end
64
-
65
- def parse_valid_classes_from(input)
66
- return if input.nil?
67
-
68
- classes = if input.is_a? String
69
- input.split(',').map { |klass_string| klass_string.strip.classify.constantize }
70
- else
71
- input
72
- end
73
-
74
- validate_classes(classes)
75
-
76
- yield classes
77
- end
78
-
79
- def validate_classes(classes)
80
- classes.each do |klass|
81
- unless valid_classes.include? klass
82
- raise ArgumentError, "Cannot create a seed file for #{klass} because it is not a subclass of ActiveRecord::Base."
83
- end
84
- end
85
- end
86
-
87
- def parse_ignored_attrs_from(input)
88
- return if input.nil?
89
-
90
- if input.is_a? String
91
- input.split(',').map(&:strip)
92
- else
93
- input.map(&:to_s).map(&:strip)
94
- end
95
- end
96
39
  end
97
40
  end
@@ -0,0 +1,13 @@
1
+ module Sprig::Reap
2
+ module Inputs
3
+ autoload :Environment, 'sprig/reap/inputs/environment'
4
+ autoload :Model, 'sprig/reap/inputs/model'
5
+ autoload :IgnoredAttrs, 'sprig/reap/inputs/ignored_attrs'
6
+
7
+ module_function
8
+
9
+ def Model(input)
10
+ input.is_a?(Model) ? input : Model.new(input)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,24 @@
1
+ module Sprig::Reap::Inputs
2
+ class Environment
3
+ attr_reader :input
4
+
5
+ def self.default
6
+ new().env
7
+ end
8
+
9
+ def self.parse(input)
10
+ new(input).env
11
+ end
12
+
13
+ def initialize(input = nil)
14
+ @input = input || Rails.env
15
+ end
16
+
17
+ def env
18
+ input.to_s.strip.downcase.tap do |target_env|
19
+ folder = Rails.root.join('db', 'seeds', target_env)
20
+ FileUtils.mkdir_p(folder) unless File.directory? folder
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ module Sprig::Reap::Inputs
2
+ class IgnoredAttrs
3
+ attr_reader :input
4
+
5
+ def self.default
6
+ new().list
7
+ end
8
+
9
+ def self.parse(input)
10
+ new(input).list
11
+ end
12
+
13
+ def initialize(input = nil)
14
+ @input = input || []
15
+ end
16
+
17
+ def list
18
+ collection = input.is_a?(String) ? input.split(',') : Array(input)
19
+
20
+ collection.map(&:to_s).map(&:strip)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,62 @@
1
+ module Sprig::Reap::Inputs
2
+ class Model
3
+ attr_reader :input
4
+
5
+ def self.valid_classes
6
+ @@valid_classes ||= begin
7
+ Rails.application.eager_load!
8
+ ActiveRecord::Base.subclasses
9
+ end
10
+ end
11
+
12
+ def self.default
13
+ valid_classes.map(&to_model_inputs)
14
+ end
15
+
16
+ def self.parse(input)
17
+ collection = if input.is_a? String
18
+ input.split(',').map { |string| string.strip }
19
+ elsif input.is_a? ActiveRecord::Relation
20
+ [input]
21
+ else
22
+ Array(input)
23
+ end
24
+
25
+ collection.map(&to_model_inputs)
26
+ end
27
+
28
+ def initialize(input)
29
+ @input = input.is_a?(String) ? eval(input) : input
30
+
31
+ validate!
32
+ end
33
+
34
+ def klass
35
+ @klass ||= relation? ? input.klass : input
36
+ end
37
+
38
+ def records
39
+ relation? ? input : input.all
40
+ end
41
+
42
+ private
43
+
44
+ def self.to_model_inputs
45
+ proc { |input| new(input) }
46
+ end
47
+
48
+ def valid_classes
49
+ self.class.valid_classes
50
+ end
51
+
52
+ def validate!
53
+ if valid_classes.exclude? klass
54
+ raise ArgumentError, "Cannot create a seed file for #{klass} because it is not a subclass of ActiveRecord::Base."
55
+ end
56
+ end
57
+
58
+ def relation?
59
+ input.is_a? ActiveRecord::Relation
60
+ end
61
+ end
62
+ end
@@ -1,10 +1,11 @@
1
1
  module Sprig::Reap
2
2
  class Model
3
3
  include Logging
4
+ include Inputs
4
5
 
5
6
  def self.all
6
7
  @@all ||= begin
7
- models = Sprig::Reap.classes.map { |klass| new(klass) }
8
+ models = Sprig::Reap.models.map { |model_input| new(model_input) }
8
9
 
9
10
  tsorted_classes(models).map do |klass|
10
11
  models.find { |model| model.klass == klass }
@@ -16,11 +17,12 @@ module Sprig::Reap
16
17
  all.find { |model| model.klass == klass }.find(id)
17
18
  end
18
19
 
19
- attr_reader :klass
20
+ attr_reader :model_input, :klass
20
21
  attr_writer :existing_sprig_ids
21
22
 
22
- def initialize(klass)
23
- @klass = klass
23
+ def initialize(model_input)
24
+ @model_input = Sprig::Reap::Inputs.Model(model_input)
25
+ @klass = @model_input.klass
24
26
  end
25
27
 
26
28
  def attributes
@@ -57,7 +59,7 @@ module Sprig::Reap
57
59
  return if records.empty?
58
60
 
59
61
  namespace = options[:namespace]
60
- formatted_records = records.map(&:to_hash)
62
+ formatted_records = [records.map(&:to_hash)]
61
63
 
62
64
  yaml = if namespace
63
65
  { namespace => formatted_records }.to_yaml
@@ -65,11 +67,12 @@ module Sprig::Reap
65
67
  formatted_records.to_yaml
66
68
  end
67
69
 
68
- yaml.gsub("---\n", '') # Remove annoying YAML separator
70
+ yaml.gsub!('- -', ' -') # Remove the extra array marker used to indent the sprig records
71
+ yaml.gsub!("---\n", '') # Remove annoying YAML separator
69
72
  end
70
73
 
71
74
  def records
72
- @records ||= klass.all.map { |record| Record.new(record, self) }
75
+ @records ||= model_input.records.map { |record| Record.new(record, self) }
73
76
  rescue => e
74
77
  log_error "Encountered an error when pulling the database records for #{to_s}:\r#{e.message}"
75
78
  []
@@ -1,5 +1,5 @@
1
1
  module Sprig
2
2
  module Reap
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -2,4 +2,6 @@ class Comment < ActiveRecord::Base
2
2
  belongs_to :post
3
3
 
4
4
  validates :post, :presence => true
5
+
6
+ has_many :votes, :as => :votable
5
7
  end
@@ -5,6 +5,9 @@ class Post < ActiveRecord::Base
5
5
 
6
6
  has_and_belongs_to_many :tags
7
7
 
8
+ scope :published, -> { where(:published => true) }
9
+ scope :with_content, -> { where('content IS NOT NULL') }
10
+
8
11
  def title
9
12
  WrappedAttribute.new(self[:title])
10
13
  end
@@ -1,3 +1,3 @@
1
- - sprig_id: 1
2
- votable_id: "<%= sprig_record(Post, 1).id %>"
3
- votable_type: Post
1
+ - sprig_id: 1
2
+ votable_id: "<%= sprig_record(Post, 1).id %>"
3
+ votable_type: Post
@@ -1,7 +1,7 @@
1
1
  records:
2
- - sprig_id: 1
3
- post_id: "<%= sprig_record(Post, 1).id %>"
4
- body:
5
- - sprig_id: 2
6
- post_id: "<%= sprig_record(Post, 2).id %>"
7
- body:
2
+ - sprig_id: 1
3
+ post_id: "<%= sprig_record(Post, 1).id %>"
4
+ body:
5
+ - sprig_id: 2
6
+ post_id: "<%= sprig_record(Post, 2).id %>"
7
+ body:
@@ -1,6 +1,6 @@
1
- - sprig_id: 1
2
- post_id: "<%= sprig_record(Post, 1).id %>"
3
- body:
4
- - sprig_id: 2
5
- post_id: "<%= sprig_record(Post, 2).id %>"
6
- body:
1
+ - sprig_id: 1
2
+ post_id: "<%= sprig_record(Post, 1).id %>"
3
+ body:
4
+ - sprig_id: 2
5
+ post_id: "<%= sprig_record(Post, 2).id %>"
6
+ body:
@@ -55,7 +55,7 @@ describe Sprig::Reap::Association do
55
55
  context "when the association is polymorphic" do
56
56
  subject { described_class.new(polymorphic_association) }
57
57
 
58
- its(:polymorphic_dependencies) { should == [Post] }
58
+ its(:polymorphic_dependencies) { should =~ [Post, Comment] }
59
59
  end
60
60
  end
61
61
 
@@ -81,7 +81,7 @@ describe Sprig::Reap::Association do
81
81
  context "when the association is polymorphic" do
82
82
  subject { described_class.new(polymorphic_association) }
83
83
 
84
- its(:dependencies) { should == [Post] }
84
+ its(:dependencies) { should =~ [Post, Comment] }
85
85
  end
86
86
 
87
87
  context "when the association is not polymorphic" do
@@ -8,140 +8,56 @@ describe Sprig::Reap::Configuration do
8
8
  end
9
9
 
10
10
  describe "#target_env" do
11
- context "from a fresh configuration" do
12
- its(:target_env) { should == Rails.env }
13
- end
14
- end
15
-
16
- describe "#target_env=" do
17
- context "when given nil" do
18
- it "does not change the target_env" do
19
- subject.target_env = nil
20
-
21
- subject.target_env.should_not == nil
22
- end
23
- end
24
-
25
- context "given a non-nil string value" do
26
- let(:input) { ' ShaBOOSH' }
27
-
28
- it "formats the given value and then sets the target environment" do
29
- subject.target_env = input
30
-
31
- subject.target_env.should == 'shaboosh'
32
- end
33
-
34
- context "and the corresponding seeds folder does not yet exist" do
35
- after do
36
- FileUtils.remove_dir('./spec/fixtures/db/seeds/shaboosh')
37
- end
38
-
39
- it "creates the seeds folder" do
40
- subject.target_env = input
41
-
42
- File.directory?('./spec/fixtures/db/seeds/shaboosh').should == true
43
- end
44
- end
45
- end
46
-
47
- context "given a non-nil symbol value" do
48
- let(:input) { :shaboosh }
49
-
50
- it "formats the given value and then sets the target environment" do
51
- subject.target_env = input
52
-
53
- subject.target_env.should == 'shaboosh'
54
- end
55
-
56
- context "and the corresponding seeds folder does not yet exist" do
57
- after do
58
- FileUtils.remove_dir('./spec/fixtures/db/seeds/shaboosh')
59
- end
60
-
61
- it "creates the seeds folder" do
62
- subject.target_env = input
11
+ context "given a fresh configuration" do
12
+ it "grabs the default" do
13
+ Sprig::Reap::Inputs::Environment.should_receive(:default)
63
14
 
64
- File.directory?('./spec/fixtures/db/seeds/shaboosh').should == true
65
- end
15
+ subject.target_env
66
16
  end
67
17
  end
68
18
  end
69
19
 
70
- describe "#classes" do
71
- context "from a fresh configuration" do
72
- its(:classes) { should == ActiveRecord::Base.subclasses }
73
- end
74
- end
75
-
76
- describe "#classes=" do
77
- context "when given nil" do
78
- it "does not set classes to nil" do
79
- subject.classes = nil
20
+ describe "#target_env=" do
21
+ it "parses the input" do
22
+ Sprig::Reap::Inputs::Environment.should_receive(:parse).with(:shaboosh)
80
23
 
81
- subject.classes.should_not == nil
82
- end
24
+ subject.target_env = :shaboosh
83
25
  end
26
+ end
84
27
 
85
- context "when given an array of classes" do
86
- context "where one or more classes are not subclasses of ActiveRecord::Base" do
87
- it "raises an error" do
88
- expect {
89
- subject.classes = [Comment, Sprig::Reap::Model]
90
- }.to raise_error ArgumentError, 'Cannot create a seed file for Sprig::Reap::Model because it is not a subclass of ActiveRecord::Base.'
91
- end
92
- end
93
-
94
- context "where all classes are subclasses of ActiveRecord::Base" do
95
- it "sets classes to the given input" do
96
- subject.classes = [Comment, Post]
28
+ describe "#models" do
29
+ context "given a fresh configuration" do
30
+ it "grabs the default" do
31
+ Sprig::Reap::Inputs::Model.should_receive(:default)
97
32
 
98
- subject.classes.should == [Comment, Post]
99
- end
33
+ subject.models
100
34
  end
101
35
  end
36
+ end
102
37
 
103
- context "when given a string" do
104
- context "where one or more classes are not subclasses of ActiveRecord::Base" do
105
- it "raises an error" do
106
- expect {
107
- subject.classes = 'Sprig::Reap::Model'
108
- }.to raise_error ArgumentError, 'Cannot create a seed file for Sprig::Reap::Model because it is not a subclass of ActiveRecord::Base.'
109
- end
110
- end
111
-
112
- context "where all classes are subclasses of ActiveRecord::Base" do
113
- it "sets classes to the parsed input" do
114
- subject.classes = ' comment, post'
38
+ describe "#models=" do
39
+ it "parses the input" do
40
+ Sprig::Reap::Inputs::Model.should_receive(:parse).with(Post)
115
41
 
116
- subject.classes.should == [Comment, Post]
117
- end
118
- end
42
+ subject.models = Post
119
43
  end
120
44
  end
121
45
 
122
46
  describe "#ignored_attrs" do
123
- context "from a fresh configuration" do
124
- its(:ignored_attrs) { should == [] }
47
+ context "given a fresh configuration" do
48
+ it "grabs the default" do
49
+ Sprig::Reap::Inputs::IgnoredAttrs.should_receive(:default)
50
+
51
+ subject.ignored_attrs
52
+ end
125
53
  end
126
54
  end
127
55
 
128
56
  describe "#ignored_attrs=" do
129
- context "when given nil" do
130
- before { subject.ignored_attrs = nil }
131
-
132
- its(:ignored_attrs) { should == [] }
133
- end
134
-
135
- context "when given an array of ignored_attrs" do
136
- before { subject.ignored_attrs = [:shaka, ' laka '] }
137
-
138
- its(:ignored_attrs) { should == ['shaka', 'laka'] }
139
- end
140
-
141
- context "when given a string" do
142
- before { subject.ignored_attrs = ' shaka, laka' }
57
+ it "parses the input" do
58
+ Sprig::Reap::Inputs::IgnoredAttrs.should_receive(:parse).with('boom, shaka, laka')
143
59
 
144
- its(:ignored_attrs) { should == ['shaka', 'laka'] }
60
+ subject.ignored_attrs = 'boom, shaka, laka'
145
61
  end
146
62
  end
147
63
 
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sprig::Reap::Inputs::Environment do
4
+ before do
5
+ stub_rails_root
6
+ end
7
+
8
+ describe ".default" do
9
+ subject { described_class }
10
+
11
+ its(:default) { should == 'test' }
12
+ end
13
+
14
+ describe ".parse" do
15
+ let(:environment) { double('Sprig::Reap::Inputs::Environment') }
16
+
17
+ before do
18
+ Sprig::Reap::Inputs::Environment.stub(:new).with(:shaboosh).and_return(environment)
19
+ end
20
+
21
+ it "instantiates a Sprig::Reap::Inputs::Environment and returns the env from it" do
22
+ environment.should_receive(:env)
23
+
24
+ described_class.parse(:shaboosh)
25
+ end
26
+ end
27
+
28
+ describe "#env" do
29
+ context "when no input is given" do
30
+ subject { described_class.new }
31
+
32
+ its(:env) { should == 'test' }
33
+ end
34
+
35
+ context "when the input is a blank value" do
36
+ subject { described_class.new(nil) }
37
+
38
+ its(:env) { should == 'test' }
39
+ end
40
+
41
+ context "when the input is a string" do
42
+ subject { described_class.new(' ShaBOOSH') }
43
+
44
+ it "formats the given value and then sets the target environment" do
45
+ subject.env.should == 'shaboosh'
46
+ end
47
+
48
+ context "and the corresponding seeds folder does not yet exist" do
49
+ after do
50
+ FileUtils.remove_dir('./spec/fixtures/db/seeds/shaboosh')
51
+ end
52
+
53
+ it "creates the seeds folder" do
54
+ subject.env
55
+
56
+ File.directory?('./spec/fixtures/db/seeds/shaboosh').should == true
57
+ end
58
+ end
59
+ end
60
+
61
+ context "given a symbol" do
62
+ subject { described_class.new(:shaboosh) }
63
+
64
+ it "formats the given value and then sets the target environment" do
65
+ subject.env.should == 'shaboosh'
66
+ end
67
+
68
+ context "and the corresponding seeds folder does not yet exist" do
69
+ after do
70
+ FileUtils.remove_dir('./spec/fixtures/db/seeds/shaboosh')
71
+ end
72
+
73
+ it "creates the seeds folder" do
74
+ subject.env
75
+
76
+ File.directory?('./spec/fixtures/db/seeds/shaboosh').should == true
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sprig::Reap::Inputs::IgnoredAttrs do
4
+ describe ".default" do
5
+ subject { described_class }
6
+
7
+ its(:default) { should == [] }
8
+ end
9
+
10
+ describe ".parse" do
11
+ let(:input) { 'boom, shaka, laka' }
12
+ let(:ignored_attrs) { double('Sprig::Reap::Inputs::IgnoredAttrs') }
13
+
14
+ before do
15
+ Sprig::Reap::Inputs::IgnoredAttrs.stub(:new).with(input).and_return(ignored_attrs)
16
+ end
17
+
18
+ it "instantiates a Sprig::Reap::Inputs::IgnoredAttrs with the given input and grabs the list from it" do
19
+ ignored_attrs.should_receive(:list)
20
+
21
+ described_class.parse(input)
22
+ end
23
+ end
24
+
25
+ describe "#list" do
26
+ let(:input) { 'boom, shaka, laka' }
27
+
28
+ subject { described_class.new(input) }
29
+
30
+ its(:list) { should == ['boom', 'shaka', 'laka'] }
31
+ end
32
+ end
@@ -0,0 +1,129 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sprig::Reap::Inputs::Model do
4
+ describe ".valid_classes" do
5
+ subject { described_class }
6
+
7
+ its(:valid_classes) { should == ActiveRecord::Base.subclasses }
8
+ end
9
+
10
+ describe ".default" do
11
+ it "instantiates a Sprig::Reap::Inputs::Model for each model" do
12
+ ActiveRecord::Base.subclasses.each do |model|
13
+ Sprig::Reap::Inputs::Model.should_receive(:new).with(model)
14
+ end
15
+
16
+ described_class.default
17
+ end
18
+ end
19
+
20
+ describe ".parse" do
21
+ context "given a list in a string" do
22
+ it "instantiates a Sprig::Reap::Inputs::Model for each piece of input" do
23
+ Sprig::Reap::Inputs::Model.should_receive(:new).with('User')
24
+ Sprig::Reap::Inputs::Model.should_receive(:new).with('Post.published')
25
+ Sprig::Reap::Inputs::Model.should_receive(:new).with('Tag')
26
+
27
+ described_class.parse('User, Post.published, Tag')
28
+ end
29
+ end
30
+
31
+ context "given an array" do
32
+ it "instantiates a Sprig::Reap::Inputs::Model for each piece of input" do
33
+ Sprig::Reap::Inputs::Model.should_receive(:new).with(User)
34
+ Sprig::Reap::Inputs::Model.should_receive(:new).with(Post.published)
35
+ Sprig::Reap::Inputs::Model.should_receive(:new).with(Tag)
36
+
37
+ described_class.parse([User, Post.published, Tag])
38
+ end
39
+ end
40
+
41
+ context "given a single item" do
42
+ it "instantiates a Sprig::Reap::Inputs::Model with the input" do
43
+ Sprig::Reap::Inputs::Model.should_receive(:new).with(Post.published)
44
+
45
+ described_class.parse(Post.published)
46
+ end
47
+ end
48
+ end
49
+
50
+ describe "#initialize" do
51
+ context "when the input is a String" do
52
+ context "representing an ActiveRecord::Base class" do
53
+ it "does not raise an error" do
54
+ expect { described_class.new('User') }.to_not raise_error
55
+ end
56
+ end
57
+
58
+ context "representing an ActiveRecord::Relation" do
59
+ it "does not raise an error" do
60
+ expect { described_class.new('Post.published') }.to_not raise_error
61
+ end
62
+ end
63
+
64
+ context "representing a non-ActiveRecord::Base class" do
65
+ it "raises an error" do
66
+ expect { described_class.new('Sprig::Reap::Model') }.to raise_error
67
+ end
68
+ end
69
+
70
+ context "representing some non-class identifier" do
71
+ it "raises an error" do
72
+ expect { described_class.new('klass') }.to raise_error
73
+ end
74
+ end
75
+ end
76
+
77
+ context "when the input is a non-ActiveRecord::Base class" do
78
+ it "raises an error" do
79
+ expect { described_class.new(Sprig::Reap::Record) }.to raise_error
80
+ end
81
+ end
82
+
83
+ context "when the input is an ActiveRecord::Base class" do
84
+ it "does not raise an error" do
85
+ expect { described_class.new(Tag) }.to_not raise_error
86
+ end
87
+ end
88
+
89
+ context "when the input is an ActiveRecord::Relation" do
90
+ it "does not raise an error" do
91
+ expect { described_class.new(Post.published) }.to_not raise_error
92
+ end
93
+ end
94
+ end
95
+
96
+ describe "#klass" do
97
+ context "when the input is an ActiveRecord::Base class" do
98
+ subject { described_class.new(Tag) }
99
+
100
+ its(:klass) { should == Tag }
101
+ end
102
+
103
+ context "when the input is an ActiveRecord::Relation" do
104
+ subject { described_class.new(Post.published) }
105
+
106
+ its(:klass) { should == Post }
107
+ end
108
+ end
109
+
110
+ describe "#records" do
111
+ let!(:bob) { User.create(:first_name => 'Bob', :last_name => 'Costas') }
112
+ let!(:john) { User.create(:first_name => 'John', :last_name => 'Madden') }
113
+
114
+ context "when the input is an ActiveRecord::Base class" do
115
+ subject { described_class.new(User) }
116
+
117
+ its(:records) { should == [bob, john]}
118
+ end
119
+
120
+ context "when the input is an ActiveRecord::Relation" do
121
+ let!(:unpublished) { Post.create(:published => false, :poster => bob) }
122
+ let!(:published) { Post.create(:published => true, :poster => john) }
123
+
124
+ subject { described_class.new(Post.published) }
125
+
126
+ its(:records) { should == [published] }
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sprig::Reap::Inputs do
4
+ describe "Model()" do
5
+ context "given a non-Sprig::Reap::Inputs::Model" do
6
+ it "instantiates a Sprig::Reap::Inputs::Model with the given input" do
7
+ described_class.Model(Post).should be_an_instance_of Sprig::Reap::Inputs::Model
8
+ end
9
+ end
10
+
11
+ context "given a Sprig::Reap::Inputs::Model" do
12
+ let(:input) { Sprig::Reap::Inputs::Model.new(Post) }
13
+
14
+ it "returns the given input" do
15
+ described_class.Model(input).should == input
16
+ end
17
+ end
18
+ end
19
+ end
@@ -13,7 +13,13 @@ describe Sprig::Reap::Model do
13
13
  end
14
14
 
15
15
  before do
16
- Sprig::Reap.stub(:classes).and_return([Comment, Post, User, Vote, Tag])
16
+ Sprig::Reap.stub(:models).and_return([
17
+ Sprig::Reap::Inputs::Model.new(Comment),
18
+ Sprig::Reap::Inputs::Model.new(Post),
19
+ Sprig::Reap::Inputs::Model.new(User),
20
+ Sprig::Reap::Inputs::Model.new(Vote),
21
+ Sprig::Reap::Inputs::Model.new(Tag)
22
+ ])
17
23
  end
18
24
 
19
25
  it "returns an dependency-sorted array of Sprig::Reap::Models" do
@@ -70,7 +76,7 @@ describe Sprig::Reap::Model do
70
76
  context "when the model is polymorphic" do
71
77
  subject { described_class.new(Vote) }
72
78
 
73
- its(:dependencies) { should == [Post] }
79
+ its(:dependencies) { should =~ [Post, Comment] }
74
80
  end
75
81
 
76
82
  context "when the model has a HABTM dependency or a dependency with an explicit :class_name" do
@@ -150,13 +156,13 @@ describe Sprig::Reap::Model do
150
156
 
151
157
  context "when passed a value for the namespace" do
152
158
  it "returns the correct yaml" do
153
- subject.to_yaml(:namespace => 'records').should == yaml_from_file('records_with_namespace.yml')
159
+ YAML.load(subject.to_yaml(:namespace => 'records')).should == YAML.load(yaml_from_file('records_with_namespace.yml'))
154
160
  end
155
161
  end
156
162
 
157
163
  context "when no namespace is given" do
158
164
  it "returns the correct yaml" do
159
- subject.to_yaml.should == yaml_from_file('records_without_namespace.yml')
165
+ YAML.load(subject.to_yaml).should == YAML.load(yaml_from_file('records_without_namespace.yml'))
160
166
  end
161
167
  end
162
168
 
@@ -166,7 +172,7 @@ describe Sprig::Reap::Model do
166
172
 
167
173
  subject = described_class.new(Vote)
168
174
 
169
- subject.to_yaml.should == yaml_from_file('polymorphic_vote_record.yml')
175
+ YAML.load(subject.to_yaml).should == YAML.load(yaml_from_file('polymorphic_vote_record.yml'))
170
176
  end
171
177
  end
172
178
 
@@ -208,6 +214,6 @@ describe Sprig::Reap::Model do
208
214
  end
209
215
 
210
216
  def yaml_from_file(basename)
211
- YAML.load(File.read('spec/fixtures/yaml/' + basename)).to_yaml.gsub("---\n", '')
217
+ File.read('spec/fixtures/yaml/' + basename).gsub("\"", '')
212
218
  end
213
219
  end
@@ -25,8 +25,8 @@ describe Sprig::Reap do
25
25
  subject.reap
26
26
  end
27
27
 
28
- it "generates a seed file for each class" do
29
- count = Sprig::Reap.classes.count
28
+ it "generates a seed file for each model" do
29
+ count = Sprig::Reap.models.count
30
30
 
31
31
  seed_file.should_receive(:write).exactly(count).times
32
32
 
@@ -49,18 +49,31 @@ describe Sprig::Reap do
49
49
  end
50
50
  end
51
51
 
52
- context "when passed a set of classes in the options hash" do
53
- context "in :classes" do
54
- it "sets the classes" do
52
+ context "when passed a set of models in the options hash" do
53
+ context "in :models" do
54
+ it "sets the models" do
55
55
  subject.reap(:models => [User, Post])
56
- subject.classes.should == [User, Post]
56
+
57
+ subject.models.all? { |model| model.class == Sprig::Reap::Inputs::Model }.should == true
58
+ subject.models.map(&:klass).should == [User, Post]
57
59
  end
58
60
  end
59
61
 
60
- context "sets the classes" do
61
- it "passes the value to its configuration" do
62
+ context "in 'MODELS'" do
63
+ it "sets the models" do
62
64
  subject.reap('MODELS' => 'User, Post')
63
- subject.classes.should == [User, Post]
65
+
66
+ subject.models.all? { |model| model.class == Sprig::Reap::Inputs::Model }.should == true
67
+ subject.models.map(&:klass).should == [User, Post]
68
+ end
69
+ end
70
+
71
+ context "as an ActiveRecord::Relation" do
72
+ it "sets the relation to one of the models" do
73
+ subject.reap(:models => [Post.published.with_content])
74
+
75
+ subject.models.all? { |model| model.class == Sprig::Reap::Inputs::Model }.should == true
76
+ subject.models.map(&:klass).should == [Post]
64
77
  end
65
78
  end
66
79
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sprig-reap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Stenberg
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-02 00:00:00.000000000 Z
11
+ date: 2015-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -31,6 +31,9 @@ dependencies:
31
31
  - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: 1.3.8
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: 1.3.8
34
37
  type: :development
35
38
  prerelease: false
36
39
  version_requirements: !ruby/object:Gem::Requirement
@@ -38,6 +41,9 @@ dependencies:
38
41
  - - ~>
39
42
  - !ruby/object:Gem::Version
40
43
  version: 1.3.8
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 1.3.8
41
47
  - !ruby/object:Gem::Dependency
42
48
  name: rspec
43
49
  requirement: !ruby/object:Gem::Requirement
@@ -45,6 +51,9 @@ dependencies:
45
51
  - - ~>
46
52
  - !ruby/object:Gem::Version
47
53
  version: 2.14.0
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: 2.14.0
48
57
  type: :development
49
58
  prerelease: false
50
59
  version_requirements: !ruby/object:Gem::Requirement
@@ -52,6 +61,9 @@ dependencies:
52
61
  - - ~>
53
62
  - !ruby/object:Gem::Version
54
63
  version: 2.14.0
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: 2.14.0
55
67
  - !ruby/object:Gem::Dependency
56
68
  name: database_cleaner
57
69
  requirement: !ruby/object:Gem::Requirement
@@ -59,6 +71,9 @@ dependencies:
59
71
  - - ~>
60
72
  - !ruby/object:Gem::Version
61
73
  version: 1.2.0
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: 1.2.0
62
77
  type: :development
63
78
  prerelease: false
64
79
  version_requirements: !ruby/object:Gem::Requirement
@@ -66,32 +81,35 @@ dependencies:
66
81
  - - ~>
67
82
  - !ruby/object:Gem::Version
68
83
  version: 1.2.0
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: 1.2.0
69
87
  - !ruby/object:Gem::Dependency
70
88
  name: pry
71
89
  requirement: !ruby/object:Gem::Requirement
72
90
  requirements:
73
- - - '>='
91
+ - - ! '>='
74
92
  - !ruby/object:Gem::Version
75
93
  version: '0'
76
94
  type: :development
77
95
  prerelease: false
78
96
  version_requirements: !ruby/object:Gem::Requirement
79
97
  requirements:
80
- - - '>='
98
+ - - ! '>='
81
99
  - !ruby/object:Gem::Version
82
100
  version: '0'
83
101
  - !ruby/object:Gem::Dependency
84
102
  name: generator_spec
85
103
  requirement: !ruby/object:Gem::Requirement
86
104
  requirements:
87
- - - '>='
105
+ - - ! '>='
88
106
  - !ruby/object:Gem::Version
89
107
  version: '0'
90
108
  type: :development
91
109
  prerelease: false
92
110
  version_requirements: !ruby/object:Gem::Requirement
93
111
  requirements:
94
- - - '>='
112
+ - - ! '>='
95
113
  - !ruby/object:Gem::Version
96
114
  version: '0'
97
115
  - !ruby/object:Gem::Dependency
@@ -116,9 +134,18 @@ executables: []
116
134
  extensions: []
117
135
  extra_rdoc_files: []
118
136
  files:
137
+ - MIT-LICENSE
138
+ - README.md
139
+ - Rakefile
140
+ - lib/sprig-reap.rb
141
+ - lib/sprig/reap.rb
119
142
  - lib/sprig/reap/association.rb
120
143
  - lib/sprig/reap/configuration.rb
121
144
  - lib/sprig/reap/file_attribute.rb
145
+ - lib/sprig/reap/inputs.rb
146
+ - lib/sprig/reap/inputs/environment.rb
147
+ - lib/sprig/reap/inputs/ignored_attrs.rb
148
+ - lib/sprig/reap/inputs/model.rb
122
149
  - lib/sprig/reap/logging.rb
123
150
  - lib/sprig/reap/model.rb
124
151
  - lib/sprig/reap/railtie.rb
@@ -127,12 +154,7 @@ files:
127
154
  - lib/sprig/reap/tsortable_hash.rb
128
155
  - lib/sprig/reap/value.rb
129
156
  - lib/sprig/reap/version.rb
130
- - lib/sprig/reap.rb
131
- - lib/sprig-reap.rb
132
157
  - lib/tasks/reap.rake
133
- - MIT-LICENSE
134
- - Rakefile
135
- - README.md
136
158
  - spec/db/activerecord.db
137
159
  - spec/fixtures/images/avatar.png
138
160
  - spec/fixtures/models/comment.rb
@@ -150,6 +172,10 @@ files:
150
172
  - spec/lib/sprig/reap/association_spec.rb
151
173
  - spec/lib/sprig/reap/configuration_spec.rb
152
174
  - spec/lib/sprig/reap/file_attribute_spec.rb
175
+ - spec/lib/sprig/reap/inputs/environment_spec.rb
176
+ - spec/lib/sprig/reap/inputs/ignored_attrs_spec.rb
177
+ - spec/lib/sprig/reap/inputs/model_spec.rb
178
+ - spec/lib/sprig/reap/inputs_spec.rb
153
179
  - spec/lib/sprig/reap/model_spec.rb
154
180
  - spec/lib/sprig/reap/record_spec.rb
155
181
  - spec/lib/sprig/reap/seed_file_spec.rb
@@ -172,17 +198,17 @@ require_paths:
172
198
  - lib
173
199
  required_ruby_version: !ruby/object:Gem::Requirement
174
200
  requirements:
175
- - - '>='
201
+ - - ! '>='
176
202
  - !ruby/object:Gem::Version
177
203
  version: '0'
178
204
  required_rubygems_version: !ruby/object:Gem::Requirement
179
205
  requirements:
180
- - - '>='
206
+ - - ! '>='
181
207
  - !ruby/object:Gem::Version
182
208
  version: '0'
183
209
  requirements: []
184
210
  rubyforge_project:
185
- rubygems_version: 2.0.14
211
+ rubygems_version: 2.4.2
186
212
  signing_key:
187
213
  specification_version: 4
188
214
  summary: Automatic seed file generation for Rails apps using Sprig.
@@ -204,6 +230,10 @@ test_files:
204
230
  - spec/lib/sprig/reap/association_spec.rb
205
231
  - spec/lib/sprig/reap/configuration_spec.rb
206
232
  - spec/lib/sprig/reap/file_attribute_spec.rb
233
+ - spec/lib/sprig/reap/inputs/environment_spec.rb
234
+ - spec/lib/sprig/reap/inputs/ignored_attrs_spec.rb
235
+ - spec/lib/sprig/reap/inputs/model_spec.rb
236
+ - spec/lib/sprig/reap/inputs_spec.rb
207
237
  - spec/lib/sprig/reap/model_spec.rb
208
238
  - spec/lib/sprig/reap/record_spec.rb
209
239
  - spec/lib/sprig/reap/seed_file_spec.rb