<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Moneypenny's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 05:54:02 GMT</pubDate>
    <description>DZone Snippets: Moneypenny's Code Snippets</description>
    <item>
      <title>using Event.observe for when a page loads</title>
      <link>http://snippets.dzone.com/posts/show/3025</link>
      <description>I just prefer this implementation as opposed to window.onload.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;Event.observe( window, 'load', function() {&lt;br /&gt;  // Put the Javascript that needs to happen when the page&lt;br /&gt;  // loads here&lt;br /&gt;} );&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;For example, here's a way to make most of your input fields and all of your textareas have a different class when they're selected.  Useful if you want to style them differently with CSS.&lt;br /&gt;&lt;code&gt;Event.observe( window, 'load', function() {&lt;br /&gt;  var inputs = document.getElementsByTagName( 'input' );&lt;br /&gt;  var textareas = document.getElementsByTagName( 'textarea' );&lt;br /&gt;&lt;br /&gt;  for ( var i=0; i&lt;inputs.length; i++ ) {&lt;br /&gt;    type = inputs[i].type;&lt;br /&gt;&lt;br /&gt;    if (&lt;br /&gt;      ( type == 'button' || type == 'reset' ||&lt;br /&gt;      type == 'password' || type == 'text' ||&lt;br /&gt;      type == 'submit' ) &amp;&amp;&lt;br /&gt;      ( inputs[i].onfocus == '' ||&lt;br /&gt;      !inputs[i].onfocus )&lt;br /&gt;    ) {&lt;br /&gt;      inputs[i].onfocus = function() {&lt;br /&gt;        this.className = 'selected';&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      inputs[i].onblur = function() {&lt;br /&gt;        this.className = '';&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  for ( var i=0; i&lt;textareas.length; i++ ) {&lt;br /&gt;    if ( textareas[i].onfocus == '' || !textareas[i].onfocus ) {&lt;br /&gt;      textareas[i].onclick = function() {&lt;br /&gt;        this.className = 'selected';&lt;br /&gt;      }&lt;br /&gt;      textareas[i].onblur = function() {&lt;br /&gt;        this.className = '';&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;} );&lt;/code&gt;</description>
      <pubDate>Mon, 20 Nov 2006 20:47:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3025</guid>
      <author>moneypenny ()</author>
    </item>
    <item>
      <title>#post method in tests with a different controller</title>
      <link>http://snippets.dzone.com/posts/show/2880</link>
      <description>I wanted a #login method in test_helper that would allow me to easily login from any of my functional tests.  However, the #post method won't allow you to set a different controller than the one in the @controller instance variable that's defined in your test's #setup.  Well, by looking at how the &lt;a href="http://api.rubyonrails.org/classes/ActionController/TestProcess.html#M000046"&gt;#process method&lt;/a&gt; works, you can see that it just grabs the controller from @controller.  Redefine that, and you're good to go:&lt;br /&gt;&lt;code&gt;old_controller = @controller&lt;br /&gt;@controller = LoginController.new&lt;br /&gt;post(&lt;br /&gt;  :attempt_login,&lt;br /&gt;  {:user =&gt; {:name =&gt; 'joe', :password =&gt; 'password'}}&lt;br /&gt;)&lt;br /&gt;@controller = old_controller&lt;/code&gt;&lt;br /&gt;If you have several login methods, such as a #login_admin and #login_regular, you could make a wrapper to reduce duplication:&lt;br /&gt;&lt;code&gt;def wrap_with_controller( new_controller = LoginController )&lt;br /&gt;  old_controller = @controller&lt;br /&gt;  @controller = new_controller.new&lt;br /&gt;  yield&lt;br /&gt;  @controller = old_controller&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def login_admin&lt;br /&gt;  wrap_with_controller do&lt;br /&gt;    post(&lt;br /&gt;      :attempt_login,&lt;br /&gt;      {:user =&gt; {:name =&gt; 'root', :password =&gt; 'password'}}&lt;br /&gt;    )&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def login_regular&lt;br /&gt;  wrap_with_controller do&lt;br /&gt;    post(&lt;br /&gt;      :attempt_login,&lt;br /&gt;      {:user =&gt; {:name =&gt; 'joe', :password =&gt; 'password'}}&lt;br /&gt;    )&lt;br /&gt;  end&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Tue, 24 Oct 2006 18:54:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2880</guid>
      <author>moneypenny ()</author>
    </item>
    <item>
      <title>classify() for alternating rows, columns, etc.</title>
      <link>http://snippets.dzone.com/posts/show/2569</link>
      <description>I often want different rows in a table to alternate in color, and I do this by assigning each row a class name and styling it with CSS.  This is a simple helper method designed to return a class name based on the given row count.&lt;br /&gt;&lt;br /&gt;It is also convenient for me to assign a class to table cells based on the type of content they hold.  For example, if I have a cell with a float value in it, I want to display it with a monospace font, whereas if I have a cell with a string in it, I want to display it in a serif font.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;# Determines the CSS class based on either the count given&lt;br /&gt;# (returns 'even' or 'odd' as the CSS class name) or the class&lt;br /&gt;# given (returns the string version of the class, lowercased,&lt;br /&gt;# as the CSS class name)&lt;br /&gt;def classify( count_or_class, include_class_text = true )&lt;br /&gt;  if count_or_class.class == Fixnum&lt;br /&gt;    value = ( count_or_class % 2 == 0 ? 'even' : 'odd' )&lt;br /&gt;  else&lt;br /&gt;    value = count_or_class.to_s.downcase&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  if include_class_text&lt;br /&gt;    'class="' &lt;&lt; value &lt;&lt; '"'&lt;br /&gt;  else&lt;br /&gt;    value&lt;br /&gt;  end&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example usage with alternating 'even'/'odd' row class names:&lt;br /&gt;&lt;code&gt;&lt;table&gt;&lt;br /&gt;&lt;% count = 0 %&gt;&lt;br /&gt;&lt;% @collection.each do |value| %&gt;&lt;br /&gt;  &lt;tr &lt;%= classify( count ) %&gt;&gt;&lt;br /&gt;    &lt;td&gt;&lt;%=h value %&gt;&lt;/td&gt;&lt;br /&gt;  &lt;/tr&gt;&lt;br /&gt;  &lt;% count += 1 %&gt;&lt;br /&gt;&lt;% end %&gt;&lt;br /&gt;&lt;/table&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example usage with data type class names:&lt;br /&gt;&lt;code&gt;&lt;tr&gt;&lt;br /&gt;&lt;% @columns.each do |column| %&gt;&lt;br /&gt;  &lt;% data = row.send( column.name ) %&gt;&lt;br /&gt;  &lt;td &lt;%= classify( data.class ) %&gt;&gt;&lt;br /&gt;    &lt;%=h data %&gt;&lt;br /&gt;  &lt;/td&gt;&lt;br /&gt;&lt;% end %&gt;&lt;br /&gt;&lt;/tr&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 08 Sep 2006 21:00:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2569</guid>
      <author>moneypenny ()</author>
    </item>
    <item>
      <title>helper to determine if radio/checkbox needs to be checked</title>
      <link>http://snippets.dzone.com/posts/show/2559</link>
      <description>I frequently have to use methods such as 'radio_button' and 'check_box_tag' when I don't have an object with a method that will automatically determine the value of the input field.  Therefore, I have to check to see if a certain parameter has been passed, and if so, if the parameter's value matches that of the input's value.  This method does that.&lt;br /&gt;&lt;br /&gt;It's designed to be used in a Rails helper.  You can either pass it the object, method, and value (the same parameters as, for example, &lt;a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M000395"&gt;radio_button&lt;/a&gt;) or name and value (the same parameters as &lt;a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#M000501"&gt;radio_button_tag&lt;/a&gt;).&lt;br /&gt;&lt;br /&gt;&lt;code&gt;def checked?( *args )&lt;br /&gt;  if args.length == 3&lt;br /&gt;    object, method, value = args&lt;br /&gt;    if params[object] &amp;&amp; params[object][method] &amp;&amp; params[object][method] == value&lt;br /&gt;      'checked'&lt;br /&gt;    end&lt;br /&gt;  elsif args.length == 2&lt;br /&gt;    name, value = args&lt;br /&gt;    if params[name] &amp;&amp; params[name] == value&lt;br /&gt;      true&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Here's an example usage:&lt;br /&gt;&lt;code&gt;&lt;%= radio_button 'person', 'age', '12', :checked =&gt; checked?( 'person', 'age', '12' ) %&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;If params[:person][:age] exists and it equals '12', then 'checked?' returns 'checked'; otherwise, it returns nil.</description>
      <pubDate>Thu, 07 Sep 2006 16:25:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2559</guid>
      <author>moneypenny ()</author>
    </item>
    <item>
      <title>conditioner for ActiveRecord-friendly conditions from a collection</title>
      <link>http://snippets.dzone.com/posts/show/2510</link>
      <description>I frequently have a collection of values that I want to match in an ActiveRecord query, but it would be nice if I could let ActiveRecord handle checking the data and escaping it properly.  So, I wrote this method to return ActiveRecord-friendly conditions, such as: &lt;code&gt;["user_id=? AND job_id=?", 3, 4]&lt;/code&gt; based on the 'raw' conditions you feed to it, such as: &lt;code&gt;[['user_id', 3], ['job_id', 4]]&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;# Returns ActiveRecord-friendly conditions based on the given&lt;br /&gt;# raw conditions; handles grouping based on like field names;&lt;br /&gt;# allows different boolean operators in raw conditions;&lt;br /&gt;# allows different comparison operators in raw conditions;&lt;br /&gt;# raw conditions setup:&lt;br /&gt;# [[field name, desired value, bool. op., comp. op.], ...]&lt;br /&gt;# name = condition[0]&lt;br /&gt;# value = condition[1]&lt;br /&gt;# bool_type = condition[2]&lt;br /&gt;# comparison = condition[3]&lt;br /&gt;# raw conditions example:&lt;br /&gt;# [['type_id', '4', 'OR'], ['created_on', Date.new, 'AND', '&lt;=']]&lt;br /&gt;def conditioner( raw_conditions )&lt;br /&gt;  return nil if raw_conditions.nil? || raw_conditions.empty?&lt;br /&gt;  &lt;br /&gt;  conditions = ['(']&lt;br /&gt;  count = 0&lt;br /&gt;  prev_name = raw_conditions[0][0]&lt;br /&gt;  raw_conditions.each do |condition|&lt;br /&gt;    name = condition[0]&lt;br /&gt;      &lt;br /&gt;    conditions[0] &lt;&lt; ') AND ' if prev_name != name&lt;br /&gt;    conditions[0] &lt;&lt; ' ' &lt;&lt; ( condition[2] || 'OR' ) &lt;&lt; ' ' unless count == 0 || prev_name != name&lt;br /&gt;    conditions[0] &lt;&lt; '(' if prev_name != name&lt;br /&gt;    conditions[0] &lt;&lt; name + ' ' &lt;&lt; ( condition[3] || '=' ) + ' ?'&lt;br /&gt;      &lt;br /&gt;    conditions &lt;&lt; condition[1]&lt;br /&gt;      &lt;br /&gt;    prev_name = name&lt;br /&gt;    count += 1&lt;br /&gt;  end&lt;br /&gt;  conditions[0] &lt;&lt; ')'&lt;br /&gt;&lt;br /&gt;  conditions&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This way, you can do something like the following:&lt;br /&gt;&lt;code&gt;model_ids = Model.find( :all ).map( &amp;:id )&lt;br /&gt;raw_conditions = model_ids.collect { |id| ['model_id', id] }&lt;br /&gt;conditions = conditioner( raw_conditions )&lt;br /&gt;desired_collection = OtherModel.find( :all, :conditions =&gt; conditions )&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;If your query needs to depend on more than one factor, you might do something like the following:&lt;br /&gt;&lt;code&gt;if test&lt;br /&gt;  raw_conditions = [['user_id', 3]]&lt;br /&gt;else&lt;br /&gt;  raw_conditions = [['user_id', 4], ['groups.name', 'dev']]&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;team_ids.each { |id| raw_conditions &lt;&lt; ['team_id', id, 'AND'] }&lt;/code&gt;</description>
      <pubDate>Thu, 31 Aug 2006 22:02:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2510</guid>
      <author>moneypenny ()</author>
    </item>
    <item>
      <title>go from model to associated table name and back</title>
      <link>http://snippets.dzone.com/posts/show/2135</link>
      <description>Given a table object, it returns the related string object; e.g. SubAttribute =&gt; 'sub-attribute'.  Useful if you want to make a list of all your tables with perhaps their fields listed out to the side.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def stringify_table( table, replace_char = '-', pluralize = false )&lt;br /&gt;  string = table.to_s.gsub( /([A-Za-z])([A-Z])/, '\1' &lt;&lt; replace_char.to_s &lt;&lt; '\2' )&lt;br /&gt;  string = string.pluralize if pluralize&lt;br /&gt;  string&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Given a string akin to the name of a table, it returns the related table object; e.g. 'sub_attributes' =&gt; SubAttribute.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def tablify_string( string )&lt;br /&gt;  eval( string.to_s.gsub( /_id/, '' ).singularize.split( '_' ).collect { |word| word.capitalize }.join )&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 03 Jun 2006 00:50:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2135</guid>
      <author>moneypenny ()</author>
    </item>
  </channel>
</rss>
