Rails 3 generators for scaffolding models according to selected ORM - how?

I have installed Mongo DB and created my first Rails 3 model generator as a gem, which can create Mongo Mapper models in a Rails 3 project :slight_smile:

See http://github.com/kristianmandrup/mongo_model_r3

Also available from gemcutter as mongo_model 0.2.1.

I am now trying to create a scaffold generator using this same model....

In my Rails 3 app:

config/application.rb     config.generators do |g|        g.orm :active_record        g.template_engine :erb        g.test_framework :test_unit, :fixture => true     end

So it works as long as I only specify controller/model and attributes

Here I set g.orm :mongo_model

But where do I map this orm symbol to the correct model generator I wonder?

$ script/generate scaffold Person name:string        error mongo_model [not found]        route resources :people       invoke scaffold_controller       create app/controllers/people_controller.rb       invoke erb ...

So it works as long as I only specify controller/model and attributes

Here I set g.orm :mongo_model

But where do I map this orm symbol to the correct model generator I

wonder?

$ script/generate scaffold Person name:string

   error  mongo_model [not found]

   route  resources :people

  invoke  scaffold_controller

  create    app/controllers/people_controller.rb

  invoke    erb

…

Just saw your repository and I think you should put the generator files in a directory called ‚Äúgenerators" or "rails_generators‚ÄĚ.

And have a look at http://github.com/snusnu/rails3_datamapper for datamapper but I am sure you will get a good idea of what needs to be done.

Anuj

Hi Anuj Dutta,

Yes, I found rails3_datamapper by snusnu previously and had a look at it since I also thought I could get some inspiration from it. I think the repository you mention is an old one, my mongo_model_m3 repo works just fine, and I have a *generators* directory!

When I try to run scaffold on the rails3_datamapper application I get an error: kristian-mandrups-macbook-pro:datamapper_on_rails3 kristianconsult$ script/generate scaffold Post /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/ vendor/gems/ruby/1.9.1/dirs/rails/activesupport/lib/active_support/ dependencies.rb:601:in `to_constant_name': Anonymous modules have no name to be referenced by (ArgumentError)   from /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/ vendor/gems/ruby/1.9.1/dirs/rails/activesupport/lib/active_support/ dependencies.rb:407:in `qualified_name_for'   from /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/ vendor/gems/ruby/1.9.1/dirs/rails/activesupport/lib/active_support/ dependencies.rb:115:in `rescue in const_missing'   from /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/ vendor/gems/ruby/1.9.1/dirs/rails/activesupport/lib/active_support/ dependencies.rb:105:in `const_missing' ...

Hi Anuj Dutta,

Yes, I found rails3_datamapper by snusnu previously and had a look at

it since I also thought I could get some inspiration from it.

I think the repository you mention is an old one, my mongo_model_m3

repo works just fine, and I have a generators directory!

When I try to run scaffold on the rails3_datamapper application I get

an error:

kristian-mandrups-macbook-pro:datamapper_on_rails3 kristianconsult$

script/generate scaffold Post

/Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/

vendor/gems/ruby/1.9.1/dirs/rails/activesupport/lib/active_support/

dependencies.rb:601:in `to_constant_name’: Anonymous modules have no

name to be referenced by (ArgumentError)

    from /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/

vendor/gems/ruby/1.9.1/dirs/rails/activesupport/lib/active_support/

dependencies.rb:407:in `qualified_name_for’

    from /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/

vendor/gems/ruby/1.9.1/dirs/rails/activesupport/lib/active_support/

dependencies.rb:115:in `rescue in const_missing’

    from /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/

vendor/gems/ruby/1.9.1/dirs/rails/activesupport/lib/active_support/

