31 October 2012 - Developer's Box

RailsCasts - #216 Generators in Rails 3

Join me on Blayze! - blayze.com/zywx Generators in Rails 3 have been completely rewritten to be more modular. This allows you to customize any generator to ...

the generators and rails 3 have been

completely rewritten they are now much more modular allowing you to customize them to really fit your preferences as you probably know we can access generators through the rails G command now if we run this without any arguments will get a list of generators inside of our rails application right now is just the ones included inside of rails 3 and really these ones pretty much behave by default similar to the rails to counterparts now if we run a specific generator with the dash help option then we'll get some health information including a list of options at the top so if we scroll to the top here you can see there's a lot of options provided by the scaffold generator and these allow you to really customize how the generator behaves now notice that some of these options have default values such as the style sheets option it defaults to true so that means that whenever we generate a scaffolding it will include the CSS files but what if we don't want that what if we don't want it to include its default scaffold style sheets well to do that we would have to disable this tile sheets option and here's a little tip if you want to

disable a boolean that defaults to true you can pass it double dash know before it so let me show you so let's just generate a scaffolding here real quick let's call it project and we'll give it a name attribute and then we don't want it to have a style sheet so we can pass double dash no style sheets and that will disable that style sheets option so if we take a look at this output we can see the list of files which were generated and you can see there's no sign of the style sheet file because we disabled it but what I really want to focus on here is the calls to invoke here now this is really interesting because what this is doing is basically invoking a separate generator which means that this scaffold generator it's really not doing a whole lot all is doing is basically passing all the work and delegating it onto other generators such as this active record generator which is going to generate the model in for active record and along with a test unit generator which that invokes to generate the tests for that active record generator and then we have a scaffold controller generator which invokes a knee RB

version of that generator to generate the views a test unit version to generate the tests and so on and so allowing this is basically a lot of little tiny generators doing all the work which makes this very modular so you can basically plug in anything that you want any other generators you want if you want to generate something else other than let's say that you are be view is maybe you want Hamel instead of er be or maybe shoulda or r-spec instead of test unit this allows you to plug those generators in to handle whatever cases you want so if we take a look back at our scaffold help documentation you can see these options might make a little bit more sense now for example the ORM option at the top here allows you to change which ORM is handling the models for you so we might want to use data mapper instead of the default which is active record and so this is going to basically customize what other generator it delegates the model generation to and you can see further down on the options here we have active record options which are options that's apply specifically to that active record version of the generator and so these options right

here at the top here are basically just the normal scaffold options these other options down here are options specific to the other generators which are invoked and so we can see test unit options this might change if we change to shoulda or r-spec and so on and we have the option to change the test framework inside of the other invoked generators like here in the active record option so as you can see this is much more modular than the rails two generators which I think will make them a lot more useful in rails 3 now normally if you're trying to pass an options to the generator such as that no style sheets option we passed an earlier you want to apply this option every time you run that generator and so this can be somewhat tedious to add every single time so in rails 3 you have the ability to define some default options for your generators if you take a look inside of the application darby file inside the config directory you'll see that there's an option to pass in some generator configurations so what we can do is uncommon this and then this will basically set those options on all generators that are run on this

application so as you can see these are actually the default values which are already passed in the RM the active record the template engine ARB and the test framework is you test unit but we can customize this to whatever we want so I'm going to delete this for now because it's already at the defaults and really any of the options which you saw inside of the help documentation you can just pass in here such as style sheets and then what you could just say that it is false and then that will make it so that style sheets will be false for all generators in this application so if you run the scaffold help again you can see that those defaults which you specify in that application dot RB file will be reflected here so we can see that the style sheets option no longer says defaults to true because it now defaults to false now let's try something a little more interesting let's change our default test framework to be shoulda instead of test unit and let's also change our fixtures to use factory girl instead of generating the normal fixtures so we can use this fixture replacement option and specify factory

