Finally we have multi env credential in Rails 6

In Rails 6, default credential is for development and staging, they'll store in the same path ``` config/credentials.yml.enc config/mast...

Wayne

When to use Hash and when to use HashWithIndifferentAccess and why

## TL;DR: * When passing to our class: Both works, because we control everything * When passing to 3rd party library: Hash is better, we...

Wayne

Rails 6 released

https://weblog.rubyonrails.org/2019/8/15/Rails-6-0-final-release/

Wayne

Rails ActiveModel with type casting

## Basic usage Use `include ActiveModel::Model` and `include ActiveModel::Attributes` can provide type casting for active model. Exampl...

Wayne

How to test ActionMailer with RSpec

This blog post will demostrate how to test ActionMailer in Rails using RSpec. ## Configuration ```rb # config/environments/test.rb # T...

Wayne

Rails lock! and with_lock

Basically, `ActiveRecord::Locking::Pessimistic#with_lock` will yield your code inside transaction but `ActiveRecord::Locking::Pessimistic...

Wayne

How to test Stripe webhook's singature with Rspec

This post shows how to test Stripe webhook's singature ```rb # spec/support/stripe_test_helper.rb module StripeTestHelper def stripe_...

Wayne

How to split rails routes.rb into smaller files - Extend

I've tried many ways to split `config/routes.rb` file, I find this approach is best to me because it requires no futher config to let dev...

Wayne

How to split rails routes.rb into smaller files - require

Anothr approach: use `require` In `config/routes.rb` ```rb Rails.application.routes.draw do root to: "home#index" get "/about get...

Wayne

How to split rails routes.rb into smaller files - instance_eval

The most common (base on my google search) way to split `config/routes.rb` in rails is by defining your own `draw` DSL. This is just one ...

Wayne

Scaling the monolith

在拆成潮潮的 Micro Service 或是超級大 Refactor 甚至是討論要不要換語言換框架前,先從小的改變做起吧。文章列了一些滿基本但是都很實用的原則、方法、工具,下次沒票可以做或是上班閒閒時都可以試一下,還可以增進一下未來工作效率。 ~~除非你只是想要潮?(威...

Wayne

儲存 sensitive 資料的一些基本作法

### 大原則: 1. sensitive 資料加密儲存 2. Web server 只放加密用的金鑰,不放可以解密的金鑰 實際做起來就是:把需要解密的金鑰放到 worker 的 server,worker server 不接受來自外面的流量、且只能透過公司的 VP...

Wayne

在 Rails 裡擴充 gem 提供的 ActiveRecord model 的方法

需求:想要為 Gem 提供的 Model 增加一些新的 method 或是 association For example, doorkeeper 的 gem 有一個 `Doorkeeper::AccessToken` 的 model,我想要有個 `Doorkeeper:...

Wayne

Rails 直接取得欄位的值(before_type_cast)

How to get column value from ActiveRecord without being affect by overrided attributes? 有時候會想要直接取得 database 裡的值,以免被 model 裡覆蓋的 attribute...

Wayne

How to test with Salesforce Heroku Connect

## What is Heroku Connect? This is a tool provided by Salesforce and Heroku, it will automatically sync all your salesforce tables int...

Wayne

sidekiq-scheduler and sidekiq-cron

There are 2 popular cron task gem for sidekiq, I find [sidekiq-scheduler](https://github.com/moove-it/sidekiq-scheduler) has more powerfu...

Wayne

在 middleware 裡塞 user 給 sidekiq 就不用每次傳

我個人是不喜歡啦,但可以參考一下有趣的作法 https://blog.bigbinary.com/2018/12/05/passing-current-user-by-default-in-sidekiq.html

Wayne

Dump production database to local with Rails

```rb # lib/tasks/db/pull.rake # # Usage # # dump the development db # rake db:dump # # dump the production db # RAILS_ENV=production r...

Wayne

ActiveSupport::Inflector#parameterize

好用的 helper ```rb "123-this-is-標題-haha".parameterize #=> "123-this-is-haha" ``` 很適合拿來做 friendly url 的簡單解決方案 ```rb class Post < Applica...

Wayne

Rails disable some unwanted generators

Rails 常常會自動產生一些不需要的檔案,可以在 generator 裡面設定,放在 `config/application.rb` 裡面就行 記錄一下個人常用的設定 ```ruby # factory_bot config.generators do...

Wayne

在 Rails 使用 custom font

參考 https://coderwall.com/p/v5c8kq/web-fonts-and-rails-asset-pipeline 把檔案放到 `app/assets/fonts` 裡面 然後在 `application.scss` 裡加上 ```scss @...

Wayne

Ansible + rails + letsencrypt

First, install nginx roles/nginx/tasks/main.yml ```yml - name: Install nginx apt: name: nginx state: latest - name: Disable ...

Wayne

Implement algolia autocomplete with rails

```js // app/javascripts/application.js const algoliasearch = require('algoliasearch/lite') const autocomplete = require('autocomplete....

Wayne

Integrate Uppy with Rails and ActiveStorage and use google drive direct upload

We're going to integrate Uppy with "upload from Google Drive" feature with rails backend and ActiveStorage, in this case we are going to ...

Wayne

Import css from npm module in rails with webpacker

I use highlight.js In `app/javascripts/styles.js` ```js import 'highlight.js/styles/github.css'; ``` And then add to layout ```erb <...

Wayne