migrant 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,6 +1,7 @@
1
1
  source :gemcutter
2
2
 
3
3
  gem "rails", ">= 3.0.0"
4
+ gem "faker"
4
5
 
5
6
  group :development do
6
7
  gem "thoughtbot-shoulda"
data/Gemfile.lock CHANGED
@@ -2,12 +2,12 @@ GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
4
  abstract (1.0.0)
5
- actionmailer (3.0.4)
6
- actionpack (= 3.0.4)
5
+ actionmailer (3.0.5)
6
+ actionpack (= 3.0.5)
7
7
  mail (~> 2.2.15)
8
- actionpack (3.0.4)
9
- activemodel (= 3.0.4)
10
- activesupport (= 3.0.4)
8
+ actionpack (3.0.5)
9
+ activemodel (= 3.0.5)
10
+ activesupport (= 3.0.5)
11
11
  builder (~> 2.1.2)
12
12
  erubis (~> 2.6.6)
13
13
  i18n (~> 0.4)
@@ -15,24 +15,26 @@ GEM
15
15
  rack-mount (~> 0.6.13)
16
16
  rack-test (~> 0.5.7)
17
17
  tzinfo (~> 0.3.23)
18
- activemodel (3.0.4)
19
- activesupport (= 3.0.4)
18
+ activemodel (3.0.5)
19
+ activesupport (= 3.0.5)
20
20
  builder (~> 2.1.2)
21
21
  i18n (~> 0.4)
22
- activerecord (3.0.4)
23
- activemodel (= 3.0.4)
24
- activesupport (= 3.0.4)
22
+ activerecord (3.0.5)
23
+ activemodel (= 3.0.5)
24
+ activesupport (= 3.0.5)
25
25
  arel (~> 2.0.2)
26
26
  tzinfo (~> 0.3.23)
27
- activeresource (3.0.4)
28
- activemodel (= 3.0.4)
29
- activesupport (= 3.0.4)
30
- activesupport (3.0.4)
27
+ activeresource (3.0.5)
28
+ activemodel (= 3.0.5)
29
+ activesupport (= 3.0.5)
30
+ activesupport (3.0.5)
31
31
  ansi (1.2.2)
32
32
  arel (2.0.9)
33
33
  builder (2.1.2)
34
34
  erubis (2.6.6)
35
35
  abstract (>= 1.0.0)
36
+ faker (0.9.5)
37
+ i18n (~> 0.4)
36
38
  git (1.2.5)
37
39
  i18n (0.5.0)
38
40
  jeweler (1.5.2)
@@ -46,22 +48,22 @@ GEM
46
48
  treetop (~> 1.4.8)
47
49
  mime-types (1.16)
48
50
  polyglot (0.3.1)
49
- rack (1.2.1)
50
- rack-mount (0.6.13)
51
+ rack (1.2.2)
52
+ rack-mount (0.6.14)
51
53
  rack (>= 1.0.0)
52
54
  rack-test (0.5.7)
53
55
  rack (>= 1.0)
54
- rails (3.0.4)
55
- actionmailer (= 3.0.4)
56
- actionpack (= 3.0.4)
57
- activerecord (= 3.0.4)
58
- activeresource (= 3.0.4)
59
- activesupport (= 3.0.4)
56
+ rails (3.0.5)
57
+ actionmailer (= 3.0.5)
58
+ actionpack (= 3.0.5)
59
+ activerecord (= 3.0.5)
60
+ activeresource (= 3.0.5)
61
+ activesupport (= 3.0.5)
60
62
  bundler (~> 1.0)
61
- railties (= 3.0.4)
62
- railties (3.0.4)
63
- actionpack (= 3.0.4)
64
- activesupport (= 3.0.4)
63
+ railties (= 3.0.5)
64
+ railties (3.0.5)
65
+ actionpack (= 3.0.5)
66
+ activesupport (= 3.0.5)
65
67
  rake (>= 0.8.7)
66
68
  thor (~> 0.14.4)
67
69
  rake (0.8.7)
@@ -75,13 +77,14 @@ GEM
75
77
  polyglot (>= 0.3.1)
76
78
  turn (0.8.1)
77
79
  ansi (>= 1.2.2)
