seed_scatter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in seed_scatter.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alex Padgett
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # SeedScatter
2
+
3
+ SeedScatter is a gem that adds utility and organization to Ruby on Rails seed files by scattering them into multiple files.
4
+
5
+ SeedScatter allows you to:
6
+
7
+ * Separate seeds into model specific files
8
+ * Dump model information already in the database to their respective seed files
9
+
10
+ ## Installation
11
+
12
+ Add it to your Gemfile:
13
+
14
+ gem 'seed_scatter'
15
+
16
+ And then run:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install seed_scatter
23
+
24
+ ## Usage
25
+
26
+ SeedScatter overrides the default rake task `db:seed` to separate seeds. Seeding is still just as easy as it was before:
27
+
28
+ $ rake db:seed
29
+
30
+ To dump a specific model's information from the database to a seed file:
31
+
32
+ $ rake db:harvest[MODEL_NAME]
33
+
34
+ To dump all of your models' information from the database to their respective seed files:
35
+
36
+ $ rake db:harvest
37
+
38
+
39
+ ## Todos
40
+
41
+ * Re-seed models one at a time
42
+ * Support for namespaced models
43
+ * Support for join tables
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ Feature:
2
+ In order to separate my model seeds
3
+ As a user
4
+ I would like to create a seed file for every model generated
5
+
6
+ Background:
7
+ Given I successfully run `bundle exec rails new testapp --database sqlite3`
8
+ And I cd to "testapp"
9
+ And I add "seed_scatter" from this project as a dependency
10
+ When I run `bundle install`
11
+
12
+ Scenario: Seed scatter creates a seed file for every model I create
13
+ When I run `bundle exec rails generate model User name:string`
14
+ Then the output should contain "db/seeds/users.rb"
15
+ And the file "db/seeds/users.rb" should contain "User.create"
16
+
17
+ Scenario: Seed scatter should use generated seed files
18
+ When I generate a User model
19
+ And I overwrite "db/seeds/users.rb" with:
20
+ """
21
+ User.create([{ name: "Test User" }])
22
+ """
23
+ And I run `bundle exec rake db:seed`
24
+ And I type `User.first` in the rails console
25
+ Then the output should contain:
26
+ """
27
+ #<User id: 1, name: "Test User"
28
+ """
@@ -0,0 +1,20 @@
1
+ When /^I add "([^"]+)" from this project as a dependency$/ do |gem_name|
2
+ append_to_file('Gemfile', %{gem "#{gem_name}", :path => "#{PROJECT_ROOT}"\n})
3
+ end
4
+
5
+ And /^I create a user with name "(.+)"$/ do |name|
6
+ step %{I run `bundle exec rails console` interactively}
7
+ step %{I type "User.create(name: #{name}"}
8
+ step %{I type "exit"}
9
+ end
10
+
11
+ And /^I generate a (.+) model$/ do |klass|
12
+ step %{I run `bundle exec rails generate model #{klass} name:string`}
13
+ step %{I run `bundle exec rake db:migrate`}
14
+ end
15
+
16
+ And /^I type `(.+)` in the rails console$/ do |command|
17
+ step %{I run `bundle exec rails console` interactively}
18
+ step %{I type "#{command}"}
19
+ step %{I type "exit"}
20
+ end
@@ -0,0 +1,15 @@
1
+ require 'aruba/cucumber'
2
+
3
+ #taken from thoughtbot's factory_girl_rails config - https://github.com/thoughtbot/factory_girl_rails
4
+ Before do
5
+ @aruba_timeout_seconds = 3600
6
+ end
7
+
8
+ PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..', '..')).freeze
9
+
10
+ Aruba.configure do |config|
11
+ config.before_cmd do |cmd|
12
+ set_env('JRUBY_OPTS', "-X-C #{ENV['JRUBY_OPTS']}") # disable JIT since these processes are so short lived
13
+ set_env('JAVA_OPTS', "-d32 #{ENV['JAVA_OPTS']}") # force jRuby to use client JVM for faster startup times
14
+ end
15
+ end if RUBY_PLATFORM == 'java'
@@ -0,0 +1,15 @@
1
+ require 'generators/seed_scatter'
2
+
3
+ module SeedScatter
4
+ module Generators
5
+ class ModelGenerator < Base
6
+ class_option :dir, :type => :string, :default => "db/seeds", :desc => "The directory where the seeds should go"
7
+ hook_for :orm, required: true
8
+
9
+ def seed_scatter_file
10
+ template 'seed.erb', File.join(options[:dir], "#{table_name}.rb")
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,8 @@
1
+ require 'generators/model/model_generator'
2
+
3
+ module SeedScatter
4
+ module Generators
5
+ class ScaffoldGenerator < SeedScatter::Generators::ModelGenerator
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ require 'rails/generators/named_base'
2
+
3
+ module SeedScatter
4
+ module Generators
5
+ class Base < Rails::Generators::NamedBase
6
+ def self.source_root
7
+ File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # This file should contain all the record creation needed to seed the database with its default values.
2
+ # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
3
+ #
4
+ # Examples:
5
+ #
6
+ # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
7
+ # Mayor.create(name: 'Emanuel', city: cities.first)
8
+
9
+ <%= class_name %>.create([
10
+ <%= @harvest_data.join(",\n ") if @harvest_data.present? %>
11
+ ], :without_protection => true)
@@ -0,0 +1,7 @@
1
+ require "seed_scatter/version"
2
+ require "seed_scatter/engine"
3
+ require "seed_scatter/railtie"
4
+ require "seed_scatter/harvest"
5
+
6
+ module SeedScatter
7
+ end
@@ -0,0 +1,11 @@
1
+ module SeedScatter
2
+ class Engine < ::Rails::Engine
3
+ paths["db/seeds.rb"] = Dir.glob("db/seeds/*.rb")
4
+ def load_seed
5
+ paths["db/seeds.rb"].each do |seed_file|
6
+ puts seed_file
7
+ load(seed_file)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,46 @@
1
+ require 'thor/group'
2
+
3
+ module SeedScatter
4
+ class Harvester < Thor::Group
5
+ include Thor::Actions
6
+
7
+ def init(model)
8
+ models = if model == :all
9
+ Dir['app/models/*.rb'].map {|f| File.basename(f, '.*').camelize.constantize.name }
10
+ else
11
+ [model]
12
+ end
13
+ models.each do |m|
14
+ @model = m.underscore.singularize
15
+ puts "Harvesting seeds for #{class_name}"
16
+ @harvest_data = []
17
+ dump_model(m)
18
+ end
19
+ end
20
+
21
+ def collect_data(model)
22
+ klass = model.camelize.constantize
23
+ klass.all.each do |m|
24
+ @harvest_data << ActiveSupport::JSON.decode(m.attributes.to_json).symbolize_keys
25
+ end
26
+ end
27
+
28
+ def dump_model(model)
29
+ collect_data(model)
30
+ template "seed.erb", destination_path(model), force: true
31
+ end
32
+
33
+ def self.source_root(path=nil)
34
+ File.expand_path(File.join(File.dirname(__FILE__), '..', 'generators', 'templates'))
35
+ end
36
+
37
+ def class_name
38
+ @model.camelize
39
+ end
40
+
41
+ def destination_path(model)
42
+ "db/seeds/#{model.downcase.pluralize}.rb"
43
+ end
44
+ end
45
+
46
+ end
@@ -0,0 +1,13 @@
1
+ module SeedScatter
2
+ class Railtie < ::Rails::Railtie
3
+
4
+ generators do
5
+ require 'rails/generators/rails/model/model_generator'
6
+ require 'rails/generators/rails/scaffold/scaffold_generator'
7
+ Rails::Generators::ModelGenerator.send(:hook_for,:seed_scatter)
8
+ Rails::Generators::ModelGenerator.send(:class_option, :seed_scatter, :default => 'seed_scatter')
9
+ Rails::Generators::ScaffoldGenerator.send(:hook_for,:seed_scatter)
10
+ Rails::Generators::ScaffoldGenerator.send(:class_option, :seed_scatter, :default => 'seed_scatter')
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module SeedScatter
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ namespace :db do
2
+ task :seed => :environment do
3
+ SeedScatter::Engine.load_seed
4
+ end
5
+ desc "Dump model seeds"
6
+ task :harvest, [:model] => :environment do |t,args|
7
+ args.with_defaults(:model => :all)
8
+ SeedScatter::Harvester.new.init args[:model]
9
+ end
10
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'seed_scatter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "seed_scatter"
8
+ spec.version = SeedScatter::VERSION
9
+ spec.authors = ["Alex Padgett"]
10
+ spec.email = ["apadgett@topicdesign.com"]
11
+ spec.description = %q{seed scatter changes some utility for rails seed files}
12
+ spec.summary = %q{seed scatter splits your model seeds into individual files}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'rails', ">= 3.0.0"
22
+
23
+ spec.add_development_dependency "sqlite3"
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec", "~> 2.13.0"
27
+ spec.add_development_dependency "cucumber", "~> 1.3.3"
28
+ spec.add_development_dependency "aruba", "~> 0.5.1"
29
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seed_scatter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alex Padgett
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: sqlite3
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 2.13.0
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 2.13.0
94
+ - !ruby/object:Gem::Dependency
95
+ name: cucumber
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 1.3.3
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.3.3
110
+ - !ruby/object:Gem::Dependency
111
+ name: aruba
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 0.5.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 0.5.1
126
+ description: seed scatter changes some utility for rails seed files
127
+ email:
128
+ - apadgett@topicdesign.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - Gemfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - features/generators.feature
139
+ - features/step_definitions/rails_steps.rb
140
+ - features/support/env.rb
141
+ - lib/generators/model/model_generator.rb
142
+ - lib/generators/scaffold/scaffold_generator.rb
143
+ - lib/generators/seed_scatter.rb
144
+ - lib/generators/templates/seed.erb
145
+ - lib/seed_scatter.rb
146
+ - lib/seed_scatter/engine.rb
147
+ - lib/seed_scatter/harvest.rb
148
+ - lib/seed_scatter/railtie.rb
149
+ - lib/seed_scatter/version.rb
150
+ - lib/tasks/seed_scatter.rake
151
+ - seed_scatter.gemspec
152
+ homepage: ''
153
+ licenses:
154
+ - MIT
155
+ post_install_message:
156
+ rdoc_options: []
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ none: false
161
+ requirements:
162
+ - - ! '>='
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ none: false
167
+ requirements:
168
+ - - ! '>='
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ requirements: []
172
+ rubyforge_project:
173
+ rubygems_version: 1.8.23
174
+ signing_key:
175
+ specification_version: 3
176
+ summary: seed scatter splits your model seeds into individual files
177
+ test_files:
178
+ - features/generators.feature
179
+ - features/step_definitions/rails_steps.rb
180
+ - features/support/env.rb