Generate only tests from existing model / controllers - ruby-on-rails

I have a Rails3 app that is based on someone else's work. For some reason they decided not to supply the tests with the app, which I am finding frustrating.
What I want to be able to do is scaffold the tests for all the existing controllers and models so I can get a head start on creating the tests myself in test::unit. I don't want to recreate the models or controllers, just create the tests.
I am new to Rails, and have hunted about for the rake command that might do this, but all with no luck so far. Any advice / direction most appreciated.

I know it's a little old, but you can do this:
rails g scaffold Post -s
The -s makes it skip the files already created. Also, if you don't use the flag it just asks you if you want to override the file, so, no worries.

To only generate the associated test files for an existing Rails 3 app, I use "generate resource" but skip everything that I don't want:
rails g resource Post --skip --no-resource-route --no-migration --no-helper --no-assets
Other options can be found using rails generate resource --help
-s, [--skip] # Skip files that already exist
--resource-route # Indicates when to generate resource route
[--helper] # Indicates when to generate helper
[--assets] # Indicates when to generate assets
[--migration] # Indicates when to generate migration
Why not use generate scaffold? Because it might generate views that I'm not using.

There's no way to do this that I'm aware of. It would be pretty easy though to just create a temporary rails project and generate scaffolds for all of your models then copy the resulting test directory into the real project.
I.e.
rails new temporary
cd temporary
rails g scaffold Post title:string body:text
rails g scaffold Comment post:references author:string body:text
cp -r test ../real_rails_app/
etc.
This answer is now out of date. Up to date rails versions allow you to generate only the missing files with the skip option.

Related

Rails hooking a custom generator into scaffold generator

In a new Rails 6.1.3 project, I'm attempting to have bin/rails generate scaffold Thing name:string invoke the typical Rails scaffolding (including active_record and scaffold_controller) as well as invoking my own custom generator to create an additional ThingForm that I want to be a part of my scaffolding.
I created a custom generator for form that works just fine by itself. bin/rails generate form Thing name:string is properly setting up an app/forms/thing_form.rb.
But in attempting to get this generator hooked into the default scaffolding generator, I'm running into different problems with different approaches I've tried.
Inspired by active_model_serializers, when I set up a hook_for :form as part of Rails::Generators::ResourceGenerator it ends up no longer invoking active_record. Here's the commit from my example repo: https://github.com/diachini/generator_hook_investigation/commit/8989b8f96b05ac1157d1d3a247bee9f0f2befe5d
To confirm I wasn't crazy, I have another branch that just confirmed generating a scaffold with active_model_serializers installed properly keeps the active_record invocation happening.
Taking a page out of this Gist explaining custom generators - I similarly attempted a hook_foron the ScaffoldGenerator and the ControllerGenerator
Just like my active_model_serializers attempt, this no longer invoked active_record.
It also began ignoring config for turning off assets and scaffold_stylesheet.
On a separate branch in my example repo: https://github.com/diachini/generator_hook_investigation/commit/86bc7b53ac34ea29d5e32d07486c73d962f86247
I saw a similar StackOverflow question with an update regarding copying the whole ScaffoldGenerator, but was hoping to avoid that approach if there's something simple that I'm missing with the hook_for approach that active_model_serializers took.

In what directory / folder do you install Rails Scaffold

I am following the Rails Tutorial. It is starting to generate a scaffold. I just don't know for sure where to pit it. In a subdirectory or right at the root. My hunch is the root. i.e. JimJones$ not JimJones$/work/newapp. The apps can keep[ changing so I would imagine I would install the scaffold at the root. Any help would be greatly appreciated. Thanks, Chris
Your hunch is wrong, you should be in your /newapp directory, assuming that's the name of your app.
If you're not sure where you're at, enter pwd at the command line, and this will
print out your current location. If it ends in /newapp, you're all good.
rails generate scaffold myscaffold creates a set of MVC scaffolds called myscaffold(s), INSIDE your current app. In fact, You shouldn't be able to run rails generate scaffold unless you are inside a rails project.
Scaffolding is specific to Rails applications. First create a new Rails application using rails new appname command. Then move to the appname directory.
Then use scaffolding
rails generate scaffold test
The above command will generate a set of model, database migration for that model, controller, views, and test suites for the resource test
More info on scaffolding : http://guides.rubyonrails.org/command_line.html
You need to be in the rails app directory to use scaffold and the code generated by scaffold will be put in appropriate locations in your rails app directory e.g. model in models directory controller in controllers

