<?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:users1596friends</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1596/friends" rel="self"/>
  <title>carlpelletier.myopenid.com friends</title>
  <updated>Fri Jan 30 01:46:09 -0800 2009</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code727</id>
    <published>2009-01-30T01:46:09-08:00</published>
    <updated>2009-01-31T02:00:14-08:00</updated>
    <title>[Java] PSiGate</title>
    <content type="html">&lt;p&gt;This seems particularly egregious, any Java people care to show them a saner way?&lt;/p&gt;

&lt;pre&gt;				URL url = new URL(&amp;quot;https://dev.psigate.com:7989/Messenger/XMLMessenger&amp;quot;);
				HttpURLConnection connection = (HttpURLConnection)url.openConnection();
				connection.setDoInput(true);		
				connection.setDoOutput(true);		
				PrintWriter out = new PrintWriter(connection.getOutputStream());

				String strData = &amp;quot;&amp;quot;;
				strData = &amp;quot;&amp;lt;Order&amp;gt;\r\n&amp;quot;;
		  	strData = strData + &amp;quot;	&amp;lt;StoreID&amp;gt;teststore055&amp;lt;/StoreID&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;   &amp;lt;Passphrase&amp;gt;testpass055&amp;lt;/Passphrase&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;PaymentType&amp;gt;CC&amp;lt;/PaymentType&amp;gt;\r\n&amp;quot;; 
				strData = strData + &amp;quot;	&amp;lt;TestResult&amp;gt;R&amp;lt;/TestResult&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;   &amp;lt;OrderID&amp;gt;Test123&amp;lt;/OrderID&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;Userid&amp;gt;User1&amp;lt;/Userid&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;Baddress1&amp;gt;123 Main St.&amp;lt;/Baddress1&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;Tax1&amp;gt;1&amp;lt;/Tax1&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;Tax2&amp;gt;2&amp;lt;/Tax2&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;Tax3&amp;gt;3&amp;lt;/Tax3&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;Tax4&amp;gt;4&amp;lt;/Tax4&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;Tax5&amp;gt;5&amp;lt;/Tax5&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;Shippingtotal&amp;gt;2.00&amp;lt;/Shippingtotal&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;Subtotal&amp;gt;10.00&amp;lt;/Subtotal&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;CardAction&amp;gt;0&amp;lt;/CardAction&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;CardHolderName&amp;gt;Somebody&amp;lt;/CardHolderName&amp;gt;\r\n&amp;quot;;
				
				strData = strData + &amp;quot;	&amp;lt;CardNumber&amp;gt;4005550000000019&amp;lt;/CardNumber&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;CardExpYear&amp;gt;08&amp;lt;/CardExpYear&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;CardExpMonth&amp;gt;08&amp;lt;/CardExpMonth&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;TransRefNumber&amp;gt;&amp;lt;/TransRefNumber&amp;gt;\r\n&amp;quot;;	
				strData = strData + &amp;quot;	&amp;lt;CustomerIP&amp;gt;216.220.59.201&amp;lt;/CustomerIP&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;CardIDNumber&amp;gt;999&amp;lt;/CardIDNumber&amp;gt;\r\n&amp;quot;;
//				strData = strData + &amp;quot;	&amp;lt;CardXid&amp;gt;cardxid&amp;lt;/CardXid&amp;gt;\r\n&amp;quot;;
//				strData = strData + &amp;quot;	&amp;lt;CardECI&amp;gt;cardeci&amp;lt;/CardECI&amp;gt;\r\n&amp;quot;;
//				strData = strData + &amp;quot;	&amp;lt;CardCavv&amp;gt;cardcavv&amp;lt;/CardCavv&amp;gt;\r\n&amp;quot;;
//				strData = strData + &amp;quot;	&amp;lt;CardLevel2PO&amp;gt;cardlevel2po&amp;lt;/CardLevel2PO&amp;gt;\r\n&amp;quot;;
//				strData = strData + &amp;quot;	&amp;lt;CardLevel2Tax&amp;gt;cardlevel2tax&amp;lt;/CardLevel2Tax&amp;gt;\r\n&amp;quot;;
//				strData = strData + &amp;quot;	&amp;lt;CardLevel2TaxExempt&amp;gt;cardlevel2taxexempt&amp;lt;/CardLevel2TaxExempt&amp;gt;\r\n&amp;quot;;
//				strData = strData + &amp;quot;	&amp;lt;CardLevel2ShiptoZip&amp;gt;CardLevel2ShiptoZip&amp;lt;/CardLevel2ShiptoZip&amp;gt;\r\n&amp;quot;;
//				strData = strData + &amp;quot;	&amp;lt;AuthorizationNumber&amp;gt;123456&amp;lt;/AuthorizationNumber&amp;gt;\r\n&amp;quot;;	
	//			strData = strData + &amp;quot;	&amp;lt;CardRefNumber&amp;gt;99999&amp;lt;/CardRefNumber&amp;gt;\r\n&amp;quot;;	

				strData = strData + &amp;quot;	&amp;lt;Item&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemID&amp;gt;apple&amp;lt;/ItemID&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemDescription&amp;gt;delicious apple&amp;lt;/ItemDescription&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemQty&amp;gt;2&amp;lt;/ItemQty&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemPrice&amp;gt;1.5&amp;lt;/ItemPrice&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                  &amp;lt;Option&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                      &amp;lt;Colour1&amp;gt;Red&amp;lt;/Colour1&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                      &amp;lt;Size1&amp;gt;11&amp;lt;/Size1&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                      &amp;lt;Maker1&amp;gt;PSiGate11&amp;lt;/Maker1&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                  &amp;lt;/Option&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;		&amp;lt;/Item&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;		&amp;lt;Item&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemID&amp;gt;book&amp;lt;/ItemID&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemDescription&amp;gt;good book&amp;lt;/ItemDescription&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemQty&amp;gt;3&amp;lt;/ItemQty&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemPrice&amp;gt;2.5&amp;lt;/ItemPrice&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                  &amp;lt;Option&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                      &amp;lt;Colour21&amp;gt;Red&amp;lt;/Colour21&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                      &amp;lt;Size21&amp;gt;21&amp;lt;/Size21&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                      &amp;lt;Maker21&amp;gt;PSiGate21&amp;lt;/Maker21&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                  &amp;lt;/Option&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;		&amp;lt;/Item&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;		&amp;lt;Item&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemID&amp;gt;computer&amp;lt;/ItemID&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemDescription&amp;gt;IBM computer&amp;lt;/ItemDescription&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemQty&amp;gt;1&amp;lt;/ItemQty&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemPrice&amp;gt;12&amp;lt;/ItemPrice&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                  &amp;lt;Option&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                      &amp;lt;Colour33&amp;gt;Red&amp;lt;/Colour33&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                      &amp;lt;Size33&amp;gt;33&amp;lt;/Size33&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                      &amp;lt;Maker33&amp;gt;PSiGate33&amp;lt;/Maker33&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                  &amp;lt;/Option&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;		&amp;lt;/Item&amp;gt;\r\n&amp;quot;;

				strData = strData + &amp;quot;&amp;lt;/Order&amp;gt;&amp;quot;;
				out.println(strData);

				out.flush();
				out.close();
					
				// code to read response
				
							
				BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
				strReturn = new StringBuffer();
				
				boolean xxx = false;
				while ((inputLine = in.readLine()) != null)
				{
				    xxx = true;
				    strReturn.append(&amp;quot;\r\n&amp;quot; + inputLine);
				}
				
				System.out.println(&amp;quot;strReturn = &amp;quot; + strReturn);
				in.close();
				connection.disconnect();
&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/727-psigate" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor28356</id>
    <published>2008-09-21T19:38:33-07:00</published>
    <title>[Ruby] On Conditional Assignment</title>
    <content type="html">&lt;p&gt;Tien - I voted 5 for your 2nd refactoring, 1 for the 1st.&lt;/p&gt;

&lt;p&gt;The 1st is bad because 1 - it's still procedural and 2- it's hard to read. The '&amp;amp;&amp;amp;' operator is generally only used for boolean operations rather than chaining assignment or other operations with side-effects. Consider the three statements for which you find more readable:&lt;/p&gt;

&lt;pre&gt;biggest_square = 0
[1,4,3,5].each do |base|
  (temp = square(base)) &amp;gt; biggest_square &amp;amp;&amp;amp; biggest_square = temp
end

# Using a loop w/ temp variable to avoid [].max sorting through the whole array
# (yes, I would leave the above line as a comment
# explaining *why* Enumerable methods weren't used)
biggest_square, temp = 0
[1,4,3,5].each do |base|
  biggest_square = temp if (temp = square(base)) &amp;gt; biggest_square
end

# Procedural style, split for clarity, self-commenting
squares        = [1,4,3,5].collect {|e| square(e) }
biggest_square = squares.max&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/500-conditional-assignment/refactors/28356" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor28324</id>
    <published>2008-09-21T16:23:52-07:00</published>
    <title>[Ruby] On Conditional Assignment</title>
    <content type="html">&lt;p&gt;Tien Dung's last function is crystal clear. If you have a function that's as predictable as square though, you can do:&lt;/p&gt;

&lt;pre&gt;def square(number)
  return number**2
end

biggest_square = square([1,4,3,5].max)
&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/500-conditional-assignment/refactors/28324" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor17978</id>
    <published>2008-09-17T14:29:29-07:00</published>
    <title>[Ruby] On Horrible loop</title>
    <content type="html">&lt;p&gt;Trying to remove more duplication. I'm fairly certain there's an easier way to do what you want, so like Ingemar have to ask what you are trying to achieve.&lt;/p&gt;

&lt;pre&gt;for feedback in @feedbacks
  1.upto(20) do |i|
    # unless (answer = feedback.question(i)).nil? # lets you avoid a second call
    unless feedback.question(i).nil?
       # answer.to_i - 1 # is also suspect. why not just store data[i][answer] ?
      data[i][feedback.question(i).to_i - 1] += 1
    end
  end
end

class Feedback
  def question(id)
    send(&amp;quot;question#{id}&amp;quot;)
  end
end&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/495-horrible-loop/refactors/17978" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor16584</id>
    <published>2008-09-06T20:31:11-07:00</published>
    <title>[Ruby] On Think this needs a loop</title>
    <content type="html">&lt;p&gt;What Elij wrote. It avoids 2 calls to RssParser.run (which could be making 2 HTTP calls, slowing your program down).&lt;/p&gt;

&lt;pre&gt;# For the initial view, there's a better way to keep track of 'count'
# each_with_index is in Enumerable
&amp;lt;ul id=&amp;quot;lastfm_update_list&amp;quot;&amp;gt;
	&amp;lt;% @songs.each_with_index do |song, index| %&amp;gt;			
		&amp;lt;li&amp;gt;&amp;lt;%= link_to song, @links[index] %&amp;gt;&amp;lt;/li&amp;gt;
	&amp;lt;% end %&amp;gt;
&amp;lt;/ul&amp;gt;

# only extract the first 3 items with [0..2]
feed[:items][0..2].each do { |item| @songs[item[:title]] = item[:link] }

&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/483-think-this-needs-a-loop/refactors/16584" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code436</id>
    <published>2008-08-14T19:47:29-07:00</published>
    <updated>2010-12-03T00:48:36-08:00</updated>
    <title>[Ruby] A shorter way to declare class constants?</title>
    <content type="html">&lt;p&gt;I added these so I could refer to Report::HITS in the API. There will be more than just hits and uniques, and I'm wondering if there's a shorter way to declare all this, or a more idiomatic way.&lt;/p&gt;

&lt;pre&gt;class Report &amp;lt; ActiveRecord::Base
  belongs_to :reportable, :polymorphic =&amp;gt; true
  
  HITS = 'hits'
  UNIQUES = 'uniques'
end&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/436-a-shorter-way-to-declare-class-constants" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14659</id>
    <published>2008-08-08T19:32:50-07:00</published>
    <title>[Ruby] On Same function repeated but with diff global vars!</title>
    <content type="html">&lt;p&gt;The two last methods don't actually use pre? And what's the rationale for line 2???&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/426-same-function-repeated-but-with-diff-global-vars/refactors/14659" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14210</id>
    <published>2008-07-31T13:51:12-07:00</published>
    <title>[Ruby] On How to make instance variable Persistent in between actions</title>
    <content type="html">&lt;p&gt;Persistent storage by adding it to the session smells. Take Fabien's first solution to avoid code duplication. If you really are facing performance issues, then look in to caching the front-end (assuming you're keeping the list of @users in a partial).&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/411-how-to-make-instance-variable-persistent-in-between-actions/refactors/14210" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14089</id>
    <published>2008-07-30T12:32:56-07:00</published>
    <title>[Ruby] On Contoller refactoring</title>
    <content type="html">&lt;p&gt;It doesn't have to be an ActiveRecord object; you can put any object you want in /app/models/&lt;/p&gt;

&lt;p&gt;Google &amp;quot;form validation without activerecord&amp;quot;&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/406-contoller-refactoring/refactors/14089" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor13600</id>
    <published>2008-07-23T01:42:25-07:00</published>
    <title>[Ruby] On rails ActiveRecord finder_sql (nested includes?)</title>
    <content type="html">&lt;p&gt;Here's where I'd see it going - not clear if a named_scope can be used instead of a delegate call, or how to keep the knowledge of :paid? in order and :downloadable? in product.&lt;/p&gt;

&lt;pre&gt;class User &amp;lt; ActiveRecord::Base
  has_many :orders
  has_many :products, :through =&amp;gt; :orders

  def downloads
    products.downloadable.collect(&amp;amp;:paid?)
  end
end

class Order
  has_many :products
end

class Product
  belongs_to :order
  named_scope :downloadable, :conditions =&amp;gt; {:downloadable =&amp;gt; true}
  delegate :paid?, :to =&amp;gt; :order
end
&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/396-rails-activerecord-finder_sql-nested-includes/refactors/13600" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor13550</id>
    <published>2008-07-22T12:54:12-07:00</published>
    <title>[Ruby] On Dashboard</title>
    <content type="html">&lt;p&gt;Argh - hopefully now I'm awake:&lt;/p&gt;

&lt;pre&gt;module DashboardHelper
  def activity_message_for(object)
    render :partial =&amp;gt; &amp;quot;#{object.class.name.downcase.pluralize}/activity&amp;quot;
  end
end
&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/392-dashboard/refactors/13550" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor13498</id>
    <published>2008-07-21T23:01:28-07:00</published>
    <title>[Ruby] On Dashboard</title>
    <content type="html">&lt;p&gt;HTML inside helpers is ugly, get that into partials and it gets way easier to deal with:&lt;/p&gt;

&lt;pre&gt;module DashboardHelper
  def activity_message_for(object)
    render :partial =&amp;gt; &amp;quot;#{object.class.name.downcase}/_#{object.class.name.downcase}_activity&amp;quot;
  end
end&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/392-dashboard/refactors/13498" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor13258</id>
    <published>2008-07-17T13:15:00-07:00</published>
    <title>[Ruby] On Password update code</title>
    <content type="html">&lt;p&gt;Managing flow with exceptions sucks. Exceptions are heavy, both from a readability point of view, and from a performance point of view. They should be reserved for unexpected situations, and ones that you want to allow to bubble up the stack and catch later.&lt;/p&gt;

&lt;p&gt;What if we used validations to handle the problem? It sure simplifies things.&lt;/p&gt;

&lt;pre&gt;## dj.rb

class Dj &amp;lt; ActiveRecord::Base
  attr_accessor :old_password
  
  validates_length_of         :password,    :minimum =&amp;gt; 1
  validates_confirmation_of   :password
  validate_on_update          :validate_old_password

  def reset_password(attrs)
    # I could see plucking out a few specific attributes to update, here, but no matter, if validations are complete
    update_attributes attrs
  end
  
  protected
    def validate_old_password
      errors.add(:old_password, 'does not match') if password_changed? &amp;amp;&amp;amp; !authenticated?(old_password)
    end
end

## accounts_controller.rb
class AccountsController &amp;lt; ApplicationController

  layout 'application'
  before_filter :login_required, :except =&amp;gt; :show

  # Change password action  
  def update
    if current_dj.reset_password(params[:dj])
      flash[:notice] = &amp;quot;Password successfully updated.&amp;quot;
      redirect_to dj_path(current_dj.login) #profile_url(current_dj.login)
    else
      # I don't know if we need this error message anymore, since the validations will take care of it
      flash[:error] = &amp;quot;An error occured, your password was not changed.&amp;quot;
      render :action =&amp;gt; 'edit'
    end
  end
end
&lt;/pre&gt;</content>
    <author>
      <name>jamesgolick</name>
      <email>jamesgolick@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/373-password-update-code/refactors/13258" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor12257</id>
    <published>2008-07-01T14:30:30-07:00</published>
    <title>[Ruby] On Template Method</title>
    <content type="html">&lt;p&gt;Maybe I'm missing what you're trying to do. But, why not just use built-in ERB?&lt;/p&gt;

&lt;pre&gt;ERB.new(File.read(path_to_template)).result(binding)&lt;/pre&gt;</content>
    <author>
      <name>jamesgolick</name>
      <email>jamesgolick@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/344-template-method/refactors/12257" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor11216</id>
    <published>2008-06-20T01:08:13-07:00</published>
    <title>[Ruby] On if else if else</title>
    <content type="html">&lt;p&gt;I prefer how this reads (with the explanation of the helper in the model)&lt;/p&gt;

&lt;pre&gt;## user.rb

def carrier?
  carrier_id != 0
end

## original submission

@email     = logged_in? ? current_user.email     : &amp;quot;&amp;quot;
@mobile_no = logged_in? ? current_user.mobile_no : &amp;quot;&amp;quot;
@carrier   = logged_in? &amp;amp;&amp;amp; current_user.carrier? ? current_user.carrier.name : &amp;quot;&amp;quot;
&lt;/pre&gt;</content>
    <author>
      <name>jamesgolick</name>
      <email>jamesgolick@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/329-if-else-if-else/refactors/11216" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor1407</id>
    <published>2008-01-08T04:34:08-08:00</published>
    <title>[JavaScript] On Anti-spam Mailto</title>
    <content type="html">&lt;p&gt;document.write really sucks. This will work with prototype.js, but should be portable to any other js lib, or raw javascript (but why would you do that?).&lt;/p&gt;

&lt;p&gt;Just pass in the id of the element you want updated, and this thing will do it (disclaimer: completely untested :)).&lt;/p&gt;

