genesis 1.1.0 → 1.2.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.
@@ -1,3 +1,8 @@
1
+ == 1.2.0 2010-09-19
2
+
3
+ * Make create_or_update_by_* methods able to convert AR objects to IDs if the attribute name includes '_id'.
4
+
5
+
1
6
  == 1.1.0 2010-07-27
2
7
 
3
8
  * Fixed issue with version attribute colliding with ActiveRecord's magic attribute name.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.2.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{genesis}
8
- s.version = "1.1.0"
8
+ s.version = "1.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["C. Jason Harrelson (midas)"]
12
- s.date = %q{2010-07-27}
12
+ s.date = %q{2010-09-19}
13
13
  s.description = %q{A data seeding solution for Ruby on Rails providing seeding facilities far more advanced than the current built in Ruby on Rails solution.}
14
14
  s.email = %q{jason@lookforwardenterprises.com}
15
15
  s.extra_rdoc_files = [
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
27
27
  "genesis.gemspec",
28
28
  "lib/generators/genesis/genesis_generator.rb",
29
29
  "lib/generators/genesis/templates/migration.erb",
30
+ "lib/generators/genesis_contexts/genesis_contexts_generator.rb",
30
31
  "lib/generators/prepare_genesis/prepare_genesis_generator.rb",
31
32
  "lib/generators/prepare_genesis/templates/genesis.rake",
32
33
  "lib/generators/prepare_genesis/templates/genesis_callbacks.rb",
@@ -39,6 +40,8 @@ Gem::Specification.new do |s|
39
40
  "rails_generators/genesis/genesis_generator.rb",
40
41
  "rails_generators/genesis/templates/genesis_override.rake",
41
42
  "rails_generators/genesis/templates/migration.rb",
43
+ "rails_generators/genesis_contexts/USAGE",
44
+ "rails_generators/genesis_contexts/genesis_contexts_generator.rb",
42
45
  "rails_generators/prepare_seeding/USAGE",
43
46
  "rails_generators/prepare_seeding/prepare_seeding_generator.rb",
44
47
  "rails_generators/prepare_seeding/templates/genesis.rake",
@@ -50,7 +53,7 @@ Gem::Specification.new do |s|
50
53
  s.homepage = %q{http://github.com/midas/genesis}
51
54
  s.rdoc_options = ["--charset=UTF-8"]
52
55
  s.require_paths = ["lib"]
53
- s.rubygems_version = %q{1.3.6}
56
+ s.rubygems_version = %q{1.3.7}
54
57
  s.summary = %q{A data seeding solution for Ruby on Rails.}
55
58
  s.test_files = [
56
59
  "spec/generators/genesis_generator_spec.rb",
@@ -62,7 +65,7 @@ Gem::Specification.new do |s|
62
65
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
63
66
  s.specification_version = 3
64
67
 
65
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
68
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
66
69
  s.add_runtime_dependency(%q<activerecord>, [">= 2.0"])
67
70
  s.add_development_dependency(%q<rspec>, [">= 0"])
68
71
  else
@@ -0,0 +1,13 @@
1
+ require 'rails/generators'
2
+
3
+ class GenesisContextsGenerator < Rails::Generators::Base
4
+ argument :contexts, :type => :string, :default => []
5
+
6
+ def self.source_root
7
+ File.join( File.dirname(__FILE__), 'templates' )
8
+ end
9
+
10
+ def install_contexts
11
+ @contexts.each { |context| empty_directory "#{Genesis::SEEDS_ROOT}/contexts/#{context}" }
12
+ end
13
+ end
@@ -2,9 +2,18 @@ namespace :db do
2
2
  desc "Loads seed data for the current environment."
3
3
  task :genesis => :environment do
4
4
  Genesis::Seeder.verify_or_create_version_table
5
- ignores = %w()
6
- seeds = Dir[File.join( RAILS_ROOT, 'db', 'seeds', '*.rb' )] +
7
- Dir[File.join( RAILS_ROOT, 'db', 'seeds', RAILS_ENV, '*.rb') ]
5
+ ignores = %w(genesis_common.rb)
6
+ seeds = Dir[File.join( Rails.root, 'db', 'seeds', '*.rb' )] +
7
+ Dir[File.join( Rails.root, 'db', 'seeds', Rails.env, '*.rb') ]
8
+
9
+ contexts = ENV['CONTEXTS']
10
+ unless contexts.nil? || contexts.empty?
11
+ contexts = expand_contexts if contexts == 'all'
12
+ contexts.split( ',' ).each do |context|
13
+ seeds += Dir[File.join( Rails.root, 'db', 'seeds', 'contexts', context, '*.rb' )]
14
+ end
15
+ end
16
+
8
17
  Genesis::Seeder.run( seeds, ENV['VERSION'] || nil, ignores )
9
18
  end
10
19
 
@@ -14,6 +23,35 @@ namespace :db do
14
23
  Rake::Task['db:genesis'].invoke
15
24
  end
16
25
 
26
+ desc "An alias for the db:genesis task"
27
+ task :seed => :environment do
28
+ Rake::Task['db:genesis'].invoke
29
+ end
30
+
31
+ desc "An alias for the db:regenesis task"
32
+ task :reseed => :environment do
33
+ Rake::Task['db:regenesis'].invoke
34
+ end
35
+
36
+ desc "Removes all data, runs migrations and then seeds the database"
37
+ task :regenesis => :environment do
38
+ ActiveRecord::Base.connection.tables.select { |t| !['schema_migrations', 'schema_seeds', 'versions', 'sessions'].include?( t ) }.each do |table|
39
+ puts "Emptying the #{table} table"
40
+ klass = table.classify.to_s.constantize
41
+ klass.delete_all
42
+ end
43
+
44
+ puts ''
45
+
46
+ Genesis::SchemaSeed.delete_all
47
+ ActiveRecord::Base.connection.execute( 'DELETE FROM `versions`' )
48
+ ActiveRecord::Base.connection.execute( 'DELETE FROM `sessions`' )
49
+
50
+ Rake::Task['db:migrate'].invoke
51
+ Rake::Task['db:test:prepare'].invoke
52
+ Rake::Task['db:genesis'].invoke
53
+ end
54
+
17
55
  namespace :genesis do
18
56
  desc "Returns the current seed version from teh schema_seeds table"
19
57
  task :version => :environment do
@@ -21,3 +59,15 @@ namespace :db do
21
59
  end
22
60
  end
23
61
  end
62
+
63
+ def seeds_root
64
+ File.join( Rails.root, 'db', 'seeds' )
65
+ end
66
+
67
+ def contexts_root
68
+ File.join( seeds_root, 'contexts' )
69
+ end
70
+
71
+ def expand_contexts
72
+ Dir[File.join( contexts_root, '*' )].map { |d| d.split( '/' ).last }.join ','
73
+ end
@@ -6,7 +6,8 @@ require 'genesis/seeder'
6
6
  require 'genesis/schema_seed'
7
7
 
8
8
  module Genesis
9
- VERSION = '1.1.0'
9
+ VERSION = '1.2.0'
10
+ SEEDS_ROOT = 'db/seeds'
10
11
  end
11
12
 
12
13
  ActiveRecord::Base.send :include, Genesis::ActiveRecordExtensions if defined? ActiveRecord::Base
@@ -6,10 +6,11 @@ module Genesis
6
6
  end
7
7
 
8
8
  module ClassMethods
9
- # Use all attributes to try and find a record. If found returns the record. Otherwise creates
9
+ # Use all attributes to try and find a record. If found returns the record. Otherwise creates
10
10
  # and returns the record.
11
11
  #
12
12
  def create_or_update_by_all( attrs={} )
13
+ attrs = extract_ar_object_ids( attrs )
13
14
  conditions = attrs
14
15
  record = find( :first, :conditions => conditions ) || self.new
15
16
  record.attributes = attrs
@@ -17,11 +18,12 @@ module Genesis
17
18
  record
18
19
  end
19
20
 
20
- # Use some attributes (the ones passed in as the find_by hash) to try and find a record. If found
21
- # returns the record. Otherwise creates and returns the record with all of the attributes (including
21
+ # Use some attributes (the ones passed in as the find_by hash) to try and find a record. If found
22
+ # returns the record. Otherwise creates and returns the record with all of the attributes (including
22
23
  # the ones in the find_by hash.)
23
24
  #
24
25
  def create_or_update_by_some( attrs={} )
26
+ attrs = extract_ar_object_ids( attrs )
25
27
  conditions = attrs.delete( :find_by )
26
28
  raise 'You must provide a :find_by hash of attributes to search with, ie. :find_by => {:id => 1}' unless conditions
27
29
  attrs.merge!( conditions )
@@ -36,6 +38,7 @@ module Genesis
36
38
  end
37
39
 
38
40
  def create_or_update_by( field, attrs={} )
41
+ attrs = extract_ar_object_ids( attrs )
39
42
  find_value = attrs[field]
40
43
  conditions = {field => find_value}
41
44
  record = find( :first, :conditions => conditions) || self.new
@@ -56,7 +59,23 @@ module Genesis
56
59
  end
57
60
  end
58
61
 
59
- #alias_method_chain :method_missing, :create_or_update
62
+ private
63
+
64
+ def extract_ar_object_ids( attrs )
65
+ # TODO: Fix this technique. It current flattens any *_attributes => [...] effectively destroying it.
66
+ # attrs = attrs.map do |attr,val|
67
+ # debugger if attr == :citations_attributes
68
+ # (attr.to_s.include?('_id') && val.is_a?( ActiveRecord::Base )) ? [attr,val.id] : [attr,val]
69
+ # end
70
+ #
71
+ # Hash[*attrs.flatten]
72
+
73
+ attrs.each do |attr,val|
74
+ if attr.to_s.include?('_id') && val.is_a?( ActiveRecord::Base )
75
+ attrs[attr] = val.id
76
+ end
77
+ end
78
+ end
60
79
  end
61
80
  end
62
81
  end
@@ -107,7 +107,7 @@ module Genesis
107
107
  end
108
108
 
109
109
  def self.run_seeds
110
- callbacks = File.join( RAILS_ROOT, 'db', 'seeds', 'genesis_callbacks.rb' )
110
+ callbacks = File.join( Rails.root, 'db', 'seeds', 'genesis_callbacks.rb' )
111
111
  if File.exists?( callbacks )
112
112
  load( callbacks )
113
113
  should_run_callbacks = true
@@ -0,0 +1,44 @@
1
+ class GenesisContextsGenerator < Rails::Generator::Base
2
+ attr_accessor :opts
3
+ attr_accessor :environments
4
+
5
+ def initialize( runtime_args, runtime_options={} )
6
+ super
7
+ @opts = {}
8
+ @contexts = []
9
+ parse_args( args )
10
+ end
11
+
12
+ def manifest
13
+ record do |m|
14
+ m.directory Genesis::SEEDS_ROOT
15
+ @contexts.each { |context| m.directory "#{Genesis::SEEDS_ROOT}/contexts/#{context}" }
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def parse_args( arguments )
22
+ arguments.each do |arg|
23
+ arg_parts = arg.split( ':' )
24
+ if arg_parts[0] == 'contexts'
25
+ handle_env_arg( arg_parts[1] )
26
+ else
27
+ opts[arg_parts[0].to_sym] = arg_parts[1]
28
+ end
29
+ end
30
+ end
31
+
32
+ def handle_contexts_arg( val )
33
+ if val.include?( '[' ) && val.include?( ']')
34
+ val.gsub!( /\[/, '' ).gsub!( /\]/, '' )
35
+ val.split( ',' ).each { |v| @contexts << v.trim.gsub( /,/, '' ) }
36
+ elsif val.include?( '[' ) || val.include?( ']' )
37
+ raise 'Error The contexts option must be formatted without any spaces in the array. ie. contexts:[accounts,users]'
38
+ elsif val.include?( ',' )
39
+ raise 'Error The contexts option must be formatted with braces at the beginning and end of the list. ie. contexts:[accounts,users]'
40
+ else
41
+ @contexts << val
42
+ end
43
+ end
44
+ end
@@ -1,7 +1,7 @@
1
1
  class PrepareSeedingGenerator < Rails::Generator::Base
2
2
  attr_accessor :opts
3
3
  attr_accessor :environments
4
-
4
+
5
5
  def initialize( runtime_args, runtime_options = {} )
6
6
  super
7
7
  @opts = {}
@@ -17,9 +17,9 @@ class PrepareSeedingGenerator < Rails::Generator::Base
17
17
  m.file 'genesis_callbacks.rb', 'db/seeds/genesis_callbacks.rb'
18
18
  end
19
19
  end
20
-
20
+
21
21
  private
22
-
22
+
23
23
  def parse_args( arguments )
24
24
  arguments.each do |arg|
25
25
  arg_parts = arg.split( ':' )
@@ -29,10 +29,10 @@ class PrepareSeedingGenerator < Rails::Generator::Base
29
29
  opts[arg_parts[0].to_sym] = arg_parts[1]
30
30
  end
31
31
  end
32
-
32
+
33
33
  validate_env_args
34
34
  end
35
-
35
+
36
36
  def handle_env_arg( val )
37
37
  if val.include?( '[' ) && val.include?( ']')
38
38
  val.gsub!( /\[/, '' ).gsub!( /\]/, '' )
@@ -45,7 +45,7 @@ class PrepareSeedingGenerator < Rails::Generator::Base
45
45
  @environments << val
46
46
  end
47
47
  end
48
-
48
+
49
49
  def validate_env_args
50
50
  @environments += %w(development production) if @environments.empty?
51
51
  end
@@ -2,9 +2,18 @@ namespace :db do
2
2
  desc "Loads seed data for the current environment."
3
3
  task :genesis => :environment do
4
4
  Genesis::Seeder.verify_or_create_version_table
5
- ignores = %w()
6
- seeds = Dir[File.join( RAILS_ROOT, 'db', 'seeds', '*.rb' )] +
7
- Dir[File.join( RAILS_ROOT, 'db', 'seeds', RAILS_ENV, '*.rb') ]
5
+ ignores = %w(genesis_common.rb)
6
+ seeds = Dir[File.join( Rails.root, 'db', 'seeds', '*.rb' )] +
7
+ Dir[File.join( Rails.root, 'db', 'seeds', Rails.env, '*.rb') ]
8
+
9
+ contexts = ENV['CONTEXTS']
10
+ unless contexts.nil? || contexts.empty?
11
+ contexts = expand_contexts if contexts == 'all'
12
+ contexts.split( ',' ).each do |context|
13
+ seeds += Dir[File.join( Rails.root, 'db', 'seeds', 'contexts', context, '*.rb' )]
14
+ end
15
+ end
16
+
8
17
  Genesis::Seeder.run( seeds, ENV['VERSION'] || nil, ignores )
9
18
  end
10
19
 
@@ -14,6 +23,35 @@ namespace :db do
14
23
  Rake::Task['db:genesis'].invoke
15
24
  end
16
25
 
26
+ desc "An alias for the db:genesis task"
27
+ task :seed => :environment do
28
+ Rake::Task['db:genesis'].invoke
29
+ end
30
+
31
+ desc "An alias for the db:regenesis task"
32
+ task :reseed => :environment do
33
+ Rake::Task['db:regenesis'].invoke
34
+ end
35
+
36
+ desc "Removes all data, runs migrations and then seeds the database"
37
+ task :regenesis => :environment do
38
+ ActiveRecord::Base.connection.tables.select { |t| !['schema_migrations', 'schema_seeds', 'versions', 'sessions'].include?( t ) }.each do |table|
39
+ puts "Emptying the #{table} table"
40
+ klass = table.classify.to_s.constantize
41
+ klass.delete_all
42
+ end
43
+
44
+ puts ''
45
+
46
+ Genesis::SchemaSeed.delete_all
47
+ ActiveRecord::Base.connection.execute( 'DELETE FROM `versions`' )
48
+ ActiveRecord::Base.connection.execute( 'DELETE FROM `sessions`' )
49
+
50
+ Rake::Task['db:migrate'].invoke
51
+ Rake::Task['db:test:prepare'].invoke
52
+ Rake::Task['db:genesis'].invoke
53
+ end
54
+
17
55
  namespace :genesis do
18
56
  desc "Returns the current seed version from teh schema_seeds table"
19
57
  task :version => :environment do
@@ -21,3 +59,15 @@ namespace :db do
21
59
  end
22
60
  end
23
61
  end
62
+
63
+ def seeds_root
64
+ File.join( Rails.root, 'db', 'seeds' )
65
+ end
66
+
67
+ def contexts_root
68
+ File.join( seeds_root, 'contexts' )
69
+ end
70
+
71
+ def expand_contexts
72
+ Dir[File.join( contexts_root, '*' )].map { |d| d.split( '/' ).last }.join ','
73
+ end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: genesis
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 31
4
5
  prerelease: false
5
6
  segments:
6
7
  - 1
7
- - 1
8
+ - 2
8
9
  - 0
9
- version: 1.1.0
10
+ version: 1.2.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - C. Jason Harrelson (midas)
@@ -14,16 +15,18 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-07-27 00:00:00 -05:00
18
+ date: 2010-09-19 00:00:00 -05:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: activerecord
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ">="
26
28
  - !ruby/object:Gem::Version
29
+ hash: 3
27
30
  segments:
28
31
  - 2
29
32
  - 0
@@ -34,9 +37,11 @@ dependencies:
34
37
  name: rspec
35
38
  prerelease: false
36
39
  requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
37
41
  requirements:
38
42
  - - ">="
39
43
  - !ruby/object:Gem::Version
44
+ hash: 3
40
45
  segments:
41
46
  - 0
42
47
  version: "0"
@@ -62,6 +67,7 @@ files:
62
67
  - genesis.gemspec
63
68
  - lib/generators/genesis/genesis_generator.rb
64
69
  - lib/generators/genesis/templates/migration.erb
70
+ - lib/generators/genesis_contexts/genesis_contexts_generator.rb
65
71
  - lib/generators/prepare_genesis/prepare_genesis_generator.rb
66
72
  - lib/generators/prepare_genesis/templates/genesis.rake
67
73
  - lib/generators/prepare_genesis/templates/genesis_callbacks.rb
@@ -74,6 +80,8 @@ files:
74
80
  - rails_generators/genesis/genesis_generator.rb
75
81
  - rails_generators/genesis/templates/genesis_override.rake
76
82
  - rails_generators/genesis/templates/migration.rb
83
+ - rails_generators/genesis_contexts/USAGE
84
+ - rails_generators/genesis_contexts/genesis_contexts_generator.rb
77
85
  - rails_generators/prepare_seeding/USAGE
78
86
  - rails_generators/prepare_seeding/prepare_seeding_generator.rb
79
87
  - rails_generators/prepare_seeding/templates/genesis.rake
@@ -91,23 +99,27 @@ rdoc_options:
91
99
  require_paths:
92
100
  - lib
93
101
  required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
94
103
  requirements:
95
104
  - - ">="
96
105
  - !ruby/object:Gem::Version
106
+ hash: 3
97
107
  segments:
98
108
  - 0
99
109
  version: "0"
100
110
  required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
101
112
  requirements:
102
113
  - - ">="
103
114
  - !ruby/object:Gem::Version
115
+ hash: 3
104
116
  segments:
105
117
  - 0
106
118
  version: "0"
107
119
  requirements: []
108
120
 
109
121
  rubyforge_project:
110
- rubygems_version: 1.3.6
122
+ rubygems_version: 1.3.7
111
123
  signing_key:
112
124
  specification_version: 3
113
125
  summary: A data seeding solution for Ruby on Rails.