dependencies.rb:105:in `const_missing’

…

‚Äď

Looking inside rails3_datamapper-0.10.2 data_mapper.rb

module DataMapper

module Generators

class Base < ::Rails::Generators::NamedBase #:nodoc:

…

end

I notice the convention is to have a module DataMapper::Generators as

a wrapper for the generator class. Is this important?

Any other required conventions to have an ORM generator binding

correctly ‚Äúpicked up‚ÄĚ by rails 3?

Kristian

Hello Kristian,

Sorry, I did not know you were doing that already. Right, so can you point me to your repository please and I will run it on my system and debug it.

Thanks

Anuj

Anuj and Kristian,

First of all, I'd like to point out that the canonical repo for rails3_datamapper is http://github.com/dkubb/rails3_datamapper .. mine is just a fork, altho i'm the primary committer to dkubb's repo. Just wanted to get that out of the way, since gems will always be built from dkubb's repo.

@Kristian From what I can tell, the namespace *is* important, in that it at least will be picked up when listing all available generators with ./script/generate. Putting the generators in the DataMapper namespace will list them below the data_mapper section.

Hi Martin and Anuj,

Thanks for the tip! I found your repo previously, but this is the pure gem. I guess I would have to integrate it with the demo rails app or maybe @Anuj could update his repo? I will upload my current repo ASAP so you can have a look :wink:

Thanks!

Kris

Here is my repo :wink: Thanks!

http://github.com/kristianmandrup/datamapper_rails3_demo

Here is my repo :wink: Thanks!

http://github.com/kristianmandrup/datamapper_rails3_demo

I will see if I can spot the issue. FYI, you do not need to check the actual gem directories in just the .gem files (in the cache).

Anuj

Here is my repo :wink: Thanks!

http://github.com/kristianmandrup/datamapper_rails3_demo

Hello,

I just created a new Rails3 app and used your gemfile and scaffold data_mapper works fine for me. How are you creating your rails3 project?

Anuj

Anuj and Kristian,

I just tried

./script/generate scaffold Profile nick:string

and it worked just fine. I probably also forgot to mention that i have a sample app showing datamapper working with rails3 too.

http://github.com/snusnu/datamapper_on_rails3

cheers snusnu

Anuj and Kristian,

I just tried

./script/generate scaffold Profile nick:string

and it worked just fine. I probably also forgot to mention that i have

a sample app showing datamapper working with rails3 too.

http://github.com/snusnu/datamapper_on_rails3

cheers

snusnu

It was working fine for me too. Thanks, anyways.

Anuj

Kristian,

Hmm, I dunno what I did to github, but currently it doesn't like to display my README.rdoc for whatever reason. The funny thing is, that it still displays the file when visiting the grandparent of the latest commit at http://github.com/snusnu/datamapper_on_rails3/tree/8a4fda1afddf014ab0d4a4f062974b8cb0ba272a

Should this problem persist, clone the repo and make sure you actually READ the README as there are important instructions on how to get the app to actually boot.

cheers snusnu

I never managed to get the datamapper_on_rails3 app to work. I tried to follow the instructions in the readme. I always get this error when I try a scaffold inside the app: kristian-mandrups-macbook-pro:datamapper_on_rails3 kristianconsult$ script/generate scaffold Profile nick:string /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/ vendor/gems/ruby/1.9.1/dirs/rails/activesupport/lib/active_support/ dependencies.rb:601:in `to_constant_name': Anonymous modules have no name to be referenced by (ArgumentError)   from /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/ vendor/gems/ruby/1.9.1/dirs/rails/activesupport/lib/active_support/ dependencies.rb:407:in `qualified_name_for'   from /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/ vendor/gems/ruby/1.9.1/dirs/rails/activesupport/lib/active_support/ dependencies.rb:115:in `rescue in const_missing'   from /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/ vendor/gems/ruby/1.9.1/dirs/rails/activesupport/lib/active_support/ dependencies.rb:105:in `const_missing'   from /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/ vendor/gems/ruby/1.9.1/dirs/rails3_datamapper/lib/rails3_datamapper/ railtie.rb:62:in `block in <class:Railtie>'   from /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/ vendor/gems/ruby/1.9.1/dirs/rails/railties/lib/rails/initializable.rb: 23:in `instance_exec'   from /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/ vendor/gems/ruby/1.9.1/dirs/rails/railties/lib/rails/initializable.rb: 23:in `run'   from /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/ vendor/gems/ruby/1.9.1/dirs/rails/railties/lib/rails/initializable.rb: 61:in `block in run_initializers'   from /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/ vendor/gems/ruby/1.9.1/dirs/rails/railties/lib/rails/initializable.rb: 60:in `each'   from /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/ vendor/gems/ruby/1.9.1/dirs/rails/railties/lib/rails/initializable.rb: 60:in `run_initializers'   from /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/ vendor/gems/ruby/1.9.1/dirs/rails/railties/lib/rails/application.rb: 63:in `initialize!'   from /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/ vendor/gems/ruby/1.9.1/dirs/rails/railties/lib/rails/application.rb: 37:in `method_missing'   from /Users/kristianconsult/my_rails/rails3_apps/datamapper_on_rails3/ config/environment.rb:5:in `<top (required)>'   from script/generate:2:in `require'   from script/generate:2:in `<main>' ..

I created a Gist to be used for installing the app as described in the readme:

$ curl -s http://gist.github.com/288787.txt | bash

But never mind... I got my Mongo DB generators to work just fine now :slight_smile:

Any idea on how to create a Thor task for creating a complete rails app (using a generator) to override the default rails generator?

Kristian,

Which Ruby version are you on?

ruby --version

You need at least 1.8.7 or higher. If you're using 1.8.6, this might be the cause of the issues (Rails 3 drops support for it).

Regards Kieran

Thanks to everyone!!!

I have been working a lot on this since this post and I have figured it all out "with a little help from some friend". I'm on Ruby 1.9.1, Rails 3.0 pre. Been creating a lot of nice new Rails 3 generators, currently working on Rails 3 templates!!!

I have created a gem "txt_file_mutator" which could be helpful in combination with Rails 3 templates I think :wink: I also created a bare-bones Rails 3 application generator, which is much easier to build on, instead of having to remove the index file, README, prototype js files etc. which is often the case...

Looking forward to the next official release of Rails 3 :slight_smile:

Cheers!