2009年9月29日 星期二

Session Management

When a new session is created, Rails automatically sends a cookie to the browser containing the session id

The data structure takes the form of a hash, identified by a unique session id, a 32-character string of random hex number



Disabling Sessions for Robots


Selectively Enabling Sessions


The Controversial CookieStore


Timing Out and Session Life Cycle
Session Timeout Plugin for Rails



Cookies

2009年9月25日 星期五

Heroku memo

Deploying Ruby Web Applications to Heroku
custom domain
學習 HeroKu 的架構設計
generate gem file by rake

2009年9月23日 星期三

亂看 redmine code memo

2009年9月22日 星期二

A Month in Rails

A Month in Rails

Master-slave
Why I am not using Masochism for my master-slave setups and why monkey-patching isn’t the only solution

Password security

Securely store passwords with bcrypt-ruby; now compatible with JRuby and Ruby 1.9


A rails plugin to store a collection of boolean attributes in a single ActiveRecord column as a bit field.
把boolean 欄位用一個integer column 取代
flag_shih_tzu

security model
acts_as_lockbox for your Rails Models

Observational: Better Observers for ActiveRecord

Observational: Better Observers for ActiveRecord
一個比較好的observer
1. 老地方, 可是寄信基本上這應該不干 User 的事, 所以把它搬走


2. 把它搬到 observer 裡, 可是如果很多地方都要寄信, 就很麻煩, 要多一堆file


3. 所以就搬到寄信的地方 然後由它observe 看起來合情合理

cookie, session and request object

* Cookie物件

Cookie物件會藉由瀏覽器所提供Cookie功能,將資訊記錄在用戶端電腦裡面,也就是利用Cookie物件來紀錄判斷某個使用者是否曾經進入本網站,當瀏覽者離線,Cookie物件資料紀錄仍然存在用戶端電腦.(當瀏覽器停用cookie設定,此功能就會失效)

Session物件:紀錄Session中的各種描述資訊與邏輯關係,和Cookie不同的是Session可以儲存任意的物件.
Session處理過程:
當伺服器皆收用戶發送的請求後,ROR就會尋找一用戶Session ID,如果沒有該用戶Session ID,會立即建立一個Session,並且分配Session ID,同時同Cookie形式將Session ID保存到用戶瀏覽器上面.之後ROR會根據Session ID來找到保存在伺服器端的Session資料,並且利用Session物件來對Session做各種操作.



* request物件:應用於使用者各種請求,它允許開發者透過某些方法來請求資訊.

常用方法:
1.delete? (用於確定是否為一個刪除操作要求)
2.get? (用於確定是否為一個GET的請求)
3.post? (用於確定是否為一個POST請求)
4.host (用於取得使用者主機的IP位置)
5.protocol(用於確定請求的協議類型)
6.env (用於取得環境變數的資訊)

cron job 跑rake

How to Schedule a Ruby on Rails Rake Task Via Cron
*/5 * * * * cd /var/www/apps/rails_app/ && /usr/local/bin/rake RAILS_ENV=production rake_task:goes_here

注意它是用 /usr/local/bin/rake

2009年9月15日 星期二

git memo

Git 版本控制系統 (1)
Git 版本控制系統(2) 開 branch 分支和操作遠端 repo.
Git 原始碼管理
使用Git與Github管理軟體開發專案

git add . 加入所有檔案,包括所有還沒有被追蹤(untracked)的檔案
git add -i 進入互動模式,你甚至可以只 Add 檔案裡面其中的一段程式碼到 staging area 去(稱作patch)
git add -u 只加更新的檔案,不加入還沒有追蹤的檔案 (跟 git commit -a 涵蓋的範圍相同)


git commit -m "blah" 如果沒加 -m 的話, 會開編輯器輸入 commit log (可以在.gitconfig中設定編輯器)
git commit -a -m "foobar" 全部修改的檔案都 add 後 commit 出去 (不包括 untracked 的新檔案)
git commit -v 會開編輯器加上 diff 註解

git rm foobar 刪除
git mv old_file new_file 改檔名

git diff 是比較 working tree 跟 staging area
git diff --cached 是比較 staging area 跟本來的 repo.
git diff HEAD 是比較 working tree 跟本來的 repo.

git reset HEAD filename 會從 staging area 狀態回到 unstaging 或 untracked (檔案內容並不會改變)
git checkout filename 會從 unstaging 狀態回到最初 repo. 的檔案(檔案內容變回修改前)



