Giter Club home page Giter Club logo

schema_to_scaffold's Introduction

Schema to Scaffold

Code Climate

This Gem generates Rails command strings based on a Rails database schema you already have. Unlike traditional migrations, which modify the database as they generate Rails scaffolding code, this Gem reads the schema for your database and generates the Rails code which matches your database's existing columns.

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:

rails generate scaffold <model_name> <field[:type]>
rails generate factory_bot:model <ModelName> <field[:type]>

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.

Schema to Scaffold output looks like this:

rails generate scaffold users fname:string lname:string bdate:date email:string encrypted_password:string

It's possible to generate scripts for all your tables at once. Just enter * when selecting the table.

Installation

Assuming that you have rubygems-bundler installed, just type:

gem install schema_to_scaffold

Usage

$ scaffold [options]

Generate a rails scaffold script for a given schema.rb

 -h             Displays help.
 -p <path>      It specifies a path to a folder or to a file.
 -c             Will copy the script to your clipboard. Requires xclip be installed on Linux.
 -f             Generates a factory_bot:model rather than a full scaffold.
 -m             Add migration (use if your schema comes from a different database)

Examples:

$ scaffold
$ scaffold -c -p ~/work/rails/my_app
$ scaffold -c -p ~/work/rails/my_app/db/schema.rb

Generators

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 and you can invoke $ rails g scaffold -h to see options.

Generator options are configured in config/application.rb. Here is an example setting:

module YourApplication
  class Application < Rails::Application
    config.generators do |g|
      g.hidden_namespaces << :test_unit << :erb # Hide unwanted generators
      g.template_engine :slim # Select template engine
      g.helper false # Don't create view helpers
      g.test_framework  :rspec, :view_specs => false
      g.integration_tool :rspec
      g.fixture_replacement :factory_bot # Choose between fixtures and factories
      g.factory_bot dir: 'spec/factories'
      g.javascript_engine :js # Disable coffeescript
      g.scaffold_controller "responders_controller" # from responders gem
    end
  end
end

If you configure factory_bot as your fixture_replacement here, there is no need to invoke factory_bot separately with the scaffold -f command.

Migrations

Schema to Scaffold is set up by default to support creating scaffolds for your existing database, presuming that you have generated schema.rb with rake db:schema:dump. Therefore, no migrations are necessary, because the database already contains the desired table. If instead you are using a schema.rb that was generated from a database other than current development database, you can use the -m option to build a generator command that includes migrations.

To install xclip on Linux

`apt-get install xclip`

Contributing

Want to contribute? Great!
  1. Fork it.
  2. Create a branch (git checkout -b my_schema_to_scaffold)
  3. Commit your changes (git commit -am "Added great stuff")
  4. Push to the branch (git push origin my_schema_to_scaffold)
  5. Open a Pull Request
  6. That's all!!

Collaborate

If you want to collaborate, please send me an email, or just create an issue or pull request.

schema_to_scaffold's People

Contributors

dankohn avatar frenesim avatar jakerella avatar jasoares avatar matt17r avatar morkevicius avatar mstark avatar mutantkeyboard avatar skreuter avatar stewartmckee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

schema_to_scaffold's Issues

Error when generating scaffold for multiple tables

I'm trying to generate scaffolds for about 300 tables and your tool would be very useful, however when trying to do multiple tables I get the error below. Doing 1 table at a time seems to work fine.

/Users/grant/.rvm/gems/ruby-2.1.2/gems/schema_to_scaffold-0.5.2/lib/schema_to_scaffold/table.rb:15:in map': undefined methodto_script' for nil:NilClass (NoMethodError)
from /Users/grant/.rvm/gems/ruby-2.1.2/gems/schema_to_scaffold-0.5.2/lib/schema_to_scaffold/table.rb:15:in to_script' from /Users/grant/.rvm/gems/ruby-2.1.2/gems/schema_to_scaffold-0.5.2/lib/schema_to_scaffold.rb:84:ingenerate_script'
from /Users/grant/.rvm/gems/ruby-2.1.2/gems/schema_to_scaffold-0.5.2/bin/scaffold:56:in block in <top (required)>' from /Users/grant/.rvm/gems/ruby-2.1.2/gems/schema_to_scaffold-0.5.2/bin/scaffold:56:ineach'
from /Users/grant/.rvm/gems/ruby-2.1.2/gems/schema_to_scaffold-0.5.2/bin/scaffold:56:in <top (required)>' from /Users/grant/.rvm/gems/ruby-2.1.2/bin/scaffold:23:inload'
from /Users/grant/.rvm/gems/ruby-2.1.2/bin/scaffold:23:in <main>' from /Users/grant/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:ineval'
from /Users/grant/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `

'

Scaffold fails where tables contain non-English characters.

I have a table whose headers are in the Arabic language - Scaffold fails with the following:
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/schema_to_scaffold-0.5.4/lib/schema_to_scaffold/table.rb:15:in map': undefined methodto_script' for nil:
NilClass (NoMethodError)
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/schema_to_scaffold-0.5.4/lib/schema_to_scaffold/table.rb:15:in to_script' from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/schema_to_scaffold-0.5.4/lib/schema_to_scaffold.rb:84:ingenerate_script'
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/schema_to_scaffold-0.5.4/bin/scaffold:57:in block in <top (required)>' from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/schema_to_scaffold-0.5.4/bin/scaffold:57:ineach'
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/schema_to_scaffold-0.5.4/bin/scaffold:57:in <top (required)>' from C:/RailsInstaller/Ruby2.0.0/bin/scaffold:23:inload'
from C:/RailsInstaller/Ruby2.0.0/bin/scaffold:23:in `

'

Is there an easy fix?

Version lock breaking compatibility

You accidentally version locked activesupport to 3.2, causing an incompatibility like this:

Bundler could not find compatible versions for gem "activesupport":
  In Gemfile:
    schema_to_scaffold (= 0.7.0) ruby depends on
      activesupport (>= 3.2.1, ~> 3.2) ruby

    activeadmin (>= 0) ruby depends on
      inherited_resources (!= 1.5.0, ~> 1.4) ruby depends on
        has_scope (~> 0.6.0.rc) ruby depends on
          activesupport (4.1.8)

Could you please release an 0.7.1 incorporating f3dc9fb

What about adding relations?

Hello, Humberto! Awesome gem! Made my life 2019839283901 times easier! What about extending it to create relations between entities within scaffold reading from Foreign keys from tables?

Lets say we have two entities: Post and Comment. A Post can have many Comments.

rails g scaffold Post title:string body:text author:string
rails g scaffold Comment body:string author:string post:references

Where post:references already creates relation. Such extra property could be read from schema.rb directives "add_index"

Just an ideia...

GREAT JOB already!

v0.5.3 causes bundle exec rake to break and generates cattr_accessor error

Freezing the schema_to_scaffold to v0.5.2 seemed to fix the issue.

rake aborted!
NoMethodError: undefined method `cattr_accessor' for ActiveSupport::Logger:Class
/usr/local/rvm/gems/ruby-2.1.2/gems/activesupport-4.1.0/lib/active_support/logger_silence.rb:7:in `block in <module:LoggerSilence>'
/usr/local/rvm/gems/ruby-2.1.2/gems/activesupport-4.1.0/lib/active_support/concern.rb:120:in `class_eval'
/usr/local/rvm/gems/ruby-2.1.2/gems/activesupport-4.1.0/lib/active_support/concern.rb:120:in `append_features'
/usr/local/rvm/gems/ruby-2.1.2/gems/activesupport-4.1.0/lib/active_support/logger.rb:7:in `include'
/usr/local/rvm/gems/ruby-2.1.2/gems/activesupport-4.1.0/lib/active_support/logger.rb:7:in `<class:Logger>'
/usr/local/rvm/gems/ruby-2.1.2/gems/activesupport-4.1.0/lib/active_support/logger.rb:6:in `<module:ActiveSupport>'
/usr/local/rvm/gems/ruby-2.1.2/gems/activesupport-4.1.0/lib/active_support/logger.rb:5:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.1.2/gems/activesupport-4.1.0/lib/active_support.rb:27:in `require'
/usr/local/rvm/gems/ruby-2.1.2/gems/activesupport-4.1.0/lib/active_support.rb:27:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.1.2/gems/activemodel-4.1.0/lib/active_model.rb:24:in `require'
/usr/local/rvm/gems/ruby-2.1.2/gems/activemodel-4.1.0/lib/active_model.rb:24:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.1.2/gems/activemodel-4.1.0/lib/active_model/railtie.rb:1:in `require'
/usr/local/rvm/gems/ruby-2.1.2/gems/activemodel-4.1.0/lib/active_model/railtie.rb:1:in `<top (required)>'
rails_app/config/application.rb:4:in `require'
rails_app/config/application.rb:4:in `<top (required)>'
rails_app/Rakefile:6:in `require'
rails_app/Rakefile:6:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'
(See full trace by running task with --trace)

