YourTradeBase Code Test

Instructions


This is a very simple rails application that allows users to manage a list of books. The app mainly contains rails auto generated scaffold code, however some code has been added to allow books to be managed via the users show page. This code has not been written very well! We would like you to refactor the code in order to make the code more maintainable and to be written in a style more suited to a well written rails application. There has also been a complaint from some end users that the site is slow to respond occasionally when creating new users. The application has some basic Rspec tests that can be run using the `rspec` command.

Please spend no more than 2 hours on the exercise excluding any time it takes to install the required software such as ruby, gems etc.

We would like you to keep a record of your thought process and actions taken in order to prepare the application and refactor the code, please be as detailed as possible. You should include your explanations as to why the code you have found is badly written and how you think your refactored code will improve the codebase.

We are interested mainly in your thought process through the exercise, don't worry if you don't manage to complete the refactoring in the time allowed, you may add any suggestions for further refactoring you would wish to carry out if you had more time.

Your taske are:

  1. Fork this git repository to your own github account then clone it

  2. Install the correct ruby version for the project using a ruby version manager of your choice

  3. Install the required gems for the project using bundler

  4. Setup the applications database

  5. Identify code to be refactored

  6. Create appropriate commits for your refactored code

  7. Create 1 commit with your notes

  8. Push the commits to your github repository

  9. Provide us with the url of your github fork of the app

Please add your notes below.

Notes


  1. As first step I solved the problem that sometimes there are slowdowns when new users are created, this is because in the create action in user controller a new user notification email is sent to admin, it's not a good practice to do long synch operations in controllers, prior to rails 4.2 was necessary to add an external gem like delayed_job, but now there is ActiveJob built in and a new method deliver_later was added in mailer.

  2. Changing the new user email to an asynch procees implies a change in rspec too, executing rspec without changing probably will not fail in a test environment because the asynch job of sending email is processed before the test is executed but it's a best practice to encapsulate create new user method in test inside a perform_enqueued_jobs to be sure that the test proceed only after the asynch job is completed.

  3. Then I added validation to user and book. For user I added that name and email must be present as first, than that email must be unique and that respect a regex using a reusable validator. To book I simply added that a title must be present.

  4. I than modified rspec according to validation rules and added various tests to check every validation rule.

  5. Another problem is that if you destroy a user with associated books they are not destroyed too, to to that I added a dependent: :destroy to has_many :books relations that means to call destroy on every boob associated with user while deleting.

  6. I added a test to check if after deleting an user with associated books they are deleted too.

  7. I changed the way how books are managed, books are entities associated to users so in this case nested resources fits better, so I changed the router, added a specific book controller and added the specific views for books.

  8. I changed in rspec the way books are tested according to the nested resources.

  9. I don't like url with id, so I added friendly_id gem to have pretty urls and added relative tests.

Others tasks for the future that I can't do in 2 hours


  1. Add books index also in user show page.

  2. Add a fast way to add books while creating or showing a user, like nested forms using cocoon or nested_form gem.

  3. Add more tests for books and users pages and also for json.

  4. Move all the strings to the locale file.