shah-seed_data 0.5.1
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/VERSION.yml +4 -0
- data/lib/seed_data.rb +59 -0
- data/test/database.yml +7 -0
- data/test/schema.rb +18 -0
- data/test/seed_data_test.rb +65 -0
- data/test/test_helper.rb +19 -0
- metadata +60 -0
data/VERSION.yml
ADDED
data/lib/seed_data.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
module SeedData
|
2
|
+
|
3
|
+
class SeedColumnMissing < Exception
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
def create_or_update_seed(options = {})
|
13
|
+
seed_info = options.delete(:SEED) || { :key => "seed_name" }
|
14
|
+
keys = seed_info[:keys] || seed_info[:key]
|
15
|
+
keys = [keys] if keys.is_a?(String) # in case it's just one column, convert it to an array
|
16
|
+
|
17
|
+
# make sure each seed key value's column is present (it's ok to be Nil, it just must be present)
|
18
|
+
conditions = {}
|
19
|
+
keys.each do |c|
|
20
|
+
unless options.key?(c) || options.key?(c.to_sym)
|
21
|
+
raise SeedColumnMissing, "Seed column #{c} is missing from seed data in #{self.class.name}.create_or_update_seed(#{options.keys.join(', ')})."
|
22
|
+
end
|
23
|
+
conditions[c.to_sym] = (options[c] || options[c.to_sym])
|
24
|
+
end
|
25
|
+
|
26
|
+
updated = true
|
27
|
+
record = find(:first, :conditions => conditions)
|
28
|
+
if ! record
|
29
|
+
record = new
|
30
|
+
updated = false
|
31
|
+
end
|
32
|
+
record.attributes = options
|
33
|
+
|
34
|
+
# run through the passed in attributes and see if there's anything dynamic required for any of them
|
35
|
+
# this is useful only if the seed data was read from an import file like a CSV or something
|
36
|
+
eval_code = ""
|
37
|
+
options.each_pair {|k,v|
|
38
|
+
if(v && v.match(/^EVAL:(.*)/))
|
39
|
+
eval_code += "record.#{k} = #{$1}\n"; # prepare the eval string with some ruby code
|
40
|
+
end
|
41
|
+
}
|
42
|
+
eval(eval_code) if eval_code
|
43
|
+
|
44
|
+
record.save!
|
45
|
+
|
46
|
+
if seed_info.member?(:log)
|
47
|
+
seed_info[:log].push("#{updated ? 'Updated' : 'Created'} #{record.class.name} seed for #{keys.join(', ')} = #{keys.collect{|k| v = record[k.to_sym]; v.nil? ? 'NULL' : v}.join(', ')}.")
|
48
|
+
end
|
49
|
+
|
50
|
+
record
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
class ActiveRecord::Base
|
58
|
+
include SeedData
|
59
|
+
end
|
data/test/database.yml
ADDED
data/test/schema.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 1) do
|
2
|
+
|
3
|
+
create_table :first_seed_data_test_table, :force => true do |t|
|
4
|
+
t.string :seed_name, :null => false
|
5
|
+
t.string :attr_1
|
6
|
+
t.integer :attr_2
|
7
|
+
t.boolean :attr_3
|
8
|
+
end
|
9
|
+
|
10
|
+
create_table :second_seed_data_test_table, :force => true do |t|
|
11
|
+
t.string :seed_name, :null => false
|
12
|
+
t.string :seed_scope
|
13
|
+
t.string :attr_1
|
14
|
+
t.integer :attr_2
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class FirstSeedDataTestTable < ActiveRecord::Base
|
4
|
+
end
|
5
|
+
|
6
|
+
class SecondSeedDataTestTable < ActiveRecord::Base
|
7
|
+
end
|
8
|
+
|
9
|
+
class SeedDataTest < Test::Unit::TestCase
|
10
|
+
context "ActiveRecord" do
|
11
|
+
should "respond to create_or_update_seed" do
|
12
|
+
assert FirstSeedDataTestTable.respond_to?('create_or_update_seed')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "FirstSeedDataTestTable" do
|
17
|
+
should "create the first seed row" do
|
18
|
+
log = []
|
19
|
+
record = FirstSeedDataTestTable.create_or_update_seed(:SEED => { :key => "seed_name", :log => log }, :seed_name => "ROW_1", :attr_1 => "ATTR_1 Test Value 01")
|
20
|
+
assert_equal "Created FirstSeedDataTestTable seed for seed_name = ROW_1.", log[0]
|
21
|
+
assert_equal "ATTR_1 Test Value 01", record.attr_1
|
22
|
+
end
|
23
|
+
|
24
|
+
should "update the first seed row" do
|
25
|
+
log = []
|
26
|
+
record = FirstSeedDataTestTable.create_or_update_seed(:SEED => { :key => "seed_name", :log => log }, :seed_name => "ROW_1", :attr_1 => "ATTR_1 Test Value 02", :attr_2 => "EVAL:1+1")
|
27
|
+
assert_equal "Updated FirstSeedDataTestTable seed for seed_name = ROW_1.", log[0]
|
28
|
+
assert_equal "ATTR_1 Test Value 02", record.attr_1
|
29
|
+
assert_equal 2, record.attr_2
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "SecondSeedDataTestTable" do
|
34
|
+
should "create the first seed row with no scope" do
|
35
|
+
log = []
|
36
|
+
record = SecondSeedDataTestTable.create_or_update_seed(:SEED => { :keys => ["seed_name", "seed_scope"], :log => log }, :seed_name => "ROW_1", :seed_scope => nil, :attr_1 => "ATTR_1 Test Value 01")
|
37
|
+
assert_equal "Created SecondSeedDataTestTable seed for seed_name, seed_scope = ROW_1, NULL.", log[0]
|
38
|
+
assert_equal "ATTR_1 Test Value 01", record.attr_1
|
39
|
+
end
|
40
|
+
|
41
|
+
should "update the first seed row with no scope" do
|
42
|
+
log = []
|
43
|
+
record = SecondSeedDataTestTable.create_or_update_seed(:SEED => { :keys => ["seed_name", "seed_scope"], :log => log }, :seed_name => "ROW_1", :seed_scope => nil, :attr_1 => "ATTR_1 Test Value 02", :attr_2 => "EVAL:5*5")
|
44
|
+
assert_equal "Updated SecondSeedDataTestTable seed for seed_name, seed_scope = ROW_1, NULL.", log[0]
|
45
|
+
assert_equal "ATTR_1 Test Value 02", record.attr_1
|
46
|
+
assert_equal 25, record.attr_2
|
47
|
+
end
|
48
|
+
|
49
|
+
should "create the first seed row with a scope" do
|
50
|
+
log = []
|
51
|
+
record = SecondSeedDataTestTable.create_or_update_seed(:SEED => { :keys => ["seed_name", "seed_scope"], :log => log }, :seed_name => "ROW_1", :seed_scope => 'SCOPE_1', :attr_1 => "ATTR_1 Test Value 01")
|
52
|
+
assert_equal "Created SecondSeedDataTestTable seed for seed_name, seed_scope = ROW_1, SCOPE_1.", log[0]
|
53
|
+
assert_equal "ATTR_1 Test Value 01", record.attr_1
|
54
|
+
end
|
55
|
+
|
56
|
+
should "update the first seed row with a scope" do
|
57
|
+
log = []
|
58
|
+
record = SecondSeedDataTestTable.create_or_update_seed(:SEED => { :keys => ["seed_name", "seed_scope"], :log => log }, :seed_name => "ROW_1", :seed_scope => 'SCOPE_1', :attr_1 => "ATTR_1 Test Value 02", :attr_2 => "EVAL:2+3")
|
59
|
+
assert_equal "Updated SecondSeedDataTestTable seed for seed_name, seed_scope = ROW_1, SCOPE_1.", log[0]
|
60
|
+
assert_equal "ATTR_1 Test Value 02", record.attr_1
|
61
|
+
assert_equal 5, record.attr_2
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
+
RAILS_ROOT = File.dirname(__FILE__)
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'test/unit'
|
6
|
+
require 'active_record'
|
7
|
+
require 'shoulda'
|
8
|
+
require 'mocha'
|
9
|
+
require "#{File.dirname(__FILE__)}/../init"
|
10
|
+
|
11
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
12
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
13
|
+
ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite3'])
|
14
|
+
ActiveRecord::Base.pluralize_table_names = false
|
15
|
+
|
16
|
+
load(File.dirname(__FILE__) + "/schema.rb") if File.exist?(File.dirname(__FILE__) + "/schema.rb")
|
17
|
+
|
18
|
+
class Test::Unit::TestCase
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shah-seed_data
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shahid N. Shah
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-18 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Create seed data rake tasks and use the new Model.create_or_update_seed() method to manage default data.
|
17
|
+
email: shahid@shah.org
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- VERSION.yml
|
26
|
+
- lib/seed_data.rb
|
27
|
+
- test/seed_data_test.rb
|
28
|
+
- test/test_helper.rb
|
29
|
+
- test/database.yml
|
30
|
+
- test/schema.rb
|
31
|
+
- test/debug.log
|
32
|
+
- test/seed_data.sqlite3.db
|
33
|
+
has_rdoc: false
|
34
|
+
homepage: http://github.com/shah/seed_data
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.2.0
|
56
|
+
signing_key:
|
57
|
+
specification_version: 2
|
58
|
+
summary: Adds methods to ActiveRecord to manage seed data that should be loaded into the database.
|
59
|
+
test_files: []
|
60
|
+
|