78
- tzinfo (0.3.24)
80
+ tzinfo (0.3.25)
79
81
 
80
82
  PLATFORMS
81
83
  ruby
82
84
 
83
85
  DEPENDENCIES
84
86
  ansi
87
+ faker
85
88
  jeweler
86
89
  rails (>= 3.0.0)
87
90
  simplecov
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.1.0
data/lib/datatype/base.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'faker'
2
+
1
3
  module DataType
2
4
  class DangerousMigration < Exception; end;
3
5
 
@@ -9,8 +11,10 @@ module DataType
9
11
  def initialize(options={})
10
12
  @options = options
11
13
  @value = options.delete(:value)
14
+ @example = options.delete(:example)
12
15
  @field = options.delete(:field)
13
16
  @aliases = options.delete(:was) || Array.new
17
+ options[:type] = options.delete(:as) if options[:as] # Nice little DSL alias for 'type'
14
18
  end
15
19
 
16
20
  # Default is 'ye good ol varchar(255)
@@ -22,8 +26,17 @@ module DataType
22
26
  @value || self.class.default_mock
23
27
  end
24
28
 
29
+ # Default mock should be overridden in derived classes
25
30
  def self.default_mock
26
- "Some string"
31
+ short_text_mock
32
+ end
33
+
34
+ def self.long_text_mock
35
+ (1..3).to_a.collect { Faker::Lorem.paragraph }.join("\n")
36
+ end
37
+
38
+ def self.short_text_mock
39
+ Faker::Lorem.sentence
27
40
  end
28
41
 
29
42
  # Decides if and how a column will be changed
@@ -3,5 +3,9 @@ module DataType
3
3
  def column
4
4
  {:type => :integer}
5
5
  end
6
+
7
+ def self.default_mock
8
+ rand(999999).to_i
9
+ end
6
10
  end
7
11
  end
@@ -3,6 +3,10 @@ module DataType
3
3
  def column
4
4
  {:type => :integer}
5
5
  end
6
+
7
+ def self.default_mock
8
+ nil # Will get overridden later by ModelExtensions
9
+ end
6
10
  end
7
11
  end
8
12
 
@@ -5,9 +5,5 @@ module DataType
5
5
  definition[:limit] = @value.max.to_s.length if @value.respond_to?(:max)
6
6
  definition
7
7
  end
8
-
9
- def self.default_mock
10
- 0..100
11
- end
12
8
  end
13
9
  end
@@ -14,7 +14,7 @@ module DataType
14
14
  end
15
15
 
16
16
  def mock
17
- @value || ((self.column[:type] == :text)? %W{Several lines of long text.}.join("\n") : "Some string")
17
+ @value || ((self.column[:type] == :text)? self.class.long_text_mock : self.class.default_mock )
18
18
  end
19
19
  end
20
20
  end
@@ -4,6 +4,16 @@ module DataType
4
4
  # Just construct whatever the user wants
5
5
  {:type => @value || :string }.merge(@options)
6
6
  end
7
+
8
+ def mock
9
+ case @value || :string
10
+ when :text then self.class.long_text_mock
11
+ when :string then self.class.short_text_mock
12
+ when :integer then Fixnum.default_mock
13
+ when :decimal, :float then Float.default_mock
14
+ when :datetime, :date then Date.default_mock
15
+ end
16
+ end
7
17
  end
8
18
  end
9
19
 
@@ -30,7 +30,7 @@ module Migrant
30
30
  end
31
31
 
32
32
  def mock(attributes={}, recursive=true)
33
- attribs = @schema.columns.reject { |column| column.is_a?(DataType::ForeignKey)}.collect { |name, data_type| [name, data_type.mock] }.flatten
33
+ attribs = @schema.columns.collect { |name, data_type| [name, data_type.mock] }.flatten
34
34
 
35
35
  # Only recurse to one level, otherwise things get way too complicated
36
36
  if recursive
@@ -28,7 +28,8 @@ module Migrant
28
28
 
29
29
  def add_associations(associations)
30
30
  associations.each do |association|
31
- field = association.association_foreign_key.to_sym
31
+ # Rails 3.1 changes primary_key_name to foreign_key (correct behaviour), so this is essentially backwards compatibility for Rails 3.0
32
+ field = (association.respond_to?(:foreign_key))? association.foreign_key.to_sym : association.primary_key_name.to_sym
32
33
  case association.macro
