schema_to_scaffold 0.7.1 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,84 +3,9 @@ require 'schema_to_scaffold/schema'
3
3
  require 'schema_to_scaffold/table'
4
4
  require 'schema_to_scaffold/attribute'
5
5
  require 'schema_to_scaffold/path'
6
+ require 'schema_to_scaffold/cli'
7
+ require 'schema_to_scaffold/help'
8
+ require 'schema_to_scaffold/clipboard'
6
9
 
7
10
  module SchemaToScaffold
8
- extend self
9
-
10
- ## Usage help text to print in all platforms
11
-
12
- GENERIC_HELP = <<-END_OF_HELP
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 folder or to a file.
18
- -c Will copy the script to your clipboard. Requires xclip be installed on Linux.
19
- -f Generates a factory_girl:model rather than a full scaffold.
20
- -m Add migration (use if your schema comes from a different database)
21
-
22
- END_OF_HELP
23
-
24
-
25
- ## Windows specific usage help text
26
-
27
- WINDOWS_HELP = <<-WINDOWS_SAMPLE
28
- Examples:
29
- scaffold
30
- scaffold -p C:\\Users\\JohnDoe
31
- scaffold -c -p C:\\Users\\JohnDoe\\Documents\\schema.rb
32
- WINDOWS_SAMPLE
33
-
34
- ## Linux specific usage help text
35
-
36
- LINUX_HELP = <<-LINUX_SAMPLE
37
- Examples:
38
- scaffold
39
- scaffold -c -p ~/work/rails/my_app
40
- scaffold -c -p ~/work/rails/my_app/db/schema.rb
41
- LINUX_SAMPLE
42
-
43
- def help_msg
44
- return GENERIC_HELP +
45
- case RUBY_PLATFORM
46
- when /darwin/i then LINUX_HELP
47
- when /linux/i then LINUX_HELP
48
- when /mingw/i then WINDOWS_HELP
49
- when /win/i then WINDOWS_HELP
50
- end
51
- end
52
-
53
- ##
54
- # Parses ARGV and returns a hash of options.
55
-
56
- def parse_arguments(argv)
57
- if argv_index = argv.index("-p")
58
- path = argv.delete_at(argv_index+1)
59
- argv.delete('-p')
60
- end
61
- args = {
62
- :clipboard => argv.delete('-c'), # check for clipboard flag
63
- :factory_girl => argv.delete('-f'), # factory_girl instead of scaffold
64
- :migration => argv.delete('-m'), # generate migrations
65
- :help => argv.delete('-h'), # check for help flag
66
- :path => path # get path to file(s)
67
- }
68
- unless argv.empty?
69
- puts "\n------\nWrong set of arguments.\n------\n"
70
- puts help_msg
71
- exit
72
- else
73
- args
74
- end
75
-
76
- end
77
-
78
- ##
79
- # Generates the rails scaffold script
80
-
81
- def self.generate_script(schema, table=nil,target,migragion_flag)
82
- schema = Schema.new(schema) unless schema.is_a? Schema
83
- return schema.to_script if table.nil?
84
- schema.table(table).to_script target, migragion_flag
85
- end
86
11
  end
@@ -17,10 +17,11 @@ EOD
17
17
  gem.files = `git ls-files`.split($/)
18
18
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
19
  gem.require_paths = ["lib"]
20
- gem.post_install_message = "Thanks for installing!"
21
20
  gem.licenses = ['MIT']
22
21
  gem.required_ruby_version = '>= 1.9.3'
23
22
 
24
- gem.add_dependency('activesupport', '>= 3.2.1')
25
- gem.add_development_dependency('pry', '~> 0.10', '>= 0.10.0')
23
+ gem.add_runtime_dependency('activesupport', '~> 7')
24
+ gem.add_development_dependency('byebug', '~> 11')
25
+ gem.add_development_dependency('rspec', '~> 3')
26
+ gem.add_development_dependency('simplecov', '~> 0.21')
26
27
  end
@@ -0,0 +1,70 @@
1
+ module SchemaToScaffold
2
+ RSpec.describe Attribute do
3
+
4
+ describe ".parse" do
5
+ it "parses _id attribute to references" do
6
+ attribute = Attribute.parse('t.integer "user_id"')
7
+ expect(attribute.type).to eq("references")
8
+ expect(attribute.name).to eq("user")
9
+ end
10
+
11
+ it "parses string attributes" do
12
+ attribute = Attribute.parse('t.string "filename"')
13
+ expect(attribute.type).to eq("string")
14
+ expect(attribute.name).to eq("filename")
15
+ end
16
+
17
+ it "parses datetime attributes" do
18
+ attribute = Attribute.parse('t.datetime "created_at", null: false')
19
+ expect(attribute.type).to eq("datetime")
20
+ expect(attribute.name).to eq("created_at")
21
+ end
22
+
23
+ it "parses limit value" do
24
+ pending
25
+ attribute = Attribute.parse('t.integer "quota", limit: 8')
26
+ expect(attribute.type).to eq("integer")
27
+ expect(attribute.name).to eq("quota")
28
+ expect(attribute.limit).to eq("8")
29
+ end
30
+
31
+ it "parses decimals precision and scale values" do
32
+ pending
33
+ attribute = Attribute.parse('t.decimal "price", precision: 20, scale: 2, default: 0.0')
34
+ expect(attribute.type).to eq("decimal")
35
+ expect(attribute.name).to eq("price")
36
+ expect(attribute.precision).to eq("20")
37
+ expect(attribute.scale).to eq("2")
38
+ end
39
+ end
40
+
41
+ describe "#to_script" do
42
+ it "returns name and type" do
43
+ attribute = Attribute.parse('t.string "filename"')
44
+ expect(attribute.to_script).to eq("filename:string")
45
+ end
46
+
47
+ it "ignores updated_at" do
48
+ attribute = Attribute.parse('t.datetime "updated_at", null: false')
49
+ expect(attribute.to_script).to be_nil
50
+ end
51
+
52
+ it "ignores created_at" do
53
+ attribute = Attribute.parse('t.datetime "created_at", null: false')
54
+ expect(attribute.to_script).to be_nil
55
+ end
56
+
57
+ it "adds limit value to integer" do
58
+ pending
59
+ attribute = Attribute.parse('t.integer "quota", limit: 8')
60
+ expect(attribute.to_script).to eq("quota:integer{8}")
61
+ end
62
+
63
+ it "adds precision and scale value to decimal" do
64
+ pending
65
+ attribute = Attribute.parse('t.decimal "price", precision: 20, scale: 2, default: 0.0')
66
+ expect(attribute.to_script).to eq("price:decimal{20,2}")
67
+ end
68
+ end
69
+ end
70
+ end
data/spec/cli_spec.rb ADDED
@@ -0,0 +1,116 @@
1
+ module SchemaToScaffold
2
+ RSpec.describe CLI do
3
+
4
+ let(:empty_schema_file) { File.expand_path(File.dirname(__FILE__)) + '/support/empty_schema.rb' }
5
+ let(:schema_file) { File.expand_path(File.dirname(__FILE__)) + '/support/schema.rb' }
6
+
7
+ describe ".start" do
8
+ it "prints help message" do
9
+ expect {
10
+ begin
11
+ SchemaToScaffold::CLI.start("-h")
12
+ rescue SystemExit
13
+ end
14
+ }.to output(/Usage:/).to_stdout
15
+ end
16
+
17
+ it "prints file not found message" do
18
+ expect {
19
+ begin
20
+ SchemaToScaffold::CLI.start("-p", "/support/schema.rb")
21
+ rescue SystemExit
22
+ end
23
+ }.to output(/Unable to open file /).to_stdout
24
+ end
25
+
26
+ it "prints could not find tables message" do
27
+ expect {
28
+ begin
29
+ SchemaToScaffold::CLI.start("-p", empty_schema_file)
30
+ rescue SystemExit
31
+ end
32
+ }.to output(/Could not find tables /).to_stdout
33
+ end
34
+
35
+ context "choose table" do
36
+ before do
37
+ allow(STDIN).to receive(:gets) { "" }
38
+ end
39
+
40
+ it "prints Not a valid input message" do
41
+ expect {
42
+ begin
43
+ SchemaToScaffold::CLI.start("-p", schema_file)
44
+ rescue SystemExit
45
+ end
46
+ }.to output(/Not a valid input/).to_stdout
47
+ end
48
+ end
49
+
50
+ context "choose all tables" do
51
+ before do
52
+ allow(STDIN).to receive(:gets).once { "*" }
53
+ end
54
+
55
+ it "prints scaffold lines message" do
56
+ expect {
57
+ begin
58
+ SchemaToScaffold::CLI.start("-p", schema_file)
59
+ rescue SystemExit
60
+ end
61
+ }.to output(/Script for/).to_stdout
62
+ end
63
+ end
64
+
65
+ context "copies first table to clipboard" do
66
+ before do
67
+ allow(STDIN).to receive(:gets).once { "0" }
68
+ expect_any_instance_of(Kernel).to receive(:exec).with("echo 'rails generate scaffold User email:string encrypted_password:string reset_password_token:string reset_password_sent_at:datetime remember_created_at:datetime sign_in_count:integer current_sign_in_at:datetime last_sign_in_at:datetime current_sign_in_ip:string last_sign_in_ip:string name:string role:integer --no-migration\n\n' | tr -d '\n' | pbcopy")
69
+ expect_any_instance_of(Clipboard).to receive(:platform).and_return("/darwin/i")
70
+ end
71
+
72
+ it "prints copied to your clipboard message" do
73
+ expect {
74
+ begin
75
+ SchemaToScaffold::CLI.start("-c", "-p", schema_file)
76
+ rescue SystemExit
77
+ end
78
+ }.to output(/copied to your clipboard/).to_stdout
79
+ end
80
+ end
81
+ end
82
+
83
+ describe ".parse_arguments" do
84
+ it "handles wrong arguments" do
85
+ expect {
86
+ begin
87
+ SchemaToScaffold::CLI.parse_arguments(["--help"])
88
+ rescue SystemExit
89
+ end
90
+ }.to output(/Wrong set of arguments/).to_stdout
91
+ end
92
+
93
+ context "handles arguments" do
94
+ it "help argument" do
95
+ expect(CLI.parse_arguments(["-h"])).to eq({clipboard: nil, factory_bot: nil, migration: nil, help: "-h", path: nil})
96
+ end
97
+
98
+ it "path argument" do
99
+ expect(CLI.parse_arguments(["-p", "/some/path"])).to eq({clipboard: nil, factory_bot: nil, migration: nil, help: nil, path: "/some/path"})
100
+ end
101
+
102
+ it "clipboard argument" do
103
+ expect(CLI.parse_arguments(["-c"])).to eq({clipboard: "-c", factory_bot: nil, migration: nil, help: nil, path: nil})
104
+ end
105
+
106
+ it "factory girl argument" do
107
+ expect(CLI.parse_arguments(["-f"])).to eq({clipboard: nil, factory_bot: "-f", migration: nil, help: nil, path: nil})
108
+ end
109
+
110
+ it "migration argument" do
111
+ expect(CLI.parse_arguments(["-m"])).to eq({clipboard: nil, factory_bot: nil, migration: "-m", help: nil, path: nil})
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,52 @@
1
+ module SchemaToScaffold
2
+ RSpec.describe Clipboard do
3
+ let(:output) { "rails g model ....." }
4
+ let(:clipboard) { Clipboard.new(output) }
5
+
6
+ describe "#command" do
7
+ context "darwin" do
8
+ before do
9
+ allow(clipboard).to receive(:platform).and_return("darwin")
10
+ expect_any_instance_of(Kernel).to receive(:exec).with("echo 'rails g model .....' | tr -d '\n' | pbcopy")
11
+ end
12
+
13
+ it "executes darwin command" do
14
+ clipboard.command
15
+ end
16
+ end
17
+
18
+ context "linux" do
19
+ before do
20
+ allow(clipboard).to receive(:platform).and_return("linux")
21
+ expect_any_instance_of(Kernel).to receive(:exec).with("echo 'rails g model .....' | tr -d '\n' | xclip -selection c")
22
+ end
23
+
24
+ it "executes linux command" do
25
+ clipboard.command
26
+ end
27
+ end
28
+
29
+ context "windows (win)" do
30
+ before do
31
+ allow(clipboard).to receive(:platform).and_return("win")
32
+ expect_any_instance_of(Kernel).to receive(:exec).with("echo 'rails g model .....' | clip")
33
+ end
34
+
35
+ it "executes windows command" do
36
+ clipboard.command
37
+ end
38
+ end
39
+
40
+ context "windows (mingw)" do
41
+ before do
42
+ allow(clipboard).to receive(:platform).and_return("mingw")
43
+ expect_any_instance_of(Kernel).to receive(:exec).with("echo 'rails g model .....' | clip")
44
+ end
45
+
46
+ it "executes windows command" do
47
+ clipboard.command
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
data/spec/help_spec.rb ADDED
@@ -0,0 +1,25 @@
1
+ module SchemaToScaffold
2
+ RSpec.describe Help do
3
+ describe ".message" do
4
+ it "returns message for darwin platform" do
5
+ allow(Help).to receive(:platform).and_return("darwin")
6
+ expect(Help.message).to include("work/rails/my_app/")
7
+ end
8
+
9
+ it "returns message for linux platform" do
10
+ allow(Help).to receive(:platform).and_return("linux")
11
+ expect(Help.message).to include("work/rails/my_app/")
12
+ end
13
+
14
+ it "returns message for windows(win) platform" do
15
+ allow(Help).to receive(:platform).and_return("win")
16
+ expect(Help.message).to include("JohnDoe\\Documents")
17
+ end
18
+
19
+ it "returns message for windows (mingw) platform" do
20
+ allow(Help).to receive(:platform).and_return("mingw")
21
+ expect(Help.message).to include("JohnDoe\\Documents")
22
+ end
23
+ end
24
+ end
25
+ end
data/spec/path_spec.rb ADDED
@@ -0,0 +1,41 @@
1
+ module SchemaToScaffold
2
+ RSpec.describe Path do
3
+
4
+ describe "#choose" do
5
+ it "prints error when given path is not an directory" do
6
+ path = Path.new("path_spec.rb")
7
+ expect {
8
+ begin
9
+ path.choose
10
+ rescue SystemExit
11
+ end
12
+ }.to output(/Sorry path_spec\.rb is not a valid directory!/).to_stdout
13
+ end
14
+
15
+ it "prints message when given path is a directory but no schema is found" do
16
+ path = Path.new(File.expand_path(File.dirname(__FILE__)) + '/support/no_schema')
17
+ expect {
18
+ begin
19
+ path.choose
20
+ rescue SystemExit
21
+ end
22
+ }.to output(/There is no/).to_stdout
23
+ end
24
+
25
+ context "needs user input" do
26
+ let(:path) { Path.new(File.expand_path(File.dirname(__FILE__)) + '/support') }
27
+ before do
28
+ allow(STDIN).to receive(:gets) { "0" }
29
+ end
30
+
31
+ it "prints message when given path is a directory" do
32
+ expect {
33
+ path.choose
34
+ }.to output(/Select a path to the target schema/).to_stdout
35
+ end
36
+ end
37
+
38
+
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,45 @@
1
+ module SchemaToScaffold
2
+ RSpec.describe Schema do
3
+
4
+ let(:schema_data) { File.read(File.expand_path(File.dirname(__FILE__)) + '/support/schema.rb') }
5
+ let(:schema) { Schema.new(schema_data) }
6
+
7
+ describe ".parse" do
8
+ it "parses given schema file" do
9
+ expect(Schema.parse(schema_data)).to be_kind_of(Array)
10
+ end
11
+ end
12
+
13
+ describe "#table_names" do
14
+ it "returns table names" do
15
+ expect(schema.table_names).to eq(["users"])
16
+ end
17
+ end
18
+
19
+ describe "#to_script" do
20
+ it "is broken" do
21
+ pending
22
+ expect(schema.to_script).to eq("users")
23
+ end
24
+ end
25
+
26
+ describe "#table" do
27
+ it "returns parsed table by given table name as symbol" do
28
+ expect(schema.table(:users)).to be_kind_of(SchemaToScaffold::Table)
29
+ end
30
+
31
+ it "returns parsed table by given table index" do
32
+ expect(schema.table(0)).to be_kind_of(SchemaToScaffold::Table)
33
+ end
34
+
35
+ it "returns nil" do
36
+ expect(schema.table([])).to be_nil
37
+ end
38
+
39
+ it "returns nil" do
40
+ pending
41
+ expect(schema.table("")).to be_nil
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ RSpec.describe SchemaToScaffold do
2
+ it "has a version number" do
3
+ expect(SchemaToScaffold::VERSION).not_to be nil
4
+ end
5
+ end
@@ -0,0 +1,111 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+
7
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
8
+
9
+ require "simplecov"
10
+ SimpleCov.start 'test_frameworks'
11
+
12
+ require "schema_to_scaffold"
13
+ require "byebug"
14
+
15
+ # Given that it is always loaded, you are encouraged to keep this file as
16
+ # light-weight as possible. Requiring heavyweight dependencies from this file
17
+ # will add to the boot time of your test suite on EVERY test run, even for an
18
+ # individual file that may not need all of that loaded. Instead, consider making
19
+ # a separate helper file that requires the additional dependencies and performs
20
+ # the additional setup, and require it from the spec files that actually need
21
+ # it.
22
+ #
23
+ # The `.rspec` file also contains a few flags that are not defaults but that
24
+ # users commonly want.
25
+ #
26
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
27
+ RSpec.configure do |config|
28
+ # rspec-expectations config goes here. You can use an alternate
29
+ # assertion/expectation library such as wrong or the stdlib/minitest
30
+ # assertions if you prefer.
31
+ config.expect_with :rspec do |expectations|
32
+ # This option will default to `true` in RSpec 4. It makes the `description`
33
+ # and `failure_message` of custom matchers include text for helper methods
34
+ # defined using `chain`, e.g.:
35
+ # be_bigger_than(2).and_smaller_than(4).description
36
+ # # => "be bigger than 2 and smaller than 4"
37
+ # ...rather than:
38
+ # # => "be bigger than 2"
39
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
40
+ end
41
+
42
+ # rspec-mocks config goes here. You can use an alternate test double
43
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
44
+ config.mock_with :rspec do |mocks|
45
+ # Prevents you from mocking or stubbing a method that does not exist on
46
+ # a real object. This is generally recommended, and will default to
47
+ # `true` in RSpec 4.
48
+ mocks.verify_partial_doubles = true
49
+ end
50
+
51
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
52
+ # have no way to turn it off -- the option exists only for backwards
53
+ # compatibility in RSpec 3). It causes shared context metadata to be
54
+ # inherited by the metadata hash of host groups and examples, rather than
55
+ # triggering implicit auto-inclusion in groups with matching metadata.
56
+ config.shared_context_metadata_behavior = :apply_to_host_groups
57
+
58
+ # The settings below are suggested to provide a good initial experience
59
+ # with RSpec, but feel free to customize to your heart's content.
60
+
61
+ # This allows you to limit a spec run to individual examples or groups
62
+ # you care about by tagging them with `:focus` metadata. When nothing
63
+ # is tagged with `:focus`, all examples get run. RSpec also provides
64
+ # aliases for `it`, `describe`, and `context` that include `:focus`
65
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
66
+ config.filter_run_when_matching :focus
67
+
68
+ # Allows RSpec to persist some state between runs in order to support
69
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
70
+ # you configure your source control system to ignore this file.
71
+ config.example_status_persistence_file_path = "spec/examples.txt"
72
+
73
+ # Limits the available syntax to the non-monkey patched syntax that is
74
+ # recommended. For more details, see:
75
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
76
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
77
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
78
+ config.disable_monkey_patching!
79
+
80
+ # This setting enables warnings. It's recommended, but in some cases may
81
+ # be too noisy due to issues in dependencies.
82
+ config.warnings = true
83
+
84
+ # Many RSpec users commonly either run the entire suite or an individual
85
+ # file, and it's useful to allow more verbose output when running an
86
+ # individual spec file.
87
+ if config.files_to_run.one?
88
+ # Use the documentation formatter for detailed output,
89
+ # unless a formatter has already been configured
90
+ # (e.g. via a command-line flag).
91
+ config.default_formatter = 'doc'
92
+ end
93
+
94
+ # Print the 10 slowest examples and example groups at the
95
+ # end of the spec run, to help surface which specs are running
96
+ # particularly slow.
97
+ config.profile_examples = 10
98
+
99
+ # Run specs in random order to surface order dependencies. If you find an
100
+ # order dependency and want to debug it, you can fix the order by providing
101
+ # the seed, which is printed after each run.
102
+ # --seed 1234
103
+ config.order = :random
104
+
105
+ # Seed global randomization in this process using the `--seed` CLI option.
106
+ # Setting this allows you to use `--seed` to deterministically reproduce
107
+ # test failures related to randomization by passing the same `--seed` value
108
+ # as the one that triggered the failure.
109
+ Kernel.srand config.seed
110
+
111
+ end
@@ -0,0 +1,7 @@
1
+ # random schema https://github.com/RailsApps/rails-devise-roles/blob/master/db/schema.rb
2
+ ActiveRecord::Schema.define(version: 20140828012915) do
3
+
4
+ create_table :users, force: :cascade do |t|
5
+ end
6
+
7
+ end
File without changes
@@ -0,0 +1,24 @@
1
+ # random schema https://github.com/RailsApps/rails-devise-roles/blob/master/db/schema.rb
2
+ ActiveRecord::Schema.define(version: 20140828012915) do
3
+
4
+ create_table "users", force: :cascade do |t|
5
+ t.string "email", default: "", null: false
6
+ t.string "encrypted_password", default: "", null: false
7
+ t.string "reset_password_token"
8
+ t.datetime "reset_password_sent_at"
9
+ t.datetime "remember_created_at"
10
+ t.integer "sign_in_count", default: 0, null: false
11
+ t.datetime "current_sign_in_at"
12
+ t.datetime "last_sign_in_at"
13
+ t.string "current_sign_in_ip"
14
+ t.string "last_sign_in_ip"
15
+ t.datetime "created_at"
16
+ t.datetime "updated_at"
17
+ t.string "name"
18
+ t.integer "role"
19
+ end
20
+
21
+ add_index "users", ["email"], name: "index_users_on_email", unique: true
22
+ add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
23
+
24
+ end
@@ -0,0 +1,17 @@
1
+ create_table "users", force: :cascade do |t|
2
+ t.string "email", default: "", null: false
3
+ t.string "encrypted_password", default: "", null: false
4
+ t.string "reset_password_token"
5
+ t.datetime "reset_password_sent_at"
6
+ t.datetime "remember_created_at"
7
+ t.integer "sign_in_count", default: 0, null: false
8
+ t.datetime "current_sign_in_at"
9
+ t.datetime "last_sign_in_at"
10
+ t.string "current_sign_in_ip"
11
+ t.string "last_sign_in_ip"
12
+ t.datetime "created_at"
13
+ t.datetime "updated_at"
14
+ t.string "name"
15
+ t.integer "role"
16
+ t.index ["basket_id"], name: "index_orders_on_basket_id", using: :btree
17
+ end
@@ -0,0 +1,33 @@
1
+ module SchemaToScaffold
2
+ RSpec.describe Table do
3
+
4
+ let(:table_data) { File.read(File.expand_path(File.dirname(__FILE__)) + '/support/table.rb') }
5
+ let(:email_attribute) { Attribute.new("email", "string") }
6
+ let(:password_attribute) { Attribute.new("password", "string") }
7
+
8
+ describe "#to_script" do
9
+ let(:table) { Table.new("users", [email_attribute, password_attribute]) }
10
+
11
+ it "returns rails generate line" do
12
+ expect(table.to_script("model", true)).to eq(["rails generate model User email:string password:string", "\n\n"])
13
+ end
14
+
15
+ it "returns rails generate line without migrations" do
16
+ expect(table.to_script("model", false)).to eq(["rails generate model User email:string password:string", " --no-migration", "\n\n"])
17
+ end
18
+
19
+ it "raises an exception" do
20
+ allow(email_attribute).to receive(:to_script).and_raise(StandardError, "something went wrong")
21
+ expect { table.to_script("model", true) }.to output(/something went wrong/).to_stdout
22
+ end
23
+ end
24
+
25
+ describe ".parse" do
26
+ let(:table) { Table.parse(table_data) }
27
+ it "parses given table data" do
28
+ expect(table).to be_kind_of(SchemaToScaffold::Table)
29
+ end
30
+ end
31
+
32
+ end
33
+ end