girl here we can set those up by adding them to our gem file so instead of our rails free application gem file we'll just set up a group here for our test environment and then inside of here will include the Jones rashida and factory girl there we go now unfortunately these gems do not include rails three generators but there is another gem called rails three generators which includes a number of generators for various plugins and gems for rails notice it includes factory girl and shoulda along with several others so this is a great gem to install if you're working with generators so let's add that gem to the gem file as well so we'll call it rails three generators and then will only add it to the development group this way it will only be applied in the development environment and then just run bundle install to install those gems so now after those gems are installed we can run the rails G command again without any arguments and that will list out all the installed generators and you can see that there's all those generators which were listed inside the rails three generators Jim so now we can go back to our application

dot RB file and customize our generator options so we want our test framework to be shoulda and then our picture replacement option to be factory girl there we go now if we run the scaffold help command again we can now see that our test framework is now shoulda and we know how these shoulda options of our generator which are fixed replacement is now factory girl so those options are now applied now we can put it to the test by generating a new scaffold for a task model will give it a project ID column and a name and here's the output that we get notice that for our active record model or should a generator was found along with a factory girl generator so it created the test and factories successfully however going down further here for the scaffold controller generator it couldn't find a a generator for this or for the helper file there's no should a generator for that either now if we take a look again at our generators with the rails G command we can see that under the show de section here there's only two generators for should only the controller and model generators there's

not one for the scaffold controller or the helper so that's why they're saying they are missing now if we take a look at the rails guides for the rails three generators you is still in the works but it has some useful information there's a section on adding generator fallbacks and you can see here that you just add these two lines at the end here and this will basically say if shoulda is not found it'll use test unit instead so all we have to do is append those two lines at the very end of our application dot RB file and that way it'll fall back to test unit if shota isn't found I want to cover one more thing on the topic of rails three generators and that is customizing the templates which are generated so as you can see here here is an index action view template which was generated by the scaffold which we just created so listing projects and then this is what was output and maybe we want the mark-up to be a little bit different here maybe instead of a break tag we want a paragraph and so on so how do we change this so that scaffolding generates that new markup every single time instead of the old one well if you go inside the lib directory of your

application you can create a new directory here and let's call it templates and this is where the rails generators will look to override various templates but in order to determine which template to override we have to look at the rail source code itself so here I am in the rails github project and so if I go to the real ties directory and then go to the lib directory and then go down to rails and then here we have our generators directory and in here notice this is where things start looking a little bit more familiar we have our test unit namespace there are er be namespace and rails which is where all the initial directories are located but we want to go into ERBB cuz we're overriding an ER be view here so if we go into our ER be we can see that we have several directories here if we go under scaffold and then we have a templates directory here and so really what we need to do is make a directory called ER be scaffold and then a file called index.html ER be and then basically take the contents of this and customize it to fit our needs so going back to our application here we need to

go inside our lib templates directory create a new folder here called ER be and then another one called scaffold and this is where we add our file called index.html ARB and then in here just paste in the contents of that generator file and then here we can just customize it to fit our needs so let's just change that to use a paragraph tag here and that's all for now so now when we run our generator for scaffolding create one called category and it should now use our new index view template oh and also notice here that for I didn't report an error this time because it's falling back to test unit so these files here generated are just normal test unit files so here's what the index view looks like that was generated for that category scaffolding now unfortunately as you can see the template was not properly overridden that we created inside the lib directory is still falling back to the original scaffolding view and I'm not exactly sure why as far as i know i'm following the documentation properly but if someone knows the correct solution to this problem please post a comment in this episode and i'll be sure to include it

in the show notes so it may be just a be a small bug in the rails beta vsat will be fixed in the future as well so but I do know there is a way to override the views in individual templates for rails generators and that's it for this episode on customizing generators I'm sorry the last part didn't work out properly but I hope you learned a few things and be sure to check out the show notes for this episode for that final solution there

XML Transcript: