<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: actionview code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 19:04:44 GMT</pubDate>
    <description>DZone Snippets: actionview code</description>
    <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>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>link_to_remote_unless_current</title>
      <link>http://snippets.dzone.com/posts/show/2183</link>
      <description>// Full discussion about this available at:&lt;br /&gt;// http://6brand.com/articles/2006/06/07/link_to_remote_unless_current&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# throw this in one of your controller helpers.&lt;br /&gt;# it works just like a combination of link_to_unless_current and link_to_remote&lt;br /&gt;def link_to_remote_unless_current(name, options = {}, html_options = {}, *parameters_for_method_reference, &amp;block)&lt;br /&gt;  if current_page?(options[:url])&lt;br /&gt;    if block_given?&lt;br /&gt;      block.arity &lt;= 1 ? yield(name) : yield(name, remote_function(options), html_options, *parameters_for_method_reference)&lt;br /&gt;    else&lt;br /&gt;      name&lt;br /&gt;    end&lt;br /&gt;  else&lt;br /&gt;    link_to_function(name, remote_function(options), html_options)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 14 Jun 2006 09:53:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2183</guid>
      <author>danger (Danger)</author>
    </item>
    <item>
      <title>Loading ActionView helpers</title>
      <link>http://snippets.dzone.com/posts/show/468</link>
      <description>This lets you load ActionView helpers globally.&lt;br /&gt;&lt;br /&gt;RAILS_ROOT/lib/action_view/helpers/widget_helper.rb:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module ActionView&lt;br /&gt;  module Helpers&lt;br /&gt;    module WidgetHelper&lt;br /&gt;      # define helper methods&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;RAILS_ROOT/config/environment.rb:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require "#{RAILS_ROOT}/lib/action_view/helpers/widget_helper.rb"&lt;br /&gt;ActionView::Base.class_eval do&lt;br /&gt;  include ActionView::Helpers::WidgetHelper&lt;br /&gt;end&lt;br /&gt;&lt;code&gt;</description>
      <pubDate>Fri, 08 Jul 2005 14:59:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/468</guid>
      <author>technoweenie (Rick Olson)</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>