undefined method `captures' for nil:NilClass

I'm having the following error when I'm trying to use the scaffold command. Can you please help?

I'm using version 0.4.2 of this gem and ruby version 2.0.0p195. Thanks!

/Users/jhephoi/.rvm/gems/ruby-2.0.0-p195@parma/gems/schema_to_scaffold-0.4.2/lib/schema_to_scaffold/attribute.rb:15:in `parse': undefined method `captures' for nil:NilClass (NoMethodError)
    from /Users/jhephoi/.rvm/gems/ruby-2.0.0-p195@parma/gems/schema_to_scaffold-0.4.2/lib/schema_to_scaffold/table.rb:22:in `block in parse'
    from /Users/jhephoi/.rvm/gems/ruby-2.0.0-p195@parma/gems/schema_to_scaffold-0.4.2/lib/schema_to_scaffold/table.rb:22:in `map'
    from /Users/jhephoi/.rvm/gems/ruby-2.0.0-p195@parma/gems/schema_to_scaffold-0.4.2/lib/schema_to_scaffold/table.rb:22:in `parse'
    from /Users/jhephoi/.rvm/gems/ruby-2.0.0-p195@parma/gems/schema_to_scaffold-0.4.2/lib/schema_to_scaffold/schema.rb:28:in `block in parse'
    from /Users/jhephoi/.rvm/gems/ruby-2.0.0-p195@parma/gems/schema_to_scaffold-0.4.2/lib/schema_to_scaffold/schema.rb:28:in `map'
    from /Users/jhephoi/.rvm/gems/ruby-2.0.0-p195@parma/gems/schema_to_scaffold-0.4.2/lib/schema_to_scaffold/schema.rb:28:in `parse'
    from /Users/jhephoi/.rvm/gems/ruby-2.0.0-p195@parma/gems/schema_to_scaffold-0.4.2/lib/schema_to_scaffold/schema.rb:7:in `initialize'
    from /Users/jhephoi/.rvm/gems/ruby-2.0.0-p195@parma/gems/schema_to_scaffold-0.4.2/bin/scaffold:30:in `new'
    from /Users/jhephoi/.rvm/gems/ruby-2.0.0-p195@parma/gems/schema_to_scaffold-0.4.2/bin/scaffold:30:in `<top (required)>'
    from /Users/jhephoi/.rvm/gems/ruby-2.0.0-p195@parma/bin/scaffold:23:in `load'
    from /Users/jhephoi/.rvm/gems/ruby-2.0.0-p195@parma/bin/scaffold:23:in `<main>'
    from /Users/jhephoi/.rvm/gems/ruby-2.0.0-p195@parma/bin/ruby_noexec_wrapper:14:in `eval'
    from /Users/jhephoi/.rvm/gems/ruby-2.0.0-p195@parma/bin/ruby_noexec_wrapper:14:in `<main>'

Multiple Tables won't -c correctly in Windows

Hello,

I've recently started developing with Ruby in several environments, one of which is Windows.

I've found a bug (or at least something that might need better documentation) where when trying to scaffold multiple tables with the -c clipboard argument, only the first line of output is processed in:

   when /mingw/i  then exec("echo '#{output}' | clip")

Only the first line of output ends up in the exec

Sample Output:

Select a table: *

Script for scaffold:

rails generate scaffold Client name:string website:string contact_name:string notes:string contact_email:string contact_phone:string --no-migration

rails generate scaffold TargetLocation name:string address:string city:string state:string zip:string cbsa_code:string cbsa_name:string latitude:decimal longitude:decimal target:references --no-migration

rails generate scaffold Target name:string website_url:string multi_location:boolean input_notes:string --no-migration


(copied to your clipboard)
'rails generate scaffold Client name:string website:string contact_name:string notes:string contact_email:string contact_phone:string --no-migration

And it just ends there. Nothing gets sent to | clip. Thankfully, because of the ' encapsulation, that single generate statement doesn't actually exec(), even though it never made it to | clip.

Obviously at hand, is how inversatile Windows CMD is, and what to do with the newline characters. I assume if I were running CYGWIN or something, it might have been handled better. Perhaps a better default behaviour for Windows multiline output is to return a format separated by some other string token that can be manually replaced later.

If I am able to come up with a solution, I'll submit the code.

Of note, from within the RubyMine terminal emulator (in Windows) I executed ruby scaffold -c -p /path/to/db and it worked as expected. So, again, I think this is due to the inversatility of cmd.exe. Perhaps this should just be in the documentation, similar to the dependency for xclip.

Windows 7 x64
Ruby 2.2.0
x64-mingw32

in `require': cannot load such file -- pry (LoadError)

