mixonic-seed-fu 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README ADDED
@@ -0,0 +1,66 @@
1
+ Seed Fu
2
+ =======
3
+
4
+ Seed Fu is an attempt to once and for all solve the problem of inserting and
5
+ maintaining seed data in a database. It uses a variety of techniques gathered
6
+ from various places around the web and combines them to create what is
7
+ hopefully the most robust seed data system around.
8
+
9
+ If you have suggestions or come across problems, please report them on
10
+ the Lighthouse project for Seed Fu:
11
+ http://mbleigh.lighthouseapp.com/projects/10223-seed-fu/overview
12
+
13
+ Usage
14
+ =======
15
+
16
+ Seed data is taken from the db/fixtures directory. Simply make descriptive .rb
17
+ files in that directory (they can be named anything, but users.rb for the User
18
+ model seed data makes sense, etc.). These scripts will be run whenever the
19
+ db:seed rake task is called. You can put arbitrary Ruby code in these files,
20
+ but remember that it will be executed every time the rake task is called, so
21
+ it needs to be runnable multiple times on the same database.
22
+
23
+ You can also have environment-specific seed data placed in
24
+ db/fixtures/ENVIRONMENT that will only be loaded if that is the current
25
+ environment.
26
+
27
+ Let's say we want to populate a few default users. In db/fixtures/users.rb we
28
+ write the following code:
29
+
30
+ User.seed(:login, :email) do |s|
31
+ s.login = "bob"
32
+ s.email = "bob@bobson.com"
33
+ s.first_name = "Bob"
34
+ s.last_name = "Bobson"
35
+ end
36
+
37
+ User.seed(:login, :email) do |s|
38
+ s.login = "bob"
39
+ s.email = "bob@stevenson.com"
40
+ s.first_name = "Bob"
41
+ s.last_name = "Stevenson"
42
+ end
43
+
44
+ That's all you have to do! You will now have two users created in the system
45
+ and you can change their first and last names in the users.rb file and it will
46
+ be updated as such.
47
+
48
+ The arguments passed to the <ActiveRecord>.seed method are the constraining
49
+ attributes: these must remain unchanged between db:seed calls to avoid data
50
+ duplication. By default, seed data will constrain to the id like so:
51
+
52
+ Category.seed do |s|
53
+ s.id = 1
54
+ s.name = "Buttons"
55
+ end
56
+
57
+ Category.seed do |s|
58
+ s.id = 2
59
+ s.name = "Bobbins"
60
+ s.parent_id = 1
61
+ end
62
+
63
+ Note that any constraints that are passed in must be present in the subsequent
64
+ seed data setting.
65
+
66
+ Copyright (c) 2008 Michael Bleigh, released under the MIT license
@@ -0,0 +1,6 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run specs.'
6
+ task :default => :spec
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init"
@@ -0,0 +1,87 @@
1
+ module SeedFu
2
+ class Seeder
3
+ def self.plant(model_class, *constraints, &block)
4
+ constraints = [:id] if constraints.empty?
5
+ seed = Seeder.new(model_class)
6
+ insert_only = constraints.last.is_a? TrueClass
7
+ constraints.delete_at(*constraints.length-1) if (constraints.last.is_a? TrueClass or constraints.last.is_a? FalseClass)
8
+ seed.set_constraints(*constraints)
9
+ yield seed
10
+ seed.plant!(insert_only)
11
+ end
12
+
13
+ def initialize(model_class)
14
+ @model_class = model_class
15
+ @constraints = [:id]
16
+ @data = {}
17
+ end
18
+
19
+ def set_constraints(*constraints)
20
+ raise "You must set at least one constraint." if constraints.empty?
21
+ @constraints = []
22
+ constraints.each do |constraint|
23
+ raise "Your constraint `#{constraint}` is not a column in #{@model_class}. Valid columns are `#{@model_class.column_names.join("`, `")}`." unless @model_class.column_names.include?(constraint.to_s)
24
+ @constraints << constraint.to_sym
25
+ end
26
+ end
27
+
28
+ def plant! insert_only=false
29
+ record = get
30
+ return if !record.new_record? and insert_only
31
+ @data.each do |k, v|
32
+ record.send("#{k}=", v)
33
+ end
34
+ record.save!
35
+ puts " - #{@model_class} #{condition_hash.inspect}"
36
+ record
37
+ end
38
+
39
+ def method_missing(method_name, *args) #:nodoc:
40
+ if args.size == 1 and (match = method_name.to_s.match(/(.*)=$/))
41
+ self.class.class_eval "def #{method_name} arg; @data[:#{match[1]}] = arg; end"
42
+ send(method_name, args[0])
43
+ else
44
+ super
45
+ end
46
+ end
47
+
48
+ protected
49
+
50
+ def get
51
+ records = @model_class.find(:all, :conditions => condition_hash)
52
+ raise "Given constraints yielded multiple records." unless records.size < 2
53
+ if records.any?
54
+ return records.first
55
+ else
56
+ return @model_class.new
57
+ end
58
+ end
59
+
60
+ def condition_hash
61
+ @constraints.inject({}) {|a,c| a[c] = @data[c]; a }
62
+ end
63
+ end
64
+ end
65
+
66
+
67
+ class ActiveRecord::Base
68
+ # Creates a single record of seed data for use
69
+ # with the db:seed rake task.
70
+ #
71
+ # === Parameters
72
+ # constraints :: Immutable reference attributes. Defaults to :id
73
+ def self.seed(*constraints, &block)
74
+ SeedFu::Seeder.plant(self, *constraints, &block)
75
+ end
76
+
77
+ def self.seed_many(*constraints)
78
+ seeds = constraints.pop
79
+ seeds.each do |seed_data|
80
+ seed(*constraints) do |s|
81
+ seed_data.each_pair do |k,v|
82
+ s.send "#{k}=", v
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1 @@
1
+ require 'seed-fu'
@@ -0,0 +1,30 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'seed-fu'
3
+ s.version = '1.1.0'
4
+ s.date = '2009-07-29'
5
+
6
+ s.summary = "Allows easier database seeding of tables."
7
+ s.description = "Seed Fu is an attempt to once and for all solve the problem of inserting and maintaining seed data in a database. It uses a variety of techniques gathered from various places around the web and combines them to create what is hopefully the most robust seed data system around."
8
+
9
+ s.authors = ["Michael Bleigh", "Matthew Beale"]
10
+ s.email = "mixonic@synitech.com"
11
+ s.homepage = 'http://github.com/mixonic/seed-fu'
12
+
13
+ s.has_rdoc = true
14
+ s.rdoc_options = ["--main", "README"]
15
+ s.extra_rdoc_files = ["README"]
16
+
17
+ s.add_dependency 'rails', ['>= 2.1']
18
+
19
+ s.files = ["README",
20
+ "Rakefile",
21
+ "init.rb",
22
+ "lib/seed-fu.rb",
23
+ "rails/init.rb",
24
+ "seed-fu.gemspec",
25
+ "spec/schema.rb",
26
+ "spec/seed_fu_spec.rb",
27
+ "spec/spec_helper.rb",
28
+ "tasks/seed_fu_tasks.rake"]
29
+
30
+ end
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Schema.define :version => 0 do
2
+ create_table :seeded_models, :force => true do |t|
3
+ t.column :login, :string
4
+ t.column :first_name, :string
5
+ t.column :last_name, :string
6
+ t.column :title, :string
7
+ end
8
+ end
@@ -0,0 +1,73 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ load(File.dirname(__FILE__) + '/schema.rb')
4
+
5
+ describe SeedFu::Seeder do
6
+ it "should create a model if one doesn't exist" do
7
+ SeededModel.seed(:id) do |s|
8
+ s.id = 1
9
+ s.login = "bob"
10
+ s.first_name = "Bob"
11
+ s.last_name = "Bobson"
12
+ s.title = "Peon"
13
+ end
14
+
15
+ bob = SeededModel.find_by_id(1)
16
+ bob.first_name.should == "Bob"
17
+ bob.last_name.should == "Bobson"
18
+ end
19
+
20
+ it "should be able to handle multiple constraints" do
21
+ SeededModel.seed(:title, :login) do |s|
22
+ s.login = "bob"
23
+ s.title = "Peon"
24
+ s.first_name = "Bob"
25
+ end
26
+
27
+ SeededModel.count.should == 1
28
+
29
+ SeededModel.seed(:title, :login) do |s|
30
+ s.login = "frank"
31
+ s.title = "Peon"
32
+ s.first_name = "Frank"
33
+ end
34
+
35
+ SeededModel.count.should == 2
36
+
37
+ SeededModel.find_by_login("bob").first_name.should == "Bob"
38
+ SeededModel.seed(:title, :login) do |s|
39
+ s.login = "bob"
40
+ s.title = "Peon"
41
+ s.first_name = "Steve"
42
+ end
43
+ SeededModel.find_by_login("bob").first_name.should == "Steve"
44
+ end
45
+
46
+ it "should be able to create models from an array of seed attributes" do
47
+ SeededModel.seed_many(:title, :login, [
48
+ {:login => "bob", :title => "Peon", :first_name => "Steve"},
49
+ {:login => "frank", :title => "Peasant", :first_name => "Francis"},
50
+ {:login => "harry", :title => "Noble", :first_name => "Harry"}
51
+ ])
52
+
53
+ SeededModel.find_by_login("bob").first_name.should == "Steve"
54
+ SeededModel.find_by_login("frank").first_name.should == "Francis"
55
+ SeededModel.find_by_login("harry").first_name.should == "Harry"
56
+ end
57
+
58
+ #it "should raise an error if constraints are not unique" do
59
+ # SeededModel.create(:login => "bob", :first_name => "Bob", :title => "Peon")
60
+ # SeededModel.create(:login => "bob", :first_name => "Robert", :title => "Manager")
61
+ #
62
+ # SeededModel.seed(:login) do |s|
63
+ # s.login = "bob"
64
+ # s.title = "Overlord"
65
+ # end
66
+ #end
67
+
68
+ it "should default to an id constraint"
69
+ it "should update, not create, if constraints are met"
70
+ it "should require that all constraints are defined"
71
+ it "should raise an error if validation fails"
72
+ it "should retain fields that aren't specifically altered in the seeding"
73
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
2
+
3
+ plugin_spec_dir = File.dirname(__FILE__)
4
+ ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
5
+
6
+ class SeededModel < ActiveRecord::Base
7
+ validates_presence_of :title
8
+ end
@@ -0,0 +1,55 @@
1
+ namespace :db do
2
+ desc <<-EOS
3
+ Loads seed data for the current environment. It will look for
4
+ ruby seed files in <RAILS_ROOT>/db/fixtures/ and
5
+ <RAILS_ROOT>/db/fixtures/<RAILS_ENV>/.
6
+
7
+ By default it will load any ruby files found. You can filter the files
8
+ loaded by passing in the SEED environment variable with a comma-delimited
9
+ list of patterns to include. Any files not matching the pattern will
10
+ not be loaded.
11
+
12
+ You can also change the directory where seed files are looked for
13
+ with the FIXTURE_PATH environment variable.
14
+
15
+ Examples:
16
+ # default, to load all seed files for the current environment
17
+ rake db:seed
18
+
19
+ # to load seed files matching orders or customers
20
+ rake db:seed SEED=orders,customers
21
+
22
+ # to load files from RAILS_ROOT/features/fixtures
23
+ rake db:seed FIXTURE_PATH=features/fixtures
24
+ EOS
25
+ task :seed => :environment do
26
+ fixture_path = ENV["FIXTURE_PATH"] ? ENV["FIXTURE_PATH"] : "db/fixtures"
27
+
28
+ global_seed_files = Dir[File.join(RAILS_ROOT, fixture_path, '*.rb')].sort
29
+ env_specific_seed_files = Dir[File.join(RAILS_ROOT, fixture_path, RAILS_ENV, '*.rb')].sort
30
+ potential_seed_files = (global_seed_files + env_specific_seed_files).uniq
31
+
32
+ if ENV["SEED"]
33
+ filter = ENV["SEED"].gsub(/,/, "|")
34
+ potential_seed_files.reject!{ |file| true unless file =~ /#{filter}/ }
35
+ puts "\n == Filtering seed files against regexp: #{filter}"
36
+ end
37
+
38
+ potential_seed_files.each do |file|
39
+ pretty_name = file.sub("#{RAILS_ROOT}/", "")
40
+ puts "\n== Seed from #{pretty_name} " + ("=" * (60 - (17 + File.split(file).last.length)))
41
+
42
+ old_level = ActiveRecord::Base.logger.level
43
+ begin
44
+ ActiveRecord::Base.validation_disabled = true
45
+ ActiveRecord::Base.logger.level = 7
46
+ ActiveRecord::Base.transaction do
47
+ load file
48
+ end
49
+ ensure
50
+ ActiveRecord::Base.validation_disabled = false
51
+ ActiveRecord::Base.logger.level = old_level
52
+ end
53
+ end
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mixonic-seed-fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Bleigh
8
+ - Matthew Beale
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-07-29 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rails
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "2.1"
25
+ version:
26
+ description: Seed Fu is an attempt to once and for all solve the problem of inserting and maintaining seed data in a database. It uses a variety of techniques gathered from various places around the web and combines them to create what is hopefully the most robust seed data system around.
27
+ email: mixonic@synitech.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - README
34
+ files:
35
+ - README
36
+ - Rakefile
37
+ - init.rb
38
+ - lib/seed-fu.rb
39
+ - rails/init.rb
40
+ - seed-fu.gemspec
41
+ - spec/schema.rb
42
+ - spec/seed_fu_spec.rb
43
+ - spec/spec_helper.rb
44
+ - tasks/seed_fu_tasks.rake
45
+ has_rdoc: true
46
+ homepage: http://github.com/mixonic/seed-fu
47
+ licenses:
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --main
51
+ - README
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.5
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: Allows easier database seeding of tables.
73
+ test_files: []
74
+