new scaffold generator with some default files - ruby-on-rails

I am making a new rails scaffold generator to replace the default one, most of the files remain the same, only some are different. So how do I invoke some of the default scaffold files within my generator?
Also, is there a way I can checkout the source code of the original scaffold generator?

The source code is in the railties section of the project on github. To gain access to the scaffold templates, I would just invoke the actual scaffold generator and then gsub the files if you need to make changes.

Related

rails generate scaffold but prevent the model file from being generated

I have looked over the documentation at rails generate scaffold -h but am still stumped. I want to create all the files from the scaffold generator except the model file.
Example:
rails generate scaffold User --no-model
This as it is does not work but what I would like to happen is that the user.rb file is prevented from being created within the models directory.
Figured it out! You just need to include --skip to tell rails that for any conflicting files to just ignore creating those files.
Example: say I already have the file user.rb inside my models directory:
rails generate scaffold User --skip
With the --skip option rails knows to skip creating the user.rb file, as well as all other already existing files that the scaffold command would generate for this resource.

How to create a custom scaffold generator in Rails 3?

There are these railscasts.
http://railscasts.com/episodes/218-making-generators-in-rails-3 With this one you find out how
to create a stylesheets and scaffold generator.
http://railscasts.com/episodes/216-generators-in-rails-3 With this one you find out how to add some files to modify the scaffolding views.
I want to do a mix of the two. I would like to create a generator that also creates scaffolding views. Kinda like Ryan Bates nifty generators or web_app_theme gem (https://github.com/pilu/web-app-theme). I have been searching for a tutorial or some information to point me in the right direction but I can't find exactly what I'm looking for.
I know I'm close. I already know how to create a generator with Railcast 218 but now, how can I make it create view files too?
I would like to run a command like this...
rails g my_scaffold_generator Post title:string body:text
This may well be too late to help, but as I found this while Googling for the same info...
It seems to me that the best approach, at least for learning the ropes, is to duplicate and then alter the existing scaffold generator.
So the first thing that tripped me up is finding the default templates, which do not live in your rails-3.2.0 directory (or whatever version you are on), but in railties-3.2.0. So for my RVM-based installation they were at:
/Users/leo/.rvm/gems/ruby-1.9.3-p194#gemset/gems/railties-3.2.0/lib/rails/generators/
[Note: your gems directory could be somewhere else entirely, use $> gem environment to find your gem paths]
In here is erb/scaffold/templates/ which has the files you'd expect (new.html.erb, _form.html.erb etc).
You can copy these files to your app's root, into lib/templates/erb/scaffold/ and they will be used instead of the default ones.
If you want to use these in a custom generator, there are two approaches:
1) Use hook_for to call the regular ERB scaffold generator from your generator.
2) Move/process the templates inside your own custom generator, using copy_file and similar methods in Thor to move them into place.
There is a decent Rails Guide on this, although I found I didn't really get it until I started digging around in .../railties-3.2.0/lib/rails/generators/... and looking at how the defaults are structured.

can i "freeze the code" of activescaffold or rails_admin in rails?

i want to generate a complex scaffold and then remove the gems
is there a way to freeze the code that rails_admin or activescaffold generates so i can edit it myself ? (similar to how rails scaffold does it)
is there another gem that generates a more complex scaffold?
In active scaffold to alter a scaffold's views you use overrides.
This depends on what version of Rails you're running. If >2.1, you can specify gems explicitly in environment.rb using config.gem, and then run rake gems:unpack to freeze those gems into the vendor/gems folder. For >=3.0 use the Builder tool to freeze the gem.
If <= 2.1, then you could do the above step manually -- copy all of the ActiveScaffold gem code into a folder in vendor/plugins, and remove the gem itself. See earlier plugin-based versions of ActiveScaffold for guidance.
You can also do this only as needed. To customize views, create an app/views/active_scaffold_overrides folder, and copy any ActiveScaffold partials to customize there. They will automatically be used across your app -- no need to duplicate them into each view. To customize controller actions, create a controller named ActiveScaffold, and then have all other scaffold controllers inherit from this new ActiveScaffoldController. Now you have somewhere to override the actions themselves, and you can override helpers in the generated ActiveScaffoldHelper file too.

Ruby on Rails add models for existing plugin?

I am new to Ruby On Rails, and currently trying to modify an existing (uncomplete) plugin from github. Things went smoothly until I am trying to add new models to this plugin.
I know script/generate model, and script/generate plugin. But how to add models into a plugin without regenerate the whole plugin? I don't want rewrite the plugin to add functions to it, and script/generate model vendor/plugin/myPlugin wilt generate other stuff into the whole project rather than the plugin directory.
Probably I could write the model class file myself, but how about migration, create my own rake file?
Note: The plugin has more than one classes and a couples of migrate schema named as datetime_create_model_name.rb. But I couldn't find any generator in the plugin Dir?
Write a generator for the plugin, to create the migration scripts for the new model. Refer to some of the existing plugins, to learn how to write a generator.
Here is one example:
1) Generator file
2) Migration template file

How can you configure Rails to use blueprint-css instead of the default scaffolding css?

What changes do you need to make to a Rails project to configure blueprintcss as the default stylesheet to be used when you generate scaffolding instead of scaffold.css?
I'd recommend writing your own generator, but if you want to alter the default you can:
1 - For a single app: Freeze rails and change the stylesheet the scaffold generator uses.
railsapp/vendor/rails/railties/lib/rails_generators/generators/components/scaffold/templates/style.css
2 - For all apps: Change the same style.css file in your systems rails installation.
Substitute your own scaffolding generation code. Instructions are here (with the caveat that they may be out of date).
An easier alternative may be to write a Rake action to do textual substitution in the (normally) generated source.
Look into Rails Templates.
You can write one to do much more than replace the css in a rails app. YOu can make it install gems, freeze rails, all kinds of things. Take a look at http://youvegotrails.com for an idea of what you can do.

Resources