Code Along Video Subscription App #18 Rubocop, Ruby Style Guide, Traceroutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello so previously we have added tom select and now we can select or create tags using the tom select so we can add a tag like hot via for example and it will be saved in our database and now we are going to make some improvements around our application so we are going to slightly stop on adding new functionality and just uh so that's what we have with luxembourg's valve so let's first have a look at what we've been doing previously so we've added the stamp select controller and actually i think it will work without uh saying on turbulence load so i will undo this move it one line back and like this and it should vocal just as before so i'm going to refresh and actually we can add some validations for our tags so now we have only the validation for name presence and we can also evaluate something like that length of attack so it has to have at least like two symbols for example and maximum 15 symbols so now we don't have any let's try adan's validation let's say validates name and how do we say it would say length and we would say minimum would be for example three symbols and maximum would be 15 symbols let's see if it works so i will say attack with the two symbols you see it doesn't get saved and what do we have here uh so no template found for text create rendering no content okay so the tag hasn't been created and we don't render the tag so makes sense of course there are lots of ways we can make it nice like if we go deeper we can actually make it so that we would render some kind of error message that the bag doesn't have enough symbols or so on but we're not going to do that now anyway we've added some basic validation well what do we want to have let's make it maximum 15 minimum will be [Music] 2 for example okay looks good uh what do you want to do next so let's first save our changes i'll say get status get add all get commit main tag so tags [Music] in stimulus without doubling slowed and tag validation okay looks good and here we actually have a pull request so using pull request is generally a good approach to working on your code and seeing all your changes you've done to a specific feature and letting others comment and view your code before it's pushed to production to your main branch so looks quite good and i think if we can merge it with our main branch so i'm gonna say squash and merge okay done and now i will go to our main branch and get checkout main and say get full and we're going to pull our changes to the main branch okay done looks fine so get status okay and now let's actually work on our ruby code quality so there's a thing is the ruby style guide ruby style guide and basically it has a big list of bad of best practices on what is considered good code and what is considered bad code so you see it is really really big and just as some examples so in the ruby you're expected to have not four spaces about two spaces and there are lots and lots of such examples like you are expected to have some space between operators and so on and now how do we know that our application adheres to this ruby style guide how can we know that it is easily readable to all other ruby developers well there are some tools that can statically analyze your code and say what is good and what is bad and i think the most popular of them is robocop so actually he has a link to robocop and we can install this static code analysis into our application and see what errors opponents it would give us so here is robocop and it actually also has a separate gem named robocop rails let's find it yeah here it is robocop rails so we will install this channel and see how it works so i'm gonna add the jump to our gem file now we are going to edit not just anywhere but we are going to add it inside the group development and test we clearly don't need it in production okay bundle and next we are going to create a file named rubicon ml in the root folder of our application so i will say echo robocop fire model here we've created robocop 5ml and now we need to initialize robocop so we will say require robocop rails just up rails for the moment and now we can run bundle exec robocop let's see and yeah it says there is a big list of offenses and some of them are auto-correctable now there are some fences that we will correct straight away these are style frozen literals basically on top of each file it wants to say frozen string literals true let's actually have a separate branch for robocop so i'll say git checkout branch robocop styling okay and uh i will start fixing the errors now i will run uh no actually before we fix the errors there are some files where i don't want to run robocop so for example inside the db folder you see we run robocop for the code style of different things that for like automatically generated and we will always have to rerun robocop on them like on all our migrations for example they've all automatically generated and uh well the code cell is the rails code style but not the robocop and these are different code styles we can say again code style is always opinionated and it cannot be the same and even here when we're going to use exactly all the robocop options we're going to use robocop in some places where we want to and not use some robocop cops so like some robocop options that are offered for example they're not going to use this style documentation now it says that it would like us to have a comment on top of each of our migrations and each of our models i'm going to disable the scope so i will say style documentation enabled pause we are not going to run the scope and now again i type bundle exec robocop and we should have errors because they're not running this specific cop so let's see okay 316 offenses and again we have the db folder we don't want to run the robocop on the db folder so i will say something like all cops and i will say yeah actually also set a target ruby version so we're on a 3.0 point fun so target ruby version it will be 3.0.1 and i will also say exclude so i will exclude the db folder it will be so we have our application we have for the b and inside the b we have folders and files let's see if it gets excluded we had 254 how many do we have now still running [Music] so 391 okay but we didn't run robocop on our db and you see we have robocop running on our config so you see on all these auto generated files now i didn't do any changes in our puma.rb and in many config files in the initialize again it's just the way uh we set up external uh gems so i don't want to run robocop on our config i would say something like uh config and uh slash slash so they'll not run robocop on our config folder now you can also say that you don't want to actually run robocop on config like environments or initializes but you do want to run it on roots for example but again it's your preference and you see we have fewer offenses okay now let's have a look so some of them they do want to fix and some that don't and you see again we have this bin folder we don't want to run robocop on the bin folder so again i will exclude it so i say bin and i think we have only one level underneath yeah so we're not running robocop on the bin folder okay so 199 offenses left and uh let's see so again we have this config.room that is in the root of our application do we want to run rebook up on it i actually wouldn't be doing it i really prefer running robocop on files inside our app folder because there's actual files that we always work on and edit so i will not run it on config dot iu again i type bundle execute trouble cop we had 199 [Music] and it should be like 198 i guess now okay 197 okay so we are running on all our tests we're running on our app models looks good we are running our helpers on our controllers okay looks good so we have decided on which files and folders they do not want to run robocop now there are also some additional cops i don't want to run so one of them is bundler or the gems so bundle audience basically i don't want to put the gems in an alphabetical order because well i prefer them being in the first the gems for added initially then the gems that i've added later on so i'm not going to run the scope i will say that it is enabled false and there is another code that i do not want to run is empty method now what is it here for example we have our static public controller and the robocop says that it will prefer it to look like this if you don't have anything inside the method but in the end we usually always add something inside and i don't like this kind of syntax so i'm not going to run this specific cop i will say style empty method and enabled false now again i type bundle execute robocop and it should have also fewer cops to run okay 186 okay and just one more that i will disable for robocop is unused method argument let's see if it is visible here no it's not so basically unused method argument would go for example to our application controller and put an underscore here and well i don't like this underscores and they can actually potentially i don't know maybe break our application so the cops name is lint slash unused method argument and for this one i will also say enabled false okay now i run bundle execute robocop and let's see what it gives us so 185 offenses and 768 are correctable auto correctable so we can actually auto correct them let's first type get status and i will say get at the [Music] now what have you changed in our gem file okay git add all get commit main install robocop okay and now we can actually do some auto correction so for this i will say git bundle execute triple cop and soft autocorrect now smaller the a is soft autocorrect let's run it and again the static code analysis tool is going through all of files and also actually doing some changes so it will try to improve the code style and in some places where it can okay so you see some have been corrected now 158 have been corrected that's a lot let's type bundle execute trouble cop and see how many offenses are left and then we can actually see what has been autocorrected just now okay you see only 59 offenses are left and 43 are autocorrectable but they need to be run with a capital letter a possibly okay you see we have this frozen straw literal well we'll come back to it later and if let's typically get status and you see we have done so many changes in so many files so we have our files and you see here an end has been placed in different place here we've added the a space after a guard clause and well we would go to any of these files and you see it just does some minor or not so minor uh improvements in the code style so here for example aft instead of having these uh square braces and uh having these uh signs inside so it offers a way to have symbols in the uh an easier right way then here instead of using unless user user equals we use this pipes and so on or let's for example go to our post dot rb and here you see actually a lot of changes so uh use and again this collection then if we have a number then we have underscores after thousands so that it is easier for us to read and here again instead of the space we have the braces here we've removed the redundant self but we still have self it is not redundant so you see a lot of small style and also like that we have operators have added spaces so uh robocop actually does a lot of work on just automatically uh improving our code let's say git add all get commit main rubric but robocop autocorrect okay and again let's say bundle execute trouble cop and have a look at what is left to do okay so here we have the style frozen show literal comment let's automatically fix this one thing around our files now it couldn't be fixed with the small letter a so we'll do it for the big letter a only this specific cop let's fix it okay it says corrected get status and we'll go to any of these files and you see we've added this frozen string literal true okay and uh we can actually also uh run bundle execute trouble cop and we are going to have another error it is that after this frozen string literal we need to have an additional space now let's see okay layout empty line after magic command so we need to have an empty line after style frozen string literal command okay let's run this one okay now again i run bundle execute trouble cop and we should have quite few things left to actually correct now some things can be autocorrected by robocop but in some cases you definitely need like a human input to make some decisions okay some still autocorrectable let's autocorrect them so for example here we have rails time zone instead of time at we would use something like time zone now for example time dot zone dot at so i'll also auto correct that fun then this style class and module children in our registrations controller for example let's go to our controllers uh users registrations controller so here we have class users registration controller and it would suggest us to have module users like this and have the class inside so i would do like this write an end statement here and let's see we still have this problem okay 35 offenses okay and here we have lots of these trail invite spaces so let's also fix them in the registrations controller okay here we have this trailing byte spaces that i have just uh basically added again bundle execute trouble cop soft autocorrect i wonder if soft autocorrect will fix these white spaces okay bundle execute trouble cop and there'll be quite a few things left okay so 23 offenses and seven of them are auto correctable now let's look at what is not auto correctable for example dependent options now basically robocop doesn't know what you want to do with a tagging if a post was destroyed or if attack was destroyed and we'll have to specify this so we will go to our models so we're going to models pose.rb and yeah let's reload and let's specify a dependent option so has many taggings dependent destroy so if we destroy a post we are going to destroy like the tagging that joins the post and the tag and they're going to do the same for our tags so has many taggings depend on destroy let's rerun robocop and we had 23 now expect us to have 21 error left okay 21 left and yeah so also we can specify a dependent strategy for our users has many entities now we definitely don't want to delete all the dependent users if a user deletes his account so we'll have something like has many entities dependent uh nullify now if i say nullify then all the users that have invited by id and whose user indeed inviter was deleted we'll just have this set to nil so if somebody was invited by me uh and i delete my account just he's invited by devil beset to nil okay now we should have around to just 20 offenses that is really little okay 20 offenses and here we have this inverse of option so let's try going to our pose dot rp and here we have as many comments as commentable depend and destroy inverse of let's say i think commentable so as many comments in those as commentable inverse of commentable something like this okay and i think that actually inside our comment uh our b in our belongs to we can also specify something so inverse of uh comments i guess or universe of comment let's see let's try creating a new comment and see if it breaks anything so i'm going to go to i guess create a new post yeah i'm compiling the application okay a title a description a body create post now add in a comment and i will add the child of this comment and a child of that comment and most likely if i did everything correct nothing should at least be broken from my inverse of updates okay seems to be fine and what if i say inverse of comment for example i will try to refresh press reply and here you see could not find the in those association so yeah this should have been correct okay again i'm running bundle execute robocop i guess we should have around 19 arrows left and actually some of them they are not going to fix uh right away so what do we still have so we're not going to fix inverse of for our user has many entities now then uh here we have user dot rb metrics abc size line 18 let's have a look so it says that on line 18 we have something that is too complex well actually we can simplify it a bit i'll just move this inside here and like this so already less syntax used but i think they still will have the same error again i type bundle execute robocop okay so we still have this error but anyway we can focus on it later it's not something critical then in application cable it's not something i want to fix especially in the test folder because it was auto generated then inside our application helper markdown has too many lines and also find that that then rails output inside our web hooks controller actually i will disable this one so i will say rails output enabled false then again some cases where we might want to refactor or might not want to refactor for example inside our web hooks controller great action it really is uh long so you see we have all these options all this logic inside our web hooks crate and we might want to maybe move it into smaller junk pieces into a model or something but we're not going to work on this in the near future then style class and module children yeah we can actually fix these so auto fix only the scope so let's go to one of these controllers for example uh omni of the callbacks controller or confirmations controller go into users and here you see all of them were put inside modules so it's kind of a better more correct way to write code instead of saying class users confirmations controller we have modal users class confirmations controller okay how many errors do we still have left okay so 24 offenses and 12 of them are auto correctable so layout indentation looks fine use two spaces not zero spaces in omniout callbacks controller okay let's uh autocorrect them i think they will create it by us running the other robocop command so you see we run one robocop commander to remove some errors but some new ones for uh created again bundle execute trouble cop okay 13 offenses and just one is auto correctable now i think we can save our changes let's say get status get add all get comment main robocop okay and let's have a look again at the what is still left we can actually maybe not fix it all at the moment again i don't want to fix some of the files some of the files can be reflected later on here we have for example past unit amount as an argument to sort by let's have a look at it so we'll go to our static public controller and let's see what will happen if we autocorrect it here is our static public controller okay so you see the stone has changed and well robocop has edited this method quite a lot but now it must be more readable and digestible okay so how many errors do we still have left 12 offenses still detected okay they're not going to fix the bonds in user of b then html safe is going to stay inside the application helper markdown then the method line length for markdown is okay then they're not going to do anything inside the webhooks controller and we are not going to change anything inside user's omniout callbacks controller now text controller can actually be refactored anyway we'll do it later and too many lines inside our crate checkout so i guess we've actually done some good work and uh let's just disable this robocop for errors for now so i would say robocop autocorrect and i would say disable uncorrectable okay okay and let's see what this does so basically if we go to our user rp for example it will say that there is a to do let's have a look current models user rb and here you see in some places it was said that there is a to do and it's so good actually we can also remove this code because we're not using it anymore so just like that we have made it so that our application adheres to the ruby style guide in most places and in some places we have a to-do comment and now we can actually add a robocop badge to our application to the readme so we can add such a badge we'll go to our readme and edit here let's first save our changes so get at all get commit main cop and now we'll add the badge and it should be visible to anybody who sees the report that we are trying to adhere to the rails to the ruby style guide so i think uh yeah looks nice let's save our changes for the read make it add get comet main robocop badge in readme okay and i will also look at couple of issues that have been opened inside the repository but before this let's actually have a look at all the changes that are done it's actually really really interesting i'll say git push i will push this branch to our github and based on the branch i will create a pull request so here we have our branch and i will create a pull request and let's have a look at it so you see 38 files have been changed that's a lot let's have a quick look at what has been changed so row copy ml settings make sense gem file again jump file so we've added the badge and here so by default we had the double braces and it was changed to single braces frozen literal true and the space uh then here for example the braces were opened here and everything inside was moved to many spaces here so again it's according to the style guide same here then we've added spaces after curly braces then the spacing inside our controllers whatever again double braces both change to singular oh let's see so actually this is interesting before we had the three lines and now we have one line here so it was actually some nice cold refactoring on the side of robocop here it made the method not so long here it is as a to do here we moved it into module users so quite a lot of changes and generally our code must become more readable and it is a good practice commercially to use robocop or a similar static code analysis tool to adhere to the rail style guide and here's another really interesting case so here you see we had the yield and instead of using yield we use and block so you see this is quite sophisticated the code improvements and in some cases you would want to double check whether it works and again before you do some kind of automatic code changes it's also great to have a test and suit and tests running in your application so that you see that your refactoring doesn't break your application anyway it is it with the robocop for now so let's merge this into our base branch so we're going to merge the spool request from robocop style to main okay i will say squash and merge meaning it will be all merged as one single comment okay we can delete the branch and let's see i will go to main git checkout main and git pull and all the changes should be available okay looks nice okay now actually another thing that we can do uh if we go to our comments controller so previously we've said that we want to merge users current user inside the comment params but there's another baby can do it we can actually do it right here in the comment params so we will say merge user as a current user and it should work just as before but it will be for everywhere we use command params of course now there is just one case but there might be more cases and we would have the current user set as user in all the comment params so again you can decide whether you want just on create or on common params but this is just another option and i will leave this option for now and one more thing i would like to do is actually install another gem for improving the code folder named traceroute so i would say github trace root yes jam and basically it helps us find the controllers without roots and roots without controllers so let's try to install the gem and see how it works so say get status get add all get com main [Music] set current user for comment in strong params okay and now i would go to the gem file and add this gem traceroute actually i would add it inside the development environment only so jam traceroute bundle and how did we run it let's say rake trace route okay and here we have unreachable roots and unreachable action methods now for now we are not using action mailbox so we can disregard it we can actually tell the gem that we want to disregard it we can create a settings file like let's say traceroute.5ml so i will say echo phrase root by ml and here inside i will say what i want to ignore so i want to ignore these unused roots like this let me run uh rake trace route once again and we still have unused roots for rails conductor action mailbox okay because i deleted it let's try once again rake traceroute okay these are not anymore in our induced roots let's have a look at our unreachable action methods so we have device for example now we don't need device here so we will disregard it i will disregard device rake trace route and now we have unused roots device so i will also add device here so again there are some defaults that we just don't want to have here and the same will be the device invitable so it is in unreachable action methods ignore device invitable so now we're basically making just the default setup that we want to use for this gem and uh here we have actually some interesting cases so users omnihealth callbacks failure and handle or alf they are unreachable so they can be set as private i guess so private and it should work just as before because we reach them from google oauth or github so we go to handle auth okay let's run rate traceroute once again and we have our application after sending path so we'll go to our application controller and here i will also add private just like this and of course it would make sense for you to check if it works so if you can log in with the social account and if you would be directed to your page as you added these to private methods but i think it should still work so i will just log out and log in once again and see if it works so going to log in sign in for github i should sign in and i should be redirected to my page okay so it works and this is basically a small example of some improvements that you can add and some like bugs that you can notice by adding these gem trace rules basically i found two places where i could move our actions into private methods but moreover if the application is big you can find uh something that you can disable in your controllers or inside your routes for example here we have if we run rail trace route it will notice that it will notice that we have our comments controller but comments create new and destroy are not available so inside our comments controller we have new create and destroy but they are not available here but we know that we are using them nested so that's why we still want to have them and this is just an example of what this gem can catch so it's just not a good gem that you can install to ensure that you are not missing anything and you're having any redundancies in your code and that's basically it for now so what we've done we've done actually quite a lot of improvements on the code quality in this episode so thanks for being with me and have fun writing beautiful code cheers
Info
Channel: SupeRails
Views: 214
Rating: undefined out of 5
Keywords: ruby, rails, ruby on rails, tutorial, programming
Id: fDOz0ljQB2k
Channel Id: undefined
Length: 43min 10sec (2590 seconds)
Published: Mon Sep 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.