<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: helpers code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 12 May 2008 11:04:45 GMT</pubDate>
    <description>DZone Snippets: helpers code</description>
    <item>
      <title>Rails helpers for A List Apart No. 256</title>
      <link>http://snippets.dzone.com/posts/show/5435</link>
      <description>I cooked up some Ruby on Rails helpers for testing the techniques in the article &lt;a href="http://www.alistapart.com/articles/accessibledatavisualization"&gt;Accessible Data Visualization with Web Standards&lt;/a&gt; from A List Apart &lt;a href="http://www.alistapart.com/issues/256"&gt;No. 256&lt;/a&gt;. If you want to know more about Rails, check out &lt;a href="http://www.alistapart.com/issues/257"&gt;No. 257&lt;/a&gt;. If you want to be smarter, read &lt;a href="http://www.alistapart.com/"&gt;A List Apart every week&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;def chartlist(data)&lt;br /&gt;  total = data.inject(0.0) { |sum, datum| sum + datum[:count] }&lt;br /&gt;  bars = ''&lt;br /&gt;&lt;br /&gt;  data.each do |datum|&lt;br /&gt;    link  = content_tag 'a', datum[:name], :href =&gt; datum[:href]&lt;br /&gt;    count = content_tag 'span', datum[:count], :class =&gt; 'count'&lt;br /&gt;    index = content_tag 'span', "(#{(datum[:count]/total*100).to_i}%)", :class =&gt; 'index', :style =&gt; "width: #{(datum[:count]/total*100).to_i}%"&lt;br /&gt;    bars &lt;&lt; content_tag('li', link &lt;&lt; count &lt;&lt; index)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  content_tag 'ul', bars, :class =&gt; 'chartlist'&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Sample data for a chartlist (&lt;a href="http://www.alistapart.com/d/accessibledata/example-barchart.html"&gt;example&lt;/a&gt;)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;data = [{ :name =&gt; 'Apples',   :count =&gt; 420, :href =&gt; 'http://www.example.com/fruits/apples/' },&lt;br /&gt;        { :name =&gt; 'Bananas',  :count =&gt; 280, :href =&gt; 'http://www.example.com/fruits/bananas/' },&lt;br /&gt;        { :name =&gt; 'Cherries', :count =&gt; 200, :href =&gt; 'http://www.example.com/fruits/cherries/' },&lt;br /&gt;        { :name =&gt; 'Dates',    :count =&gt; 100, :href =&gt; 'http://www.example.com/fruits/dates/' }]&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;def sparkline(data)&lt;br /&gt;  max = data.sort.last.to_f&lt;br /&gt;  sparklines = ''&lt;br /&gt;&lt;br /&gt;  data.each_with_index do |datum, index|&lt;br /&gt;    count_string = datum.to_s&lt;br /&gt;    '(' &lt;&lt; count_string if index == 0&lt;br /&gt;    count_string &lt;&lt; ',' if index != data.length&lt;br /&gt;    count_string &lt;&lt; ')' if index == data.length&lt;br /&gt;    count = content_tag 'span', count_string, :class =&gt; 'count', :style =&gt; "height: #{(datum/max*100).to_i}%"&lt;br /&gt;    index = content_tag 'span', count &lt;&lt; ' ', :class =&gt; 'index'&lt;br /&gt;    sparklines &lt;&lt; index&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  content_tag('span', sparklines, :class =&gt; 'sparkline')&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Sample data for sparklines (&lt;a href="http://www.alistapart.com/d/accessibledata/example-sparklines.html"&gt;example&lt;/a&gt;)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;data = [60, 220, 140, 80, 110, 90, 180, 140, 120, 160, 175, 225, 175, 125]&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;def timeline(data)&lt;br /&gt;  max = data.sort { |a, b| a[:count] &lt;=&gt; b[:count] }.last[:count]&lt;br /&gt;  bars = ''&lt;br /&gt;&lt;br /&gt;  data.each do |datum|&lt;br /&gt;    label = content_tag 'span', datum[:label], :class =&gt; 'label'&lt;br /&gt;    count = content_tag 'span', "(#{datum[:count]})", :class =&gt; 'count', :style =&gt; "height: #{(datum[:count]/max.to_f*100).to_i}%"&lt;br /&gt;    link  = content_tag 'a', label &lt;&lt; count, :href =&gt; datum[:href], :title =&gt; "#{datum[:label]}: #{datum[:count]}"&lt;br /&gt;    bars &lt;&lt; content_tag('li', link, :style =&gt; "width: #{(100.0/data.length).to_i}%")&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  content_tag 'ul', bars, :class =&gt; 'timeline'&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Sample data for a timeline (&lt;a href="http://www.alistapart.com/d/accessibledata/example-timeline.html"&gt;example&lt;/a&gt;)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;data = [{ :date =&gt; '2007-12-01', :count =&gt;  40, label =&gt; '1' },&lt;br /&gt;        { :date =&gt; '2007-12-02', :count =&gt; 100, label =&gt; '2' },&lt;br /&gt;        { :date =&gt; '2007-12-03', :count =&gt; 150, label =&gt; '3' }]&lt;/code&gt;</description>
      <pubDate>Sun, 27 Apr 2008 23:01:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5435</guid>
      <author>sporkyy (Todd Sayre)</author>
    </item>
    <item>
      <title>PHP Select Form Helper</title>
      <link>http://snippets.dzone.com/posts/show/5035</link>
      <description>// takes an array of values and a value to match, and outputs formatted &lt;option&gt;s with the &lt;option&gt; matching $match selected&lt;br /&gt;// must be manually wrapped in &lt;select&gt;&lt;/select to allow for maximum flexibility&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function selectHelper($values, $match)&lt;br /&gt;{&lt;br /&gt;  $keys = array_keys($values);&lt;br /&gt;  $i = 0;&lt;br /&gt;	&lt;br /&gt;  foreach($values as $option)&lt;br /&gt;  {&lt;br /&gt;    $selected = null;&lt;br /&gt;		&lt;br /&gt;    if($match == $keys[$i])&lt;br /&gt;      $selected = " selected";&lt;br /&gt;			&lt;br /&gt;    echo "	&lt;option value=\"".$keys[$i]."\"$selected&gt;".$option."&lt;/option&gt;\n";&lt;br /&gt;		&lt;br /&gt;    $i++;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;//sample usage:&lt;br /&gt;$values = array(&lt;br /&gt;  "lb" =&gt; "Pounds",&lt;br /&gt;  "ea" =&gt; "Each",&lt;br /&gt;  "oz" =&gt; "Ounces");&lt;br /&gt;				&lt;br /&gt;  selectHelper($values, $product-&gt;unit);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 25 Jan 2008 02:13:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5035</guid>
      <author>nathancarnes (Nathan Carnes)</author>
    </item>
    <item>
      <title>Mask all but last 4 digits</title>
      <link>http://snippets.dzone.com/posts/show/3049</link>
      <description>If you need to mask all but the last 4 digits of a nubmer - like a credit card number - this helper works nicely.&lt;br /&gt;&lt;br /&gt;Just pass it a number or string.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def mask_number(number)&lt;br /&gt;  number.to_s.size &lt; 5 ? number.to_s : (('*' * number.to_s[0..-5].length) + number.to_s[-4..-1])&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 28 Nov 2006 08:51:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3049</guid>
      <author>jrm02t ()</author>
    </item>
    <item>
      <title>Block to Partial Rails Helper</title>
      <link>http://snippets.dzone.com/posts/show/2483</link>
      <description>From &lt;a href="http://errtheblog.com/post/13"&gt;http://errtheblog.com/post/13&lt;/a&gt;.&lt;br /&gt;Create a helper which takes a block and renders that block within the context of a partial.&lt;br /&gt;&lt;br /&gt;Create this helper:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def block_to_partial(partial_name, options = {}, &amp;block)&lt;br /&gt;  options.merge!(:body =&gt; capture(&amp;block))&lt;br /&gt;  concat(render(:partial =&gt; partial_name, :locals =&gt; options), block.binding)&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Create this helper, too:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def sidebar_module(title, options = {}, &amp;block)&lt;br /&gt;  block_to_partial('shared/sidebar_module', options.merge(:title =&gt; title), &amp;block)&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Create this partial (app/views/shared/_sidebar_module.rhtml):&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;div &lt;%= %[id="#{id}"] unless id.blank? %&gt; class="sidebar_module"&gt;&lt;br /&gt;  &lt;h2&gt;&lt;%= title %&gt;&lt;/h2&gt;&lt;br /&gt;  &lt;%= body %&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Use it in your views:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;% sidebar_module 'Recent Stories' do %&gt;&lt;br /&gt;  &lt;%= render :partial =&gt; 'recent_stories', :collection =&gt; @recent_stories %&gt;&lt;br /&gt;&lt;% end %&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 28 Aug 2006 05:49:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2483</guid>
      <author>err (Chris Wanstrath)</author>
    </item>
    <item>
      <title>Javascript Link Helper</title>
      <link>http://snippets.dzone.com/posts/show/1943</link>
      <description>Ruby on Rails helper to create a link tag to 'name' wich execute the 'javascript' statement onclick event. The last both parameters are the same as for link_to.&lt;br /&gt;&lt;br /&gt;Helper para o Ruby on Rails para criar um tag de link associado ao 'name' que executa o c&#243;digo 'javascript' no evento onclick. Os &#250;ltimos dois par&#226;metros s&#227;o os mesmos utilizados no link_to&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;	def javascript_link_to(name, javascript, html_options = nil, *parameters_for_method_reference)&lt;br /&gt;		options = [ :action =&gt; @params["action"] ]&lt;br /&gt;		html_options = { :onclick =&gt; javascript+"return false" }&lt;br /&gt;		link_to(name, options, html_options, *parameters_for_method_reference)&lt;br /&gt;	end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 20 Apr 2006 10:50:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1943</guid>
      <author>rafee (Rafael Lima)</author>
    </item>
    <item>
      <title>PNG-24 Alpha support for IE</title>
      <link>http://snippets.dzone.com/posts/show/1836</link>
      <description>What so much webdesigners dream about!&lt;br /&gt;Get alpha channel on web, that's possible with PNG-24 images and this trick.&lt;br /&gt;BE CAREFUL: that seem to work only for 10 images per page.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  # Display PNG-24 images with alpha channel on IE&lt;br /&gt;  # BE CAREFUL: with this trick, only 10 PNGs seems to be supported by IE&lt;br /&gt;  # Don't forget to set the size of your div&lt;br /&gt;  def transpng(id, png)&lt;br /&gt;    '&lt;style type="text/css"&gt;&lt;br /&gt;      &lt;!--&lt;br /&gt;        ' + id + ' {&lt;br /&gt;          background-image: url(' + png + ');&lt;br /&gt;        }&lt;br /&gt;      --&gt;&lt;br /&gt;    &lt;/style&gt;&lt;br /&gt;    &lt;!--[if IE]&gt;&lt;br /&gt;    &lt;style&gt;&lt;br /&gt;      ' + id + ' {&lt;br /&gt;        background-image: none;&lt;br /&gt;        filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=' + png + ', sizingMethod=\'scale\');&lt;br /&gt;      }&lt;br /&gt;    &lt;/style&gt;&lt;br /&gt;    &lt;![endif]--&gt;'&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 04 Apr 2006 02:03:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1836</guid>
      <author>Seb (S&#233;bastien Grosjean)</author>
    </item>
    <item>
      <title>Using helpers inside a controller</title>
      <link>http://snippets.dzone.com/posts/show/1799</link>
      <description>Ripped from&lt;br /&gt;http://gabriel.gironda.org/articles/2006/02/08/using-helpers-inside-a-controller&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is incredibly straightforward and more of an occasional convenience, but I thought I'd throw it out there anyway. You may find that sometimes, even though the controller obviously isn't the view, that you want to use some of the helper methods available.&lt;br /&gt;&lt;br /&gt;The only case I've come across so far is wanting to use pluralize() in a flash message and not have to do it by hand using the inflector. You could include ActionView::Helpers::TextHelper in the controller, but that fills your namespace with crap.&lt;br /&gt;&lt;br /&gt;Put this in the class ApplicationController instead:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def help&lt;br /&gt;    Helper.instance&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  class Helper&lt;br /&gt;    include Singleton&lt;br /&gt;    include ActionView::Helpers::TextHelper&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Then you can just use it like so:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def check_for_max_donkeys&lt;br /&gt;    if Donkey.find_fit_donkeys.size == APP_SETTINGS['max_fit_donkeys']&lt;br /&gt;      flash_error "The maximum of #{help.pluralize(APP_SETTINGS['max_fit_donkeys'], 'donkey')} has been reached."&lt;br /&gt;      redirect_to_index&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Update: Don't use the method name "helper" because Rails already uses that. Just "help" works fine.</description>
      <pubDate>Wed, 29 Mar 2006 11:16:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1799</guid>
      <author>Namor ()</author>
    </item>
    <item>
      <title>Alternative zebra stripes approach using a helper</title>
      <link>http://snippets.dzone.com/posts/show/411</link>
      <description>This is a tidy way of alternating classes on some element.&lt;br /&gt;&lt;br /&gt;In the view:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    &lt;tr class="&lt;%= alternate %&gt;"&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;In your application helper:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def alternate(str1 = "odd", str2 = "even")&lt;br /&gt;   @alternate_odd_even_state = true if @alternate_odd_even_state.nil?&lt;br /&gt;   @alternate_odd_even_state = !@alternate_odd_even_state&lt;br /&gt;   @alternate_odd_even_state ? str2 : str1&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 22 Jun 2005 18:38:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/411</guid>
      <author>davidnorth (David North)</author>
    </item>
    <item>
      <title>Custom captures</title>
      <link>http://snippets.dzone.com/posts/show/390</link>
      <description>I made a layout with some slightly ugly code for making rounded boxes with CSS and HTML, and wanted a way to automate the boxes. I had thought of having two functions to print starting and ending code, but this seems nicer.&lt;br /&gt;&lt;br /&gt;This goes with the other application helpers:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def rounded_box(&amp;block)&lt;br /&gt;    concat '&lt;div class="rounded"&gt;&lt;div class="top"&gt;&lt;/div&gt;&lt;div class="body"&gt;' + capture(&amp;block) + '&lt;/div&gt;&lt;div class="bottom"&gt;&lt;/div&gt;&lt;/div&gt;', block.binding&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This goes in a view:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;% rounded_box do %&gt;&lt;br /&gt;&lt;br /&gt;... stuff to put in the rounded box ...&lt;br /&gt;&lt;br /&gt;&lt;% end %&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 17 Jun 2005 09:18:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/390</guid>
      <author>mdaines (Michael Daines)</author>
    </item>
  </channel>
</rss>
