schema_to_scaffold 0.7.1 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 88d13a7ac7e248b01bf4f47fe37656accfcf0a49
4
- data.tar.gz: 992dcc7e74ac24461f3e863424d5286a7eed3e97
2
+ SHA256:
3
+ metadata.gz: 229ebc7daea413c98b93c50601c9849af0e827f2fbb2d338b09872b54470a46d
4
+ data.tar.gz: 0c514edaa979279a1a2a8f764c198618c07040662f830c1c4fb133ec7b0ce23e
5
5
  SHA512:
6
- metadata.gz: 107e2ad6462e047b9961f8df3f326c320f3e356498d2e1594f1ad9f68f5c5d8f129cd78f850a9f96540685e3fd6b1306a27409f2eee970848b4b5a744d9cee17
7
- data.tar.gz: 5b8439e9a1006827a3132dbac38b05bc6b042bd6f4e89e5b872ed7895656103ced35b103acb915820cfefc7d8d7996577c14eed3596a2c8629f35519151e9689
6
+ metadata.gz: faf0fa5ce0d3c2a3c773c2840bb5a8fac8507d722e3a1f681a65f71aaea31db204f7a22d95dc68bd9f99bc30ecc01f5ed392eb5df171ea96d897dd1f4867dd36
7
+ data.tar.gz: 2c85e8f978b9e831671a9ae1512a9c6103f3f9a12bd59e8d27e395dac5848766f38ab04229561e70910b00448e41cdf157f175fa750992e619d63f90ddd149d5
data/.gitignore CHANGED
@@ -17,4 +17,6 @@ test/version_tmp
17
17
  tmp
18
18
  .project
19
19
  /schema.rb
20
- .idea
20
+ .idea
21
+ spec/examples.txt
22
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ schema-to-scaffold
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.1.1@schema-to-scaffold
1
+ 2.7.4
data/README.md CHANGED
@@ -7,7 +7,7 @@ This Gem generates Rails command strings based on a Rails database schema you al
7
7
  This Gem does not modify anything; it simply prints a string which you can then use to invoke the Rails generators, and optionally copies the string to your clipboard. Generated string commands available are:
8
8
  ```bash
9
9
  rails generate scaffold <model_name> <field[:type]>
10
- rails generate factory_girl:model <ModelName> <field[:type]>
10
+ rails generate factory_bot:model <ModelName> <field[:type]>
11
11
  ```
12
12
 
13
13
  Use your schema.rb file from `<rails_app>/db` or generated with `rake db:schema:dump`. You can optionally rename schema.rb to `schema_your_fav_name.rb` and it will still be found. Unique schema file names will prevent schema.rb from being overwritten if you use migrations and run `rake db:migrate`.
@@ -26,24 +26,28 @@ Assuming that you have rubygems-bundler installed, just type:
26
26
 
27
27
  ## Usage
28
28
 
29
+ ```bash
30
+ $ scaffold [options]
29
31
  ```
30
- Usage: scaffold [options]
32
+
31
33
  Generate a rails scaffold script for a given schema.rb
34
+ ```
32
35
  -h Displays help.
33
36
  -p <path> It specifies a path to a folder or to a file.
34
37
  -c Will copy the script to your clipboard. Requires xclip be installed on Linux.
35
- -f Generates a factory_girl:model rather than a full scaffold.
38
+ -f Generates a factory_bot:model rather than a full scaffold.
36
39
  -m Add migration (use if your schema comes from a different database)
37
-
40
+ ```
38
41
  Examples:
39
- scaffold
40
- scaffold -c -p ~/work/rails/my_app
41
- scaffold -c -p ~/work/rails/my_app/db/schema.rb
42
-
42
+ ```bash
43
+ $ scaffold
44
+ $ scaffold -c -p ~/work/rails/my_app
45
+ $ scaffold -c -p ~/work/rails/my_app/db/schema.rb
43
46
  ```
47
+
44
48
  ## Generators
45
49
 