git branch 建立本地 local branch
git branch -m 改名字 (如果有同名會失敗,改用 -M 可以強制覆蓋)
git branch 列出目前有那些 branch 以及目前在那個 branch
git checkout 切換 branch (注意到如果你有檔案修改了卻還沒 commit,會不能切換 branch,解法稍後會談)
git checkout -b () 本地建立 branch 並立即 checkout 切換過去
git branch -d 刪除 local branch

git merge 合併另一個 branch,若沒有 conflict 衝突會直接 commit。若需要解決衝突則會再多一個 commit。
git merge --squash 將另一個 branch 的 commit 合併為一筆,特別適合需要做實驗的 fixes bug 或 new feature,最後只留結果。合併完不會幫你先 commit。
git cherry-pick 321d76f 只合併特定其中一個 commit。如果要合併多個,可以加上 -n 指令就不會先幫你 commit,這樣可以多 pick幾個要合併的 commit,最後再 git commit 即可。



git push 預設的遠端是 origin,並且會將所有有和 remote 有對應的 local branch 都 push 上去。如果要把新的 local branch push 上去,需要下 git push origin 指令。



如果今天tom的test repo有了新的變更,告訴billy,billy要將變更merge到自己的repo中,可以在本地端輸入

git pull git://github.com/tom/test.git



pull這個指令其實涵蓋了fetch(將變更複製回來)以及merge。
因此經過merge後,tom的變更就加入到billy的repo囉!

另外版本控制系統的branch功能也很有意思,若您的程式碼同時要修改bug,又要加入新功能,可以fork出一個branch,一個專門修bug,一個專門加入新功能,等到穩定後再來merge

git branch bug_fix #建立branch,名為bug_fix
git checkout bug_fix #切換到bug_fix這個branch
git checkout master #換為主要的repo
git merge bug_fix #把bug_fix這個branch和現在的branch合併



若有branch在remote,想要查看並checkout出來

git branch -r #查看遠端branch
git checkout -b bug_fix_local bug_fix_remote #把本機端切換為遠端的bug_fix_remote branch,並命名為bug_fix_local



還有其他可以觀看repo狀態的工具

git log #可以查看每次commit的改變
git diff #可以查看最近一次改變的內容,加上參數可以看其他的改變,並互相比較
git show #可以看某次的變更

2009年9月14日 星期一

如何設計有效的佈局?

如何設計有效的佈局?

將任務相關的重要信息要直接表現在控件上。用戶更傾向於關注交互控件上的標籤,而不是輔助型的靜態文本。

* 以8px為橫向柵格單位,頁面所有元素寬度都可以是2的倍數,包括圖片和版塊寬度,這樣可以在一定程度上加快頁面(特別是對於J-PEG圖片)的渲染速度(基於計算機內部二進制的運算機制)。其在擴展和兼容性上也有一定優勢。

* 在阿里巴巴中文站中,佈局間距的最小單位為8px,佈局區塊採用32px(8px*4)和24(8px*3)兩種粒度單位,分別組成以下兩種可實現的柵格系統:

在綜合考慮當下主流分辨率情況、瀏覽器外觀對顯示空間的佔用、人機工程學中對水平視角和聚焦範圍的規定以及8px單位等多種因素後,我們認為960px是一個相對更加合理的頁面寬度。在阿里巴巴中文網站中,推薦使用定寬960px的頁面,去除左右各4px的邊距,中間的可視寬度為952px。

網頁編碼問題 與 META

一個常用又不知道是做什麼用的 META 設定

正確來說,這段 META 是定義「網頁送出的編碼字集」,而不是用來定義「該網頁應該顯示的字集」!

關於各瀏覽器對網頁與文字編碼的處理規則研究整理




當你的網頁編碼為 Big5 字集,但你需要表單送出時用 GB2312 字集送出的話,就可以利用以下步驟完成此任務:

1. 伺服器回應網頁要求時明確指定瀏覽器應顯示的字集

Response.ContentType = "text/html; charset=big5";

2. 在網頁 HTML 中加入 META 標籤,明確指定 Content-Type 與字集

<[meta http-equiv="Content-Type" content="text/html; charset=gb2312" /]>

git memo

github 不知道為為啥我複製出來的網址是https 的
所以跑出 remote HEAD refers to nonexistent ref, unable to checkout. 的錯誤

git clone git@github.com:ooxx 這樣的才對

2009年9月13日 星期日

Railscast skill(9)