&lt;pre&gt;function showEMail(element)
{
   var e = { name: 'myemail',
             domain: 'somedomain',
             extension: 'com' };
   var address = [e.name, '@', e.domain, e.extension].join('');
   $(element).update('&amp;lt;a href=&amp;quot;mailto:'+address'&amp;quot;&amp;gt;'+address+'&amp;lt;/a&amp;gt;');
}&lt;/pre&gt;</content>
    <author>
      <name>jamesgolick</name>
      <email>jamesgolick@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/206-anti-spam-mailto/refactors/1407" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor1225</id>
    <published>2007-12-20T23:55:52-08:00</published>
    <title>[Ruby] On Challenge: Ugliest Ruby FizzBuzz</title>
    <content type="html">&lt;p&gt;oh my... I'd like to see somebody beat that.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>jamesgolick</name>
      <email>jamesgolick@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/192-challenge-ugliest-ruby-fizzbuzz/refactors/1225" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code192</id>
    <published>2007-12-20T21:54:34-08:00</published>
    <updated>2007-12-22T03:14:03-08:00</updated>
    <title>[Ruby] Challenge: Ugliest Ruby FizzBuzz</title>
    <content type="html">&lt;p&gt;So, after seeing some ugly fizzbuzzes, instead of trying to create the best fizzbuzz ever (an easy task), we decided the worst fizzbuzz ever would be a bigger challenge.&lt;/p&gt;