Great idea for a gem by the way, just a little problem:
Installed schema_to_scaffold, using Rails v 4.1.7 and after running scaffold console outputs:

/home/jordan/.rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121:in require': cannot load such file -- pry (LoadError) from /home/jordan/.rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121:in require'

Non interactive use

Thanks for the project. It might be nice to add a non interactive option or maybe just an output path. I was able to bash something together with:

✔ ~/Development/foo [master L|✔] $ echo '*' | scaffold -c -p db/schema.rb  > tmp/foo.sh
/home/kyle/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/schema_to_scaffold-0.8.0/lib/schema_to_scaffold/schema.rb:31: warning: constant ::Fixnum is deprecated
✔ ~/Development/foo [master L|✔] $ head tmp/foo.sh 
Loaded tables:
0. active_guests
1. active_users
...

✔ ~/Development/foo [master L|✔] $ tail tmp/foo.sh 
rails generate scaffold StatusVersion opportunity:references status:string latest:boolean --no-migration
...

This seemed to work, but also included the front matter menu ( which is easy enough to remove by hand).

unexpected token `<'`

This gem seems like A good idea to help people like me learn how to make forms and controllers for joined tables, but it's not working
$ rails generate scaffold <todo> <title[:string]> -bash: syntax error near unexpected token <'`

Is this project forgotten about or did I miss an install script?

Bad table names with uuid ids

Rails 4 supports uuid ids in Postgres. schema.rb looks like:

  create_table "doctors", id: :uuid, default: "uuid_generate_v4()", force: true do |t|
    t.string "identifier", null: false
    t.string "company"
    t.string "name"
    t.string "street1"
    t.string "street2"
    t.string "city"
    t.string "state"
    t.string "zip"
    t.string "phone"
    t.string "fax"
    t.string "contact"
  end

You need to make the table name regex non-greedy to avoid capturing an extra ", like this: 40. doctors", id: :uuid, default: "uuid_generate_v4()

I have a pull request for you.

undefined method `to_script' for nil:NilClass

Description

Hullo.

I'm seeing the error below when running scaffold. It occurs when being run for single, or multiple tables, so it's similar to, but different from #14.

undefined method `to_script' for nil:NilClass

    attributes_list = attributes.map(&:to_script).reject { |x| x.nil? || x.empty? }.join(' ')
                                ^^^^

Table

#<SchemaToScaffold::Table:0x0000000101dcbb38 @name="competitions", @attributes=[#<SchemaToScaffold::Attribute:0x0000000101f208a8 @name="opta", @type="references">, #<SchemaToScaffold::Attribute:0x0000000101f20678 @name="perform", @type="references">, #<SchemaToScaffold::Attribute:0x0000000101f20498 @name="perform_code", @type="string">, #<SchemaToScaffold::Attribute:0x0000000101f202b8 @name="name", @type="string">, #<SchemaToScaffold::Attribute:0x0000000101f200d8 @name="created_at", @type="timestamptz">, #<SchemaToScaffold::Attribute:0x0000000101dcbd68 @name="updated_at", @type="timestamptz">, nil, nil]>

Cause

I've tracked the issue down to these two lines in my schema.rb file:

t.unique_constraint ["opta_id"], name: "competitions_opta_id_key"
t.unique_constraint ["perform_id"], name: "competitions_perform_id_key"

Once I delete those I'm able to run scaffold without issue.

Context

Ruby: 3.1.2
Rails: 7.1.2
OS: MacOS 12.6.3
Scaffold: 0.8.2

Unable to open file

brybtg@DESKTOP-LDQ86BQ:/mnt/d/iPlay-Devs/webpanel-project/fivempanel$ scaffold -c -p ~/fivempanel/db/schema.rb

Unable to open file '/home/brybtg/fivempanel/db/schema.rb'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.