ubuntu, ror, jQuery, css, website memo
太嫩, 沒啥心得, 用來紀錄每天學的
2009年9月7日 星期一
railscast skill
Refactoring User Name Part 2
<br /># models/user.rb<br />def full_name<br /> [first_name, middle_initial_with_period, last_name].compact.join(' ')<br />end<br /><br />def middle_initial_with_period<br /> "#{middle_initial}." unless middle_initial.blank?<br />end<br />
Fun with Find Conditions
Task.find(:all, :conditions => ["complete=? and priority=?", false, 3])<br />Task.find(:all, :conditions => ["complete=? and priority IS ?", false, nil])<br />Task.find(:all, :conditions => ["complete=? and priority IN (?)", false, [1,3]])<br />Task.find(:all, :conditions => ["complete=? and priority IN (?)", false, 1..3])<br /><br />Task.find(:all, :conditions => { :complete => false, :priority => 1 })<br />Task.find(:all, :conditions => { :complete => false, :priority => nil })<br />Task.find(:all, :conditions => { :complete => false, :priority => [1,3] })<br />Task.find(:all, :conditions => { :complete => false, :priority => 1..3 })<br /><br />Task.find_all_by_priority(1..3)<br />
Multibutton Form
<br /><!-- projects/new.rhtml --><br /><% if params[:preview_button] %><br /> <%= textilize @project.description %><br /><% end %><br />...<br /><%= submit_tag 'Create' %><br /><%= submit_tag 'Preview', :name => 'preview_button' %><br /><br /><br /><br /># projects_controller.rb<br />def create<br /> @project = Project.new(params[:project])<br /> if params[:preview_button] || !@project.save<br /> render :action => 'new'<br /> else<br /> flash[:notice] = "Successfully created project."<br /> redirect_to project_path(@project)<br /> end<br />end<br />
Refactoring Long Methods
<br /># models/cart.rb<br />def shipping_price<br /> if total_weight == 0<br /> 0.00<br /> elsif total_weight <= 3<br /> 8.00<br /> elsif total_weight <= 5<br /> 10.00<br /> else<br /> 12.00<br /> end<br />end<br /><br />def total_weight<br /> @total_weight ||= line_items.to_a.sum(&:weight)<br />end<br /><br /># models/line_item.rb<br />def weight<br /> for_download? ? 0 : product.weight<br />end<br />
Cleaning Up the View
<br /><br /><br /><!-- views/carts/show.rhtml --><br /><% title "Shopping Cart" %><br /><br /><table id="line_items"><br /> <tr><br /> <th>Product</th><br /> <th>Qty</th><br /> <th class="price">Unit Price</th><br /> <th class="price">Full Price</th><br /> </tr><br /> <%= render :partial => 'line_item', :collection => @cart.line_items %><br /> <tr><br /> <td class="total price" colspan="4"><br /> Total: <%= number_to_currency @cart.total_price %><br /> </td><br /> </tr><br /></table><br /><br /><!-- views/carts/_line_item.rhtml --><br /><[tr class="<%= cycle :odd, :even %>"]><br /> <td><%=h line_item.product.name %></td><br /> <td class="qty"><%= line_item.quantity %></td><br /> <td class="price"><%= free_when_zero(line_item.unit_price) %></td><br /> <td class="price"><%= free_when_zero(line_item.full_price) %></td><br /><[/tr]><br /><br /><br /><br /># models/line_item.rb<br />def full_price<br /> unit_price*quantity<br />end<br /><br /># models/cart.rb<br />def total_price<br /> line_items.to_a.sum(&:full_price)<br />end<br /><br /># helpers/carts_helper.rb<br />def free_when_zero(price)<br /> price.zero? ? "FREE" : number_to_currency(price)<br />end<br />
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言