&lt;p&gt;Oh, and before you ask, it's because spaceships are awesome.&lt;/p&gt;

&lt;pre&gt;class Fixnum
  def &amp;lt;=&amp;gt;(b)
    self % b
  end
  
  def fizz?
    self % 3 == 0
  end
  
  def buzz?
    self % 5 == 0
  end
end

(1..100).each do |number|
  if number.fizz?
    if number.buzz?
      puts 'FizzBuzz'
    else
      puts 'Fizz'
    end
  else
    puts 'Buzz'
  end
end&lt;/pre&gt;</content>
    <author>
      <name>jamesgolick</name>
      <email>jamesgolick@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/192-challenge-ugliest-ruby-fizzbuzz" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor746</id>
    <published>2007-11-05T22:42:49-08:00</published>
    <title>[Ruby] On resource_controller: Redesign My API</title>
    <content type="html">&lt;p&gt;I love those. The *_finder macros are definitely possible. I really like the acts_as_resource_controller too. Currently, that syntax is just resource_controller, which I'm not crazy about.&lt;/p&gt;

&lt;p&gt;I agree with you about the respond_to, although, I think for open syntax it should be responds_to, just because it reads like english - update responds to js and html. OTOH, I don't see much of a problem with having all of these names as aliases, and so I think I'll probably do that.&lt;/p&gt;

&lt;pre&gt;update.failure.responds_to %(js html)&lt;/pre&gt;</content>
    <author>
      <name>jamesgolick</name>
      <email>jamesgolick@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/137-resource_controller-redesign-my-api/refactors/746" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor743</id>
    <published>2007-11-05T20:28:27-08:00</published>
    <title>[Ruby] On resource_controller: Redesign My API</title>
    <content type="html">&lt;p&gt;First refactoring from my blog...&lt;/p&gt;

&lt;pre&gt;## Alternate Model/Route/Object Naming

class PostsController &amp;lt; ResourceController::Base
  model_name :blog_post
  route_name :article
end&lt;/pre&gt;</content>
    <author>
      <name>jamesgolick</name>
      <email>jamesgolick@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/137-resource_controller-redesign-my-api/refactors/743" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code137</id>
    <published>2007-11-05T20:27:18-08:00</published>
    <updated>2011-05-01T18:42:24-07:00</updated>
    <title>[Ruby] resource_controller: Redesign My API</title>
    <content type="html">&lt;p&gt;Backstory on my blog: &lt;a href="http://jamesgolick.com/2007/11/5/resource_controller-redesign-my-api-at-refactormycode-com" target="_blank"&gt;http://jamesgolick.com/2007/11/5/resource_controller-redesign-my-api-at-refactormycode-com&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;## Before / After / Response

