Skip to content

Instantly share code, notes, and snippets.

@stevenyap
stevenyap / tailwind-string-to-elm.js
Created September 17, 2021 05:49
Convert Tailwind CSS classes into Tailwind elm-css Functions
// Usage:
// node tailwind-string-to-elm.js flex hover:mt-3 mt-2 sm:mt-0 sm:pt-1 md:w-auto
// node tailwind-string-to-elm.js flex md:w-auto | pbcopy
//
// Script will throw if there ia an unrecognised pseudo=class eg. invalid:border-red-500
const [nodeCmd, scriptFile, ...classes] = process.argv;
const twElm = {
base: [],
@stevenyap
stevenyap / Git_Bisect.md
Last active May 10, 2021 10:34
Finding bugs in Git commits using git bisect

You have a git commit in your history that is causing a bug but you do not know which commit it is.
Here's how to use git bisect to find the commit that causes the bug.

# in the git root, start the git bisect
git bisect start

# mark current commit as bad
git bisect bad
@stevenyap
stevenyap / dotenv.md
Created November 12, 2014 03:24
Using Dotenv for multiple environments

Storing configuration in the environment is one of the tenets of a twelve-factor app and Dotenv is a good gem to extract your environment variables from your app to a .env file which is added to .gitignore so that it is kept private. Dotenv Github: https://github.com/bkeepers/dotenv

However, dotenv only works in development AND test environment and is not able to differentiate the environment in order to load different environment variables. eg. Your development uses a different API url than your test case.

Luckily the author has also published another gem that makes dotenv environment-aware at https://github.com/bkeepers/dotenv-deployment

Install the gem

#Gemfile
@stevenyap
stevenyap / vcr_timecop.md
Last active December 19, 2022 18:36
Optimizing time-sensitive and HTTP-based test case using VCR and Timecop

Problem

You need to get the last hour data from an API but your test case runs too slow with the connections to API or test cases fail consistently with VCR due to different HTTP request sent in the params.

For example, expect(api.latest_data_time).eq 1.hour.ago in which 1.hour.ago will be different each time you execute the test case and the api http request param of the time will also change according to the current system time like http://api.com?starting_time=<new_current_time>

Solution

We need VCR to record the HTTP request and we use Timecop to freeze the system so that the HTTP request remains the same.

@stevenyap
stevenyap / Capture_output.md
Created October 30, 2014 05:23
Capture/Suppress output via STOUT, STDERR, etc

Suppress all outputs:

quietly { puts 'this message will not be shown'}

Suppress outputs except error messages (eg STDERR):

silence_stream(STDOUT) do
@stevenyap
stevenyap / Rake Task Testing.md
Last active August 29, 2015 14:08
Rake task testing

Given a rake file with a task:

# lib/tasks/test_run.rake

desc 'Sample description of a rake task'
namespace :test do
  task run: :environment do
    # do something here that adds to database
 end
@stevenyap
stevenyap / Travis.md
Created August 12, 2014 08:47
Travis CI

Setup

Sign up an account with your github http://www.travis-ci.org.
Follow the guide to link it your github repository.
You need to setup the .travis.yml file in order for Travis to run your build.

Travis CI file

# .travis.yml
@stevenyap
stevenyap / repl-with-pry.md
Created June 20, 2014 10:21
REPL-driven development in Ruby
gem 'pry'

Setup a WYSIWYG editor with CKEditor. Includes instruction for Rails_Admin

  • Add gem 'ckeditor' to your Gemfile and bundle
  • Checkout https://github.com/galetahub/ckeditor for instruction to generate the correct model to generate for uploading of files (eg. ActiveRecord + Paperclip)
  • Run rake db:migrate
  • Add //= require ckeditor/override in your application.js (before require_tree .)

To configure the editor to be used in Rails_Admin, follow this https://github.com/sferik/rails_admin/wiki/CKEditor

Sample Rails_Admin model config