<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Boss Thinking &#187; i18n</title>
	<atom:link href="http://blog.bossyang.com/tag/i18n/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.bossyang.com</link>
	<description>photo, programming &#38; life</description>
	<lastBuildDate>Sat, 04 Sep 2010 03:32:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Review: Rails 3.0 release note</title>
		<link>http://blog.bossyang.com/rails-3-release-note.html</link>
		<comments>http://blog.bossyang.com/rails-3-release-note.html#comments</comments>
		<pubDate>Tue, 02 Mar 2010 03:21:05 +0000</pubDate>
		<dc:creator>Boss Yang</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Active Record]]></category>
		<category><![CDATA[AWAR]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[Rails 3]]></category>
		<category><![CDATA[Railties]]></category>
		<category><![CDATA[筆記]]></category>

		<guid isPermaLink="false">http://blog.bossyang.com/?p=370</guid>
		<description><![CDATA[Woooow~真的是好長一篇release note ，花了不少時間看過，順便作點摘要紀錄。第一個想法就是巨大的變革，大量的去耦合(decoupling)，引入merb 2設計風格，幾乎是嶄新的架構。 回想當時第一次接觸到Ruby on Rails框架時還是1.2版呢，不知不覺3.0 Beta已經釋出，大概過了近四年吧！書架上還有DHH (David Heinemeier Hansson)的Agile Web Development with Rails第一版哩，只不過到現在也沒用RoR寫出啥像樣的應用程式，我想可能還是不夠投入，工作環境裡也無法讓我應用該技術，但我仍持續地關心。一想到好不容易習慣2.x版了，突然要轉換到3.0可能沒那麼簡單，等到正式釋出再到現有應用轉換升級可還有一大段路要走呢！ Upgrading to Rails 3 需要Ruby 1.8.7+ 新的Rails Application物件 腳本script/* 換成 script/rails 套件依賴性改用bundler與Gemfile &#8220;config.gem is dead, long live bundler&#8221; 升級程序有外掛可協助 &#60;= 沒試過 更詳細的升級說明文件。 Rails Architectural Changes &#8230; <a href="http://blog.bossyang.com/rails-3-release-note.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Woooow~真的是好長一篇release note ，花了不少時間看過，順便作點摘要紀錄。第一個想法就是巨大的變革，大量的去耦合(decoupling)，引入merb 2設計風格，幾乎是嶄新的架構。</p>
<p><a href="http://pragprog.com/titles/rails3/agile-web-development-with-rails-third-edition"><img class="alignright" title="Agile Web Development with Rails, 3rd Edition" src="http://assets0.pragprog.com/images/covers/190x228/rails3.jpg?1236205162" alt="Agile Web Development with Rails, 3rd Edition" width="190" height="228" /></a> 回想當時第一次接觸到Ruby on Rails框架時還是1.2版呢，不知不覺3.0 Beta已經釋出，大概過了近四年吧！書架上還有DHH (David Heinemeier Hansson)的<a title="Agile Web Development with Rails, Third Edition" href="http://pragprog.com/titles/rails3/agile-web-development-with-rails-third-edition" target="_blank"><em>Agile Web Development with Rails</em></a>第一版哩，只不過到現在也沒用RoR寫出啥像樣的應用程式，我想可能還是不夠投入，工作環境裡也無法讓我應用該技術，但我仍持續地關心。一想到好不容易習慣2.x版了，突然要轉換到3.0可能沒那麼簡單，等到正式釋出再到現有應用轉換升級可還有一大段路要走呢！</p>
<p><span id="more-370"></span></p>
<h2>Upgrading to Rails 3</h2>
<ol>
<li>需要Ruby 1.8.7+</li>
<li>新的Rails Application物件</li>
<li>腳本script/* 換成 script/rails</li>
<li>套件依賴性改用bundler與Gemfile<br />
&#8220;config.gem is dead, long live bundler&#8221;</li>
<li>升級程序有<a title="Rails Upgrade plugin" href="http://github.com/rails/rails_upgrade" target="_blank">外掛</a>可協助 &lt;= 沒試過</li>
</ol>
<p>更詳細的<a title="The Path ro Rails 3: Approaching the upgrade" href="http://omgbloglol.com/post/353978923/the-path-to-rails-3-approaching-the-upgrade" target="_blank">升級說明文件</a>。</p>
<h2>Rails Architectural Changes</h2>
<p>架構有六個主要的變更</p>
<ol>
<li>Railties 重構</li>
<li>所有主要的核心元件去耦合</li>
<li>Active Model 抽象層</li>
<li>Controller 抽象化</li>
<li>Arel (Active Relation) 整合</li>
<li>郵件抽取 (Mail Extraction)</li>
</ol>
<h2>Railties</h2>
<p>我不太懂這個字眼要怎樣解釋，在framework裡就好像是一根一根的支架，連接設定與各種元件。</p>
<ul>
<li>每個應用程式現在豈有自己的名稱空間， 應用程式使用 YourAppName.boot 啟動，方便與其他應用程式溝通。</li>
<li>任何放在Rails.root/app下的程式都加入load path，所以你可以撰寫app/observers/user_observer.rb，Rails將會自動載入無須任何修改。</li>
<li>Rails 3.0提供一個 Rails.config物件，提供集中式的設定選項貯存區域。</li>
</ul>
<p>Railities generator也有大量的修改</p>
<ul>
<li>Generator完全重寫而且無法向下相容&#8230;.Oops!!</li>
<li>Rails template API 與 generator API 合併</li>
<li>generator不再從特定路徑載入，只搜尋Ruby載入路徑，例如呼叫 rails generate foo 將尋找 generator/foo_generator。 ??</li>
<li>新的generator允許你覆寫樣板，放置副本到RAILS_ROOT/lib/templates目錄下。</li>
<li>Rails::Generators::TestCase也被支援，所以你可以創建自己的generator並且測試它。</li>
</ul>
<p>Railties generator產生的view也有些更動</p>
<ul>
<li>不使用p標籤，改用div標籤</li>
<li>Scaffold 使用 <span style="color: #0000ff;">_form</span> partial，避免 edit 與 new 畫面重複程式碼。</li>
<li>Scaffold 表單現在使用 f.submit ，依照物件純入的狀態傳回 &#8220;Create ModelName&#8221; 或 &#8220;Update ModelName&#8221;字樣。</li>
</ul>
<p>最後 rake tasks 也做了加強：</p>
<ul>
<li>增加 <span style="color: #993366;">rake db:forward</span>，允許個別地向前啟用遷移檔。</li>
<li>增加<span style="color: #993366;"> rake routes CONTROLLER=x</span>，允許你只檢視特定controller的路由。</li>
</ul>
<p>Railties現在廢止：</p>
<ul>
<li>RAILS_ROOT =&gt; Rails.root</li>
<li>RAILS_ENV =&gt; Rails.env</li>
<li>RAILS_DEFAULT_LOGGER =&gt; Rails.logger</li>
</ul>
<p>PLUGIN/rails/tasks 與 PLUGIN/tasks 不再載入所有task，現在必須放到 PLUGIN/lib/tasks。<!--more--></p>
<h2>Internationalization</h2>
<p>國際化使用最新的<a title="github - I18n" href="http://github.com/svenfuchs/i18n" target="_blank">i18n gem</a>，詳細的變動可以參考<a title="Rails 3 I18n changes" href="http://blog.plataformatec.com.br/2010/02/rails-3-i18n-changes/" target="_blank"> Rails 3 I18n changes</a>。多國語言在ruby 1.8 做得不是很好，而Rails國際化常用的第三方解決方案有<a title="Ruby-GetText-Package" href="http://gettext.rubyforge.org/" target="_blank">Gettext</a>或是<a title="Globalize for Ruby on Rails" href="http://globalize-rails.org/" target="_blank">Globalize</a>。內建的I18n使用可參考ihower<a title="Rails I18n 多國語言" href="http://ihower.tw/training/rails-i18n.html" target="_blank">這篇</a>，Globalize可參考國網中心<a title="國際化 Internationalization" href="http://www.qweruiop.org/nchcrails/posts/40" target="_blank">這篇</a>或是英文<a title="Get stated with Globalize for Ruby on Rails" href="http://www.squidoo.com/globalize-rails" target="_blank">這篇</a>。Globalize採用資料庫系統儲存翻譯字串，與目前<a title="Open BlueWhale" href="http://openbw.org" target="_blank">OBW</a>作法一樣，好處就是方便撰寫前端維護介面。國際化與地區化議題很龐大，一般小型應用程式要做到這塊並不容易，就以前開發軟體經驗，為了支援英日語系介面，只好請外面的翻譯公司協助，拿到翻譯搞後仍需反覆校對，若是允許客製修改的系統，客戶可能會挑你翻譯毛病，你還得教他怎麼改(凸顯彈性介面語系字串維護介面的必要性)，或是幫他修改。更可怕的還有內容(資料面)的地區化呢！</p>
<h2>Action Pack</h2>
<p>學習RoR過程中遭遇最大挫折大概就是UI部份，這一向是我的罩門，JavaScript不熟，也不是很懂CSS與網頁設計，每每寫到顯示部份就頭疼。Rails 2.x內建的scaffold與helper還是過於基礎，對於進階的畫面控制你往往需要自行撰寫helper或使用AJAX，找現成的元件或他人的解決方案，很容易與原有畫面風格不搭配。我曾試著學習使用純JavaScript UI, like ExtJS，但過於費時反而降低開發效率，並不適合變動快速的企業需求(特別是台灣的中小企業!!)，與個人或微小團隊，如果自己或成員都是geek級的或許沒有問題吧？</p>
<h3>ActionView</h3>
<p>文中說明大量改寫原有的Action View helpers，實現unobtrusive JavaScript，異動清單提示的都是小的便利性的變更，並沒令人驚豔的東西。啥??HTML 5 ?!我承認很先進，但我不懂&gt;&#8221;&lt;。可能我比較貪心，希望View Helper預設提供陽春的多重選擇器，樹狀元件等，這在目前公司的系統還挺常用的。</p>
<ul>
<blockquote>
<li>You no longer need to call <tt>h(string)</tt> to escape HTML output, it is on by default in all view templates. If you want the unescaped string, call <tt>raw(string)</tt>.</li>
<li>Helpers now output HTML 5 by default.</li>
<li>Form label helper now pulls values from I18n with a single value, so <tt>f.label :name</tt> will pull the <tt>:name</tt> translation.</li>
<li>I18n select label on should now be :en.helpers.select instead of :en.support.select.</li>
<li>You no longer need to place a minus sign at the end of a ruby interpolation inside an ERb template to remove the trailing carriage return in the HTML output.</li>
<li>Added <tt>grouped_collection_select</tt> helper to Action View.</li>
<li>Action View now will raise exceptions if CSS stylesheets and javascript files listed in the <tt>javascript_include_tag</tt> and <tt>stylesheet_include_tag</tt> helpers are missing.</li>
<li><tt>content_for?</tt> has been added allowing you to check for the existence of content in a view before rendering.</li>
</blockquote>
</ul>
<h2>Active Model</h2>
<p>這是個新玩意兒，特地從原來的ORM中切分出來，扮演了ORM的抽象層。以後如果要替原來的ActiveRecord寫擴充，改由實做Active Model介面。Yehuda Katz寫了一篇<a title="Make Any Ruby Object Feel Like ActiveRecord" href="http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/" target="_blank">如何讓一般Ruby物件像ActiveRecord</a>，說明了ORM抽象化與Action Pack介面的好處。</p>
<h3>Validations</h3>
<p>資料模型驗證一直是我非常欣賞ActiveRecord的地方，如今搬移到Active Model內，如此一般的物件也可以引用，又是去耦合帶來的好處。之前使用時覺得方法名稱又臭又長，例如 <span style="color: #993366;">validates_numericality_of,  validates_presence_of</span>，經常記不住，加上選用參數又不太一樣，教人有些困擾。現在提供</p>
<div class="codecolorer-container ruby twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">validates <span style="color:#ff3333; font-weight:bold;">:attribute</span>, option_hash</div></div>
<p>簡便寫法，就好記許多。選項部份大致上差不多，容易理解。</p>
<blockquote><p>:acceptance =&gt; Boolean<br />
:confirmation =&gt; Boolean<br />
:exclusion =&gt; { :in =&gt; Ennumerable }<br />
:inclusion =&gt; { :in =&gt; Ennumerable }<br />
:format =&gt; { :with =&gt; Regexp, <img src='http://blog.bossyang.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> n =&gt; :create }<br />
:length =&gt; { :maximum =&gt; Fixnum }<br />
:numericality =&gt; Boolean<br />
:presence =&gt; Boolean<br />
:uniqueness =&gt; Boolean</p></blockquote>
<p>自訂驗證類別主要繼承ActiveMode::EachValidator，並撰寫validate_each(record, attribute, value)方法，放在lib下就會自動載入，透過名稱慣例很容易就可以在ActiveRecord類別中引用，參考一下範例應該不難理解。<!--more--></p>
<h2>Active Record</h2>
<p>許多玩RoR大概都是被這個神奇套件吸引，我也不例外(成了Java叛徒!!)。查詢介面整合了名為Arel關聯技術(relational algebra)，將令原來的查詢語法到了3.2後就不支援，例如以前可能會寫find(:first), find(:all)，改成first與all。詳細的範例可參考<a title="Active Record Query Interface" href="http://m.onkey.org/2010/1/22/active-record-query-interface" target="_blank">這篇</a>。</p>
<h3>Patches and Deprecations</h3>
<p>額外的修正最讓我注意的是大量的Oracle adapter的臭蟲修正，目前公司資料庫以Oracle為主軸，能不能正確地透過AR操作現有資料庫，是RoR被列入企業開發平台考量的重要關鍵。</p>
<blockquote><p>A large amount of work done on the Oracle adapter as well with many bug fixes.</p></blockquote>
<h2>Active Resource</h2>
<p>Active Resource也被抽離到Active Model。這部份大致上都是處理XML, JSON之類的，自己暫時用得較少，先前就算要提供給ExtJS的Json格式資料，也是透過ActiveRecord查詢結果轉換(to_json, to_ext_json)，所以囉&#8230;</p>
<h2>Active Support</h2>
<p>這個套件本來就是一堆拉拉雜雜的輔助方法構成，因此3.0大改版當然也包括一堆細碎的修正，就不多說了，自己看吧。</p>
<h2>Action Mailer</h2>
<p>電子郵件在企業應用程式內大量使用，幾近氾濫，三不五時就發通知信，有員工遲到就發出勤異常通知，有帳款沒收回來也發催收信，連宿舍租約快到期都要發（不知道其他公司是不是也是這樣@@）。新的Action Mailer揚棄原有的TMail，使用新的<a title="mikel's mail - GitHub" href="http://github.com/mikel/mail" target="_blank">Mail</a> gem改寫API。『郵件類別集中到app/mailer資料夾』，管理起來應該比之前的直覺一點。</p>
<h2>小結</h2>
<p>Rails 3.0 修改幅度實在太大，這表示現有的插件可能都不能用了，未來考慮升級時，除了原本的程式碼翻寫，還得重新檢討選用的插件，如果是一個全新的開始，或許就不那麼痛苦了吧！？</p>
<p>註一：圖片與鍊結都是書本AWDR第三版</p>
<h3>reference</h3>
<ul>
<li>原文參考：<a title="Rails 3.0: Release Notes" href="http://guides.rails.info/3_0_release_notes.html" target="_blank">Rails 3.0: Release Notes</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.bossyang.com/rails-3-release-note.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
