model_mill 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +3 -0
- data/VERSION +1 -1
- data/lib/generators/model_mill/models/models_generator.rb +17 -0
- data/lib/generators/model_mill/models/templates/model.rb +2 -0
- data/model_mill.gemspec +5 -2
- data/spec/dummy/app/models/schema_migration.rb +6 -0
- data/spec/dummy/app/models/user.rb +6 -0
- data/spec/dummy/app/models/widget.rb +6 -0
- data/spec/dummy/db/migrate/20110125024703_create_some_tables.rb +1 -0
- data/spec/dummy/db/schema.rb +1 -0
- data/spec/generators/model_generator_spec.rb +10 -2
- metadata +10 -4
data/CHANGELOG.md
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
@@ -15,6 +15,8 @@ module ModelMill
|
|
15
15
|
@model_name = model_name_from_table_name table_name
|
16
16
|
@model_file_name = model_file_name_from_table_name table_name
|
17
17
|
@columns = connection.columns(table_name)
|
18
|
+
@indexes = connection.indexes(table_name)
|
19
|
+
@associations = detected_associations(table_name, @columns)
|
18
20
|
template 'model.rb', "app/models/#{options[:namespace] ? options[:namespace]+'/' : ''}#{@model_file_name}.rb"
|
19
21
|
end
|
20
22
|
puts %Q(config.autoload_paths += %W(\#{config.root}/app/models/#{options[:namespace].downcase})) if options[:namespace]
|
@@ -44,6 +46,21 @@ module ModelMill
|
|
44
46
|
def model_file_name_from_table_name tn
|
45
47
|
tn.singularize.underscore
|
46
48
|
end
|
49
|
+
|
50
|
+
def detected_associations(table_name, columns)
|
51
|
+
{:belongs_to => [], :has_many => []}.tap do |associations|
|
52
|
+
columns.each do |col|
|
53
|
+
associations[:belongs_to].push({:name => $1}) if col.name =~ /(.*)\_id$/
|
54
|
+
end
|
55
|
+
|
56
|
+
discovered_tables.each do |tn|
|
57
|
+
next if tn == table_name
|
58
|
+
connection.columns(tn).each do |col|
|
59
|
+
associations[:has_many].push({:name => tn}) if col.name =~ /#{table_name.singularize}\_id$/
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
47
64
|
end
|
48
65
|
end
|
49
66
|
end
|
@@ -1,4 +1,6 @@
|
|
1
1
|
class <%= options[:namespace] ? options[:namespace].camelize + '::' : '' %><%= @model_name %> < <%= options[:namespace] ? options[:namespace].camelize : 'ActiveRecord' %>::Base
|
2
2
|
attr_accessible <%= @columns.map(&:name).sort.map{|c| ":#{c}"}.join(', ') %>
|
3
|
+
|
4
|
+
<% @associations.each do |cardinality, associations| -%><% associations.each do |a| -%><%= "#{cardinality} :#{a[:name]}" %><% end %><% end %>
|
3
5
|
end
|
4
6
|
|
data/model_mill.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{model_mill}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Joe Martinez"]
|
12
|
-
s.date = %q{2011-01-
|
12
|
+
s.date = %q{2011-01-26}
|
13
13
|
s.description = %q{Generates bare model files that can be used for a migration of legacy data to new models or to get a jump on development for an existing database}
|
14
14
|
s.email = %q{joe@joemartinez.name}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -77,6 +77,9 @@ Gem::Specification.new do |s|
|
|
77
77
|
s.test_files = [
|
78
78
|
"spec/dummy/app/controllers/application_controller.rb",
|
79
79
|
"spec/dummy/app/helpers/application_helper.rb",
|
80
|
+
"spec/dummy/app/models/schema_migration.rb",
|
81
|
+
"spec/dummy/app/models/user.rb",
|
82
|
+
"spec/dummy/app/models/widget.rb",
|
80
83
|
"spec/dummy/config/application.rb",
|
81
84
|
"spec/dummy/config/boot.rb",
|
82
85
|
"spec/dummy/config/environment.rb",
|
data/spec/dummy/db/schema.rb
CHANGED
@@ -67,13 +67,13 @@ describe 'models_generator' do
|
|
67
67
|
@tables = %w(users widgets)
|
68
68
|
@table_columns = {
|
69
69
|
'users' => %w(created_at email first_name id last_name updated_at),
|
70
|
-
'widgets' => %w(color created_at id quantity updated_at)
|
70
|
+
'widgets' => %w(color created_at id quantity updated_at user_id)
|
71
71
|
}
|
72
72
|
end
|
73
73
|
|
74
74
|
after :each do
|
75
75
|
@tables.each do |tn|
|
76
|
-
FileUtils.rm_r File.expand_path("app/models/#{tn.singularize.underscore}.rb", ::Rails.root)
|
76
|
+
#FileUtils.rm_r File.expand_path("app/models/#{tn.singularize.underscore}.rb", ::Rails.root)
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
@@ -96,6 +96,14 @@ describe 'models_generator' do
|
|
96
96
|
end
|
97
97
|
end
|
98
98
|
end
|
99
|
+
|
100
|
+
it 'adds associations' do
|
101
|
+
GeneratorSpec.with_generator do |g|
|
102
|
+
g.run_generator
|
103
|
+
file_contents("app/models/widget.rb").should match(/belongs_to :user/)
|
104
|
+
file_contents("app/models/user.rb").should match(/has_many :widgets/)
|
105
|
+
end
|
106
|
+
end
|
99
107
|
end
|
100
108
|
end
|
101
109
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: model_mill
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 4
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Joe Martinez
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-01-
|
18
|
+
date: 2011-01-26 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -216,6 +216,9 @@ files:
|
|
216
216
|
- spec/integration/navigation_spec.rb
|
217
217
|
- spec/model_mill_spec.rb
|
218
218
|
- spec/spec_helper.rb
|
219
|
+
- spec/dummy/app/models/schema_migration.rb
|
220
|
+
- spec/dummy/app/models/user.rb
|
221
|
+
- spec/dummy/app/models/widget.rb
|
219
222
|
- spec/matchers/all.rb
|
220
223
|
- spec/matchers/generate_file.rb
|
221
224
|
- spec/matchers/generate_migration.rb
|
@@ -258,6 +261,9 @@ summary: Generates ActiveRecord models from existing database tables.
|
|
258
261
|
test_files:
|
259
262
|
- spec/dummy/app/controllers/application_controller.rb
|
260
263
|
- spec/dummy/app/helpers/application_helper.rb
|
264
|
+
- spec/dummy/app/models/schema_migration.rb
|
265
|
+
- spec/dummy/app/models/user.rb
|
266
|
+
- spec/dummy/app/models/widget.rb
|
261
267
|
- spec/dummy/config/application.rb
|
262
268
|
- spec/dummy/config/boot.rb
|
263
269
|
- spec/dummy/config/environment.rb
|