<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users178</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/178" rel="self"/>
  <title>Barry Hess</title>
  <updated>Thu Jul 17 14:39:43 -0700 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor13261</id>
    <published>2008-07-17T14:39:43-07:00</published>
    <title>[JavaScript] On Rails-like number_to_currency formatting</title>
    <content type="html">&lt;p&gt;This is JavaScript - I don't think JS or Prototype _has_ a number_to_currency method.  At least not when I wrote this.  You may need to look at some syntax things - I ripped this out of a JS file, and there could be some cruft that doesn't belong or syntax may be different in your application.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Barry Hess</name>
      <email>barry@bjhess.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/85-rails-like-number_to_currency-formatting/refactors/13261" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor3594</id>
    <published>2008-03-02T04:00:31-08:00</published>
    <title>[Ruby] On Action Mailer - It has to be easier?</title>
    <content type="html">&lt;p&gt;You could try some meta-programming.  This way your list of method names is nice and centralized.&lt;/p&gt;

&lt;pre&gt;class FormMailer &amp;lt; ActionMailer::Base

  %w{contact_us help_request service customer_care}.each do |method_name|

    define_method(method_name) do |params|
      from params[:email]
      recipients params[:to]
      subject params[:subject]
      body :params =&amp;gt; params
    end

  end

end&lt;/pre&gt;</content>
    <author>
      <name>Barry Hess</name>
      <email>barry@bjhess.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/246-action-mailer-it-has-to-be-easier/refactors/3594" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code129</id>
    <published>2007-11-02T15:15:22-07:00</published>
    <updated>2007-11-19T15:11:50-08:00</updated>
    <title>[Ruby] Finding all in a date range (safely)</title>
    <content type="html">&lt;p&gt;So my User has an association proxy for her expenses.  spent_at is a Date field in the DB.  The day being passed in is a Ruby Date.  Mainly, I want to be sure hashed conditions are safe from hacks.  Also wondering if there is a more convenient way to do beginning_of and end_of calcs on Date objects.&lt;/p&gt;

&lt;p&gt;NOTE: At Rails 1.2.5, so no end_of_week on Time.  I think that's available in edge.&lt;/p&gt;

&lt;pre&gt;has_many :expenses, :dependent =&amp;gt; :destroy do
  def for_week(day)
    find(:all, :conditions =&amp;gt; { :spent_at =&amp;gt; day.to_time.beginning_of_week.to_date..(day.to_time.beginning_of_week.to_date + 6)})
  end
end&lt;/pre&gt;</content>
    <author>
      <name>Barry Hess</name>
      <email>barry@bjhess.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/129-finding-all-in-a-date-range-safely" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code107</id>
    <published>2007-10-25T14:09:02-07:00</published>
    <updated>2007-10-25T14:12:57-07:00</updated>
    <title>[Ruby] A DST-adjusted UTC offset</title>
    <content type="html">&lt;p&gt;I need to get the Daylight-Savings-Time-adjusted UTC offset for time zones.  So, for instance, Central Time US is currently [1] UTC -5 since it is DST.  During Standard Time, it is UTC -6.&lt;/p&gt;

&lt;p&gt;The easiest way I found to do this is to leverage everything from tzinfo_timezone [2] to tztime[3].  I'm only using tztime for access to it's &amp;quot;dst?&amp;quot; method.  Wondering if (a) there's an easier way to do this that I'm missing or (b) if there is a more direct way to do this without requiring two plugins and a gem (tzinfo).&lt;/p&gt;

&lt;p&gt;TIA&lt;/p&gt;

&lt;p&gt;[1] &lt;a href="http://www.timeanddate.com/worldclock/results.html?query=minneapolis" target="_blank"&gt;http://www.timeanddate.com/worldclock/results.html?query=minneapolis&lt;/a&gt;
&lt;br /&gt;[2] &lt;a href="http://agilewebdevelopment.com/plugins/tzinfo_timezone" target="_blank"&gt;http://agilewebdevelopment.com/plugins/tzinfo_timezone&lt;/a&gt;
&lt;br /&gt;[3] &lt;a href="http://weblog.jamisbuck.org/2007/2/2/introducing-tztime" target="_blank"&gt;http://weblog.jamisbuck.org/2007/2/2/introducing-tztime&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;## script/console output for utc_offset
# Note: Kuwait does not use DST.  
# See how the utc_offset value represents 
# the Standard Time offset for Dublin.

&amp;gt;&amp;gt; TimeZone[&amp;quot;Kuwait&amp;quot;].class
=&amp;gt; TzinfoTimezone
&amp;gt;&amp;gt; TimeZone[&amp;quot;Kuwait&amp;quot;].utc_offset/60/60
=&amp;gt; 3
&amp;gt;&amp;gt; TimeZone[&amp;quot;Dublin&amp;quot;].utc_offset/60/60
=&amp;gt; 0
&amp;gt;&amp;gt; Time.now.utc
=&amp;gt; Thu Oct 25 04:09:57 UTC 2007
&amp;gt;&amp;gt; TimeZone[&amp;quot;Kuwait&amp;quot;].now
=&amp;gt; Thu Oct 25 07:10:14 UTC 2007
&amp;gt;&amp;gt; TimeZone[&amp;quot;Dublin&amp;quot;].now
=&amp;gt; Thu Oct 25 05:10:08 UTC 2007

## Method to return DST adjusted UTC offset
def dst_utc_offset(zone_name)
  zone        = TimeZone[zone_name]
  TzTime.zone = zone
  # If Daylight Savings Time, add an hour (3600 seconds) to the offset.
  TzTime.now.dst? ? zone.utc_offset + 3600 : zone.utc_offset
end&lt;/pre&gt;</content>
    <author>
      <name>Barry Hess</name>
      <email>barry@bjhess.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/107-a-dst-adjusted-utc-offset" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor498</id>
    <published>2007-10-22T02:07:05-07:00</published>
    <title>[JavaScript] On Rails-like number_to_currency formatting</title>
    <content type="html">&lt;p&gt;Thanks, Ali.  So am I the only unusual being who doesn't find Ali's refactor to be &amp;quot;much easier?&amp;quot;  I don't know - I find the original more readable.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Barry Hess</name>
      <email>barry@bjhess.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/85-rails-like-number_to_currency-formatting/refactors/498" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor435</id>
    <published>2007-10-16T03:47:41-07:00</published>
    <title>[Ruby] On Merging two sets of conditions</title>
    <content type="html">&lt;p&gt;Thanks, all.&lt;/p&gt;

&lt;p&gt;I really like that class-based version, but it's a little heavy for my purposes.  Basically, I'm not real fond of instantiating an object to do this.  My refactor takes macournoyer's and adds a couple blank condition checks.  Without those, a blank condition may result in SQL like &amp;quot;(id=1) AND ()&amp;quot;, which I'm pretty sure fails in MySQL.&lt;/p&gt;

&lt;p&gt;For a bonus, I also added a function that takes a set of conditions plus a typical options hash, pulls the conditions out of the hash and merges the conditions for return.  You'd use this if you want to merge conditions and then also merge the current method's options with options passed into it without overwriting the newly built conditions.&lt;/p&gt;

&lt;pre&gt;# Merges two sets of condition options for ActiveRecord::Base find calls
def merge_conditions(base_conditions, new_conditions, boolean_operator=&amp;quot;AND&amp;quot;)
  return base_conditions if new_conditions.blank?
  return new_conditions  if base_conditions.blank?

  &amp;quot;(#{sanitize_sql(base_conditions)}) #{boolean_operator} (#{sanitize_sql(new_conditions)})&amp;quot;
end

# Permanently pulls any conditions found in options and merges them with the base set of conditions
def pull_conditions_and_merge!(base_conditions, options, boolean_operator=&amp;quot;AND&amp;quot;)
  option_conditions = options.delete(:conditions)
  merge_conditions(base_conditions, option_conditions, boolean_operator)
end&lt;/pre&gt;</content>
    <author>
      <name>Barry Hess</name>
      <email>barry@bjhess.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/84-merging-two-sets-of-conditions/refactors/435" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code85</id>
    <published>2007-10-16T03:18:50-07:00</published>
    <updated>2011-01-20T19:47:34-08:00</updated>
    <title>[JavaScript] Rails-like number_to_currency formatting</title>
    <content type="html">&lt;p&gt;This is pretty much a port of the Ruby on Rails number_to_currency method, right down to the hashed options.&lt;/p&gt;

&lt;pre&gt;number_to_currency: function (number, options) {
  try {
    var options   = options || {};
    var precision = options[&amp;quot;precision&amp;quot;] || 2;
    var unit      = options[&amp;quot;unit&amp;quot;] || &amp;quot;$&amp;quot;;
    var separator = precision &amp;gt; 0 ? options[&amp;quot;separator&amp;quot;] || &amp;quot;.&amp;quot; : &amp;quot;&amp;quot;;
    var delimiter = options[&amp;quot;delimiter&amp;quot;] || &amp;quot;,&amp;quot;;
  
    var parts = parseFloat(number).toFixed(precision).split('.');
    return unit + reports.number_with_delimiter(parts[0], delimiter) + separator + parts[1].toString();
  } catch(e) {
    return number
  }
},

number_with_delimiter: function (number, delimiter, separator) {
  try {
    var delimiter = delimiter || &amp;quot;,&amp;quot;;
    var separator = separator || &amp;quot;.&amp;quot;;
    
    var parts = number.toString().split('.');
    parts[0] = parts[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, &amp;quot;$1&amp;quot; + delimiter);
    return parts.join(separator);
  } catch(e) {
    return number
  }
}
&lt;/pre&gt;</content>
    <author>
      <name>Barry Hess</name>
      <email>barry@bjhess.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/85-rails-like-number_to_currency-formatting" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code84</id>
    <published>2007-10-15T21:10:47-07:00</published>
    <updated>2007-10-18T14:10:00-07:00</updated>
    <title>[Ruby] Merging two sets of conditions</title>
    <content type="html">&lt;p&gt;I found myself in a situation where I had a nice tidy model method encapsulating a complex finder.  The finder included conditions and other parameters.  I'm using the method within proxy method definitions found in other models, so I'd like to be able to pass options down to the method for use on the finder.  This includes conditions.&lt;/p&gt;

&lt;p&gt;So I wrote up a method that merges two sets of conditions.  Conditions, as you know, can be defined either as a hash, an array or a string.  I account for most of those scenarios, and error on the rest&lt;/p&gt;

&lt;pre&gt;# Merges two sets of condition options for ActiveRecord::Base find calls
def merge_conditions(base_conditions, new_conditions, boolean_operator=&amp;quot;AND&amp;quot;)
  return base_conditions if new_conditions.blank?
  return new_conditions  if base_conditions.blank?
  
  case base_conditions
  when Hash
    case new_conditions
    when Hash
      base_conditions.merge(new_conditions)
    else
      raise &amp;quot;Hash conditions can only be merged with other hash conditions&amp;quot;
    end
  when Array
    case new_conditions
    when Array
      [&amp;quot;#{base_conditions[0]} #{boolean_operator} #{new_conditions[0]}&amp;quot;, base_conditions[1..-1], new_conditions[1..-1]].flatten
    when String
      conditions = base_conditions.dup
      conditions[0] = &amp;quot;#{base_conditions[0]} #{boolean_operator} #{new_conditions}&amp;quot;
      conditions
    else
      raise &amp;quot;Array conditions can only be merged with array or string conditions&amp;quot;
    end
  when String
    case new_conditions
    when String
      &amp;quot;#{base_conditions} #{boolean_operator} #{new_conditions}&amp;quot;
    when Array
      conditions = new_conditions.dup
      conditions[0] = &amp;quot;#{base_conditions} #{boolean_operator} #{new_conditions[0]}&amp;quot;
      conditions
    else
      raise &amp;quot;String conditions can only be merged with string or array conditions&amp;quot;
    end
  end
end&lt;/pre&gt;</content>
    <author>
      <name>Barry Hess</name>
      <email>barry@bjhess.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/84-merging-two-sets-of-conditions" rel="alternate"/>
  </entry>
</feed>

