Ruby on Rails App-Tester

Ruby on Rails (Rails) is a framework for creating simple web applications using the Ruby language. in this post I will walk you through App-Tester, my employee testing app. If you’re interested in the code, you can find it in this Github repository. You are welcome to make any contributions.

Devise Mailer setup

The Getting Started section of Devises README says the next step is, “At this point, a number of instructions will appear in the console. Among these instructions, you’ll need to set up the default URL options for the Devise mailer in each environment. ” To do this, navigate to your config/environments/development.rb file and find the line config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }. If it is commented out, make sure to delete the # at the beginning of the line, and adjust your host and port accordingly. This will allow devise to send emails to the models it generates, if you use an email service that is.


Starting the MVC Structure

The most challenging task for any beginner in Rails (like me) is to structure the Model Views Controller correctly. Specifically in this case, the process of setting up the models. A helpful tool that can ease the structuring process of the Model Views Controller is a cool gem called Devise this is the link to the repo. Devise is designed and created by a large team and is a Ruby gem with the purpose of generating Users and other types of app accounts like Admin. You only need to install the gem by adding gem 'devise' to your gemfile and run bundle install in the terminal. Then, run rails generate devise:install.


Building the ActiveRecord associations

ActiveRecord has built-in macros for setting up object relationships. All you need to do is find the appropriate macros for your project. This application has many different relationships. For example, the client object looks like this:

include DeviseInvitable::Inviter

has_many :applicants, class_name: "User"
has_many :job_posts
has_many :exams, through: :job_posts
has_many :exam_questions, through: :exams

because it has the most control of the other objects. Whereas the Test model is used as a join table, creating an opportunity for a many to many relationship.

class Test < ApplicationRecord
belongs_to :user
belongs_to :exam
has_many :test_answers
accepts_nested_attributes_for :test_answers, :reject_if => :all_blank, :allow_destroy => true

Notice how Test belongs_to both :user and :exam. The User has_many :tests and has_many :exams, through: :tests. At the same time, the Exam has_many :tests and has_many :users, through: :tests. Now that a User and an Exam have the many-many relationship, they can interact with the child models of one another. This allows the User to take a test which gives the user an Exam. With that Exam, the User may also interact with the exam_questions and exam_answers they need to be able to even see the test they are taking. This is achieved by using:

accepts_nested_attributes_for :exam_questions, allow_destroy: true
accepts_nested_attributes_for :exam_answers, allow_destroy: true

Correctly establishing the model relations is the first step to making a fluid application. It is better to spend more time thinking about how you will set up the model relationships before you even start and have a solid plan going into the project than to start the project without a plan and establish relations along the way. A lesson I learned all too well with App-Tester. The plethora of bugs that were caused by improper relations at the beginning led to nearly countless headaches.


Uses Reasonable Validations

Encryption is crucial to protect a companies data. Using Devise to generate both Clients and Users adds password with their DatabaseAuthenticatable module. A description of the module can be found here. Devise also makes use of the module Validatable, which defends against invalid data for emails and password. If an email is unsuccessful in it’s login attempts too many times, the module for Lockable locks the account, making the user validate their account via email to use their account again. Devise is a widely used and versatile addition to any app that uses account validation to access features. The features that I have included which are only accessible to those with accounts are:

  1. The list of Job_posts
  2. The Job_posts corresponding Exams
  3. The ability for Users to take Tests that use the Exams as a building block

Because no “Guest” can access these without signing up, the User and Client models also act as validations for nearly all the data types in App-Tester.


Testing the pages

It is a rule of thumb to start carefully and test each step you do. Otherwise, you will find yourself in a series of unpredictable errors. I started with the application controller, confirming that the view pages work using the resource routes. Because there were multiple nested routes, I had to constantly look at each page when configuring any part of a nested relationships path.

Authenticate with GoogleOauth2

While devise enables excellent user authentication/validation, it is not as secure as Googles profile protections. At the same time, using an already existing Google account is significantly more convenient than filling out a new form. The steps to using omniauth-google-oauth2 can be found here

Clients controller

The Clients controller directory was generated by using the command rails g devise Client. It holds the controllers responsible for implementing the Client registrations, sessions, passwords, unlocks and confirmations. The authenticate method was used to confirm the password with the one being tracked by the password_digest column in the db.

Using Simple_form and partials

Simple_form tags allow us to easily make forms. I made use of the simple_fields_for method to create my nested forms which started with exam and went down the hierarchy of has_many/belongs_to until it went down to exam_answers and exam_questions which required input from the Client to create an exam. This process quickly got complex with the amount of nesting my models had. Because of the complexity of my multi-nested models, I opted for a _form partial. This is a file which has your form on it, but on the pages that use the form, you simply put

<% content_for :page_name, "Submit Your Test" %>

<%= render 'form' %>

This helped avoid a many headaches from needing to make multiple different forms.

Validating your entry fields

You can block the Client from bad entries, such as bad email formatting or leaving one of the fields empty. To do so, you need to use ActiveRecord validations for the entry fields. In the Client class model, I used the validatable shortcut format to authenticate the entry. A shortcut in accomplishing this is using the Devise gem. When a model is generated with rails g devise MODEL it automatically is generated with the validatable module that devise offers.

