schema_to_scaffold 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +23 -30
- data/bin/scaffold +20 -6
- data/lib/CommandLineHelpers.rb +24 -0
- data/lib/schema_to_scaffold.rb +20 -13
- data/lib/schema_to_scaffold/path.rb +35 -0
- data/lib/schema_to_scaffold/table.rb +2 -1
- data/lib/schema_to_scaffold/version.rb +1 -1
- metadata +5 -3
data/README.md
CHANGED
@@ -1,41 +1,34 @@
|
|
1
|
-
Schema to Scaffold
|
2
|
-
==================
|
1
|
+
# Schema to Scaffold #
|
3
2
|
|
4
|
-
|
3
|
+
Generate rails scaffold pages based on a rails database schema you already have.
|
5
4
|
|
6
|
-
|
7
|
-
-------
|
5
|
+
Use your schema.rb file from `<rails_app>/db` or generated with `rake db:schema:dump`
|
8
6
|
|
9
|
-
The code is very simple and the usage is not friendly but almost.
|
10
7
|
|
11
|
-
|
12
|
-
2. extract to folder
|
13
|
-
3. cd folder
|
14
|
-
4. if your projects are outside your user folder: edit the main.rb and change the path to point to your schema.rb (generated with the rake db:schema:dump)
|
15
|
-
5. ruby main.rb
|
16
|
-
6. chose the path to your schema.rb
|
17
|
-
7. chose your table
|
18
|
-
8. copy the rails generate scaffold command
|
8
|
+
SchemaToScaffold will generate rails scaffolding scripts by table like this:
|
19
9
|
|
20
|
-
|
21
|
-
* It will give some alerts if your names are not following the rails convention.
|
10
|
+
rails g scaffold users fname:string lname:string bdate:date email:string encrypted_password:string
|
22
11
|
|
23
|
-
read a litle about [rails convention](http://itsignals.cascadia.com.au/?p=7)
|
24
12
|
|
25
|
-
|
26
|
-
------------
|
13
|
+
## Installation ##
|
27
14
|
|
28
|
-
|
15
|
+
We assume you have rubygems-bundler installed, just type:
|
29
16
|
|
30
|
-
|
31
|
-
2. Create a branch (`git checkout -b my_schema_to_scafold`)
|
32
|
-
3. Commit your changes (`git commit -am "Added great stuff"`)
|
33
|
-
4. Push to the branch (`git push origin my_schema_to_scafold`)
|
34
|
-
5. Open a [Pull Request][1]
|
35
|
-
6. That's all!!
|
17
|
+
gem install schema_to_scaffold
|
36
18
|
|
37
|
-
[1]: http://github.com/frenesim/schema_to_scaffold/pulls
|
38
19
|
|
39
|
-
|
40
|
-
|
41
|
-
|
20
|
+
## Usage ##
|
21
|
+
|
22
|
+
Just type:
|
23
|
+
|
24
|
+
scaffold [options]
|
25
|
+
|
26
|
+
[options]
|
27
|
+
-h Displays help.
|
28
|
+
-p <path> It specifies a file ex: -p /path/to/your/schema.rb
|
29
|
+
-c Works only on linux. Will copy the script copied to your clipboard.
|
30
|
+
You will need to have xclip installed(see below).
|
31
|
+
|
32
|
+
### To install xclip ###
|
33
|
+
|
34
|
+
sudo apt-get install xclip
|
data/bin/scaffold
CHANGED
@@ -1,19 +1,28 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
$:.unshift File.expand_path('../lib', File.dirname(__FILE__))
|
3
3
|
require 'schema_to_scaffold'
|
4
|
+
require 'find'
|
4
5
|
|
5
6
|
## Argument conditions
|
6
7
|
|
7
8
|
opts = SchemaToScaffold.parse_arguments(ARGV)
|
8
|
-
ARGV.
|
9
|
+
ARGV.clear
|
9
10
|
|
10
|
-
if opts[:help]
|
11
|
+
if opts[:help]
|
11
12
|
puts SchemaToScaffold.help_msg
|
12
13
|
exit 0
|
13
14
|
end
|
14
15
|
|
16
|
+
## looking for schema.rb in user directory
|
17
|
+
if opts[:path].nil?
|
18
|
+
paths = SchemaToScaffold::Path.new
|
19
|
+
paths.search
|
20
|
+
end
|
21
|
+
|
22
|
+
## Opening file
|
15
23
|
begin
|
16
|
-
|
24
|
+
path = opts[:path] || paths.choose
|
25
|
+
data = File.open(path, 'r') {|f| f.read }
|
17
26
|
rescue
|
18
27
|
puts "Unable to open file '#{path}'"
|
19
28
|
exit 1
|
@@ -23,11 +32,16 @@ end
|
|
23
32
|
|
24
33
|
schema = SchemaToScaffold::Schema.new(data)
|
25
34
|
|
26
|
-
puts "\nLoaded tables:"
|
27
|
-
schema.table_names.each_with_index {|name,i| puts "#{i}. #{name}" }
|
28
|
-
|
29
35
|
begin
|
36
|
+
if schema.table_names.empty? then
|
37
|
+
raise "Could not find tables in '#{path}'"
|
38
|
+
end
|
39
|
+
puts "\nLoaded tables:"
|
40
|
+
schema.table_names.each_with_index {|name,i| puts "#{i}. #{name}" }
|
30
41
|
print "\nSelect a table: "
|
42
|
+
rescue
|
43
|
+
puts "Could not find tables in '#{path}'"
|
44
|
+
exit 1
|
31
45
|
end while schema.table_names[(table_id = gets.to_i)].nil?
|
32
46
|
|
33
47
|
script = SchemaToScaffold.generate_script(schema, table_id)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
|
3
|
+
module CommandLineHelpers
|
4
|
+
extend self
|
5
|
+
|
6
|
+
class NotFollowingConventionsError < StandardError ; end
|
7
|
+
|
8
|
+
class HelpMessages
|
9
|
+
|
10
|
+
def help
|
11
|
+
print "Usage: schemafold [OPTION] PATH
|
12
|
+
Generate a rails scaffold script for the given schema.rb file
|
13
|
+
PATH is the path to the schema.rb file.
|
14
|
+
|
15
|
+
OPTIONS:
|
16
|
+
-x put the rails scaffold script in your clipboard using xclip, type:
|
17
|
+
sudo apt-get install xclip
|
18
|
+
if you don't have it
|
19
|
+
"
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
data/lib/schema_to_scaffold.rb
CHANGED
@@ -2,6 +2,7 @@ require 'schema_to_scaffold/version'
|
|
2
2
|
require 'schema_to_scaffold/schema'
|
3
3
|
require 'schema_to_scaffold/table'
|
4
4
|
require 'schema_to_scaffold/attribute'
|
5
|
+
require 'schema_to_scaffold/path'
|
5
6
|
|
6
7
|
module SchemaToScaffold
|
7
8
|
extend self
|
@@ -9,9 +10,11 @@ module SchemaToScaffold
|
|
9
10
|
## Usage help text to print in all platforms
|
10
11
|
|
11
12
|
GENERIC_HELP = <<-END_OF_HELP
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
|
14
|
+
Usage: scaffold [options]
|
15
|
+
Generate a rails scaffold script for a given schema.rb
|
16
|
+
-h Displays help.
|
17
|
+
-p <path> It specifies a path to a file
|
15
18
|
|
16
19
|
END_OF_HELP
|
17
20
|
|
@@ -19,24 +22,27 @@ END_OF_HELP
|
|
19
22
|
## Windows specific usage help text
|
20
23
|
|
21
24
|
WINDOWS_HELP = <<-WINDOWS_SAMPLE
|
22
|
-
|
23
|
-
|
25
|
+
Examples:
|
26
|
+
scaffold
|
27
|
+
or
|
28
|
+
scaffold -p C:\\Users\\JohnDoe\\Documents\\schema.rb
|
24
29
|
WINDOWS_SAMPLE
|
25
30
|
|
26
31
|
## Linux specific usage help text
|
27
32
|
|
28
33
|
LINUX_HELP = <<-LINUX_SAMPLE
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
+
-c Works only on linux. Will copy the script copied to your clipboard. You will need to have xclip installed(see below).
|
35
|
+
Examples:
|
36
|
+
scaffold
|
37
|
+
or
|
38
|
+
scaffold -x -p ~/work/rails/my_app/db/schema.rb
|
34
39
|
LINUX_SAMPLE
|
35
40
|
|
36
41
|
def help_msg
|
37
42
|
return GENERIC_HELP +
|
38
43
|
case RUBY_PLATFORM
|
39
44
|
when /win/i then WINDOWS_HELP
|
45
|
+
when /mingw/i then WINDOWS_HELP
|
40
46
|
when /linux/i then LINUX_HELP
|
41
47
|
end
|
42
48
|
end
|
@@ -45,10 +51,11 @@ LINUX_SAMPLE
|
|
45
51
|
# Parses ARGV and returns a hash of options.
|
46
52
|
|
47
53
|
def parse_arguments(argv)
|
54
|
+
argv.index("-p") ? path = argv[argv.index("-p")+1] : nil
|
48
55
|
{
|
49
|
-
xclip: argv.delete('-c'),
|
50
|
-
help:
|
51
|
-
path:
|
56
|
+
xclip: argv.delete('-c'), # check for xclip flag
|
57
|
+
help: argv.delete('-h'), # check for help flag
|
58
|
+
path: path # get path to file(s)
|
52
59
|
}
|
53
60
|
end
|
54
61
|
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'find'
|
2
|
+
module SchemaToScaffold
|
3
|
+
|
4
|
+
PATH_NAMES = ["HOME", "HOMEPATH", "USERPROFILE"]
|
5
|
+
|
6
|
+
class Path
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@paths = PATH_NAMES.detect {|h| ENV[h] != nil}
|
10
|
+
end
|
11
|
+
|
12
|
+
##
|
13
|
+
# Will search for schema.rb in the user directory
|
14
|
+
def search
|
15
|
+
puts 'looking for schema.rb in '+ ENV[@paths]
|
16
|
+
@schema_paths = Array.new
|
17
|
+
Find.find(ENV[@paths]) do |path|
|
18
|
+
@schema_paths<<path if path[/schema\.rb$/]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
##
|
23
|
+
# Return the chosen path
|
24
|
+
def choose
|
25
|
+
@schema_paths.each_with_index {|path,i| puts "#{i}. #{path}" }
|
26
|
+
begin
|
27
|
+
print "\nSelect a path to the target schema: "
|
28
|
+
end while (id = gets.chomp.to_i).nil?
|
29
|
+
@schema_paths[id]
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -12,7 +12,8 @@ module SchemaToScaffold
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def self.parse(table_data)
|
15
|
-
name = table_data
|
15
|
+
return unless name = table_data[/table "(.+)"/]
|
16
|
+
name = $1
|
16
17
|
atts = table_data.lines.to_a.select {|line| line =~ /t\.\w+/ }.map {|att| Attribute.parse att }
|
17
18
|
Table.new(name, atts)
|
18
19
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schema_to_scaffold
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-12-05 00:00:00.000000000 Z
|
14
14
|
dependencies: []
|
15
15
|
description: ! ' Command line app which parses a schema.rb file obtained from your
|
16
16
|
rails repo or by running rake:schema:dump
|
@@ -30,8 +30,10 @@ files:
|
|
30
30
|
- README.md
|
31
31
|
- Rakefile
|
32
32
|
- bin/scaffold
|
33
|
+
- lib/CommandLineHelpers.rb
|
33
34
|
- lib/schema_to_scaffold.rb
|
34
35
|
- lib/schema_to_scaffold/attribute.rb
|
36
|
+
- lib/schema_to_scaffold/path.rb
|
35
37
|
- lib/schema_to_scaffold/schema.rb
|
36
38
|
- lib/schema_to_scaffold/table.rb
|
37
39
|
- lib/schema_to_scaffold/version.rb
|
@@ -56,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
58
|
version: '0'
|
57
59
|
requirements: []
|
58
60
|
rubyforge_project:
|
59
|
-
rubygems_version: 1.8.
|
61
|
+
rubygems_version: 1.8.16
|
60
62
|
signing_key:
|
61
63
|
specification_version: 3
|
62
64
|
summary: Generate rails scaffold script from a schema.rb file.
|