View Mapper will generate scaffolding illustrating how to write view code using a specified plugin or feature with your existing models. It can also generate new models.
A couple simple examples:
script/generate view_for office --view auto_complete:address
… will generate Rails scaffolding code that displays a form for an existing “office” model, with auto complete on the “address” field.
script/generate scaffold_for_view office address:string code:string
--view auto_complete:address
… will generate the same form, but also create the “office” model class file, a migration file containing the “address” and “code” columns, and other standard scaffolding files as well.
The idea behind View Mapper is that it’s easy to write simple, concise model classes representing your domain objects using ActiveRecord, but very hard to implement the corresponding views using a combination of HTML, Javascript Rails helper functions, routes, controllers, etc. If you’re not very familiar with a certain plugin you want to use in your app, View Mapper can help you get started in the right direction by generating a working example with scaffolding code.
If you’re developing a Rails plugin or gem it’s easy to write your own View Mapper module for your plugin’s users to call with View Mapper.
Code: http://github.com/patshaughnessy/view_mapper
Install:
sudo gem install view_mapper
Usage:
Two generators are provided, called view_for and scaffold_for_view:
script/generate view_for model [ --view view_name:param ]
This will generate the specified view code for an existing model. The view_for generator will look for your model, inspect all of its columns and then generate standard Rails scaffolding containing a form field for each existing column.
If you also specify a view, then a custom view will be created using the specified Rails feature or plugin, using the specified parameter.
script/generate scaffold_for_view model attributes [ --view view_name:param ]
If you don’t specify a view, then this command is identical to the standard Rails scaffold generator.
If you do specify a view, then the entire working set of a model, views and controller will be generated to implement the specified Rails feature or plugin, using the specified parameter.
Views:
Right now, I’ve implemented eight views:
- auto_complete: Uses the standard Rails auto_complete plugin to implement type ahead behavior for the specified field.
- paperclip: Uses the Paperclip plugin to upload and download file attachments.
- has_many: Displays a complex form to edit two or more associated models.
- has_many_auto_complete: This is the same as has_many but also uses the auto_complete plugin to implement type ahead behavior for each text field. This view requires you to install my fork of the Rails auto_complete plugin.
- belongs_to: Generates scaffolding that allows you to select an existing, associated model.
- belongs_to_auto_complete: Generates scaffolding that allows you to select an existing, associated model using auto_complete.
- has_many_existing: Generates scaffolding for a complex form to edit two models that have a has_many, :through association with a third model. Use this if you have a many-many relationship with existing data.
- (Default) If no view is specified, then standard Rails scaffold code will be generated.
I’ll be implementing more views in the coming weeks and months. There is also an API for implementing your own View Mapper module, for example to generate code illustrating how to use a plugin or gem you are working on. In the future I’ll document this as well.
7 responses so far ↓
1 Chris Bloom // Oct 08, 2009 at 09:19 PM
2 Mark Thomas // Dec 11, 2009 at 08:37 AM
3 pat // Dec 11, 2009 at 05:37 PM
That’s a great idea; I’m not familiar with Ryan’s generators but I just took a quick look at his project on github and it looks very interesting. It should be straightforward to add a module to my gem called “ViewMapper::NiftyView” which would subclass or override the templates from the nifty_scaffold generator. This would enable you to use a command like this:
Would this be close enough to what you’re looking for? I’m not sure I like the idea of changing the default; to me it seems to make sense to have the default view match the out-of-the-box behavior of Rails.
4 Jim // Apr 30, 2010 at 08:24 AM
Pat, this is fantastic! Thank you for the work and the write-ups. I’m a little confused by the present state of things, in that viewmapper is looking for autocomplete, but it looks like the current gem is repeatedautocomplete (I haven’t found autocomplete in any repository). I’ll dig into the viewmapper code and compare against the repeatedautocomplete api to see if they’re compatible such that changing the test that is looking for autocomplete can merely be adjusted to look for repeatedauto_complete. In the meantime, if you have insight….
5 Jim // Apr 30, 2010 at 08:41 AM
Okay, that was dumb. I forgot to add the config.gem statement in Rails::Initializer (which you cleverly instruct users to do in the README.doc on github).
But I had a fun read through your code… it’s beautifully written and therefore a good lesson is writing well-factored code!
6 pat // Apr 30, 2010 at 11:02 AM
Hi Jim, Wow - thanks a lot for the kind words about my code :) Here’s some explanation around auto_complete:
The auto_complete view will work with the standard auto_complete plugin. This just creates standard Rails scaffolding but with autocomplete on all of the text fields. To use this, you can just install the standard Rails auto_complete plugin using script/plugin install etc…
But the has_many_auto_complete view requires my fork of the standard auto_complete plugin, which I also packaged up as the repeated_auto_complete gem in case you prefer to install using RubyGems. The reason for this is that the has_many_auto_complete scaffolding produces multiple autocompleting text fields on the same form, which isn’t supported by the standard auto_complete plugin. In fact, I included the code for this view (the ViewMapper::HasManyAutoCompleteView module) inside my version of the auto_complete plugin, to avoid making this view even available if you didn’t have repeated_auto_complete.
7 Matt Slay // Jun 02, 2010 at 12:52 AM
Well, I’m a total Ruby/Rails newbie here, but guess what?… I was able to make this work in just a few minutes reading and a few clicks! Awesome. Just awesome.
When I was first hearing/learning about Rails, I thought the “belongs_to” statement in the model would just automatically play out into the generated views, but (as you all know) it does not.
But this tool does it! Awesome.
Leave a Comment