Complex Partials
Rendering partials with layouts
view 重複的部份 不只是裡面 也有可能是外面, render 的參數也可以代layout
Partials can have their own layouts applied to them. These layouts are different than the ones that are specified globally
for the entire action, but they work in a similar fashion. Imagine a list with two types of users:


Customize Field Error
可以自訂error msg 的格式

2009年9月11日 星期五

Railscast skill(8)

Logging Variables

eval 執行後面的 ruby code
binding 可以把當時的環境存起來


The Logger
message 四個等級 debug, info , error, fatal
development default 是 debug, 表示debug 以上的都可以看到, mysql instruction message 是在debug mode 所以 production 看不到
production 是 info


YAML Configuration File

整理變數的好方法, 放在yml 裡, 還可以靠程式根據不同的環境load 進不同的變數

2009年9月10日 星期四

Railscast skill (7)

in_groups_of
定量分組時在用的


group_by Month
group_by 好棒, 想用啥分類就分

Railscast skill (6)

Restricting Access

helper_method 可以把某些controller 的method 弄到helper 一起用


Console Tricks

Handling Exceptions


Catch-all Route
可以利用這招把所有奇怪的route 路徑都拿去 在controller 裡面在做些有的沒的處理

Railscast skill (5)

Refactoring Out Helper Object

某些 method 需要很多其他的小 method 可是放在application helper 裡 又不是算大家都會用到的東西, 這時候就可以把它搬到外面弄成一個class,




Site Wide Announcements
又用了controller 的 javascripts 的技巧 不過這邊這個要注意一下 route 要加上format

2009年9月9日 星期三

Railscast skill (4)

Pretty Page Title

把content_for 弄簡潔了, yield 也要有default


Markaby in Helper
也可以在helper 裡用render :partial 或者是用content_tag 去拼view

oink , logger parser, memory

oink
Log parser to identify actions which significantly increase VM heap size

可以讓log 多出memory 的紀錄
Feb 08 11:39:54 ey33-s00302 rails[9076]: Memory usage: 316516 | PID: 9076

也可做report 來查 哪些request or action 耗 memory 比較多

benchmark

convenient benchmark

在 ~/.irbrc

就可以在consloe 輕鬆的做benchmark
>> benchmark{ User.find(2).records.wake.count}
0.00197 seconds (-1% change)

index , mysql slow qurey

A grand piano for your violin, 一篇關於哪些地方需要建index 的好文
table indexes in rails

找出 MySQL 哪些 Query 速度較慢
檢查 mysql sql 效能使用 MySQL Slow Queries
MySQL 常用指令筆記(問答)

Railscast skill (3)

Auto-Complete Association

2009年9月8日 星期二

Screencasts skill (II)

Dynamic Select Menus
可以用生個javascript stylesheets controller 來動態生js , css
在利用content_for 動態 include


Formatting Time


Performing Calculations on Models


Optimistic Locking

2009年9月7日 星期一

railscast skill

Refactoring User Name Part 2


Fun with Find Conditions


Multibutton Form


Refactoring Long Methods


Cleaning Up the View

2009年9月5日 星期六

Create Model Through Text Field

Create Model Through Text Field

daemon_generator vs Starling and Workling

Custom Daemon
Starling and Workling

Custom Daemon 比較適合就是讓daemon 去檢查 之後做事情

Starling 比較適合 就是真的把動作丟到background 去做, 有working 在, 有message queue 也可以配memcache

Positive and Negative Lookahead, regexp

Lookahead and Lookbehind Zero-Width Assertions
The negative lookahead construct is the pair of round brackets, opening bracket followed by a question mark and an exclamation point

把 1234567 這樣的數字在印出加上逗號變成 1,234,567 這樣的格式

2009年9月4日 星期五

hacking_activerecord, include module , extend, class_eval

Hacking ActiveRecord
module,class_eval,extend

memo, include rails environment and some File Dir method




Putting it in ~/web/lib is correct, but did you also require it from an initializer?

1. Create an initializer in ~/web/config/initializers called patches.rb
2. In the file put require File.join(Rails.root, 'lib', 'active_record_ndbcluster.rb')
3. Restart your webserver/Rails console

It should then work

rails , lib, module


2009年9月3日 星期四

block in view, yield, capture

Blocks in View
Pretty Blocks in Rails Views

2009年9月1日 星期二

subdomain-fu, subdomain

Railscast Subdomains-Fu
SubdomainFu: A New Way To Tame The Subdomain
Don't Forget To Reserve Subdomains For Yourself





Implementing A Simple Account Key System