<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: on code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 11 May 2008 20:30:01 GMT</pubDate>
    <description>DZone Snippets: on code</description>
    <item>
      <title>Custom validation with rails: words with more than 26 characters</title>
      <link>http://snippets.dzone.com/posts/show/5266</link>
      <description>This goes in whatever model you're validating.  "Subject" can be any of the symbols in your model and doesn't need the : in front of it.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def validate&lt;br /&gt;	if subject.split.any?{|w| w.length &gt; 26}&lt;br /&gt;		errors.add(:subject, "cannot have words more than 26 consecutive characters")&lt;br /&gt;	end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 21 Mar 2008 19:40:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5266</guid>
      <author>ioda006 ()</author>
    </item>
    <item>
      <title>Rails auto select text field</title>
      <link>http://snippets.dzone.com/posts/show/4561</link>
      <description>A simple helper to create those snazzy on-click-auto-select text fields you see on all the video-sharing sites.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def auto_select_text_field_tag(name, value = nil, options = {})&lt;br /&gt;  text_field_tag(name,&lt;br /&gt;                 value,&lt;br /&gt;                 { :onclick =&gt; "$(&#8217;#{name}&#8217;).select()",&lt;br /&gt;                   :readonly =&gt; "true" }.merge(options))&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A _little_ more info &lt;a href="http://bjhess.com/bjhessblog/2007/09/12/rails-auto-select-text-field-tag/"&gt;on my blog&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;~Barry Hess</description>
      <pubDate>Thu, 20 Sep 2007 20:10:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4561</guid>
      <author>bjhess (Barry Hess)</author>
    </item>
    <item>
      <title>Produce GIF or PNG barcode images from a Ruby on Rails application using RMagick and Gbarcode</title>
      <link>http://snippets.dzone.com/posts/show/4154</link>
      <description>This is a Ruby on Rails controller that produces PNG barcode images using RMagick (http://rmagick.rubyforge.org/) and Gbarcode (http://gbarcode.rubyforge.org/), the Ruby Gnu Barcode ( http://www.gnu.org/software/barcode/barcode.html ) wrapper. You will need to install RMagick and Gbarcode. &lt;br /&gt;&lt;br /&gt;On Mac OS X, you can use the Locomotive RMagick bundle (http://locomotive.raaum.org/bundles/index.html) if you install the gbarcode gem into it first, for instance: &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;% export GEM_HOME=/Applications/Locomotive2/Bundles/rmagickRailsMar2007_i386.locobundle/framework/lib/ruby/gems/1.8/&lt;br /&gt;% gem install gbarcode&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;To use it, save this code as barcode_controller.rb in the apps/controllers directory of your Rails application. Generate the barcodes using a URL like this:&lt;br /&gt;&lt;br /&gt;http://localhost:3000/barcode/get_barcode_image?string=foo&lt;br /&gt;&lt;br /&gt;This example uses the BARCODE_128 encoding; you can use different barcode encodings by adjusting the code. To produce PNG images, make sure you have have the PNG libraries installed on your system, and that your ImageMagick is compiled with PNG support, and then change 'image/gif' to 'image/png' and im.format = "GIF" to im.format = "PNG". &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# $RAILS_ROOT/apps/controllers/barcode_controller.rb&lt;br /&gt;class BarcodeController &lt; ApplicationController&lt;br /&gt;&lt;br /&gt;  def get_barcode_image&lt;br /&gt;     string_to_encode = params[:string]&lt;br /&gt;     barcode_image = BarcodeGenerator.get_barcode_image(string_to_encode)&lt;br /&gt;     send_data(barcode_image, :type     =&gt; 'image/gif',&lt;br /&gt;                                  :disposition =&gt; 'inline')&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# $RAILS_ROOT/app/helpers/barcode_generator.rb&lt;br /&gt;#&lt;br /&gt;# note: this will not work without rmagick (Ruby ImageMagick interface) and gbarcode (GNU barcode) &lt;br /&gt;# gems installed. rmagick needs ImageMagick plus dependencies.&lt;br /&gt;&lt;br /&gt;class BarcodeGenerator&lt;br /&gt;&lt;br /&gt;  # Uses subprocesses because &lt;br /&gt;  # 1. ImageMagick/RMagick leaks memory,&lt;br /&gt;  #    and doesn't work in a long-running process. The fork makes it safe.&lt;br /&gt;  # 2. The output from the Gbarcode and ImageMagick is often longer than the pipe buffer,&lt;br /&gt;  #    so we have to empty the buffer from another subprocess&lt;br /&gt;  def BarcodeGenerator.get_barcode_image(barcode_string)&lt;br /&gt;    return BarcodeGenerator.get_subprocess_output do&lt;br /&gt;                   barcode_generator = BarcodeGenerator.new&lt;br /&gt;                   $stdout.write(barcode_generator.get_barcode_image(barcode_string))&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def initialize&lt;br /&gt;    # we do the imports here to protect long-running processes (like mongrel) from ImageMagick's memory leaks&lt;br /&gt;    require 'RMagick'&lt;br /&gt;    require 'gbarcode'&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def get_barcode_image(string_to_encode)&lt;br /&gt;    if string_to_encode.nil?&lt;br /&gt;      string_to_encode = "No string specified"&lt;br /&gt;    end&lt;br /&gt;    string_to_encode = remove_rails_file_extension(string_to_encode)&lt;br /&gt;    eps_barcode = get_barcode_eps(string_to_encode)&lt;br /&gt;    gif_barcode = convert_eps_to_gif(eps_barcode)&lt;br /&gt;    return gif_barcode&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def remove_rails_file_extension(string_to_encode)&lt;br /&gt;    if string_to_encode[-4..-1] == ".png"&lt;br /&gt;      string_to_encode = string_to_encode[0..-5]&lt;br /&gt;    end&lt;br /&gt;    return string_to_encode&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def get_barcode_eps(string_to_encode)&lt;br /&gt;    barcode_object = Gbarcode.barcode_create(string_to_encode)&lt;br /&gt;    Gbarcode.barcode_encode(barcode_object, Gbarcode::BARCODE_128)&lt;br /&gt;    return BarcodeGenerator.get_subprocess_output do&lt;br /&gt;        Gbarcode.barcode_print(barcode_object, $stdout, Gbarcode::BARCODE_OUT_EPS)    &lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def convert_eps_to_gif(eps_image)&lt;br /&gt;    base64_eps_image = Base64.encode64(eps_image)&lt;br /&gt;    im = Magick::Image::read_inline(base64_eps_image).first&lt;br /&gt;    im.format = "GIF"&lt;br /&gt;    return BarcodeGenerator.get_subprocess_output do&lt;br /&gt;       im.write($stdout) &lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  # execute a block's code in a subprocess, returning any output&lt;br /&gt;  def BarcodeGenerator.get_subprocess_output()&lt;br /&gt;    data = ""&lt;br /&gt;    IO.popen('-', 'r+') do |child_filehandle|&lt;br /&gt;      if child_filehandle&lt;br /&gt;        begin&lt;br /&gt;          data = child_filehandle.read&lt;br /&gt;        ensure&lt;br /&gt;          child_filehandle.close_write&lt;br /&gt;        end&lt;br /&gt;      else&lt;br /&gt;        yield&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    return data&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;end    &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 17 Jun 2007 20:26:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4154</guid>
      <author>adamf (Adam Feuer)</author>
    </item>
    <item>
      <title>create an array or split a long string seperated by tabs newlines carriage returns and commas.</title>
      <link>http://snippets.dzone.com/posts/show/4120</link>
      <description>create an array or split a long string seperated by tabs newlines carriage returns and commas.&lt;br /&gt;&lt;br /&gt;EG&lt;br /&gt;&lt;br /&gt;emails = "skdjf@sdklfj.com sdflj@kj.com sldfj@klj.com, slkj@kjl.com,lskjdf@lkj.com, \r\nlkjkjl@kjk.com"&lt;br /&gt;&lt;br /&gt;becomes&lt;br /&gt;&lt;br /&gt;=&gt; ["skdjf@sdklfj.com", "sdflj@kj.com", "sldfj@klj.com", "slkj@kjl.comlskjdf@lkj.com", "lkjkjl@kjk.com"]&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;emails = emails.split(/,/).join().split()&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 09 Jun 2007 00:48:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4120</guid>
      <author>mcarr (matt)</author>
    </item>
    <item>
      <title>ActiveRecord: cache column information</title>
      <link>http://snippets.dzone.com/posts/show/3926</link>
      <description>// Cache the column information so that no more query will be &lt;br /&gt;// sent to the server thereafter&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class ActiveRecord::Base&lt;br /&gt;	&lt;br /&gt;	@@_cached_columns = {}&lt;br /&gt;	class &lt;&lt; self&lt;br /&gt;		&lt;br /&gt;		alias :old_columns :columns&lt;br /&gt;		&lt;br /&gt;		def columns&lt;br /&gt;			return @@_cached_columns[table_name] if @@_cached_columns[table_name]&lt;br /&gt;			@@_cached_columns[table_name] = old_columns&lt;br /&gt;		end&lt;br /&gt;		&lt;br /&gt;	end&lt;br /&gt;	&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 27 Apr 2007 21:24:48 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3926</guid>
      <author>jbeduya (Junrey Beduya)</author>
    </item>
    <item>
      <title>Helper for quicly creating standard tables</title>
      <link>http://snippets.dzone.com/posts/show/3623</link>
      <description>I often want to display an array of objects as a table on a page, and I end up doing the the same things over and over again. I wrote this helper method to speed things up:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def table(collection, headers, options = {}, &amp;proc)&lt;br /&gt;  options.reverse_merge!({&lt;br /&gt;    :placeholder  =&gt; 'Nothing to display',&lt;br /&gt;    :caption      =&gt; nil,&lt;br /&gt;    :summary      =&gt; nil,&lt;br /&gt;    :footer       =&gt; ''&lt;br /&gt;  })&lt;br /&gt;  placeholder_unless collection.any?, options[:placeholder] do&lt;br /&gt;    summary = options[:summary] || "A list of #{collection.first.class.to_s.pluralize}"&lt;br /&gt;    output = "&lt;table summary=\"#{summary}\"&gt;\n"&lt;br /&gt;    output &lt;&lt; content_tag('caption', options[:caption]) if options[:caption]&lt;br /&gt;    output &lt;&lt; "\t&lt;caption&gt;#{options[:caption]}&lt;/caption&gt;\n" if options[:caption]&lt;br /&gt;    output &lt;&lt; content_tag('thead', content_tag('tr', headers.collect { |h| "\n\t" + content_tag('th', h) }))&lt;br /&gt;    output &lt;&lt; "&lt;tfoot&gt;&lt;tr&gt;" + content_tag('th', options[:footer], :colspan =&gt; headers.size) + "&lt;/tr&gt;&lt;/tfoot&gt;\n" if options[:footer]&lt;br /&gt;    output &lt;&lt; "&lt;tbody&gt;\n"&lt;br /&gt;    concat(output, proc.binding)&lt;br /&gt;    collection.each do |row|&lt;br /&gt;      proc.call(row, cycle('odd', 'even'))&lt;br /&gt;    end&lt;br /&gt;    concat("&lt;/tbody&gt;\n&lt;/table&gt;\n", proc.binding)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Writing...&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;% table(@posts, %w{ID title}) do |post, klass| -%&gt;&lt;br /&gt;    &lt;tr class="&lt;%= klass %&gt;"&gt;&lt;br /&gt;      &lt;td&gt;&lt;%= post.id&lt;/td&gt;&lt;br /&gt;      &lt;td&gt;&lt;%= post.title &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;% end -%&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;results in...&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;table summary="A list of posts"&gt;&lt;br /&gt;  &lt;thead&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;th&gt;ID&lt;/th&gt;&lt;br /&gt;      &lt;th&gt;Title&lt;/th&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;  &lt;/thead&gt;&lt;br /&gt;  &lt;tfoot&gt;&lt;tr&gt;&lt;td colspan="2"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tfoot&gt;&lt;br /&gt;  &lt;tbody&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;1&lt;/td&gt;&lt;br /&gt;      &lt;td&gt;My first post&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;  &lt;/tbody&gt;&lt;br /&gt;&lt;/table&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Or, when the collection is an empty array (collection.any? returns false), a &lt;a href="http://snippets.dzone.com/posts/show/2929"&gt;placeholder message&lt;/a&gt; is displayed:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;p class="placeholder"&gt;Nothing to display&lt;/p&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;So you pass in your collection and an array of strings as your table headers as the first two arguments, and the third is a hash of options you can use to set the contents of the table's summary-attribute, the caption and footer-elements and the placeholder. &lt;br /&gt;&lt;br /&gt;The summary attribute defaults to "A list of [objects]", where 'objects' is derived from the class name of the collection. &lt;br /&gt;&lt;br /&gt;The function finally takes a block for every element in the collection, yeilding it the element and either 'odd' or 'even' so you can use CSS-classes to apply a zebra stripes effect.</description>
      <pubDate>Sat, 03 Mar 2007 20:52:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3623</guid>
      <author>arjanvandergaag (Arjan van der Gaag)</author>
    </item>
    <item>
      <title>Helpers for placeholder messages</title>
      <link>http://snippets.dzone.com/posts/show/2929</link>
      <description>When there are no results to display for a user you often want to tell so using a placeholder message, such as 'Sorry, no posts matched your criteria' or 'You have no recent purchases'.&lt;br /&gt;&lt;br /&gt;I created these two helpers to make using placeholder a little bit easier.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def placeholder(message = 'Nothing to display', options = {}, &amp;proc)&lt;br /&gt;  # set default options&lt;br /&gt;  o = { :class =&gt; 'placeholder', :tag =&gt; 'p' }.merge(options)&lt;br /&gt;&lt;br /&gt;  # wrap the results of the supplied block, or&lt;br /&gt;  # just print out the message&lt;br /&gt;  if proc&lt;br /&gt;    t = o.delete(:tag)&lt;br /&gt;    concat tag(t, o, true), proc.binding&lt;br /&gt;    yield&lt;br /&gt;    concat "&lt;/#{t}&gt;", proc.binding&lt;br /&gt;  else&lt;br /&gt;    content_tag o.delete(:tag), message, o&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;  &lt;br /&gt;def placeholder_unless(condition, *args, &amp;proc)&lt;br /&gt;  condition ? proc.call : concat(placeholder(args), proc.binding)&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Now you can use it as follows:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;%= placeholder :message =&gt; 'Nothing found' %&gt;&lt;br /&gt;&lt;br /&gt;&lt;% placeholder do %&gt;&lt;br /&gt;Nothing found.&lt;br /&gt;&lt;% end %&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Results in:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;p class="placeholder"&gt;Nothing found&lt;/p&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The second function allows the following usage:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;% placeholder_unless @posts.any?, 'No posts found' do %&gt;&lt;br /&gt;  &lt;%= render :partial =&gt; @post %&gt;&lt;br /&gt;&lt;% end %&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The point is it all results in a nice, painless message with consistent markup and styling and I find it makes my code a bit more readable.&lt;br /&gt;&lt;br /&gt;I'm sure the code could be optimized but for now it works.</description>
      <pubDate>Mon, 30 Oct 2006 20:27:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2929</guid>
      <author>arjanvandergaag (Arjan van der Gaag)</author>
    </item>
    <item>
      <title>Don't display the delete link unless no service events exist</title>
      <link>http://snippets.dzone.com/posts/show/2256</link>
      <description>// This is the link I'll use when I get to adding the delete methods in the controllers.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;% if @client.service_events.empty? -%&gt; | &lt;%= link_to "delete", :controller =&gt; "admin", :action =&gt; "destroy_client", :id =&gt; @client %&gt;&lt;% end -%&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 07 Jul 2006 23:12:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2256</guid>
      <author>lcummings (Larry Cummings)</author>
    </item>
    <item>
      <title>Highlight link for current action</title>
      <link>http://snippets.dzone.com/posts/show/2016</link>
      <description>It's very common to want to highlight the current link within a navigation list for the current action being performed.&lt;br /&gt;&lt;br /&gt;What follows is an extremely simplistic approach to solving this, so build upon it as you wish.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The first thing to do is add the following code to your application_helper.rb file&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def section_link(name,options)&lt;br /&gt;    if options[:action] == @current_action and options[:controller] == @current_controller&lt;br /&gt;       link_to(name, options, :class =&gt; 'on')&lt;br /&gt;    else&lt;br /&gt;      link_to(name,options)&lt;br /&gt;    end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The above method always expects an :action key to be passed in with the options hash, so if you're one of those people who just pass in the :controller and not the :action so as to default to whatever method you've set it in your routes.rb file, then this won't work for you.&lt;br /&gt;&lt;br /&gt;Finally put this in you're application.rb file, as it makes the current executing action and controller name available to the helper.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;before_filter :instantiate_controller_and_action_names&lt;br /&gt;&lt;br /&gt;def instantiate_controller_and_action_names&lt;br /&gt;      @current_action = action_name&lt;br /&gt;      @current_controller = controller_name&lt;br /&gt;end &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In your view use &lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;%=section_link('Home',:controller =&gt; 'articles', :action =&gt; 'index')%&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Make sure you have a CSS class called "on" or edit the above code. The following CSS is just an example.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;a.on{&lt;br /&gt;  border-bottom: solid 2px #000;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt; &lt;br /&gt;</description>
      <pubDate>Sun, 14 May 2006 21:33:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2016</guid>
      <author>noodlesinmysandals (Jonathan Conway)</author>
    </item>
  </channel>
</rss>