class PostsController &amp;lt; ResourceController::Base

  # block syntax
  create do
    before { @post.user = current_user }
    response do |wants|
      wants.html
      wants.js
    end
  end
  
  # chain syntax
  update.failure.wants.js
  
  # hybrid syntax
  update.failure do
    flash &amp;quot;There was a problem saving your object.&amp;quot;
    wants.js
  end

end

## Alternate Model/Route/Object Naming

class PostsController &amp;lt; ResourceController::Base
  private
    def model_name
      'blog_post'
    end
    
    def route_name
      'article'
    end
end

## Alternate find syntax

class PostsController &amp;lt; ResourceController::Base
  private
    def object
      @object ||= end_of_association_chain.find_by_permalink(param)
    end
    
    def collection
      @collection ||= end_of_association_chain.find(:all, :page =&amp;gt; {:current =&amp;gt; params[:page], :size =&amp;gt; 10})
    end
end

## Specific Actions

class PostsController &amp;lt; ResourceController::Base
  actions :all, :except =&amp;gt; :edit
end

## Possible Parents

class PostsController &amp;lt; ResourceController::Base
  belongs_to :user, :category
end&lt;/pre&gt;</content>
    <author>
      <name>jamesgolick</name>
      <email>jamesgolick@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/137-resource_controller-redesign-my-api" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor53</id>
    <published>2007-09-27T12:06:12-07:00</published>
    <title>[Ruby] On restful_authentication role requirements</title>
    <content type="html">&lt;p&gt;As an aside, it's helpful to have this in your user model, if you want current_user.send(&amp;quot;#{role}?&amp;quot;) to work&lt;/p&gt;

