2009年12月31日 星期四

Changing Your Origin

Changing Your Origin

目前這樣只到可以pull的地步

但是如果需要push 到不同的remote 端 還需要修改 .git/config
需要補上push 的部份

2009年12月16日 星期三

fb comment

FBML 和 XFBML 不同阿 小心 orz

http://wiki.developers.facebook.com/index.php/Fb:comments
http://wiki.developers.facebook.com/index.php/Fb:comments_(XFBML)

2009年12月8日 星期二

write rails plugin memo

The Basic of Creating Rails Plugins

3 Extending Core Classes
This section will explain how to add a method to String that will be available anywhere in your rails app.

http://guides.rubyonrails.org/plugins.html#extending-core-classes

2009年12月3日 星期四

Paginating with the Fragment Cache

Paginating with the Fragment Cache


group count

2009年11月29日 星期日

fbml in iframe canvas by serverfbml

Fb:serverfbml

Resizable IFrame

Resizable IFrame
JavaScript Client Library
Note: You must include the <[script]> tag that loads the FeatureLoader in the <[body]> element, not in the <[head]> element, because the FeatureLoader writes body elements directly to the document, and some browsers report an error if this happens when FeatureLoader is in the <[head]> tag.
http://wiki.developers.facebook.com/index.php/Cross_Domain_Communication_Channel

2009年11月22日 星期日

rjs page call

RJS Page.call
Passing objects from Rails RJS to a javascript function call without quoting the values?
http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html

 
#有些時候page.call paramter work 不是很正常
page.call "get_details", "info-id", "message","{phone:5487895, mobile:9888565875}"
page.call("buildYUITreeView", "project_tree", @current_project.get_directory_contents(@current_project.local_path, 0))
#可以用這樣的方式去取代
page << "get_details('info-id','message','{phone:5487895, mobile:9888565875}')"
page << "buildUYITreeView('project_tree', #{@current_project.get_directory_contents(@current_project.local_path, 0))})"


respond_to do |format|
format.js {render(:update) { |page| page << 'alert("Wrong Nick");'}}
end


