Automate all the things!

Reading Time: 4 minutes

Motivation

In a normal software development day, I do a lot of activities related to the task that
I’m working on, and sometimes these activities take more time than expected, making them very repetitive. Tasks like starting the project, modifying the status of some values in the
database, creating a new user and performing default actions such as adding a new address,
among others. In this blog post, I want to share with you some approaches that I use in oder
to keep my workflow simple and help you simplify yours as well.


##Simplifying the start of the project

Starting the project in your development environment should not be a task that requires
a lot of your attention. You can keep this step of your process simple by using
a tool like tmux, iterm scripts, docker, among
others for automatizing the start of the project.

In my case, I like to use tmux as part of my development stack since I can use
it in MacOs and Linux with very few changes in my configuration file.

The following tmux script generates two windows. The first window is destined for
my vim editor. The second window is to start the services that
I need in order to begin this project, such as rails server’, ‘rails console,
a custom task, a guard session, and a console session for general purposes.

#!/bin/bash
BASE="$HOME/MagmaLabs/myProject"
cd $BASE

tmux send-keys "cd $BASE" C-m
tmux new-window -n project-stack

# Split panes
tmux split-window -h
tmux split-window -v
tmux split-window -v
tmux select-pane -t 1
tmux split-window -v

# start rails server in first pane
tmux select-pane -t 1
tmux send-keys "be rails s " C-m

# start rails console in second pane
tmux select-pane -t 2
tmux send-keys "be rails c" C-m

# start custom rake task in third pane
tmux select-pane -t 3
tmux send-keys "be rake custom:task" C-m

# start guard in last pane
tmux select-pane -t 4
tmux send-keys "be guard" C-m

In this way, you stop repeating the same task on a daily basis and you limit
yourself to just running your script. If you want to include docker in your project my
Adding Docker to your Rails workflow article is for you.

Create scripts for things that you notice you are repeating

Sometimes when you are working in a new feature you start to perform some setup
tasks in your development console. Most of the times these tasks start to be repetitive.
Identifying the tasks that you are doing many times and moving them to little scripts
which you can reuse in the future is a good idea. I started to integrate this habit in my
workflow because I noticed that I used to utilize a lot the ctrl + r command in my console
to find instructions that I wrote in the past. But very often I found myslef needing to do this three or even four times to complete the whole task that I wanted to perform. For example, the following allows me to update the shipment date for all the subscriptions recorded in my local.

def today
Date.today.day
end

def users
User.
subs_day(today).
joins(:subscriptions).
merge(Subscription.active).
distinct
end

Subscription.all.update_all(next_shipment: Time.zone.today)

users.find_each do |user|
user.update(subs_day: today)
::SubsJob.perform_now(user)
end

I run this script by using bundle exec rails r advance_subs.rb.

I know that this is a simple activity and probably I could run it in my rails
console every time that I need to advance the subscriptions, but in this way, I can
save time and be focused on the feature. Also, if you find a repetitive task which
could be useful for the whole team, you could consider writing it in a rake task.

Use an automation suite for speeding up things

Although is common to add integration tests to the projects, sometimes you need to
perform some manual tests for a specific task, such as looking for a bug. In these cases,
you can use a library like watir for automatizing all the steps you
need to perform over and over. In this way, you will save time by creating simple scripts
that help you reduce the time it takes to find a bug. Next time, I will share an example of a
little class that I have been building with tasks that I used to perform manually
in my day-to-day.

require 'watir'

module MyProject
class Automate
attr_reader :browser

def initialize
Watir.default_timeout = 60
@browser = Watir::Browser.new
end

def sign_up_user(email: 'user@example.com', password: 'qwerty')
browser.goto('localhost:3000/sign_up')
browser.input(id: 'email').send_keys(email)
browser.input(id: 'password').send_keys(password)
browser.button(class: ['login-button', 'new-user']).click
end

def login_user(email: 'user@example.com', password: 'qwerty')
...
end

def add_payment_method
...
end

def add_new_address
...
end

def add_to_cart(url:)
...
end

def complete_order
...
end
end
end

Conclusion

Creating the habit of automatizing all the things you notice you are doing
many times can help you save a lot of time in your project. In the beginning,
you may feel you are spending time on meaningless activities, but in the
future this little changes in your workflow will help you be more efficient
and will let you focus on adding value to your project.

0 Shares:
You May Also Like