Giter Club home page Giter Club logo

reactive_record's Introduction

Reactive record logo
Logo design: Kelly Rauwerdink, @missingdink, Dribbble.

Gem Version

Generates ActiveRecord models to fit a pre-existing Postgres database. Now you can use Rails with the db schema you always wanted. It's your convention over configuration.

Why?

  1. Your app is specific to Postgres and proud of it. You use the mature declarative data validation that only a real database can provide.
  2. You have inherited a database or are more comfortable creating one yourself.
  3. You're a grown-ass DBA who doesn't want to speak ORM baby-talk.

Features

  • Fully automatic. It just works.
  • Creates a model for every table.
  • Creates a comprehensive initial migration.
  • Declares key-, uniqueness-, and presence-constraints.
  • Creates associations.
  • Adds custom validation methods for CHECK constraints.

Usage

Already familiar with Rails?

  • Set up a postgres db normally
  • Set config.active_record.schema_format = :sql to use a SQL schema.rb
  • After you have migrated up a table, use rails generate reactive_record:install
  • Go examine your generated models

Want more details?

First Include the reactive_record gem in your project's Gemfile. Oh by the way, you'll have to use postgres in your project. Setting up Rails for use with postgres is a bit outside the scope of this document. Please see [Configuring a Database] (http://guides.rubyonrails.org/configuring.html#configuring-a-database) for what you need to do.

gem 'reactive_record'

Bundle to include the library

$ bundle

Next Tell ActiveRecord to go into beast-mode. Edit your config/application.rb, adding this line to use sql as the schema format:

module YourApp
  class Application < Rails::Application
    # other configuration bric-a-brac...
    config.active_record.schema_format = :sql
  end
end

Next Create the database(s) just like you normally would:

rake db:create

Next Generate a migration that will create the initial table:

$ rails generate migration create_employees

Use your SQL powers to craft some DDL, perhaps the "Hello, World!" of DB applications, employees?

class CreateEmployees < ActiveRecord::Migration
  def up
    execute <<-SQL
      CREATE TABLE employees (
        id         SERIAL,
        name       VARCHAR(255) NOT NULL,
        email      VARCHAR(255) NOT NULL UNIQUE,
        start_date DATE NOT NULL,

        PRIMARY KEY (id),
        CONSTRAINT company_email CHECK (email LIKE '%@example.com')
      );
    SQL
  end

  def down
    drop_table :employees
  end
end

Lastly Deploy the reactive_record generator:

$ rails generate reactive_record:install

Go look at the generated file:

class Employees < ActiveRecord::Base
  set_table_name 'employees'
  set_primary_key :id
  validate :id, :name, :email, :start_date, presence: true
  validate :email, uniqueness: true
  validate { errors.add(:email, "Expected TODO") unless email =~ /.*@example.com/ }
end

Reactive record does not currently attempt to generate any kind of reasonable error message (I'm working on it) :)

Enjoy

Credits

Firstly, thank you, contributors!

Also a special thanks to Joe Nelson, @begriffs, for contributions and inspiration; Reactive Record would not exist without his efforts. Thanks to Bendyworks for the 20% time to work on this project!

And, of course, a huge thank you to Kelly Rauwerdink for her amazing ability to make "an art" even when all I can do is sorta half-articulate what I'm talking about. Thanks!

Footer

reactive_record's People

Contributors

twopoint718 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

reactive_record's Issues

Missing parser gem dependency

After adding reactive_record to my Gemfile I had this error:

$ rails server
/home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/reactive_record-0.0.1/lib/reactive_record.rb:5:in `require': cannot load such file -- parser (LoadError)
    from /home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/reactive_record-0.0.1/lib/reactive_record.rb:5:in `<top (required)>'
....

I just added gem 'parser' to my Gemfile to fix it

parser.rb missing from lib

Just installed v0.0.1 of the gem, and ran

$ rails generate reactive_record:install

which resulted in:

/home/rich/.gem/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `require': cannot load such file -- parser (LoadError)
    from /home/rich/.gem/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `block in require'
    from /home/rich/.gem/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /home/rich/.gem/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `require'
    from /home/rich/.gem/ruby/1.9.1/gems/reactive_record-0.0.1/lib/reactive_record.rb:5:in `<top (required)>'
    from /home/rich/.gem/ruby/1.9.1/gems/bundler-1.2.3/lib/bundler/runtime.rb:68:in `require'
    from /home/rich/.gem/ruby/1.9.1/gems/bundler-1.2.3/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
    from /home/rich/.gem/ruby/1.9.1/gems/bundler-1.2.3/lib/bundler/runtime.rb:66:in `each'
    from /home/rich/.gem/ruby/1.9.1/gems/bundler-1.2.3/lib/bundler/runtime.rb:66:in `block in require'
    from /home/rich/.gem/ruby/1.9.1/gems/bundler-1.2.3/lib/bundler/runtime.rb:55:in `each'
    from /home/rich/.gem/ruby/1.9.1/gems/bundler-1.2.3/lib/bundler/runtime.rb:55:in `require'
    from /home/rich/.gem/ruby/1.9.1/gems/bundler-1.2.3/lib/bundler.rb:128:in `require'
    from /home/rich/Projects/inpharmics/config/application.rb:7:in `<top (required)>'
    from /home/rich/.gem/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:24:in `require'
    from /home/rich/.gem/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:24:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

looking at ~/.gem/ruby/1.9.1/gems/reactive_record-0.0.1/lib I only see these files:

.
├── code_generator.rb
├── generators
│   └── reactive_record
│       └── install_generator.rb
├── lexer.rb
├── parser.y
├── reactive_record
│   └── version.rb
└── reactive_record.rb

3 directories, 6 files

Thanks!

uninitialized constant ReactiveRecord::ConstraintParser (NameError), Rails 4

While running: rails generate reactive_record:install

it gives me this error:

      create  app/models/cmn_application_preferences.rb
      create  app/models/cmn_event_trackings.rb
      create  app/models/cmn_number_of_results_per_pages.rb
/home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/reactive_record-0.0.1/lib/reactive_record.rb:68:in `block in parse_constraint': uninitialized constant ReactiveRecord::ConstraintParser (NameError)
    from /home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/reactive_record-0.0.1/lib/reactive_record.rb:60:in `map'
    from /home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/reactive_record-0.0.1/lib/reactive_record.rb:60:in `generate_constraints'
    from /home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/reactive_record-0.0.1/lib/reactive_record.rb:18:in `model_definition'
    from /home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/reactive_record-0.0.1/lib/generators/reactive_record/install_generator.rb:16:in `block in create_models'
    from /home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/reactive_record-0.0.1/lib/generators/reactive_record/install_generator.rb:14:in `each'
    from /home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/reactive_record-0.0.1/lib/generators/reactive_record/install_generator.rb:14:in `create_models'
    from /home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/thor-0.18.1/lib/thor/command.rb:27:in `run'
    from /home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/thor-0.18.1/lib/thor/invocation.rb:120:in `invoke_command'
    from /home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/thor-0.18.1/lib/thor/invocation.rb:127:in `block in invoke_all'
    from /home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/thor-0.18.1/lib/thor/invocation.rb:127:in `each'
    from /home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/thor-0.18.1/lib/thor/invocation.rb:127:in `map'
    from /home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/thor-0.18.1/lib/thor/invocation.rb:127:in `invoke_all'
    from /home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/thor-0.18.1/lib/thor/group.rb:233:in `dispatch'
    from /home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/thor-0.18.1/lib/thor/base.rb:439:in `start'
    from /home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/generators.rb:171:in `invoke'
    from /home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/generate.rb:12:in `<top (required)>'
    from /home/user/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:29:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Did I miss a configuration option?

Fix shift/reduce conflicts

code/reactive_record(master)✓ rake
racc -l -o lib/parser.rb lib/parser.y
9 shift/reduce conflicts
/Users/chris/.rvm/rubies/ruby-1.9.3-p448/bin/ruby -S rspec ./spec/lexer_spec.rb ./spec/parser_spec.rb ./spec/reactive_record_spec.rb
........................

Finished in 0.45743 seconds
24 examples, 0 failures

Add foreign key constraints

Turn them into associations
• belongs_to
• has_one
• has_many
• has_and_belongs_to_many

Distinguishing foreign keys that were intended as has_one vs has_many might be tricky.

CHECK ((fees_discount >= (0)::numeric)) generates Unknown type: #<IdentNode:0xaaa> (RuntimeError)

I have this error after running: rails generate reactive_record:install

/home/user/.gem/ruby/2.0.0/gems/reactive_record-0.0.2/lib/code_generator.rb:82:in `initialize': Unknown type: #<IdentNode:0x007f4dadb40da0> (RuntimeError)
    from /home/user/.gem/ruby/2.0.0/gems/reactive_record-0.0.2/lib/parser.rb:251:in `new'
    from /home/user/.gem/ruby/2.0.0/gems/reactive_record-0.0.2/lib/parser.rb:251:in `_reduce_8'
    from /home/user/.rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/racc/parser.rb:258:in `_racc_do_parse_c'
    from /home/user/.rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/racc/parser.rb:258:in `do_parse'
    from /home/user/.gem/ruby/2.0.0/gems/reactive_record-0.0.2/lib/parser.rb:25:in `parse'
    from /home/user/.gem/ruby/2.0.0/gems/reactive_record-0.0.2/lib/reactive_record.rb:70:in `block in parse_constraint'
    from /home/user/.gem/ruby/2.0.0/gems/reactive_record-0.0.2/lib/reactive_record.rb:62:in `map'
    from /home/user/.gem/ruby/2.0.0/gems/reactive_record-0.0.2/lib/reactive_record.rb:62:in `generate_constraints'
    from /home/user/.gem/ruby/2.0.0/gems/reactive_record-0.0.2/lib/reactive_record.rb:18:in `model_definition'
    from /home/user/.gem/ruby/2.0.0/gems/reactive_record-0.0.2/lib/generators/reactive_record/install_generator.rb:16:in `block in create_models'
    from /home/user/.gem/ruby/2.0.0/gems/reactive_record-0.0.2/lib/generators/reactive_record/install_generator.rb:14:in `each'
    from /home/user/.gem/ruby/2.0.0/gems/reactive_record-0.0.2/lib/generators/reactive_record/install_generator.rb:14:in `create_models'
    from /home/user/.gem/ruby/2.0.0/gems/thor-0.18.1/lib/thor/command.rb:27:in `run'
    from /home/user/.gem/ruby/2.0.0/gems/thor-0.18.1/lib/thor/invocation.rb:120:in `invoke_command'
    from /home/user/.gem/ruby/2.0.0/gems/thor-0.18.1/lib/thor/invocation.rb:127:in `block in invoke_all'
    from /home/user/.gem/ruby/2.0.0/gems/thor-0.18.1/lib/thor/invocation.rb:127:in `each'
    from /home/user/.gem/ruby/2.0.0/gems/thor-0.18.1/lib/thor/invocation.rb:127:in `map'
    from /home/user/.gem/ruby/2.0.0/gems/thor-0.18.1/lib/thor/invocation.rb:127:in `invoke_all'
    from /home/user/.gem/ruby/2.0.0/gems/thor-0.18.1/lib/thor/group.rb:233:in `dispatch'
    from /home/user/.gem/ruby/2.0.0/gems/thor-0.18.1/lib/thor/base.rb:439:in `start'
    from /home/user/.gem/ruby/2.0.0/gems/railties-4.0.0/lib/rails/generators.rb:156:in `invoke'
    from /home/user/.gem/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands/generate.rb:11:in `<top (required)>'
    from /home/user/.gem/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
    from /home/user/.gem/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `block in require'
    from /home/user/.gem/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
    from /home/user/.gem/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
    from /home/user/.gem/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:49:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

Could not find generator reactive_record:install.

Pulled in latest version of gem with:

  gem 'reactive_record', "0.0.2", :git => 'git://github.com/alexdunae/w3c_validators.git'

Then got:

$ rails g reactive_record:install
Could not find generator reactive_record:install.

I looked through the RR source and it wasn't obvious to me what was wrong.

parse error on value "AND" (IDENT) , ruby 2.0.0-p247

I have this error after running: rails generate reactive_record:install

/home/user/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/racc/parser.rb:529:in `on_error':  (Racc::ParseError)
parse error on value "AND" (IDENT)
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/racc/parser.rb:258:in `_racc_do_parse_c'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/racc/parser.rb:258:in `do_parse'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/reactive_record-0.0.1/lib/parser.rb:25:in `parse'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/reactive_record-0.0.1/lib/reactive_record.rb:69:in `block in parse_constraint'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/reactive_record-0.0.1/lib/reactive_record.rb:62:in `map'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/reactive_record-0.0.1/lib/reactive_record.rb:62:in `generate_constraints'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/reactive_record-0.0.1/lib/reactive_record.rb:18:in `model_definition'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/reactive_record-0.0.1/lib/generators/reactive_record/install_generator.rb:16:in `block in create_models'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/reactive_record-0.0.1/lib/generators/reactive_record/install_generator.rb:14:in `each'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/reactive_record-0.0.1/lib/generators/reactive_record/install_generator.rb:14:in `create_models'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/thor-0.18.1/lib/thor/command.rb:27:in `run'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/thor-0.18.1/lib/thor/invocation.rb:120:in `invoke_command'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/thor-0.18.1/lib/thor/invocation.rb:127:in `block in invoke_all'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/thor-0.18.1/lib/thor/invocation.rb:127:in `each'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/thor-0.18.1/lib/thor/invocation.rb:127:in `map'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/thor-0.18.1/lib/thor/invocation.rb:127:in `invoke_all'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/thor-0.18.1/lib/thor/group.rb:233:in `dispatch'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/thor-0.18.1/lib/thor/base.rb:439:in `start'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/generators.rb:156:in `invoke'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands/generate.rb:11:in `<top (required)>'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `block in require'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
    from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:49:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

The constraint is: CHECK (((port >= 0) AND (port <= 65635)))

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.