update_page do |page|
page.insert_html :bottom, 'list', "
  • #{@item.name}
  • "
    page.visual_effect :highlight, 'list'
    page.hide 'status-indicator', 'cancel-link'
    end

    2009年11月17日 星期二

    Problem updating to Rubygems 1.3.5

    更新又失敗了

    直接用source 裝吧

    http://railsforum.com/viewtopic.php?id=34044 <= Problem
    http://docs.rubygems.org/read/chapter/3 <= install rubygem

    2009年11月16日 星期一

    facebook iframe use fbml

    http://wiki.developers.facebook.com/index.php/Fb_force_mode

    除了用xfbml之外, 還有這招可以用

    For example, if you were using an iframe application, and wanted one page to be rendered as FBML, you could append ?fb_force_mode=fbml to the address, which will cause it to render as FBML. This can be particularly useful when using fb:request-form with iframe applications.

    If your application is an iframe application and you wish to use FBML, you will need to specify the full address, including http://apps.facebook.com.

    2009年11月15日 星期日

    include vs joins

    http://railscasts.com/episodes/181-include-vs-joins

    2009年11月13日 星期五

    dynamic asset_host

    google seo memo

    Google SEO Starter Guide
    github meta tag plugin

    1. title
    <[title]> 標記應該放在 HTML 文件的 <[head]> 標記區段中。
    理想情況下,您應該為網站的每一個網頁建立獨特的網頁標題

    您的文件出現在搜尋結果網頁中,標題標記的內容通常會出現在搜尋結果的第一行



    2. meta desc
    Google 網站管理員工具」提供了好用的內容分析工具,可以指出太短、太長或過分重複的描述中繼標記使用簡短但描述明確的標題

    improve-snippets-with-meta-description


    3. 有意義的url
    http://www.google.com/support/webmasters/bin/answer.py?answer=76329
    在網址中使用文字
    建立簡單的目錄架構
    為同一文件提供同一版本的網址

    如果您確實發現有使用者透過不同的網址來瀏覽同樣的內容,您可以設定 301 重新導向,將不喜歡的網址重新導向到您所期望使用的網址

    請避免:
    無論從子域還是根目錄 (例如「domain.com/page.htm」和「sub.domain.com/page.htm」) 都可以瀏覽到相同的內容
    在您的內部連結架構中混合使用 www. 和非 www. 版本的網址
    在網址中使用奇怪的大寫字母 (大部分使用者喜歡小寫的網址,並且小寫也便於使用者記憶)

    4. 讓您的網站更易於瀏覽

    您也可以使用 Google 的「網站管理員工具」提交 XML Sitemap (字母大寫),讓 Google 更容易發現您網站上的網頁。使用 Sitemap 還有一個好處 (雖然我們無法做出保證),就是您可以使用它來告知Google 哪個版本的網址是您偏好的標準網址 (例如:http://brandonsbaseballcards.com/ 或http://www.brandonsbaseballcards.com/;請瀏覽更多關於什麼是偏好網域的詳情)。Google 協助建立了開放原始碼的 Sitemap 產生器指令碼,以幫助您為自己的網站建立 Sitemap 檔案。如需有關Sitemap 的進一步資訊,請前往網站管理員說明中心,瀏覽實用的 Sitemap 檔案指南。

    5. 網站導覽實作典範

    建立自然流暢的層級架構
    請避免:
    建立複雜的導覽連結網。例如,將網站上都每個網頁都連結至所有其他網頁
    過度細分內容 (使用者必須要按二十次才能找到位於深層的內容)

    導覽中盡量使用文字連結
    請避免:
    完全依靠下拉功能表、圖片或動畫來建立導覽 (雖然大多數搜尋引擎能夠發現
    網站的此類連結,但如果讓使用者能透過正常文字連結到達網站上的所有網頁,
    將可以提高網站的可用性;請瀏覽更多關於 Google 如何處理非文字檔案的
    詳情)

    使用「麵包屑」導覽 — 麵包屑導覽是指在網頁頂端或底部放置一排內部連結,讓訪客可以
    快速回到上一個網頁或根網頁。大多數的麵包屑導覽通常會將最具概括性的網頁 (通常是根網
    頁) 放在最左邊的第一位,越靠近右邊,列出的網頁包含的內容更越具體。

    在您的網站上放置 HTML Sitemap 網頁,並使用 XML Sitemap 檔案 — 一個包含您網站
    上所有網頁或最重要網頁 (如果您有成百上千個網頁) 的簡單 Sitemap 網頁,將會非常有用。

    請避免:
    在 HTML Sitemap 網頁中放置無效連結,使得網頁沒什麼用處
    建立的 HTML Sitemap 僅簡單列出網頁,而沒有進行整理,例如依主題整理

    請避免:
    讓搜尋引擎建立 404 網頁的索引 (請務必將網站伺服器設定為當使用者要求的
    網頁不存在時,能傳回 404 HTTP 狀態碼)
    只提供類似於「Not found」、「404」等模糊的訊息,或者甚至連 404 網頁都沒有
    404 網頁的設計與您網站的其他網頁不一致

    6. 撰寫更佳的錨定文字
    這個錨定文字有時會告知使用者和 Google 有關您要連結之網頁的部分內容。您網頁上的連結可能是
    內部連結 (即指向您網站上其他網頁的連結),也可能是外部連結 (即指向其他網站之內容的連結)。
    無論是哪種情況,您的錨定文字寫得越詳細,使用者就越容易瀏覽,Google 也越容易瞭解您所連結的
    網頁內容。

    請避免:
    撰寫一般的錨定文字,比如「網頁」、「文章」或「按一下此處」
    使用偏離主題或與要連結之網頁內容無關的文字
    在多數情況下,使用網頁網址作為錨定文字 (即使某些情況可以這麼做,例如

    7. 適當使用標題標記
    標題標記 (請不要將其和 <[head]> HTML 標記或 HTTP 標頭混淆) 用於為使用者呈現網頁的結構。標
    題標記有六種不同大小,從 <[h1]> 到 <[h6]>,其重要性依次降低。

    請避免:
    在標題標記中放置對定義網頁結構沒有任何幫助的文字
    在適合使用其他標記 (例如 <[em]> 和 <[strong]>) 的地方,不要使用標題標記
    隨意調整標題標記的大小

    8. 最佳化圖片的使用
    圖片似乎是比較簡單的網站元件,但您可以對圖片的使用進行最佳化。所有圖片都有不同的檔案名稱和
    「alt」屬性,您可善加利用這兩個特點。

    請避免:
    儘可能使用像「image1.jpg」、「pic.gif」、「1.jpg」這樣的通用檔案名稱 (有
    些網站的圖片超過數千張,因此可能會為圖片自動命名)
    撰寫過長的檔案名稱
    在替代文字中堆砌關鍵字,或者複製並貼上完整的句子

    將圖片儲存在專用的目錄中 — 考慮將您的圖片合併到一個單獨的目錄中 (例如
    brandonsbaseballcards.com/images/),而不是將圖片散佈在整個網域的很多目錄和子目錄下。
    這樣可以簡化圖片的連結路徑。

    9. 利用 robots.txt
    「robots.txt」檔案可告知搜尋引擎是否可以存取您網站的某些部分,進而對這些部分進行檢索。此檔案
    必須要命名為「robots.txt」,並放置在您網站的根目錄中。

    http://baseball.com/robots.txt

    user-agent: *
    Disallow: /images/
    Disallow: /search
    所有符合條件的搜尋引擎漫遊器 (標有萬用字元符號 *) 都不會存取和檢索 /images/ 下的內容,
    或者任何以 /search 作為路徑開頭的網址

    「Google 網站管理員工具」中有一個很好用的
    robots.txt 產生器,可協助您建立此檔案。請注意,如果您的網站使用了子網域,且您不希望搜尋引擎檢索特定子網域中的某些網頁,則您必須為該子網域建立一個單獨的 robots.txt 檔案。如需更多有關
    robots.txt 詳情,我們建議您參閱有關使用 robots.txt 檔案的網站管理員說明中心指南。


    對敏感的內容使用更為安全的方法 — 您或許對使用 robots.txt 來封鎖敏感或保密的材料感
    覺不是特別放心。其中一個原因是,如果網際網路上碰巧存在連至您封鎖的網址的連結 (如參照記錄),則搜尋引擎仍然可以參照該網址 (只是顯示網址,而不顯示標題或片段內容)。此外,不遵守漫遊器排除標準的一些非符合條件的或惡意搜尋引擎可能會違背您的 robots.txt 指示。
    最後,好奇的使用者可能會查看您 robots.txt 檔案中的目錄或子目錄,並猜中您不願被看到
    的內容的網址。對內容加密或使用 .htaccess 對內容進行密碼保護是更安全的選擇。

    10. 瞭解連結的 rel="nofollow"

    將某個連結的「rel」屬性值設定為「nofollow」便會告知 Google 不應隨著您網站上的某些連結連至
    他處,也不應將您網頁的信譽情況傳給連至的網頁。不隨著某個連結連至他處的方法是,在連結的錨定
    標記中新增 rel="nofollow"。

    <[a href='http://www.shadyseo.com/' rel='nofollow'>Comment spammer<[/a]>


    最後,如果您有意不隨著網頁中的任何連結連至他處,則可以在您的 robots 中繼標記中使用
    「nofollow」,將這個 robots 中繼標記放入該網頁 HTML 的 <[head]> 標記中。網站管理員中心網誌
    提供了一篇有關使用 robots 中繼標記的很有益的文章。這種方法的寫法是 <[meta name="robots"
    content="nofollow"]>。



    建立並提交 Sitemap

    2009年10月28日 星期三

    simple memo, initializer run

    Class Rails::Initializer

    2009年10月26日 星期一

    Symmetric Encryption vs Asymmetric Encryption

    http://support.oss.org.tw/wiki/index.php/

    Symmetric Encryption – 對稱式加密簡介
    其特色在於加解密所使用的金鑰(key)是相同的!
    ●兩方要進行秘密訊息的傳送之前,必須協議出一個共同的,用來加解密的金鑰(key)以及所使用的演算法;如此訊息一來一往之間,才可以被正確的加解密。
    ●當然這樣的方式會衍生出一些問題,因此才會有 Asymmetric Encryption(非對稱式加密) 的產生,來改善一些 Symmetric Encryption 的缺點;然而 Symmetric Encryption 在實際的應用上有其實用性,因此目前還是持續不斷的被使用當中

    在加解密運作的機制中,有兩大重要部分,除了金鑰(key)之外,另外一個即為加解密所使用的演算法(algorithm)。
    在實際應用上,金鑰(key)是一個檔案,內容為一群無意義的英文與數字的集合,長度範圍有可能介於 64~1024 bits (甚至更大) 之間,妥善由通訊雙方秘密保管好。
    而演算法(algorithm)則是透明公開的,隨時可以從網路或是其他地方取得演算法的相關使用方法與規則。



    Asymmetric Encryption (非對稱式加密) 即是為了改良 Symmetric Encryption(對稱式加密) 的缺點而產生的。
    在對稱式加密中,通訊雙方往來的訊息是由同一把金鑰進行加密;假設 A 與 B 通訊,兩個人必須有一把相同的金鑰;而若是 A 也要與 C 通訊,則 A 與 C 則必須擁有另外一把不同的金鑰;否則若是都使用相同金鑰,C 就可以解密出 A 要給 B 的訊息,相對的,B 也可以解密出 A 要給 C 的訊息,如此一來資訊在傳送就不再安全!
    除此之外,還要防範金鑰被竊取的問題,只要通訊雙方任一方把金鑰洩露出去,就破壞了原本建立的安全機制了。
    因此,Asymmetric Encryption(非對稱式加密) 使用了一對金鑰(key pair)的方式解決了這個問題

    在 Asymmetric Encryption 的架構中,要通訊的雙方都各持有一對金鑰,分別是私鑰(private key)以及公鑰(public key)。
    private key 是要妥善且由自己秘密保管的,而 public key 則是可以公開出去。
    假設使用者 A 有一對金鑰,若是 B 要與 A 進行通訊,則 B 必須使用 A 所提供的 public key 進行加密,再將加密的內容傳送給 A,接著 A 可以用自己的 private key 進行解密。同樣的,A 要是要傳訊息給 B,則是要使用 B 所提供的 public key 進行加密,而 B 則可以用自己的 private key 進行解密。
    有趣的是,雖然訊息是由 public key 所加密,但是卻無法利用 public key 將原本的訊息還原回來,這就是非對稱式加密的精華所在,也是目前非常受到歡迎的原因。而這一來一往之間,所使用的演算法,即為 RSA 演算法。

    如此看來,其實 RSA 的加解密過程是很簡單的,因此重點就是在於金鑰的產生,目前已經被證明長度 1024 bits 不夠安全,因此建議使用長度為 2048 bits 的金鑰作為加解密之用,來提升重要資訊傳輸的安全性。
    最後,雖然非對稱式加密解決了金鑰交換的問題,但是卻衍生出加解密效率不彰的問題,因為相較於 DES,RSA 的速度只有其百分之一不到



    Asymmetric + Symmetric

    真的沒有更好的 solution 嗎? 當然是有的...只要巧妙的結合兩者的優點即可啦!! 其中的原理很簡單,假設 A 要傳訊息給 B,就會發生大概類似以下流程:
    (1) A 透過對稱式演算法,產生出一把對稱式加密用的金鑰
    (注意:這把金鑰只用在這一次的傳輸)
    (2) 接著 A 使用 B 所提供的 public key,將這把金鑰加密,
    並將加密後的內容傳給 B
    (3) B 接收到後,使用自己的 private key 解密,取得這把
    一次性金鑰
    (4) 之後雙方訊息的往來,都使用對稱式加密
    如此一來,就不僅解決了金鑰交換的問題,也解決了非對稱式加解密效率不彰的問題。
    當然,這樣的 solution 其實還不算最安全,因此才會有 Digital Signature(數位簽章) 的誕生

    facebook 發post ( story ) 到 wall

    http://wiki.developers.facebook.com/index.php/Feed_forms
    http://wiki.developers.facebook.com/index.php/FB.Connect.streamPublish
    http://wiki.developers.facebook.com/index.php/Attachment_%28Streams%29
    http://wiki.developers.facebook.com/index.php/Stream.publish

    http://wiki.developers.facebook.com/index.php/XFBML
    http://blog.yangtheman.com/2009/08/23/how-to-integrate-facebook-connect-with-a-rails-app/

    這邊用的是iframe canvas下的 push 方式, 基本上是跟 facebook connect 相同的

    需要include XFBML 和 facebook connect 的 js

    2009年10月21日 星期三

    Authorizing Applications, share button

    Authorizing_Applications
    share button

    Once a user has authorized your application, you can utilize the following URLs, which you specify in your application's settings in the Developer application:

    * Post-Authorize Callback URL: Facebook pings this URL when a user authorizes your application for the first time. You can also call users.isAppUser to determine if the user has authorized your application.
    * Post-Authorize Redirect URL: You can redirect a user to this URL after the user authorizes your application for the first time. You can use this URL only if the user authorizes your application through login.php and not the login dialog.
    * Post-Remove Callback URL: Facebook pings the URL when a user removes your application.


    For FBML canvas page applications, the parameters are sent as POST parameter. For iframe applications, the parameters are sent as GET parameters, unless you are preloading FQL using the fb_iframe_post option, in which case the parameters are sent as POST parameters.

    Choosing between an FBML or IFrame Application(2)

    Now, with XFBML, in addition to being able to embed things like fb:name and fb:profile-pic directly into your HTML, you can render any FBML tag by using server-side FBML. If you wrap your FBML inside fb:serverfbml tags, as in:


    ...then this opens an IFrame to Facebook on the page being served from your domain, and Facebook will render the FBML in there.

    However, a fair amount of overhead is involved in opening the IFrame, and your CSS won't propagate into the FBML IFrame, so you won't want to put too many blocks of server-side FBML on a page. But if you need one or two snippets of FBML to make your application richer, or you want a large chunk of your page to use FBML, this makes that possible and pretty easy.




    Preload FQL


    Preload FQL lets you do these sorts of queries on both FBML and IFrame canvas pages, though it works differently for each. In general, you specify a few regular expressions that map to lists of FQL queries. If the page being requested matches any of the regular expressions, the corresponding FQL queries will be executed by Facebook when the user's browser sends the request to Facebook.

    For FBML applications, the results get sent along to your application's servers when Facebook requests the FBML from you. Most of the time, this makes calls to the Facebook API unnecessary, even when you need fairly complicated data from Facebook to generate the page.

    This is what the flow would look like for an FBML canvas page using preload FQL

    FBML Canvas Page Using Preload FQL



    Preloading FQL works differently with IFrames. In order for Facebook to send data to your server along with the request for the IFrame, it has to encode the data in the URL being requested. Instead of sending the FQL result set to your server, the Facebook server sends the FQL result data down to the user's browser, attached to the outer frame that contains the Facebook chrome on the canvas. You can choose whether you want to have a lightweight redirect occur, which encodes the FQL results in the request made for your IFrame page. Or, if you only want to access that data on the client side, you can use the PreloadFQL_get method from the JavaScript client library and rewrite parts of the page on the fly using the data after it's initially rendered.


    IFrame Canvas Page using Preload FQL


    Note that the preload FQL will only work on the first page that loads in your IFrame. If you point your links to apps.facebook.com URLs using target="_top" instead of just linking to different pages within the frame, you could use preload FQL on every page, but this would make all your pages load slowly

    Choosing between an FBML or IFrame Application(1)

    Choosing between an FBML or IFrame Application


    Traditional IFrame Canvas Page


    FBML Canvas Page



    XFBML as part of Facebook Connect as a way to quickly get social content onto any Web page, and since IFrames are just Web pages, XFBML can speed those up as well.

    Before XFBML, IFrame applications always had to make API calls to render social content like user names and profile pictures, and those API calls required another round trip communication between your server and Facebook before sending back content to the user's browser

    with XFBML, you can embed various FBML tags like fb:name and fb:profile-pic directly into the HTML that your application sends to the user's browser. Then you simply include some Facebook-specific JavaScript, and that code gets executed and scans the DOM for those tags. The JavaScript then determines all the data needed to render that content and batches it up into one API call from the user's browser to Facebook

    The rest of the page that isn't social content can render to the user before the API call completes. And in XFBML, we cache data on the browser so that in many cases, it isn't even necessary to make any API call to Facebook at all.

    IFrame Canvas Page Using XFBML - First Page Load by a User


    IFrame Canvas Page Using XFBML - Subsequent Page Loads by a User

    2009年10月19日 星期一

    找該加index 的地方

    rails_indexes

    check index exist

    add_index 之前檢查一下

    2009年10月18日 星期日

    mustache 把view logic 的部份再拆出來

    就是要讓view 更乾淨把logic 的部份再拆出來
    mustache

    Ruby on Rails Code Quality Checklist

    Ruby on Rails Code Quality Checklist

    1. Each controller action only calls one model method other than an initial find or new.
    (Make custom .new or .update methods in the model with all necessary). ...
    就是盡量把邏輯擺到model 裡, 唯一的例外就是在決定要render view or redirect的時候


    2. Only one or two instance variables are shared between each controller and view. ...
    保持一兩個instance variable 在controller 和 view 就好
    這樣可以讓所有相關連的只有在需要的時候在被call 到, 而且還可以做instance-variable cache in one place
    例如不要在controller 裡用到 @post and @related_posts 兩個instance variable, 而是create a method 給 @post, 這樣你就可以在 view 裡面直接用 @post.related_posts


    3. All model and variable names are both immediately obvious (to a new developer) and as short as possible without using abbreviations. ...
    取個好名字


    4. All custom "finds" accessed from more than one place in the code use named_scope instead of a custom method. ...
    有關find 什麼東西的, 都用named_scope 來做


    5. A .find or .find_by_ is never called in a view or view helper. ...
    別直接在view , find or find_by anything, 至少用一下namde_scope or custom methods to do that


    6. There is zero custom code that duplicates functionality of a built-in function in rails. ...
    7. Code has been aggressively DRYed during development. ...
    8. All functionality used in two or more models has been turned into a library/module. ...
    9. All logic duplicated between two or more apps has been turned into a gemified plugin. ...

    10. STI is not used anywhere ...
    別用STI, 用module 來代替, 如果你用STI 你會需要至少bind 兩個model, 這樣你很難回頭
    data migration 你會死
    但是polymorphic association 是鼓勵的


    11. Every design choice should yield the most simplistic design possible for the need of users at the current time.
    No guesses for future functionality were designed into the application. ...
    對於現在的情況做最簡單的設計, 不要猜測未來的情況, 你可能會猜錯


    12. Close to full test coverage exists at the highest level of the application: on and between controller actions. Coverage is highest for code used by the most number of end users. ...

    先從user 最常用的開始測起
    13. All tests pass before code is merged into a shared repository. ...

    merge 前要測過
    14. Every fixed defect on a deployed product has tests added to prevent regression. ...
    替解掉的defeat 加上測試

    15. Every plugin installed has been code reviewed. ...
    檢查一下你的plugin code

    Install any HTML theme/template into your Rails app

    Install any HTML theme/template into your Rails app
    install_theme
    open source web design

    幫你用來快速套template 的

    2009年10月17日 星期六

    [Scale] Scale Frontend 應該注意的五件事

    [Scale] Scale Frontend 應該注意的五件事
    Rails in the Wild: 5 Client-Side Performance Observations

    1. It’s easy to forget to compress your JavaScript and CSS

    要記得 :cache => true
    要記得壓 js 和 css

    2. Watch out for slow third party services

    都是慢在第三方
    google ads, analytics 也會托慢, 請用 postload_google_ads


    3. Using multiple image hosts doesn’t always mean higher performance

    拆那麼多台未必會比較快

    4. S3 is NOT a Webserver!

    S3 是 storage ,不是 web server。所以不要一股腦的把東西往上丟,要注意有沒有敏感資訊,而且 s3p 是沒有幫忙做 gzip 的。

    5. Most performance variability is NOT attributable to page factors

    速度慢通常是慢在後端..

    2009年10月16日 星期五

    clearance 的 generator

    clearance
    Class Rails::Generator::Commands::Create


    JavaScript 中的 document.createDocumentFragment

    JavaScript 中的 document.createDocumentFragment

    在這段程式碼中,使用了 document.createDocumentFragment() 這個函式產生一個 document fragment,你可以把它想作是一個處理 DOM element 的 buffer,把欲生成的 DOM element 先加入到這個 fragment 中,最後再一次加到頁面的 DOM 結構中,這樣的作法比起在迴圈中一直 appendChild 來得快上許多,當然更重要的是,問題也被解決了 :)

    2009年10月6日 星期二

    ruby binary code

    有時候想要return 一些binary code 東西要怎樣帶勒

    2009年10月5日 星期一

    console tips

    Hirb - Irb On The Good Stuff
    可以看到sql 結果的


    可以看到sql 指令的

    has_many , time now, assocation

    Do you know when your code runs?
    有時候你時候你會發現你在你下的conditions 有關於時間的, 跑起來有點怪怪的

    在development 沒問題, 在production 卻不正常

    仔細看會發現時間統統都是static 的了, 因為production 並不會每次都去reload class

    這時候只要換個寫法

    2009年10月2日 星期五

    跨browser 播音樂

    http://www.htmlcodetutorial.com/embeddedobjects/_EMBED.html

    2009年10月1日 星期四

    Facebook app memo

    Facebook development: choosing FBML or iframe as your application type
    http://apps.new.facebook.com/facebooker_tutorial/

    1. Set up New application
    http://www.facebook.com/developers/

    2. remember your api key and serect key

    3. Canvas/畫布 setting



    api key, serect key

    they let our application verify that requests are coming from Facebook.



    Facebook sends us the ID of the current user and their whole list of friends on
    every request. That makes our life quite a bit easier. It also can cause
    some security problems.



    All requests come from Facebook. To make sure this is the case, we can
    verify the signature that is sent by Facebook on every request. A digital
    signature is a way to use cryptography to verify that something actually
    came from the person who it appears to be from.4 Facebook sends a
    number of parameters that start with fb_sig. All these parameters are
    used in the signature validation.

    When Facebook sends our applications a request, it builds a string
    that includes all the fb_sig parameters in alphabetical order. It then
    adds our secret key to the end of that string and calculates the MD5 sum.

    When Facebooker receives a request, it goes through the
    same steps to recalculate the signature. If the value that Facebooker
    calculates matches the one in our request, it proves that the request
    came from somebody who knows our secret key.



    Rails included a feature to stop cross-site forgery
    attacks. Unfortunately, this feature doesn’t play well with Facebook.
    We’ll need to disable it by editing environment.rb and setting allow_forgery_
    protection to false,







    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

    2009年8月31日 星期一

    在model 用url_for

    Calling url_for from a Model in Rails
    url_for’ method – according to its Rails source code documentation – is only available to controllers, views and mailers by default. (It’s actually a method found in the ActionController module so it’s no surprise that ActiveRecord classes don’t have access to this method.)

    2009年8月28日 星期五

    資料流重導向
    Rake in Background

    daemon

    Custom Daemon
    Cron Jobs in Ruby on Rails

    2009年8月21日 星期五

    2009年8月20日 星期四

    Rails Rake

    Ruby on Rails Rake Tutorial (aka. How rake turned me into an alcoholic)
    How do I write Rake tasks for my Rails application?


    Can I access my Rails Models inside a Task?

    2009年8月5日 星期三

    unique, auto_increment

    execute "ALTER TABLE tou_service ADD UNIQUE (service_index)"
    execute "ALTER TABLE tou_service CHANGE service_index service_index int(11) DEFAULT NULL AUTO_INCREMENT"

    2009年7月24日 星期五

    ruby benchmark

    Benchmarking Makes it Better

    2009年7月19日 星期日

    X-Sendfile

    mod_xsendfile
    Reference : Advanced Rails p121

    The X-Sendfile protocol is a very simple standard, first introduced in the Lighttpd
    web server, which directs the web server to send a file from the filesystem to the client
    rather than a response generated by the application server.

    X-Sendfile uses the X-Sendfile HTTPheader pointing to the server’s path to the file
    to send, in conjunction with the other standard HTTPheaders. A typical response
    using X-Sendfile would look something like this:


    From Rails, you can set the response headers by modifying the response.headers
    hash:

    2009年7月18日 星期六

    delegate

    Rails delegate method


    那個 :to => :user 其實是由customer object 再去找 user 的 instance method
    所以其實可以在 overwirte 掉做其他的處理

    2009年7月16日 星期四

    Metric Fu

    Metric Fu railscast
    ASCIIcast Metric Fu
    About metric_fu 1.1.4
    flog_file error
    test_helper error

    apache 設定

    一些apache 設定 Etags and expire gzip compression
    Speed up your Apache/Passenger Rails app in 2min

    capistrano, fast_remote_cache

    fast_remote_cache

    37signals 用hard link 取代 copy 的方式

    因為有需要一直用新的branch 更新, cache_copy 有需要一直變換其svn repository

    所以需要overwrite 一下 fast_remote_cache 的一些 methods

    ssh 免打密碼自動登入 memo

    from here
    在 自己電腦 Client 端


    server 端

    2009年7月15日 星期三

    capistrano 指令順序