&lt;pre&gt;## user.rb

ROLES = %w( administrator staff )
ROLES.each do |current_role|
  send(:define_method, &amp;quot;#{current_role}?&amp;quot;) do
    self.role == current_role
  end
end
&lt;/pre&gt;</content>
    <author>
      <name>jamesgolick</name>
      <email>jamesgolick@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/20-restful_authentication-role-requirements/refactors/53" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor52</id>
    <published>2007-09-27T11:59:34-07:00</published>
    <title>[Ruby] On restful_authentication role requirements</title>
    <content type="html">&lt;p&gt;Marc's suggestion.  Which one does everybody like better?&lt;/p&gt;

&lt;pre&gt;def self.requires_role(role, options={})
  before_filter(options) { |controller| controller.instance_eval { logged_in? &amp;amp;&amp;amp; current_user.send(&amp;quot;#{role}?&amp;quot;) ? true : controller.access_denied } }
end&lt;/pre&gt;</content>
    <author>
      <name>jamesgolick</name>
      <email>jamesgolick@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/20-restful_authentication-role-requirements/refactors/52" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor51</id>
    <published>2007-09-27T11:56:32-07:00</published>
    <title>[Ruby] On restful_authentication role requirements</title>
    <content type="html">&lt;p&gt;Problem is, that block seems to get called at the class's scope.  The controller is passed to it, but all those methods are protected.  Send hack to the rescue!&lt;/p&gt;

&lt;p&gt;NoMethodError: undefined method `logged_in?' for ProductsController:Class&lt;/p&gt;

&lt;pre&gt;def self.requires_role(role, options={})
  before_filter(options) { |controller| controller.send(:logged_in?) &amp;amp;&amp;amp; controller.send(:current_user).send(&amp;quot;#{role}?&amp;quot;) ? true : controller.send(:access_denied) }
end&lt;/pre&gt;</content>
    <author>
      <name>jamesgolick</name>
      <email>jamesgolick@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/20-restful_authentication-role-requirements/refactors/51" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor48</id>
    <published>2007-09-27T10:41:28-07:00</published>
    <title>[Ruby] On restful_authentication role requirements</title>
    <content type="html">&lt;p&gt;So, I added this&lt;/p&gt;

&lt;pre&gt;## application.rb

def self.requires_role(role)
  before_filter :login_required
  send(:define_method, :authorized?) do
    logged_in? &amp;amp;&amp;amp; current_user.send(&amp;quot;#{role}?&amp;quot;)
  end
end

## controller

requires_role :administrator&lt;/pre&gt;</content>
    <author>
      <name>jamesgolick</name>
      <email>jamesgolick@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/20-restful_authentication-role-requirements/refactors/48" rel="alternate"/>
  </entry>
</feed>

