(Rails5) Commandline Options

Recently I wanted to set up a quick Rails test app, to show people how to get from zero to the Rails’ “Hello World” view, and how adding new content works.

I had trouble setting up Postgres, so I ended...

Read More

(Rails5) Redis and Sidekiq

Here is my workflow for setting up sidekiq as the background job processing adapter for Rails5.

1. Include the gem

gem 'sidekiq', '~> 4.1.4' # the hard-hitting background worker gem...
Read More

(Pattern) Asynchronize With Job

Sometimes Ruby code takes too long for web standarts, or there is some heavy processing.
A pattern I use (and have trouble remembering) is to write code in a modular way.
Consider this:

Read More

Have Puma Boot With Server

Puma has been the de-facto recommended web server for Rails since 2015.
It provides considerable memory savings over Unicorn and can be ran with a combination of settings for “workers” and “threads” to give your app the flexibility...

Read More

Use NewRelic for App Tracking

NewRelic tracks your app and provides info and graphs about throughput, slow requests, errors, etc.
Follow this workflow to set it up for your Rails app. You will need an account, get one.

1. Add the gem...

Read More

Use FactoryGirl's Traits

Define a solid base factory with all appropriate traits

FactoryGirl.define do factory :todo_item, aliases: [:incomplete_todo_item] do # default attribute values name 
      Read More

Use Delegations

Recently I received a link to an opinionated blog post about how to observe the law of demeter in Rails. It strongly suggests using delegations in Rails models, and here is the TL;DR of how you do it. This...

Read More

Make Engines

In previous post I discuessed ruby gem creation.
In this post I will expound on creation of Rails engines - a variant of ruby gem that assumes inclusion in a rails app and, usually, provides functionality through models/controllers. Read More

Make Gems with Bundler

1. Init gem project

$ rvm install ruby-<version> e.g. $ rvm install ruby-2.2.3 $ rvm use 2.2.3@<gemset> --create $ gem install bundler $ gem install pry $ bundle gem <gemname> 

2. Fill out <gemname>.gemspec,...

Read More

Follow Model Best Practices

Say you want to keep your model definitions clean and consistent.
Do this:

class User < ActiveRecord::Base # use this class method in strong parameters # params.require!(:user).permit(User.params) 
      Read More

Initialize Hash with Default Value

Say you want a hash to return some programmatic value based on the key called without assigning the return value explicitly.

Passing a block to Hash.new specifies what happens when you ask for a non-existent key.

Use Global Scopes

Say you want all your models to have a scope that lets you select them by date of their created_at.

  1. Make a module
# in /app/models/concerns/global_scopes.rb module GlobalScopes def 
      Read More

Use Relative Image Reference in README.md

So you want to have an image or two in your project README.md, but do not want those images present in your sleek binary-only project side.
Seperate asset branch to the rescue!
Push your images there and then...

Read More

Use Method Missing

You can override Ruby’s method_missing method to make dynamic methods based on regex.
For example, a trivial case, User can have three string gender settings: "male", "female" and "other" and you want checker methods of the style...

Read More

(Rails) Initialize App in 2016

# Replace <project> with _myapp_ and you are good to go. # Make new gemset of latest ruby found at https://www.ruby-lang.org/en/downloads/ $ rvm install 2.2.2 $ rvm use 2.2.2@<project> --create $ gem install bundler $ gem install rails...
Read More

Deploy Rails applications with Mina

Modern deployments require finesse. Long gone are the days of FTP file upload to server. These days we deploy version-controlled (GIT) code over SSH connections using automated systems. On Rails, Capistrano is a widely used deployment system, however, we at...

Read More

Use WickedPDF to make PDFs in Rails

1 Add these two gems to the gemfile

ruby # PDFs gem 'wicked_pdf' gem 'wkhtmltopdf-installer', github: 'vovayartsev/wkhtmltopdf-installer-ruby', branch: 'master'

2 Make an init file and load the wkhtmltopdf binary path

ruby require...

Read More

Rails4 Use concern namespaces

1 Set up your rails 4 app to load subfolders of /app/models/concerns

# /config/application.rb class Application < Rails::Application config.autoload_paths += Dir["#{
      Read More

Setting up FISH shell on Mac OSX

Use homebrew to install fish v2+.
Use Oh-My-Fish (get via curl) to make it pretty.

$ brew install fish $ curl -L https://github.com/bpinto/oh-my-fish/raw/master/tools/install.fish | fish # add https://gist.github.com/Epigene/9bccb811c8b15524cfbc contents to ~/.config/fish/config.fish $ sudo nano /etc/shells # and...
Read More

Break apart long queries

Complex queries can be chained like this

base = Transaction.joins(order: {page: :pipeline}) .on_date(date) # Ensures day dimension .where(
      Read More

Deep nested queries in ActiveRecord

Contributed the backend of a statistics panel today.


Wrote complex, deep-nested queries for associated object data in Activerecord, for example

@base_query = Payment.joins(order: {page:...
Read More

Arguments in rake task

There are three ways to do this.

Method 1, environmental variables

desc "Example of a multi-argument raketask via environmental variables" # rake with_environmentals name=Liene surname=Viktus task with_environmentals: :environment do 
      Read More

Download sounds from Google Translate

Sometimes I need a robotic voice saying things. A quick and easy way to get a sound file with the pronunciation of virtually any text is by using Google Translate’s text-to-speech API. Here is ruby code that achieves this. NB...

Read More