46
- Since this gem assists you in invoking Rails generators for your scaffolding, make sure you've configured your generators with your preferred settings before you start. There's a good [Rails Guide](http://guides.rubyonrails.org/generators.html) and you can invoke `rails g scaffold -h` to see options.
50
+ Since this gem assists you in invoking Rails generators for your scaffolding, make sure you've configured your generators with your preferred settings before you start. There's a good [Rails Guide](http://guides.rubyonrails.org/generators.html) and you can invoke `$ rails g scaffold -h` to see options.
47
51
 
48
52
  Generator options are configured in `config/application.rb`. Here is an example setting:
49
53
 
@@ -56,15 +60,15 @@ module YourApplication
56
60
  g.helper false # Don't create view helpers
57
61
  g.test_framework :rspec, :view_specs => false
58
62
  g.integration_tool :rspec
59
- g.fixture_replacement :factory_girl # Choose between fixtures and factories
60
- g.factory_girl dir: 'spec/factories'
63
+ g.fixture_replacement :factory_bot # Choose between fixtures and factories
64
+ g.factory_bot dir: 'spec/factories'
61
65
  g.javascript_engine :js # Disable coffeescript
62
66
  g.scaffold_controller "responders_controller" # from responders gem
63
67
  end
64
68
  end
65
69
  end
66
70
  ```
67
- If you configure factory_girl as your fixture_replacement here, there is no need to invoke factory_girl separately with the `scaffold -f` command.
71
+ If you configure factory_bot as your fixture_replacement here, there is no need to invoke factory_bot separately with the `scaffold -f` command.
68
72
 
69
73
  ## Migrations
70
74
 
@@ -72,7 +76,7 @@ Schema to Scaffold is set up by default to support creating scaffolds for your e
72
76
 
73
77
  ## To install xclip on Linux
74
78
 
75
- sudo apt-get install xclip
79
+ `apt-get install xclip`
76
80
 
77
81
  ## Contributing
78
82
 
@@ -83,10 +87,10 @@ Schema to Scaffold is set up by default to support creating scaffolds for your e
83
87
  3. Commit your changes (`git commit -am "Added great stuff"`)
84
88
  4. Push to the branch (`git push origin my_schema_to_scaffold`)
85
89
  5. Open a [Pull Request][1]
86
- 6. That's all!!
90
+ 6. That's all!!
87
91
 
88
92
  [1]: http://github.com/frenesim/schema_to_scaffold/pulls
89
93
 
90
94
  ## Collaborate
91
95
 
92
- If you want to collaborate, please send me an email, or just create an issue or pull request.
96
+ If you want to collaborate, please send me an email, or just create an issue or pull request.
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/scaffold CHANGED
@@ -1,78 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
- $:.unshift File.expand_path('../lib', File.dirname(__FILE__))
3
- require 'schema_to_scaffold'
4
- require 'pry'
5
- ## Argument conditions
2
+ # encoding: UTF-8
6
3
 
7
- opts = SchemaToScaffold.parse_arguments(ARGV)
8
- ARGV.clear
4
+ # resolve bin path, ignoring symlinks
5
+ require "pathname"
6
+ bin_file = Pathname.new(__FILE__).realpath
7
+ # add self to libpath
8
+ $:.unshift File.expand_path("../../lib", bin_file)
9
9
 
10
- if opts[:help]
11
- puts SchemaToScaffold.help_msg
12
- exit 0
13
- end
10
+ require 'schema_to_scaffold/cli'
14
11
 
15
- ## looking for /schema\S*.rb$/ in user directory
16
- paths = SchemaToScaffold::Path.new(opts[:path])
17
- path = paths.choose unless opts[:path].to_s.match(/\.rb$/)
18
-
19
- ## Opening file
20
- path||=opts[:path]
21
- begin
22
- data = File.open(path, 'r') {|f| f.read }
23
- rescue
24
- puts "\nUnable to open file '#{path}'"
25
- exit 1
26
- end
27
-
28
- ## Generate scripts from schema
29
-
30
- schema = SchemaToScaffold::Schema.new(data)
31
-
32
- begin
33
- raise if schema.table_names.empty?
34
- puts "\nLoaded tables:"
35
- schema.table_names.each_with_index {|name,i| puts "#{i}. #{name}" }
36
-
37
- puts "\nOptions are:\n4 for table 4; (4..6) for table 4 to 6; [4,6] for tables 4 and 6; * for all Tables"
38
-
39
- print "\nSelect a table: "
40
-
41
- rescue
42
- puts "Could not find tables in '#{path}'"
43
- exit 1
44
- end
45
-
46
- result = gets.strip
47
- begin
48
- case result
49
- when "*"
50
- tables = (0..schema.table_names.count-1).map{|i|i}
51
- when /^\d/
52
- tables = [result.to_i]
53
- else
54
- tables = eval(result).to_a.map{|i|i}
55
- end
56
- raise if tables.empty?
57
- rescue Exception => e
58
- puts "Not a valid input, \nOptions are:\n4 for table 4; (4..6) for table 4 to 6; [4,6] for tables 4 and 6; * for all Tables"
59
- exit 1
60
- end
61
-
62
- script = []
63
- target = opts[:factory_girl] ? "factory_girl:model" : "scaffold"
64
- migragion_flag = opts[:migration] || opts[:factory_girl]
65
- tables.each { |table_id| script << SchemaToScaffold.generate_script(schema, table_id, target, migragion_flag) }
66
- output = script.join("")
67
- puts "\nScript for #{target}:\n\n"
68
- puts output
69
-
70
- if opts[:clipboard]
71
- puts("\n(copied to your clipboard)")
72
- case RUBY_PLATFORM
73
- when /darwin/i then exec("echo '#{output}' | tr -d '\n' | pbcopy")
74
- when /linux/i then exec("echo '#{output}' | tr -d '\n' | xclip -selection c")
75
- when /mingw/i then exec("echo '#{output}' | clip")
76
- when /win/i then exec("echo '#{output}' | clip")
77
- end
78
- end
12
+ SchemaToScaffold::CLI.start(*ARGV)
@@ -8,18 +8,17 @@ module SchemaToScaffold
8
8
  end
9
9
 
10
10
  def to_script
11
- "#{name}:#{type}" unless ["created_at","updated_at"].include? name
11
+ "#{name}:#{type}" unless ["created_at","updated_at"].include?(name)
12
12
  end
13
13
 
14
14
  def self.parse(attribute)
15
15
  match = attribute.match(/t\.(\w+)\s+"(\w+)"/)
16
16
  if match
17
- name = match.captures[1].sub(/_id$/,'')
17
+ name = match.captures[1].sub(/_id$/, "")
18
18
  type = $&.nil? ? match.captures[0] : "references"
19
- Attribute.new(name,type)
19
+ Attribute.new(name, type)
20
20
  end
21
21
  end
22
22
 
23
-
24
23
  end
25
24
  end
@@ -0,0 +1,107 @@
1
+ require "schema_to_scaffold"
2
+ module SchemaToScaffold
3
+ class CLI
4
+
5
+ TABLE_OPTIONS = "\nOptions are:\n4 for table 4; (4..6) for table 4 to 6; [4,6] for tables 4 and 6; * for all Tables"
6
+
7
+ def self.start(*args)
8
+ ## Argument conditions
9
+ opts = parse_arguments(args)
10
+
11
+ if opts[:help]
12
+ puts Help.message
13
+ exit 0
14
+ end
15
+
16
+ ## looking for /schema\S*.rb$/ in user directory
17
+ paths = Path.new(opts[:path])
18
+ path = paths.choose unless opts[:path].to_s.match(/\.rb$/)
19
+
20
+ ## Opening file
21
+ path ||= opts[:path]
22
+ begin
23
+ data = File.open(path, 'r') { |f| f.read }
24
+ rescue
25
+ puts "\nUnable to open file '#{path}'"
26
+ exit 1
27
+ rescue Interrupt => e
28
+ exit 1
29
+ end
30
+
31
+ ## Generate scripts from schema
32
+ schema = Schema.new(data)
33
+
34
+ begin
35
+ raise if schema.table_names.empty?
36
+ puts "\nLoaded tables:"
37
+ schema.print_table_names
38
+ puts TABLE_OPTIONS
39
+ print "\nSelect a table: "
40
+ rescue
41
+ puts "Could not find tables in '#{path}'"
42
+ exit 1
43
+ end
44
+
45
+ input = STDIN.gets.strip
46
+ begin
47
+ tables = schema.select_tables(input)
48
+ raise if tables.empty?
49
+ rescue
50
+ puts "Not a valid input. #{TABLE_OPTIONS}"
51
+ exit 1
52
+ rescue Interrupt => e
53
+ exit 1
54
+ end
55
+
56
+ script = []
57
+ target = opts[:factory_bot] ? "factory_bot:model" : "scaffold"
58
+ migration_flag = opts[:migration] ? true : false
59
+
60
+ tables.each do |table_id|
61
+ script << generate_script(schema, table_id, target, migration_flag)
62
+ end
63
+ output = script.join("")
64
+ puts "\nScript for #{target}:\n\n"
65
+ puts output
66
+
67
+ if opts[:clipboard]
68
+ puts("\n(copied to your clipboard)")
69
+ Clipboard.new(output).command
70
+ end
71
+ end
72
+
73
+ ##
74
+ # Parses ARGV and returns a hash of options.
75
+ def self.parse_arguments(argv)
76
+ if argv_index = argv.index("-p")
77
+ path = argv.delete_at(argv_index + 1)
78
+ argv.delete('-p')
79
+ end
80
+
81
+ args = {
82
+ clipboard: argv.delete('-c'), # check for clipboard flag
83
+ factory_bot: argv.delete('-f'), # factory_bot instead of scaffold
84
+ migration: argv.delete('-m'), # generate migrations
85
+ help: argv.delete('-h'), # check for help flag
86
+ path: path # get path to file(s)
87
+ }
88
+
89
+ if argv.empty?
90
+ args
91
+ else
92
+ puts "\n------\nWrong set of arguments.\n------\n"
93
+ puts Help.message
94
+ exit
95
+ end
96
+ end
97
+
98
+ ##
99
+ # Generates the rails scaffold script
100
+ def self.generate_script(schema, table=nil, target, migration_flag)
101
+ schema = Schema.new(schema) unless schema.is_a?(Schema)
102
+ return schema.to_script if table.nil?
103
+ schema.table(table).to_script(target, migration_flag)
104
+ end
105
+
106
+ end
107
+ end
@@ -0,0 +1,36 @@
1
+ module SchemaToScaffold
2
+ class Clipboard
3
+ attr_reader :output
4
+
5
+ def initialize(output)
6
+ @output = output
7
+ end
8
+
9
+ def command
10
+ case platform
11
+ when /darwin/i then darwin_command
12
+ when /linux/i then linux_command
13
+ when /mingw/i then win_command
14
+ when /win/i then win_command
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def darwin_command
21
+ exec("echo '#{output}' | tr -d '\n' | pbcopy")
22
+ end
23
+
24
+ def linux_command
25
+ exec("echo '#{output}' | tr -d '\n' | xclip -selection c")
26
+ end
27
+
28
+ def win_command
29
+ exec("echo '#{output}' | clip")
30
+ end
31
+
32
+ def platform
33
+ RUBY_PLATFORM
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,46 @@
1
+ module SchemaToScaffold
2
+ class Help
3
+ ## Usage help text to print in all platforms
4
+ GENERIC_HELP = <<-END_OF_HELP
5
+
6
+ Usage: scaffold [options]
7
+ Generate a rails scaffold script for a given schema.rb
8
+ -h Displays help.
9
+ -p <path> It specifies a path to a folder or to a file.
10
+ -c Will copy the script to your clipboard. Requires xclip be installed on Linux.
11
+ -f Generates a factory_bot:model rather than a full scaffold.
12
+ -m Add migration (use if your schema comes from a different database)
13
+
14
+ END_OF_HELP
15
+
16
+ ## Windows specific usage help text
17
+ WINDOWS_HELP = <<-WINDOWS_SAMPLE
18
+ Examples:
19
+ scaffold
20
+ scaffold -p C:\\Users\\JohnDoe
21
+ scaffold -c -p C:\\Users\\JohnDoe\\Documents\\schema.rb
22
+ WINDOWS_SAMPLE
23
+
24
+ ## Linux specific usage help text
25
+ LINUX_HELP = <<-LINUX_SAMPLE
26
+ Examples:
27
+ scaffold
28
+ scaffold -c -p ~/work/rails/my_app
29
+ scaffold -c -p ~/work/rails/my_app/db/schema.rb
30
+ LINUX_SAMPLE
31
+
32
+ def self.message
33
+ return GENERIC_HELP +
34
+ case platform
35
+ when /darwin/i then LINUX_HELP
36
+ when /linux/i then LINUX_HELP
37
+ when /mingw/i then WINDOWS_HELP
38
+ when /win/i then WINDOWS_HELP
39
+ end
40
+ end
41
+
42
+ def self.platform
43
+ RUBY_PLATFORM
44
+ end
45
+ end
46
+ end
@@ -1,54 +1,58 @@
1
1
  require 'find'
2
2
  module SchemaToScaffold
3
-
4
3
  ##
5
4
  # Deal with the path argument
6
5
 
7
6
  class Path
8
-
7
+
9
8
  def initialize(path)
10
- @search_path = Dir.pwd
11
- @path = path
9
+ @path = path || Dir.pwd
12
10
  end
13
11
 
14
12
  ##
15
- # Validate if a given path leads to a directory
16
- def check_directory
17
- unless File.directory?(@search_path)
18
- puts "\nSorry #{@search_path} is not a valid directory!\nHere is an example:\nscaffold -p /home/foo/bar"
13
+ # Return the chosen path
14
+ def choose
15
+ validate_path
16
+ search_paths_list = search_paths
17
+ if search_paths_list.empty?
18
+ puts "\nThere is no /schema[^\/]*.rb$/ in the directory #{@path}"
19
19
  exit
20
20
  end
21
- puts "\nLooking for schema.rb in #{@search_path}"
21
+
22
+ search_paths_list.each_with_index {|path,indx| puts "#{indx}. #{path}" }
23
+
24
+ begin
25
+ print "\nSelect a path to the target schema: "
26
+ while search_paths_list[(id = STDIN.gets.to_i)].nil?; end
27
+ rescue Interrupt => e
28
+ exit 1
29
+ end
30
+
31
+
32
+ search_paths_list[id]
22
33
  end
23
-
24
34
 
35
+ private
25
36
  ##
26
- # Return the chosen path
27
- def choose
28
- search_rb
29
- if @schema_paths.empty?
30
- puts "\nSorry there is none /schema[^\/]*.rb$/ in the directory #{@search_path}"
37
+ # Validate if a given path leads to a directory
38
+ def validate_path
39
+ if File.directory?(@path.to_s)
40
+ puts "\nLooking for schema.rb in #{@path}"
41
+ else
42
+ puts "\nSorry #{@path} is not a valid directory!\nHere is an example:\nscaffold -p /home/foo/bar"
31
43
  exit
32
44
  end
33
- @schema_paths.each_with_index {|path,indx| puts "#{indx}. #{path}" }
34
- begin
35
- print "\nSelect a path to the target schema: "
36
- end while @schema_paths[(id = gets.to_i)].nil?
37
- @schema_paths[id]
38
45
  end
39
46
 
40
- private
41
47
  ##
42
48
  # Will search for /schema[^\/]*.rb$/ in the current directory
43
- def search_rb
44
- @search_path = @path.to_s unless @path.nil?
45
- check_directory
46
- @schema_paths = Array.new
47
- Find.find(@search_path) do |s_p|
48
- @schema_paths<<s_p if s_p[/schema[^\/]*.rb$/]
49
+ def search_paths
50
+ result = []
51
+ Find.find(@path) do |s_p|
52
+ result << s_p if s_p[/schema[^\/]*.rb$/]
49
53
  end
54
+ result
50
55
  end
51
56
 
52
57
  end
53
-
54
58
  end
@@ -11,11 +11,24 @@ module SchemaToScaffold
11
11
  tables.map(&:name)
12
12
  end
13
13
 
14
+ def print_table_names
15
+ table_names.each_with_index { |name, i| puts "#{i}. #{name}" }
16
+ end
17
+
18
+ def select_tables(input)
19
+ case input
20
+ when "*"
21
+ table_range.to_a
22
+ when /^\d/
23
+ table_range.include?(input.to_i) ? [input.to_i] : []
24
+ end
25
+ end
26
+
14
27
  def table(id)
15
28
  case id
16
29
  when Symbol then table(id.to_s)
17
30
  when String then tables[table_names.index(id)]
18
- when Fixnum then tables[id]
31
+ when Integer then tables[id]
19
32
  else nil
20
33
  end
21
34
  end
@@ -25,7 +38,13 @@ module SchemaToScaffold
25
38
  end
26
39
 
27
40
  def self.parse(data)
28
- data.split(/^\s*create_/)[1..-1].map {|table_data| Table.parse table_data }
41
+ data.split(/^\s*create_/)[1..-1].map {|table_data| Table.parse(table_data) }.reject{ |e| e.nil? }
42
+ end
43
+
44
+ private
45
+
46
+ def table_range
47
+ 0...table_names.count
29
48
  end
30
49
  end
31
50
  end
@@ -11,31 +11,37 @@ module SchemaToScaffold
11
11
  @name, @attributes = name, attributes
12
12
  end
13
13
 
14
- def to_script(target, migragion_flag)
14
+ def to_script(target, migration_flag)
15
15
  begin
16
16
  attributes_list = attributes.map(&:to_script).reject { |x| x.nil? || x.empty? }.join(' ')
17
- rescue Exception => e
17
+ rescue => e
18
18
  puts "\n ---------------------------------------------"
19
19
  puts e.message
20
20
  puts "Table \n\n\n #{self.inspect} \n\n\n"
21
21
  puts "\n ---------------------------------------------"
22
22
  end
23
23
  script = []
24
- script << "rails generate #{target} #{modelize name} #{attributes_list}"
25
- script << " --no-migration" unless migragion_flag
24
+ script << "rails generate #{target} #{modelize(name)} #{attributes_list}"
25
+ script << " --no-migration" unless migration_flag
26
26
  script << "\n\n"
27
- return script
27
+ script
28
28
  end
29
29
 
30
30
  def self.parse(table_data)
31
31
  return unless name = table_data[/table "([^"]+?)"/]
32
32
  name = $1
33
- atts = table_data.lines.to_a.select { |line| line =~ /t\.\w+/ }.map { |att| Attribute.parse att }
34
- Table.new(name, atts)
33
+ table_fields = table_fields_of(table_data)
34
+ Table.new(name, table_fields)
35
35
  end
36
36
 
37
37
  private
38
- def modelize (string)
38
+ def self.table_fields_of(table_data)
39
+ table_data.lines.to_a.select { |line| line =~ /t\.(?!index)\w+/ }.map { |att| Attribute.parse(att) }
40
+ end
41
+
42
+ private
43
+
44
+ def modelize(string)
39
45
  string.camelize.singularize
40
46
  end
41
47
 
@@ -1,6 +1,6 @@
1
1
  module SchemaToScaffold
2
2
  MAJOR = 0
3
- MINOR = 7
4
- REVISION = 1
3
+ MINOR = 8
4
+ REVISION = 2
5
5
  VERSION = [MAJOR, MINOR, REVISION].join('.')
6
6
  end