<?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:users2</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/2" rel="self"/>
  <title>jamesgolick</title>
  <updated>Thu Jul 17 13:15:00 -0700 2008</updated>
  <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>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code20</id>
    <published>2007-09-27T10:40:32-07:00</published>
    <updated>2007-09-27T10:40:32-07:00</updated>
    <title>[Ruby] restful_authentication role requirements</title>
    <content type="html">&lt;p&gt;This is a couple of lines that I was writing a lot&lt;/p&gt;

&lt;pre&gt;## Controller that can only be accessed by an administrator

before_filter :login_required

# ... the controller

private
  def authorized?
    logged_in? &amp;amp;&amp;amp; current_user.administrator?
  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" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor24</id>
    <published>2007-09-20T11:54:03-07:00</published>
    <title>[Ruby] On Knowing mocha</title>
    <content type="html">&lt;p&gt;After&lt;/p&gt;

&lt;pre&gt;def test_should_create_image_if_necessary_before_trying_to_give_it_data
  file = mock :content_type =&amp;gt; &amp;quot;jpg&amp;quot;, :size =&amp;gt; 10, :original_filename =&amp;gt; &amp;quot;something.jpg&amp;quot;, :path =&amp;gt; &amp;quot;/home&amp;quot;

  assert_nothing_raised do
    @p.uploaded_data = file
  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/12-knowing-mocha/refactors/24" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code12</id>
    <published>2007-09-20T11:53:21-07:00</published>
    <updated>2007-09-20T11:53:21-07:00</updated>
    <title>[Ruby] Knowing mocha</title>
    <content type="html">&lt;p&gt;Before&lt;/p&gt;

&lt;pre&gt;def test_should_create_image_if_necessary_before_trying_to_give_it_data
  file = mock()
  file.expects(:content_type).returns('jpg')
  file.expects(:size).returns(10)
  file.expects(:original_filename).returns('something.jpg')
  file.expects(:path).returns('/root?')
  
  assert_nothing_raised do
    @p.uploaded_data = file
  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/12-knowing-mocha" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code10</id>
    <published>2007-09-20T08:16:49-07:00</published>
    <updated>2007-09-20T08:16:49-07:00</updated>
    <title>[Ruby] Linking that doesn't suck as much</title>
    <content type="html">&lt;p&gt;Tammer Saleh, creator/maintainer of the awesome shoulda plugin posted this to his blog (&lt;a href="http://blog.tammersaleh.com/articles/2007/07/26/its-the-little-things" target="_blank"&gt;http://blog.tammersaleh.com/articles/2007/07/26/its-the-little-things&lt;/a&gt;).  It's an awesome start, but I can see a lot of ways it could be improved, and become truly awesome.  What are your takes? &lt;/p&gt;

&lt;pre&gt;def link(item, msg = nil)
  msg ||= item.send([:name, :title, :id].detect {|n| item.respond_to? n})
  method = &amp;quot;#{item.class.name.underscore}_path&amp;quot;
  parents = item.parents rescue []
  link_to(msg, self.send(method, *parents, item))
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/10-linking-that-doesn-t-suck-as-much" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor19</id>
    <published>2007-09-19T13:32:52-07:00</published>
    <title>[Ruby] On Typical restful scaffold controller</title>
    <content type="html">&lt;p&gt;Why not use make_resourceful&lt;/p&gt;

&lt;pre&gt;class PostsController &amp;lt; ApplicationController

  make_resourceful do
    actions :all
  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/1-typical-restful-scaffold-controller/refactors/19" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor17</id>
    <published>2007-09-19T13:18:55-07:00</published>
    <title>[Ruby] On How to make time_ago_in_words cachable ?</title>
    <content type="html">&lt;p&gt;But document.write is still gross, so here's my take&lt;/p&gt;

&lt;pre&gt;def cachable_time_ago_in_words(from, id_prefix = '')
  id = &amp;quot;#{id_prefix}_#{from.to_i}&amp;quot;
  content_tag :span, &amp;quot;&amp;quot;, :id =&amp;gt; id
  javascript_tag &amp;quot;$('#{id}').update(DateHelper.timeAgoInWords(#{(from.to_i * 1000).to_json}))&amp;quot;
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/7-how-to-make-time_ago_in_words-cachable/refactors/17" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14</id>
    <published>2007-09-19T13:02:09-07:00</published>
    <title>[Ruby] On Ruby simple loop</title>
    <content type="html">&lt;p&gt;or even shorter&lt;/p&gt;

&lt;pre&gt;(1..10).each { |i| puts i }&lt;/pre&gt;</content>
    <author>
      <name>jamesgolick</name>
      <email>jamesgolick@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/2-ruby-simple-loop/refactors/14" rel="alternate"/>
  </entry>
</feed>

