ar-model-generator 0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ar-model-generator.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Changeme
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ # Ar::Model::Generator
2
+
3
+ Fast generation of activerecord scaffold models to an existing (mysql?) database.
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ gem 'ar-model-generator'
9
+
10
+ And then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install ar-model-generator
17
+
18
+ ## Usage
19
+
20
+ Run:
21
+ ```bash
22
+ ar-model-generator
23
+ ```
24
+
25
+ The programm will ask you about the connection details for the database as well as the namespace. Afterwards it will generate all model files under PWD/app/models/NAMESPACE.
26
+
27
+ Only tested with mysql2 so far.
28
+
29
+ There are some corrections:
30
+
31
+ * if there is no 'id' column, there will be a ``self.primary_key = 'key'`` line in the model, with the first column which has "id" inside it's name
32
+ * if there is a type column, there will be a ``self.inheritance_column = :sti_type``, otherwise there are conflicts
33
+ * All models inherit from YourNamespace::Base, which is a abstract class to hold the connection information. The benefit is, that you can connect to multple databases, e.g. all Models under YourNamespace will use that connection instead your global development/production etc.
34
+
35
+ Usage of classes:
36
+
37
+ in your initializers:
38
+
39
+ ```ruby
40
+ MyNamespace::Base.establish_connection :some_database_yml_key
41
+
42
+ # Now you can use your models like:
43
+
44
+ MyNamespace::User.where(:all_the_awesomeness => true)
45
+ ```
46
+
47
+ ## warning:
48
+
49
+ * no generation of habtm hm belongs\_to
50
+ * no composited primary keys
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Stefan Wienert"]
5
+ gem.email = ["stefan.wienert@pludoni.de"]
6
+ gem.description = %q{Useful to generate namespaced database models for a legacy database}
7
+ gem.summary = %q{We used this, to generate the basic scaffold of our 50+ tables legacy database. This gem subclasses each of the models in a given namespace which inherits from a namespace::Base class. }
8
+ gem.homepage = "https://github.com/zealot128/ar-model-generator"
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.name = "ar-model-generator"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = "0.3"
16
+ gem.add_dependency "activerecord"
17
+ gem.add_dependency "highline"
18
+ gem.add_dependency "mysql2"
19
+ gem.add_dependency "activerecord-mysql2-adapter"
20
+ gem.add_development_dependency "rake"
21
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require "ar-model-generator"
3
+
4
+
5
+ ArModelGenerator.cli
@@ -0,0 +1,84 @@
1
+ require "highline"
2
+ require "active_record"
3
+ require "active_support/all"
4
+ require "fileutils"
5
+ module ArModelGenerator
6
+ module_function
7
+ def cli
8
+
9
+ connection = {}
10
+ connection[:database] = h.ask "Database Name?"
11
+ connection[:username] = h.ask "Database User?"
12
+ connection[:password] = h.ask "Database Password? (not shown)" do |q|
13
+ q.echo = false
14
+ end
15
+ connection[:adapter] = h.ask "Database Adapter?", lambda{|s| s.to_sym} do |q|
16
+ q.default = :mysql2
17
+ end
18
+ connection[:host] = h.ask "Database Host?" do |q|
19
+ q.default = "localhost"
20
+ end
21
+
22
+ ActiveRecord::Base.establish_connection connection
23
+ ActiveRecord::Base.connection
24
+ $stdout.puts h.color("Connection established", :green)
25
+
26
+ namespace = h.ask "Namespace for models (lowercase singular ok)?"
27
+ create_models(namespace, connection)
28
+
29
+ $stdout.puts "DONE. please look inside the Base.rb and delete/replace
30
+ connection information with something like :#{namespace.downcase} from your database.yml"
31
+ end
32
+
33
+ def h
34
+ @h ||= HighLine.new
35
+ end
36
+
37
+ def create_models(namespace, connection)
38
+ path = "app/models/#{namespace}/"
39
+ namespace_up = namespace.classify
40
+ FileUtils.mkdir_p path
41
+
42
+ # Base-File
43
+ base_path = path + "base.rb"
44
+ File.open(base_path , "w+") do |f|
45
+ f.write "class #{namespace_up}::Base < ActiveRecord::Base\n self.abstract_class = true\n establish_connection(#{connection.inspect}) end\n"
46
+ end
47
+ $stdout.puts h.color("Writing #{base_path}", :green)
48
+
49
+ ActiveRecord::Base.connection.tables.each do |table_name|
50
+ table_up = table_name.classify
51
+ string = "class #{namespace_up}::#{table_up} < #{namespace_up}::Base\n self.table_name = '#{table_name}'"
52
+
53
+ if table_has_type_column?(table_name)
54
+ $stderr.puts "Table #{table_name} has column :type. Adding inheritance_column."
55
+ string += "\n self.inheritance_column = :sti_type"
56
+ end
57
+ string += add_table_id_column(table_name)
58
+ string += "\nend\n"
59
+
60
+ target_path = path + "#{table_up.underscore}.rb"
61
+ $stdout.puts h.color("Writing #{namespace_up}::#{table_up} to #{target_path}", :green)
62
+ File.open(target_path, "w+") { |f|
63
+ f.write string
64
+ }
65
+ end
66
+ end
67
+
68
+ def table_has_type_column?(table)
69
+ ActiveRecord::Base.connection.columns(table).map(&:name).include? "type"
70
+ end
71
+
72
+ def add_table_id_column(table)
73
+ columns = ActiveRecord::Base.connection.columns(table).map(&:name)
74
+ return "" if columns.include? "id"
75
+ if col = columns.find{|i| i[/id$/i]}
76
+ message = "Automatically inferred ID colum of #{table} to #{col}"
77
+ $stderr.puts
78
+ "\n self.primary_key = '#{col}'\n # #{message}\n"
79
+ else
80
+ $stderr.puts "WARNING: Could not determine id-colum of #{table}"
81
+ ""
82
+ end
83
+ end
84
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ar-model-generator
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.3'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stefan Wienert
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: highline
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: mysql2
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: activerecord-mysql2-adapter
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rake
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Useful to generate namespaced database models for a legacy database
95
+ email:
96
+ - stefan.wienert@pludoni.de
97
+ executables:
98
+ - ar-model-generator
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - .gitignore
103
+ - Gemfile
104
+ - LICENSE
105
+ - README.md
106
+ - Rakefile
107
+ - ar-model-generator.gemspec
108
+ - bin/ar-model-generator
109
+ - lib/ar-model-generator.rb
110
+ homepage: https://github.com/zealot128/ar-model-generator
111
+ licenses: []
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ segments:
123
+ - 0
124
+ hash: 2474709819767188495
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ segments:
132
+ - 0
133
+ hash: 2474709819767188495
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 1.8.24
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: We used this, to generate the basic scaffold of our 50+ tables legacy database.
140
+ This gem subclasses each of the models in a given namespace which inherits from
141
+ a namespace::Base class.
142
+ test_files: []