33
34
  when :belongs_to
34
35
  if association.options[:polymorphic]
data/migrant.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{migrant}
8
- s.version = "1.0.2"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Pascal Houliston"]
12
- s.date = %q{2011-03-01}
12
+ s.date = %q{2011-03-28}
13
13
  s.description = %q{Easier schema management for Rails that compliments your domain model.}
14
14
  s.email = %q{101pascal@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -144,6 +144,7 @@ Gem::Specification.new do |s|
144
144
 
145
145
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
146
146
  s.add_runtime_dependency(%q<rails>, [">= 3.0.0"])
147
+ s.add_runtime_dependency(%q<faker>, [">= 0"])
147
148
  s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
148
149
  s.add_development_dependency(%q<ansi>, [">= 0"])
149
150
  s.add_development_dependency(%q<jeweler>, [">= 0"])
@@ -152,6 +153,7 @@ Gem::Specification.new do |s|
152
153
  s.add_development_dependency(%q<simplecov>, [">= 0"])
153
154
  else
154
155
  s.add_dependency(%q<rails>, [">= 3.0.0"])
156
+ s.add_dependency(%q<faker>, [">= 0"])
155
157
  s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
156
158
  s.add_dependency(%q<ansi>, [">= 0"])
157
159
  s.add_dependency(%q<jeweler>, [">= 0"])
@@ -161,6 +163,7 @@ Gem::Specification.new do |s|
161
163
  end
162
164
  else
163
165
  s.add_dependency(%q<rails>, [">= 3.0.0"])
166
+ s.add_dependency(%q<faker>, [">= 0"])
164
167
  s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
165
168
  s.add_dependency(%q<ansi>, [">= 0"])
166
169
  s.add_dependency(%q<jeweler>, [">= 0"])
@@ -10,7 +10,7 @@ class Business < ActiveRecord::Base
10
10
  website "http://www.google.co.za/", :was => [:site, :homepage]
11
11
  address ["11 Test Drive", "Gardens", "Cape Town" ,"South Africa"].join("\n")
12
12
  summary :string
13
- description :text
13
+ description "Founded in 1898", :as => :text
14
14
  landline :string
15
15
  mobile :string
16
16
  operating_days 0..6
@@ -39,7 +39,6 @@ class TestDataSchema < Test::Unit::TestCase
39
39
  end
40
40
 
41
41
  should "generate a text column when given a long paragraph" do
42
- assert_schema(Business, :description, :type => :text)
43
42
  assert_schema(Business, :address, :type => :text)
44
43
  end
45
44
 
@@ -69,6 +68,10 @@ class TestDataSchema < Test::Unit::TestCase
69
68
  assert_schema(User, :email, :type => :string)
70
69
  end
71
70
 
71
+ should "generate the column type specified by the :as key irrespective of the data type" do
72
+
73
+ end
74
+
72
75
  should "generate indexes for all foreign keys automatically" do
73
76
  assert_contains(Business.schema.indexes, :user_id, 'Missing index on belongs_to')
74
77
  assert_contains(Business.schema.indexes, [:owner_type, :owner_id], 'Missing index on polymorphic belongs_to')
@@ -101,25 +101,33 @@ class TestMigrationGenerator < Test::Unit::TestCase
101
101
  assert_equal(Dir.glob(File.join(File.dirname(__FILE__), 'rails_app', 'db' ,'migrate', '*.rb')).select { |migration_file| migration_file.include?('new_field_i_made_up') }.length,
102
102
  1,
103
103
  "Migration should have been generated (without a duplicate)")
104
+ rake_migrate
104
105
  end
105
106
 
106
107
  should "recursively generate mocks for every model" do
107
108
  BusinessCategory.structure do
108
- test_currency_mockup DataType::Currency
109
- test_date_mockup DataType::Date
110
- test_float_mockup DataType::Float
111
- test_range_mockup DataType::Range
112
-
109
+ test_mockup_of_text :text
110
+ test_mockup_of_string :string
111
+ test_mockup_of_integer :integer
112
+ test_mockup_of_float :float
113
+ test_mockup_of_datetime :datetime
114
+ test_mockup_of_currency DataType::Currency
113
115
  end
114
- BusinessCategory.belongs_to(:nonexistant_class, :polymorphic => true)
116
+
117
+ BusinessCategory.belongs_to(:notaclass, :polymorphic => true)
115
118
  assert_equal true, Migrant::MigrationGenerator.new.run, "Migration Generator reported an error"
116
119
  rake_migrate
117
120
  BusinessCategory.reset_column_information
118
121
  mock = BusinessCategory.mock
119
122
  assert_not_nil(mock)
120
- # TODO: Spice this up a bit .. it covers everything, but doesn't test output
123
+ assert(mock.test_mockup_of_text.is_a?(String))
124
+ assert(mock.test_mockup_of_string.is_a?(String))
125
+ assert(mock.test_mockup_of_integer.is_a?(Fixnum))
126
+ assert(mock.test_mockup_of_float.is_a?(Float))
127
+ assert(mock.test_mockup_of_currency.is_a?(BigDecimal))
128
+ assert(mock.test_mockup_of_datetime.is_a?(Time))
129
+ assert(DataType::Base.default_mock.is_a?(String))
121
130
  end
122
-
123
131
  end
124
132
  end
125
133
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: migrant
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.2
5
+ version: 1.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Pascal Houliston
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-01 00:00:00 +00:00
13
+ date: 2011-03-28 00:00:00 +00:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -25,18 +25,18 @@ dependencies:
25
25
  prerelease: false
26
26
  version_requirements: *id001
27
27
  - !ruby/object:Gem::Dependency
28
- name: thoughtbot-shoulda
28
+ name: faker
29
29
  requirement: &id002 !ruby/object:Gem::Requirement
30
30
  none: false
31
31
  requirements:
32
32
  - - ">="
33
33
  - !ruby/object:Gem::Version
34
34
  version: "0"
35
- type: :development
35
+ type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: *id002
38
38
  - !ruby/object:Gem::Dependency
39
- name: ansi
39
+ name: thoughtbot-shoulda
40
40
  requirement: &id003 !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
@@ -47,7 +47,7 @@ dependencies:
47
47
  prerelease: false
48
48
  version_requirements: *id003
49
49
  - !ruby/object:Gem::Dependency
50
- name: jeweler
50
+ name: ansi
51
51
  requirement: &id004 !ruby/object:Gem::Requirement
52
52
  none: false
53
53
  requirements:
@@ -58,7 +58,7 @@ dependencies:
58
58
  prerelease: false
59
59
  version_requirements: *id004
60
60
  - !ruby/object:Gem::Dependency
61
- name: turn
61
+ name: jeweler
62
62
  requirement: &id005 !ruby/object:Gem::Requirement
63
63
  none: false
64
64
  requirements:
@@ -69,7 +69,7 @@ dependencies:
69
69
  prerelease: false
70
70
  version_requirements: *id005
71
71
  - !ruby/object:Gem::Dependency
72
- name: sqlite3
72
+ name: turn
73
73
  requirement: &id006 !ruby/object:Gem::Requirement
74
74
  none: false
75
75
  requirements:
@@ -80,7 +80,7 @@ dependencies:
80
80
  prerelease: false
81
81
  version_requirements: *id006
82
82
  - !ruby/object:Gem::Dependency
83
- name: simplecov
83
+ name: sqlite3
84
84
  requirement: &id007 !ruby/object:Gem::Requirement
85
85
  none: false
86
86
  requirements:
@@ -90,6 +90,17 @@ dependencies:
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: *id007
93
+ - !ruby/object:Gem::Dependency
94
+ name: simplecov
95
+ requirement: &id008 !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ type: :development
102
+ prerelease: false
103
+ version_requirements: *id008
93
104
  description: Easier schema management for Rails that compliments your domain model.
94
105
  email: 101pascal@gmail.com
95
106
  executables: []
@@ -192,7 +203,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
192
203
  requirements:
193
204
  - - ">="
194
205
  - !ruby/object:Gem::Version
195
- hash: -270062193
206
+ hash: 480929263
196
207
  segments:
197
208
  - 0
198
209
  version: "0"