Rails will not let me destroy scaffolding from nifty generator

I was following one of the railscasts tutorials and decided to install nifty generators. Well, being a rails noob I didn't realize that the way parameters are handled changed. Now I can't undo any of my changes. So far I managed to roll back the database but every time I try to run
rails destroy nifty:scaffold mymodel
I get the error message
attr_accessible is extracted out of rails into a gem. Please use new recommended protection model for params(strong_parameters) or add protected_attributes to your Gemfile to use old one.
So I did. I added
gem 'protected_attributes'
and ran
bundle install
Then I tried to destroy it and it errored out again. I really hope nifty didn't just screw up my project. Can anyone help?
Um, this not a real solution, but a possible workaround: if the output of the rails generate nifty:scaffold mymodel command is still in your terminal buffer, you could manually delete the files it created.
And if the output isn't available, you could do rails generate nifty:scaffold mymodel2 in order to see what files nifty:scaffold created before manually deleting the corresponding files for mymodel.
Not elegant, but it might get you over the hump.

RSpec added midway - how to generate stubs of all existing resource

I added RSpec mid way and want to have RSpec create all the stub test cases for the existing resources. Any quick way to do so?
rails generate <type> <name> -s
If its a scaffold, you need to temporarily remove the migration so as to fool the generator, and then answer "no".
You could try:
rails generate scaffold <model_name>
and answer "no" when asked to overwrite existing files. It might be less stressful emotionally to do this in a new project and then copy over the generated spec files.

Rails: renaming a controller and corresponding model

Is there an easy way to rename a controller and model in my app and all the instances in the corresponding code?
I'm using textmate, would this be as simple as using the replace function and replacing the word Post with Report?
You need to change the name of the Controller and the associated Model, Views, Helpers, Tests & Routes directories, file names, class names & the names in the class definitions.
I found two ways to do this but before you try anything I recommend you back-up your app, preferably with a Software Version Control system like Git & Github.com.
Your first option is to do it manually & there is a good explanation on how to do this here: How to rename rails controller and model in a project
Another way is to destroy your controller & model, and then generate a new one, this will remove all the files which were generated the first time round & replace them with new ones. Michael Hartl explains this solution well in his online guide to Ruby on Rails here: http://ruby.railstutorial.org/chapters/static-pages#sidebar-undoing_things
This is the solution I followed when I needed to make this change to my app, I needed to replace a MVC scaffold I generated called board with a new one called product.
1. First
I made a back-up of the work I did in the layout of the board view, app/views/boards/index.html.erb
2. Then
I ran the below rails commands in the terminal window.
$ rake db:rollback
$ rails destroy scaffold board name:string description:text image:string price:decimal
$ rails generate scaffold product product_type:string name:string description:text image:string price:decimal
$ rake db:migrate
3. Finally
I copied my backed-up boards/index.html.erb file into the newly generated app/views/products/index.html.erb & did a find & replace in my text editor on this file to replace board with product.
I think the second option is much more reliable & quicker but it’s important to make this change early on in your project before you make too many manual changes to the code. It would be better to just take a little more time planning your MVC names & database tables properly before you start your project.
You can also use rails_refactor gem to rename controller, model, etc
for more info check https://github.com/jcrisp/rails_refactor
To rename controller and model use this gem https://github.com/jcrisp/rails_refactor
If you are using textmate, use 'command-shift-f" to look for a string throughout your entire project.
Yes and no. You can rename it that way, but you'll also need to rename the files as well or Rails won't know where to look for the files corresponding to the new Report model/controller/etc.

Resources