Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Mask all but last 4 digits

If you need to mask all but the last 4 digits of a nubmer - like a credit card number - this helper works nicely.

Just pass it a number or string.

def mask_number(number)
  number.to_s.size < 5 ? number.to_s : (('*' * number.to_s[0..-5].length) + number.to_s[-4..-1])
end

Helpers for placeholder messages

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'.

I created these two helpers to make using placeholder a little bit easier.

def placeholder(message = 'Nothing to display', options = {}, &proc)
  # set default options
  o = { :class => 'placeholder', :tag => 'p' }.merge(options)

  # wrap the results of the supplied block, or
  # just print out the message
  if proc
    t = o.delete(:tag)
    concat tag(t, o, true), proc.binding
    yield
    concat "</#{t}>", proc.binding
  else
    content_tag o.delete(:tag), message, o
  end
end
  
def placeholder_unless(condition, *args, &proc)
  condition ? proc.call : concat(placeholder(args), proc.binding)
end


Now you can use it as follows:

<%= placeholder :message => 'Nothing found' %>

<% placeholder do %>
Nothing found.
<% end %>


Results in:

<p class="placeholder">Nothing found</p>


The second function allows the following usage:

<% placeholder_unless @posts.any?, 'No posts found' do %>
  <%= render :partial => @post %>
<% end %>


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.

I'm sure the code could be optimized but for now it works.

link_to_remote_unless_current

// Full discussion about this available at:
// http://6brand.com/articles/2006/06/07/link_to_remote_unless_current

# throw this in one of your controller helpers.
# it works just like a combination of link_to_unless_current and link_to_remote
def link_to_remote_unless_current(name, options = {}, html_options = {}, *parameters_for_method_reference, &block)
  if current_page?(options[:url])
    if block_given?
      block.arity <= 1 ? yield(name) : yield(name, remote_function(options), html_options, *parameters_for_method_reference)
    else
      name
    end
  else
    link_to_function(name, remote_function(options), html_options)
  end
end

Loading ActionView helpers

This lets you load ActionView helpers globally.

RAILS_ROOT/lib/action_view/helpers/widget_helper.rb:
module ActionView
  module Helpers
    module WidgetHelper
      # define helper methods
    end
  end
end


RAILS_ROOT/config/environment.rb:

require "#{RAILS_ROOT}/lib/action_view/helpers/widget_helper.rb"
ActionView::Base.class_eval do
include ActionView::Helpers::WidgetHelper
end

Custom captures

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.

This goes with the other application helpers:

  def rounded_box(&block)
    concat '<div class="rounded"><div class="top"></div><div class="body">' + capture(&block) + '</div><div class="bottom"></div></div>', block.binding
  end


This goes in a view:

<% rounded_box do %>

... stuff to put in the rounded box ...

<% end %>
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS