# All talk but no code...

## Rails 4.1 upgrade and FactoryGirl.create with association

I was trying to upgrade from Rails 4.0 to Rails 4.1. Some specs broke, and I found a small behaviour change.

So my user has_one profile.

Prior to my upgrade, the profile association will not be loaded before/after FactoryGirl.create call.

However after Rails 4.1

This means I have to be careful to not have stale profile in tests, calling reload more often.

P.S. my factory_girl gem has remain unchanged in version 4.4.0 and factory_girl_rails gem in 4.4.1. So this is solely due to Rails behavior change.

## 從 sqlite 轉換到 postgresql

sqlite 的 data dump 其實需要更改不少地方，才能用在其他資料庫。這部份很容易犯錯，所以我想找其他方法。

## 注意的地方

• 有使用 foreign key 的話，記得依照 key 的 dependency 把匯入的順序調整，被 Key 指向的 table 移到前面。
• 匯出前也要先把 orphan record 先刪除（就是有 foreign key 指向已經不存在的資料時）
• 匯入前，先轉換資料庫然後跑 migration
• 匯入後，每個 table 的 id 並不會自動設成從目前最大的 id 開始繼續遞增。必須跑類似 ALTER SEQUENCE product_id_seq RESTART WITH 1453; 來對每個 table 一一作設定，不然的話可能新增的 record 會從前面沒有的 id 開始建立。
• 以前在 MySQL 或是 Sqlite 時似乎沒有指定 order 就會以 primary key 自動作排序。但是在 Postgresql 我發現丟回來的東西順序不是這樣（我看到的是相反的 id desc）。這代表 query 都得手動加入 order(:id) 才能確保跟以前一樣以 id desc 排序。

## Middleware模式的隨想

When I am integrating offsite payments in ActiveMerchant, I always need to provide an endpoint to receive payment notificaiton from gateway providers. After a couple of projects, I have come up with a best-practice to do it, and I thought this might be useful for others.

I have a PaymentNotification class solely to record notifications. This is because I want to decouple the persistence of notification from the order status change. If something goes wrong when I change Order, the notification will still be safely persisted in the database for later analysis.

As you can see, the controller action is pretty light. It only saves the notification. The notification request is setup so it contains order_id and payment method.

The interesting part is the PaymentNotification model. It has a few columns for persisting different information (mostly serialized). It is also linked to Order, for easier analysis.

The process method is to check the notification's validity and trigger order status update. It is to be called separately (maybe via a scheduled job). A few kinds of custom exceptions may be raised during process, because they are handled differently. Either way, errors will be recorded in the errors column.

Any improvement is welcomed :D

## 歐付寶有 < 跟 & 就會認證失敗

<input id="TradeDesc" name="TradeDesc" type="hidden" value="&lt;&amp;" />

THX.

API 文件並沒有標明不能特別限制特殊符號，經過測試其他特殊符號都可以使用。但是就只有 <& 不行。是說這兩個符號也沒有特殊到哪裡去，要是英文商店用個 & 字元也是很正常的吧。

THX.

## ActiveMerchant / offsite_payments gem needs to improve

ActiveMerchant is a very useful library for connecting to payment gateways. However it is also quite dated, with many flaws, and lack of proper documentation in one part (offsite-payment). In my experience, I feel it can be improved in the following ways:

The name "integration" in confusing, and makes it diffcult to search online. The gem should be split into two, one for on-site payment (gateway/billing module), and one for off-site payment (integration/Billing::Integration module). This is because the two are very different ways of paying. It can be confusing when you search for solutions but it turns out to be not what you want. Seems Shopify is alreadying doing that, splitting the offsite-payment module into its own gem. Good move!

The semantic of common fields in form helper need to be defined. Otherwise implementors will choose different names for the same concept, greatly reducing the interchangeability between gateways. Some consistency issues should also be fixed. For example in Paypal we pass our order id as the "order" field, and Paypal notification will return that as item_id attribute. This mismatch is confusing.

The gross() currently does not specify a return type. Paypal returns a string. Base class says it should "the money amount we received in X.2 decimal". Though there is a amount() method for returning a money object, the gross() method should still specify the return type to avoid inconsistencies when switching gateways.

The base framework code should be separated from gateway implementations. Users usually just want two or three gateways out of the 50 implementations. Having a separate repository per gateway also makes it easier to document gateway specific changes and settings in separate readme files.

There should be a check() method in the notification. It acts as the central method to check if the notification is valid. Currently there are many which implemented the acknowledge() function. However not all providers provide an API to verify the notification. With the generalized check() method it can call acknowledge() if it is implemented.

Adding hooks for form helper would be helpful. Many gateways require post-processing such as adding a checksum field. A hook would allow the interface to automatically call these post-processing instead of the developer having to remember calling them in view.

## Markdown 或是 LATEX

Ｍarkdown 跟 LATEX 其實要解決的是不同的問題，也就是你要產生需要排版有頁數的實體文件，還是就只是一長串的文件？

LATEX 要解決的一個核心問題就是必須解決分頁時放置圖片或是表格的問題。如果沒有分頁，那麼這些東西自然依照文字排放往下排即可，可是因為這些東西要是正好排在分頁邊緣時，不能腰斬只顯示一半。此外，因為分頁所以排版上也冒出許多限制，這些都是 LATEX 的強項。

## TrueCrypt 官網突然說自己不安全

TrueCrypt 是一個開源免費的加密軟體，能製作加密過的虛擬硬碟，供使用者存放資料。

TrueCrypt 本身雖然是開源軟體，不過似乎沒人知道後面的作者到底是誰。所以也引來許多陰謀論，比如說 TrueCrypt 本身是就是美國政府旗下製作的陷阱。或者這是作者跟NSA妥協下的結果。到目前為止似乎沒人能證明這不是網站被駭客修改。不過由於這次的改變十分細緻，比如說程式碼換了授權，而且還做了修正變成只能解碼。所以感覺不像是外來駭客會作的事情。

## chef, sunspot solr 與正體中文分詞

--

Solr 4.3.1 也跟 2.0.1 不相容。

solr_schema （與sunspot搭配所以直接修改 text fieldType）:

solr_solrconfig 添加:

hipsnip-solr cookbook 也要用新一點的，才會自動幫你把 logger lib裝好。

sudo ufw allow 8983

## RubyConf Taiwan 2014 感想

• 投影主螢幕太暗，尤其是跟旁邊的推特牆比的時候，要看很吃力。
• 講台上的人很少打光，常常處在背光面，我想攝影師應該也很辛苦才拍的出清楚又光線夠的講師演講照。
• 講台下不想聽的人要寫程式也因為沒有光線很容易眼睛酸痛。
• 無法轉換氣氛，導致會場有點悶悶的，也容易想睡。