Conclusion

Rails is basically magic, and as a striving developer, I must practice and learn my craft until I become a wizard. To know how to use ActiveRecord, you need to parse through innumerable documents… Hence, you would learn a huge number of methods that you can add to your wizards tome.


Acknowledging contributing articles

  1. Rails-portfolio-project Github repository
  2. Devise gem this is the link to the repo
  3. Rails quiz blog post by Tressa Sanders
  4. DB browser for sqlite.
  5. RESTful guide table — Railsguide
  6. Validation methods ActiveRecord:Validation

Sinatra Applicant Tester

Sinatra is a Ruby framework for creating simple web applications with minimal effort. in this post I will walk you through my Applicant Tester list app. If you’re interested in the code, you can find it in this Github repository . You are welcome to make any contributions.


Structure the MVC

The most challenging task for any beginner in Sinatra (like me) is to structure the Model Views Controller correctly. Specifically, the process of setting up the environment. A helpful tool that can ease the structuring process of the Model Views Controller is a cool gem called corneal — this is the link to the repo. Corneal is designed and created by Brian Emory and is a Ruby gem with the purpose of generating Sinatra apps. You only need to install the gem by running gem install corneal and run corneal new <app name> in the terminal. Then, run bundle install to install all missing gems, that’s it! You may refer to the full instructions in the corneal README file for more information.


Building the ActiveRecord associations

ActiveRecord has built-in macros for setting up object relationships. All you need to do is find the appropriate macros for your project. This application has two relationships: the company object has_many applicants and the applicant object belongs_to a company.


Uses a password for authentication

Encryption is crucial to protect a companies data. ActiveRecord’s has_secure_password method accomplishes this perfectly. To utilize this macro, I took these steps:

  1. The application must have bcrypt-ruby gem installed.
  2. The companies create_table must have a string column for “password_digest”.

Then, you can use has_secure_password macro.


Start running your database(db)

In the db/migrate directory, you’ll see the companies_table and applicant_table. You have to create the table and identify the type of data. Then, in the terminal, run rake db:migrate to migrate your tables and obtain the schema file.

Testing the pages

It is a rule of thumb to start carefully and test each step you did. Otherwise, you will find yourself in a series of unpredictable errors. I started with the application controller, confirming that the index page works using get.

Applying CRUD and using RESTful routes

CRUD means Create, Read, Update and Delete. “… ,a RESTful route provides mapping between HTTP verbs, controller actions, and (implicitly) CRUD operations in a database.” (Railsguide)

Companies controller

The companies controller was responsible for implementing the company sign-in/out/up, and to edit the company profile. The authenticate method was used to confirm the password with the one being tracked by the password_digest column in the db.

Sign-up/sign-in forms

These simple HTML form tags allow us to use two main attributes: the action attribute, which I assigned to /signup route; and the method attribute, which is set to “POST.” Sign-in forms are easy, but can be tedious.

The ‘show.html.erb’ company profile page

To show the company his profile page, you will need to pull out company.name from the company object. This is achieved by using the ERB syntax — iterating over the company object and set the dynamic route to print out the company name of the current company using the company.id route.

Edit the company profile information

To edit, you have to use the PATCH HTTP verb, but it won’t work directly! The browser doesn’t support the POST and DELETE verbs. To overcome this issue, you have to make the following two steps:

  1. Use the Rack::MethodOverride inside your config.ru file. Allow the method to be overridden when the params[:_method] is set. This is the “middleman” that supports the PATCH/PUT and DELETE HTTP methods.
  2. Establish an input tag with the following attributes: id and type set to hidden, name to a “_method” and finally the value to “PATCH”.

and that’s it, the company can edit their profile.

Read a specific companies Applicant list

The second most challenging task (behind setting up a perfect environment) is to guarantee that the company is the only one permitted to view/edit/delete their related applicants. To do so, I used the binding.pry and checked out @applicants.methods to grab the targeted one. The QueryMethod that I chose was named “where.” It’s job is to search for a saved company ID in the db that matches the current company ID and display the corresponding Applicants list.

Validating your entry fields

You can block the company from bad entries, such as bad email formatting or leaving one of the fields empty. To do so, you need to use ActiveRecord validations for the entry fields. In the Company class model, I used the validates shortcut format to authenticate the entry. In the Models folder of the Company class, I created a constant for email format using RegEx.

Conclusion

ActiveRecord is basically magic, but to be a real modern day wizard, much more knowledge is required. To know how to use ActiveRecord, you need to parse through innumerable documents… Hence, you would learn a huge number of methods that you can add to your wizards tome.


Acknowledging contributing articles

  1. Sinatra-portfolio-project Github repository
  2. Corneal gem this is the link to the repo
  3. Github ToDo list repo for Salma71
  4. DB browser for sqlite.
  5. RESTful guide table — Railsguide
  6. Rack::MethodOverride documentation rubyonrails
  7. ‘Where’ query-method documentation ActiveRecord
  8. Validation methods ActiveRecord:Validation