在 Rails 裡擴充 gem 提供的 ActiveRecord model 的方法
需求:想要為 Gem 提供的 Model 增加一些新的 method 或是 association For example, doorkeeper 的 gem 有一個 `Doorkeeper::AccessToken` 的 model,我想要有個 `Doorkeeper:...
Ruby 2.6 的 Range 可以 accept nil 當作 Float::INFINITY
### Before 2.6 (v < 2.5) ```rb end_of_range = nil (1..end_of_range).map { |i| do_something(i) } #=> ArgumentError (bad value for rang...
Golang assignment (=) vs short variable declaration (:=) vs regular variable declaration (var)
## assignment 跟 declaration 差在哪? https://stackoverflow.com/questions/23227486/difference-between-declaration-statement-and-assignment-st...
What is PKCE (Proof Key for Code Exchange)
## PKCE 什麼時候適用: 只有手機 App 沒有 Server 的時候 ## Why PKCE? Oauth2 提供很多種 Grant Type https://oauth.net/2/grant-types/ ,就是提供很多種拿到 Token 的方法,最常看...
Basic Auth 裡的 Realm 是什麼意思?
According to the RFC 7235, the realm parameter is reserved for defining protection spaces (set of pages or resources where credentials ar...
Mac: add ssh key to ssh-agent
```sh $ ssh-add -K ~/.ssh/id_rsa ``` https://help.github.com/en/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
Rails 直接取得欄位的值(before_type_cast)
How to get column value from ActiveRecord without being affect by overrided attributes? 有時候會想要直接取得 database 裡的值,以免被 model 裡覆蓋的 attribute...
Function Composition in Ruby
Ruby 2.6 introduced `<<` and `>>` methods, this article explains function composition and history of Ruby new methods throughoutly, wort...
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...
What’s Object Marshalling?
## What’s Object Marshalling? Object Marshalling 將資料轉換,用在不同程式間傳輸,就稱為 Object Marshalling (序列化) 例如 Sidekiq 會把 Object 變成 JSON 存進 ...
DNS Challenge for HTTPs
簡而言之 你要在DNS加一筆record 每次更新他會要求你放不同的值 https://community.letsencrypt.org/t/i-am-confused-about-dns-challenge/18754
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...
在 middleware 裡塞 user 給 sidekiq 就不用每次傳
我個人是不喜歡啦,但可以參考一下有趣的作法 https://blog.bigbinary.com/2018/12/05/passing-current-user-by-default-in-sidekiq.html
ignore and untrack committed file with git
``` git rm --cached config/database.yml ``` https://www.git-tower.com/learn/git/faq/ignore-tracked-files-in-git https://askjong.com/howt...
Linux 防火牆: iptables
搜尋 linux 防火牆的話就會看到 `iptables` 和 `ufw`,常常會誤認為有兩個防火牆軟體,但其實不是,因為 `ufw` 只是 `iptables` 的 CLI 而已,參考: https://ubuntuforums.org/showthread.php?t=...
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...
ActiveSupport::Inflector#parameterize
好用的 helper ```rb "123-this-is-標題-haha".parameterize #=> "123-this-is-haha" ``` 很適合拿來做 friendly url 的簡單解決方案 ```rb class Post < Applica...
Rails disable some unwanted generators
Rails 常常會自動產生一些不需要的檔案,可以在 generator 裡面設定,放在 `config/application.rb` 裡面就行 記錄一下個人常用的設定 ```ruby # factory_bot config.generators do...
Heoku pipeline stateless builds
Heroku pipeline 從 staging promot 到 production 的時候會把 compile 過的 file 一起 copy 過去,所以如果用到 Node + Webpack 讀取環境變數再 compile 成 js 的 file 就會吃到 st...
在 Rails 使用 custom font
參考 https://coderwall.com/p/v5c8kq/web-fonts-and-rails-asset-pipeline 把檔案放到 `app/assets/fonts` 裡面 然後在 `application.scss` 裡加上 ```scss @...
Ruby instance_eval & block
```rb class Test def initialize(&block) @name = "Hello World" instance_eval(&block) if block_given? end def name @n...
Ansible + rails + letsencrypt
First, install nginx roles/nginx/tasks/main.yml ```yml - name: Install nginx apt: name: nginx state: latest - name: Disable ...
Implement algolia autocomplete with rails
```js // app/javascripts/application.js const algoliasearch = require('algoliasearch/lite') const autocomplete = require('autocomplete....
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 ...