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 CHANGED
@@ -1,41 +1,34 @@
1
- Schema to Scaffold
2
- ==================
1
+ # Schema to Scaffold #
3
2
 
4
- After I cross with rake db:schema:dump I felt that it could do a litle more in my case. So here it is.
3
+ Generate rails scaffold pages based on a rails database schema you already have.
5
4
 
6
- Usage
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
- 1. download the zip (or copy paste de code)
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
- * Eight quick steps that will save you a lot of time
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
- Contributing
26
- ------------
13
+ ## Installation ##
27
14
 
28
- Want to contribute? Great!
15
+ We assume you have rubygems-bundler installed, just type:
29
16
 
30
- 1. Fork it.
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
- Colaborate
40
- ------------
41
- if you want to colaborate send me an email please.
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.shift until ARGV.empty?
9
+ ARGV.clear
9
10
 
10
- if opts[:help] or opts[:path].nil?
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
- data = File.open(opts[:path], 'r') {|f| f.read }
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
@@ -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
- Usage: scaffold [OPTION] PATH
13
- Generate a rails scaffold script for the given schema.rb file
14
- PATH is the path to the schema.rb file.
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
- Example: scaffold C:\Users\John\ Doe\Documents\schema.rb
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
- OPTIONS:
30
- -x put the rails scaffold script in your clipboard using xclip which should be installed
31
-
32
- Example: scaffold ~/work/rails/my_app/db/schema.rb
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'), # check for xclip flag
50
- help: argv.delete('-h'), # check for help flag
51
- path: argv[0], # get path to file, must be the last for argv[0] to work
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.match(/table "(.+)"/).captures.first;
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
@@ -1,3 +1,3 @@
1
1
  module SchemaToScaffold
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  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.1.1
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-11-27 00:00:00.000000000 Z
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.24
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.