2018年4月23日 星期一

slug 是啥意思?

https://stackoverflow.com/questions/4230846/what-is-the-etymology-of-slug

簡單說就是代稱的意思



2018年4月22日 星期日

第一年回顧

2017/4/4 ~ 2018/4/3

共發布 138 篇文章
草稿 27 篇


只達標 37.8%......

Rspec aggregate_failures syntax


意思就是在 aggregate_failures block 底下任一個的 expectation 有 error 時會針對每個 expectation 顯示 error 的 message

example

```ruby
    it "returns false if any of balance, locked, saving is negative" do
      negative_balance_account = build(:account, balance: -1)
      negative_locked_account = build(:account, locked: -1)
      negative_saving_account = build(:account, saving: -1)

      aggregate_failures "testing all senarios" do
        expect(negative_balance_account.has_negative_asset?).to be true
        expect(negative_locked_account.has_negative_asset?).to be true
        expect(negative_saving_account.has_negative_asset?).to be true
      end
    end
```

假設這個 test failed 了,那 console 的訊息會是像這樣:

```
  1) Account#has_negative_asset? returns false if any of balance, locked, saving is negative
     Got 2 failures from failure aggregation block "testing all senarios".
     # ./spec/models/account_spec.rb:15:in `block (3 levels) in <top (required)>'

     1.1) Failure/Error: expect(negative_locked_account.has_negative_asset?).to be true

            expected true
                 got false
          # ./spec/models/account_spec.rb:17:in `block (4 levels) in <top (required)>'

     1.2) Failure/Error: expect(negative_saving_account.has_negative_asset?).to be true

            expected true
                 got false
          # ./spec/models/account_spec.rb:18:in `block (4 levels) in <top (required)>'
```

會顯示哪幾個 test failed 了

當有很多 test case 很相近其實可以寫在一個senario 裡時用 aggregate_failures 會讓 test 變得更簡潔,但要小心不要亂用,該分開的 test 還是用 it 分開比較好。


https://relishapp.com/rspec/rspec-core/docs/expectation-framework-integration/aggregating-failures

2018年4月18日 星期三

Git don't blame, we are asking for context bot blaming people

Nice little trick to avoid blaming mindset

```
➜  ~ cat .gitconfig
[alias]
context = blame
```

git bisect

debugging 的時候可以使用,原理就是

1. 先找到確定沒有問題的 commit
2. git bisect 會用二分法切換到中間的 commit,然後就可以 test 是否存在相同的 bug,持續使用二分法直到找到有問題的 commit

2018年4月3日 星期二

PaperTrail whodunnit find object (ex: find User object)


PaperTrail::Verison object 的 methods 可以去 PaperTrail::VersionConcern 裡面查

查了一下沒有可以找到 whodunnit object 的方法,只好自己加一個:

```rb
#initializers/paper_trail.rb
PaperTrail::Version.class_eval do
  def whodunnit_object
     User.find_by(id: whodunnit)
  end
end
```


http://blog.nrowegt.com/papertrail-polymorphic-whodunnit/

2018年4月2日 星期一

釐清 DatabaseCleaner strategy 跟 clean_with


```rb
config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end
```

### 釐清 strategy 跟 clean_with

上面這段 code 指的是「當 test 開始時,用 truncation,然後之後都用 transaction」

為什麼要這樣呢?因為 truncation 比較慢 transaction 比較快

所以這就引發另一個問題,什麼是 truncation 什麼是 transaction?還有什麼其他的 strategy?

## Strategy

https://github.com/DatabaseCleaner/database_cleaner#what-strategy-is-fastest

可以選擇:

* transaction (優先使用)
* truncation
* deletion


這些 startegy 之間的差異:

https://stackoverflow.com/questions/10904996/difference-between-truncation-transaction-and-deletion-database-strategies



* transaction (優先使用): 代表把執行的 SQL 包在 `BEGIN TRANSACTION` 裡然後用 `ROLLBACK` 回逤成初始狀態
* truncation: 使用SQL `TRUNCATE TABLE` 直接把 table 清空
* deletion: 使用 SQL `DELETE FROM` 來刪除資料,速度慢

關於 delete 與 truncate:

https://dba.stackexchange.com/questions/30325/delete-vs-truncate

https://stackoverflow.com/questions/139630/whats-the-difference-between-truncate-and-delete-in-sql/12900557#12900557

有時間再寫關於 delete 與 truncate 之間的差別。