<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Tony Amoyal</title>
	
	<link>http://www.tonyamoyal.com</link>
	<description>iPhone Developer and Product Strategist</description>
	<lastBuildDate>Mon, 14 Jan 2013 18:00:22 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/TonyAmoyal" /><feedburner:info uri="tonyamoyal" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>TonyAmoyal</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Recursive custom to_json in Ruby for arrays of objects or nested objects</title>
		<link>http://feedproxy.google.com/~r/TonyAmoyal/~3/qd6ljAIj-Zo/</link>
		<comments>http://www.tonyamoyal.com/2011/03/25/recursive-custom-to_json-in-ruby-for-arrays-of-objects-or-nested-objects/#comments</comments>
		<pubDate>Fri, 25 Mar 2011 15:42:52 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[as_json]]></category>
		<category><![CDATA[custom json]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[to_json]]></category>

		<guid isPermaLink="false">http://www.tonyamoyal.com/?p=684</guid>
		<description><![CDATA[Let&#8217;s say we want to customize the json template for an object:

class User
  def to_json
    super&#40;:except =&#62; &#91;:password, :password_salt, :encrypted_password,
      :last_sign_in_ip, :updated_at, :current_sign_in_ip,
      :remember_token, :reset_password_token, :remember_created_at&#93;&#41;
  end
end

Apparently this is fixed in Ruby 1.9 but for now only converting an object [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.tonyamoyal.com%2F2011%2F03%2F25%2Frecursive-custom-to_json-in-ruby-for-arrays-of-objects-or-nested-objects%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.tonyamoyal.com%2F2011%2F03%2F25%2Frecursive-custom-to_json-in-ruby-for-arrays-of-objects-or-nested-objects%2F" height="61" width="51" /></a></div><p>Let&#8217;s say we want to customize the json template for an object:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User
  <span style="color:#9966CC; font-weight:bold;">def</span> to_json
    <span style="color:#9966CC; font-weight:bold;">super</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:except</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:password</span>, <span style="color:#ff3333; font-weight:bold;">:password_salt</span>, <span style="color:#ff3333; font-weight:bold;">:encrypted_password</span>,
      <span style="color:#ff3333; font-weight:bold;">:last_sign_in_ip</span>, <span style="color:#ff3333; font-weight:bold;">:updated_at</span>, <span style="color:#ff3333; font-weight:bold;">:current_sign_in_ip</span>,
      <span style="color:#ff3333; font-weight:bold;">:remember_token</span>, <span style="color:#ff3333; font-weight:bold;">:reset_password_token</span>, <span style="color:#ff3333; font-weight:bold;">:remember_created_at</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Apparently this is fixed in Ruby 1.9 but for now only converting an object directly to custom json works:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">first</span>
<span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">to_json</span>
<span style="color:#008000; font-style:italic;"># works fine</span></pre></div></div>

<p>Cool but we want that to work for arrays:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@users</span> = User.<span style="color:#9900CC;">all</span>
<span style="color:#0066ff; font-weight:bold;">@users</span>.<span style="color:#9900CC;">to_json</span>
<span style="color:#008000; font-style:italic;"># prints all attributes (does not hit our custom to_json)</span></pre></div></div>

<p>And we also want custom responses in our APIs:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@users</span> = User.<span style="color:#9900CC;">all</span>
data = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
data<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:results</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0066ff; font-weight:bold;">@users</span>
data<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:count</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0066ff; font-weight:bold;">@users</span>.<span style="color:#9900CC;">length</span>
data.<span style="color:#9900CC;">to_json</span>
<span style="color:#008000; font-style:italic;"># prints all attributes (does not hit our custom to_json)</span></pre></div></div>

<p>To fix this override <strong>as_json</strong> and use ActiveSupport&#8217;s encoding method. Don&#8217;t override to_json:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User
<span style="color:#9966CC; font-weight:bold;">def</span> as_json<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">super</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:except</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:password</span>, <span style="color:#ff3333; font-weight:bold;">:password_salt</span>, <span style="color:#ff3333; font-weight:bold;">:encrypted_password</span>,
      <span style="color:#ff3333; font-weight:bold;">:last_sign_in_ip</span>, <span style="color:#ff3333; font-weight:bold;">:updated_at</span>, <span style="color:#ff3333; font-weight:bold;">:current_sign_in_ip</span>,
      <span style="color:#ff3333; font-weight:bold;">:remember_token</span>, <span style="color:#ff3333; font-weight:bold;">:reset_password_token</span>, <span style="color:#ff3333; font-weight:bold;">:remember_created_at</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
...
<span style="color:#0066ff; font-weight:bold;">@users</span> = User.<span style="color:#9900CC;">order_by</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;last_name&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@users</span>
        data = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
        data<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:results</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0066ff; font-weight:bold;">@users</span>
        res = data.<span style="color:#9900CC;">as_json</span>
        <span style="color:#6666ff; font-weight:bold;">ActiveSupport::JSON</span>.<span style="color:#9900CC;">encode</span><span style="color:#006600; font-weight:bold;">&#40;</span>res<span style="color:#006600; font-weight:bold;">&#41;</span>
...</pre></div></div>

<div style='clear:both'></div><img src="http://feeds.feedburner.com/~r/TonyAmoyal/~4/qd6ljAIj-Zo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tonyamoyal.com/2011/03/25/recursive-custom-to_json-in-ruby-for-arrays-of-objects-or-nested-objects/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.tonyamoyal.com/2011/03/25/recursive-custom-to_json-in-ruby-for-arrays-of-objects-or-nested-objects/</feedburner:origLink></item>
		<item>
		<title>Deduping join data in ruby based on multiple attributes</title>
		<link>http://feedproxy.google.com/~r/TonyAmoyal/~3/FPcrKor5rZg/</link>
		<comments>http://www.tonyamoyal.com/2011/03/23/deduping-join-data-in-ruby-based-on-multiple-attributes/#comments</comments>
		<pubDate>Thu, 24 Mar 2011 04:53:39 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[de-duplication]]></category>
		<category><![CDATA[habtm]]></category>
		<category><![CDATA[removing duplicates]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[unique index]]></category>

		<guid isPermaLink="false">http://www.tonyamoyal.com/?p=681</guid>
		<description><![CDATA[In the past I created an app using Rails built in HABTM and no unique index on the join tables.  I&#8217;m more into the has_many :through choice today.  
In order to migrate my data with a unqiue index, I had a bunch of de-duping to do.  It was a bit trickier than [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.tonyamoyal.com%2F2011%2F03%2F23%2Fdeduping-join-data-in-ruby-based-on-multiple-attributes%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.tonyamoyal.com%2F2011%2F03%2F23%2Fdeduping-join-data-in-ruby-based-on-multiple-attributes%2F" height="61" width="51" /></a></div><p>In the past I created an app using <a href="http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many">Rails built in HABTM</a> and no <a href="http://stackoverflow.com/questions/1449459/how-to-make-column-unique-and-index-it-in-rails-migration">unique index</a> on the join tables.  I&#8217;m more into <a href="http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many">the has_many :through choice</a> today.  </p>
<p>In order to migrate my data with a unqiue index, I had a bunch of de-duping to do.  It was a bit trickier than usual because I had to de-dupe based on multiple attributes.  I wrote a quick class in ruby to take of this and decided I should share it.  Let me know if you have a better way. I sorta went with the fastest because I&#8217;m up against a deadline right now.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> JoinDuplicateRemover
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">de_dup</span>
    models_dedup_hash = <span style="color:#006600; font-weight:bold;">&#123;</span>
      AccountEventType <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:account_id</span>, <span style="color:#ff3333; font-weight:bold;">:event_type_id</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
      ClientEventType <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:client_id</span>, <span style="color:#ff3333; font-weight:bold;">:event_type_id</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
      AccountProductPackage <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:account_id</span>, <span style="color:#ff3333; font-weight:bold;">:product_package_id</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
      ClientProductPackage <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:client_id</span>, <span style="color:#ff3333; font-weight:bold;">:product_package_id</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
      ClientEventCountry <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:client_id</span>, <span style="color:#ff3333; font-weight:bold;">:country_id</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
      ClientResearchCountry <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:client_id</span>, <span style="color:#ff3333; font-weight:bold;">:country_id</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
      AccountEventCountry <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:account_id</span>, <span style="color:#ff3333; font-weight:bold;">:country_id</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
      AccountResearchCountry <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:account_id</span>, <span style="color:#ff3333; font-weight:bold;">:country_id</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
    models_dedup_hash.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>klass,scope_attrs<span style="color:#006600; font-weight:bold;">|</span>
&nbsp;
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>Start de-duping #{klass.to_s} - #{Time.now}&quot;</span>
      rows_to_del = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      index = <span style="color:#006666;">0</span>
&nbsp;
      klass.<span style="color:#9900CC;">all</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>instance<span style="color:#006600; font-weight:bold;">|</span>
&nbsp;
        index <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
        <span style="color:#9966CC; font-weight:bold;">if</span> index <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">10</span> == <span style="color:#006666;">0</span>
          <span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;.&quot;</span>
          STDOUT.<span style="color:#9900CC;">flush</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        conditions_hash = <span style="color:#CC00FF; font-weight:bold;">Hash</span>.<span style="color:#9900CC;">new</span>
        scope_attrs.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>sa<span style="color:#006600; font-weight:bold;">|</span>
          conditions_hash<span style="color:#006600; font-weight:bold;">&#91;</span>sa<span style="color:#006600; font-weight:bold;">&#93;</span> = instance.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>sa<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
        klass.<span style="color:#9900CC;">where</span><span style="color:#006600; font-weight:bold;">&#40;</span>conditions_hash<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each_with_index</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>dupl,i<span style="color:#006600; font-weight:bold;">|</span>
          rows_to_del <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> dupl.<span style="color:#9900CC;">id</span> <span style="color:#9966CC; font-weight:bold;">unless</span> i == <span style="color:#006666;">0</span> <span style="color:#9966CC; font-weight:bold;">or</span> rows_to_del.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>dupl.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#41;</span> 
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      rows_to_del.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>dupl<span style="color:#006600; font-weight:bold;">|</span>
        instance = klass.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>dupl<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Destroying: #{instance.inspect}&quot;</span>
        instance.<span style="color:#9900CC;">destroy</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>Finished de-duping #{klass.to_s} - #{Time.now}&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<div style='clear:both'></div><img src="http://feeds.feedburner.com/~r/TonyAmoyal/~4/FPcrKor5rZg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tonyamoyal.com/2011/03/23/deduping-join-data-in-ruby-based-on-multiple-attributes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.tonyamoyal.com/2011/03/23/deduping-join-data-in-ruby-based-on-multiple-attributes/</feedburner:origLink></item>
		<item>
		<title>More cool stuff with ruby’s tap method</title>
		<link>http://feedproxy.google.com/~r/TonyAmoyal/~3/VlvRbYyGDl4/</link>
		<comments>http://www.tonyamoyal.com/2011/01/28/more-cool-stuff-with-rubys-tap-method/#comments</comments>
		<pubDate>Sat, 29 Jan 2011 04:28:31 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tap]]></category>

		<guid isPermaLink="false">http://www.tonyamoyal.com/?p=676</guid>
		<description><![CDATA[I had to write up a quick data processor at work today and I wanted a decent output at the end of execution.  The ruby tap method came in handy here.

class AssetDeliveryPopulater
&#160;
  def self.populate
    results_text = &#91;&#93;
    asset_secrets_count, asset_delivery_count, asset_not_found_count, asset_delivery_creation_errors = 0,0,0,0
&#160;
    Email.all.tap&#123;&#124;emails&#124; [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.tonyamoyal.com%2F2011%2F01%2F28%2Fmore-cool-stuff-with-rubys-tap-method%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.tonyamoyal.com%2F2011%2F01%2F28%2Fmore-cool-stuff-with-rubys-tap-method%2F" height="61" width="51" /></a></div><p>I had to write up a quick data processor at work today and I wanted a decent output at the end of execution.  The ruby tap method came in handy here.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> AssetDeliveryPopulater
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">populate</span>
    results_text = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    asset_secrets_count, asset_delivery_count, asset_not_found_count, asset_delivery_creation_errors = <span style="color:#006666;">0</span>,<span style="color:#006666;">0</span>,<span style="color:#006666;">0</span>,<span style="color:#006666;">0</span>
&nbsp;
    Email.<span style="color:#9900CC;">all</span>.<span style="color:#9900CC;">tap</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>emails<span style="color:#006600; font-weight:bold;">|</span> results_text <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;#{emails.length} emails parsed.&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#9966CC; font-weight:bold;">unless</span> e.<span style="color:#9900CC;">body</span>
&nbsp;
      asset_secrets = extract_asset_secrets_from_text<span style="color:#006600; font-weight:bold;">&#40;</span>e.<span style="color:#9900CC;">body</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#9966CC; font-weight:bold;">unless</span> asset_secrets
&nbsp;
      asset_secrets.<span style="color:#9900CC;">tap</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>as<span style="color:#006600; font-weight:bold;">|</span> asset_secrets_count <span style="color:#006600; font-weight:bold;">+</span>= as.<span style="color:#9900CC;">length</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>asset_secret<span style="color:#006600; font-weight:bold;">|</span>
        <span style="color:#9966CC; font-weight:bold;">if</span> a = Asset.<span style="color:#9900CC;">find_by_secret</span><span style="color:#006600; font-weight:bold;">&#40;</span>asset_secret<span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#9966CC; font-weight:bold;">if</span> ad = AssetDelivery.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span>
              <span style="color:#ff3333; font-weight:bold;">:asset</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> a,
              <span style="color:#ff3333; font-weight:bold;">:campaign</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e.<span style="color:#9900CC;">campaign</span>,
              <span style="color:#ff3333; font-weight:bold;">:client</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e.<span style="color:#9900CC;">client</span>,
              <span style="color:#ff3333; font-weight:bold;">:created_at</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e.<span style="color:#9900CC;">created_at</span>
            <span style="color:#006600; font-weight:bold;">&#41;</span>
            asset_delivery_count <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
            <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Recorded an asset delivery: #{ad.inspect}&quot;</span>
          <span style="color:#9966CC; font-weight:bold;">else</span>
            asset_delivery_creation_errors <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
            <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Could not record the delivery: #{ad.errors.inspect}&quot;</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#9966CC; font-weight:bold;">else</span>
          asset_not_found_count <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
          <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Could not find asset with secret #{asset_secret}&quot;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    results_text <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;#{asset_secrets_count} asset secrets found.&quot;</span>
    results_text <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;#{asset_delivery_count} asset deliveries recorded.&quot;</span>
    results_text <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;#{asset_not_found_count} assets not found.&quot;</span>
    results_text <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;#{asset_delivery_creation_errors} asset delivery records not created due to errors.&quot;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> results_text.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Given some text, extract secret asset hashes and return an array</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">extract_asset_secrets_from_text</span><span style="color:#006600; font-weight:bold;">&#40;</span>text<span style="color:#006600; font-weight:bold;">&#41;</span>
    text.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>http:\<span style="color:#006600; font-weight:bold;">/</span>\<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#91;</span>^<span style="color:#996600;">'<span style="color:#000099;">\/</span>]+<span style="color:#000099;">\/</span>products<span style="color:#000099;">\/</span>([^'</span>\<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">first</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<div style='clear:both'></div><img src="http://feeds.feedburner.com/~r/TonyAmoyal/~4/VlvRbYyGDl4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tonyamoyal.com/2011/01/28/more-cool-stuff-with-rubys-tap-method/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.tonyamoyal.com/2011/01/28/more-cool-stuff-with-rubys-tap-method/</feedburner:origLink></item>
		<item>
		<title>Tap before you squash</title>
		<link>http://feedproxy.google.com/~r/TonyAmoyal/~3/w23VKc23jUA/</link>
		<comments>http://www.tonyamoyal.com/2011/01/27/tap-before-you-squash/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 19:46:02 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tap]]></category>

		<guid isPermaLink="false">http://www.tonyamoyal.com/?p=672</guid>
		<description><![CDATA[Tap is a pretty awesome ruby method I recently discovered.
Let&#8217;s say I have the following request:

post  '/api/v1/users/', 
          params.merge&#40;:sig =&#62; 
             Authentication.calculate&#40;TestAuthData.secret_access_key,params.to_query&#41;
          &#41;.to_json

I realize [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.tonyamoyal.com%2F2011%2F01%2F27%2Ftap-before-you-squash%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.tonyamoyal.com%2F2011%2F01%2F27%2Ftap-before-you-squash%2F" height="61" width="51" /></a></div><p>Tap is a pretty awesome ruby method I recently discovered.</p>
<p>Let&#8217;s say I have the following request:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">post  <span style="color:#996600;">'/api/v1/users/'</span>, 
          params.<span style="color:#9900CC;">merge</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:sig</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 
             Authentication.<span style="color:#9900CC;">calculate</span><span style="color:#006600; font-weight:bold;">&#40;</span>TestAuthData.<span style="color:#9900CC;">secret_access_key</span>,params.<span style="color:#9900CC;">to_query</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_json</span></pre></div></div>

<p>I realize something is screwed and I want to print out the sig before sending.  Normally I would actually rewrite my code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">sig = Authentication.<span style="color:#9900CC;">calculate</span><span style="color:#006600; font-weight:bold;">&#40;</span>TestAuthData.<span style="color:#9900CC;">secret_access_key</span>,params.<span style="color:#9900CC;">to_query</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;sig = #{sig}&quot;</span>
post  <span style="color:#996600;">'/api/v1/users/'</span>, params.<span style="color:#9900CC;">merge</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:sig</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> sig<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_json</span></pre></div></div>

<p>That&#8217;s a bit annoying.  With tap I can just do this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">post  <span style="color:#996600;">'/api/v1/users/'</span>, 
          params.<span style="color:#9900CC;">merge</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:sig</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 
            Authentication.<span style="color:#9900CC;">calculate</span><span style="color:#006600; font-weight:bold;">&#40;</span>TestAuthData.<span style="color:#9900CC;">secret_access_key</span>,params.<span style="color:#9900CC;">to_query</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">tap</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>s<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;sig = #{s}&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
          <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_json</span></pre></div></div>

<div style='clear:both'></div><img src="http://feeds.feedburner.com/~r/TonyAmoyal/~4/w23VKc23jUA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tonyamoyal.com/2011/01/27/tap-before-you-squash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.tonyamoyal.com/2011/01/27/tap-before-you-squash/</feedburner:origLink></item>
		<item>
		<title>RSpec 2, Sinatra and Mongoid example</title>
		<link>http://feedproxy.google.com/~r/TonyAmoyal/~3/VXuypFnRg8g/</link>
		<comments>http://www.tonyamoyal.com/2011/01/19/rspec-2-sinatra-and-mongoid-example/#comments</comments>
		<pubDate>Wed, 19 Jan 2011 15:39:48 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[mongoid]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[rspec-2]]></category>
		<category><![CDATA[rspec2]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sinatra]]></category>

		<guid isPermaLink="false">http://www.tonyamoyal.com/?p=662</guid>
		<description><![CDATA[I spent yesterday coding a very abbreviated version of an authentication service that uses Sinatra, Mongoid and RSpec 2.  It took more than a couple hours because there are so many outdated code samples on the web that just don&#8217;t work.  I put my code up on github for people to see.  [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.tonyamoyal.com%2F2011%2F01%2F19%2Frspec-2-sinatra-and-mongoid-example%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.tonyamoyal.com%2F2011%2F01%2F19%2Frspec-2-sinatra-and-mongoid-example%2F" height="61" width="51" /></a></div><p>I spent yesterday coding a very abbreviated version of an authentication service that uses Sinatra, Mongoid and RSpec 2.  It took more than a couple hours because there are so many outdated code samples on the web that just don&#8217;t work.  I put <a href="https://github.com/tamoyal/Identity-Service-Example">my code up on github</a> for people to see.  Just clone the repository and run:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">rake spec</pre></div></div>

<p>While the current example is very basic, I will update it with enhancements and talk more about how to write a good authentication service.  For this example I quickly rolled my own basic authentication since the main purpose is to get you up and running quickly with Sinatra, Mongoid, and RSpec2 .  I may end up using Authlogic, Devise, or improving on my own authentication functionality.</p>
<p>Get the code <a href="https://github.com/tamoyal/Identity-Service-Example">here</a>.</p>
<p>Feel free to contribute to the example.  Keep in mind this is a service to be consumed by front end apps, not a full application.  Also I won&#8217;t accept contributions without tests.</p>
<div style='clear:both'></div><img src="http://feeds.feedburner.com/~r/TonyAmoyal/~4/VXuypFnRg8g" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tonyamoyal.com/2011/01/19/rspec-2-sinatra-and-mongoid-example/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.tonyamoyal.com/2011/01/19/rspec-2-sinatra-and-mongoid-example/</feedburner:origLink></item>
		<item>
		<title>Rails Authentication with Devise and CanCan part 2 – Restful Resources for Administrators</title>
		<link>http://feedproxy.google.com/~r/TonyAmoyal/~3/caA0Mb8Y7-0/</link>
		<comments>http://www.tonyamoyal.com/2010/09/29/rails-authentication-with-devise-and-cancan-part-2-restful-resources-for-administrators/#comments</comments>
		<pubDate>Wed, 29 Sep 2010 17:26:04 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[cancan]]></category>
		<category><![CDATA[CRUD]]></category>
		<category><![CDATA[devise]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[rubyonrails]]></category>
		<category><![CDATA[user-administration]]></category>
		<category><![CDATA[user-permissions]]></category>
		<category><![CDATA[user-roles]]></category>

		<guid isPermaLink="false">http://www.tonyamoyal.com/?p=613</guid>
		<description><![CDATA[About two months ago I wrote an article on getting started with Devise and CanCan.  Since then, I&#8217;ve implemented the Devise + CanCan combo on three projects and wrote a couple specs for Ryan Bates to help improve CanCan functionality.  This article will focus more on Devise with some CanCan sprinkled in there. [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.tonyamoyal.com%2F2010%2F09%2F29%2Frails-authentication-with-devise-and-cancan-part-2-restful-resources-for-administrators%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.tonyamoyal.com%2F2010%2F09%2F29%2Frails-authentication-with-devise-and-cancan-part-2-restful-resources-for-administrators%2F" height="61" width="51" /></a></div><p>About two months ago I wrote an article on <a href="http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/">getting started with Devise and CanCan</a>.  Since then, I&#8217;ve implemented the Devise + CanCan combo on three projects and wrote a couple specs for Ryan Bates to help improve CanCan functionality.  This article will focus more on Devise with some CanCan sprinkled in there. </p>
<p>If you read <a href="http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/">my first article on Devise + CanCan</a>, you have some questions on your mind. The most common question was how to create a RESTful interface for a super admin to CRUD users.  This is a common requirement for managing users and/or keeping registration private. This is really easy.<br />
<h2 style="padding-top: 0;">I&#8217;m going to show you how to keep the awesome functionality provided by Devise while adding your custom stuff with no major time cost!</h2>
<p><img src="http://www.tonyamoyal.com/wp-content/uploads/2010/09/free_sign.gif" alt="Adding custom functionality to a Devise enabled model is easy" title="Adding custom functionality to a Devise enabled model is easy" width="400" height="286" class="size-full wp-image-630" /></p>
<p><strong>Step 1 &#8211; Configure Routes</strong><br />
Since you probably want to keep your public interface for logging in and password recovery,  leave your devise routes and add RESTful routes for users:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">devise_for <span style="color:#ff3333; font-weight:bold;">:users</span>
resources <span style="color:#ff3333; font-weight:bold;">:users</span></pre></div></div>

<p><strong>Step 2 &#8211; The User Controller</strong><br />
Next you need to set up your CRUD actions in the controller. Most of the actions are typical:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> UsersController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  before_filter <span style="color:#ff3333; font-weight:bold;">:get_user</span>, <span style="color:#ff3333; font-weight:bold;">:only</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:index</span>,:new,:edit<span style="color:#006600; font-weight:bold;">&#93;</span>
  before_filter <span style="color:#ff3333; font-weight:bold;">:accessible_roles</span>, <span style="color:#ff3333; font-weight:bold;">:only</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:new</span>, <span style="color:#ff3333; font-weight:bold;">:edit</span>, <span style="color:#ff3333; font-weight:bold;">:show</span>, <span style="color:#ff3333; font-weight:bold;">:update</span>, <span style="color:#ff3333; font-weight:bold;">:create</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  load_and_authorize_resource <span style="color:#ff3333; font-weight:bold;">:only</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:show</span>,:new,:destroy,:edit,:update<span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># GET /users</span>
  <span style="color:#008000; font-style:italic;"># GET /users.xml                                                </span>
  <span style="color:#008000; font-style:italic;"># GET /users.json                                       HTML and AJAX</span>
  <span style="color:#008000; font-style:italic;">#-----------------------------------------------------------------------</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> index
    <span style="color:#0066ff; font-weight:bold;">@users</span> = User.<span style="color:#9900CC;">accessible_by</span><span style="color:#006600; font-weight:bold;">&#40;</span>current_ability, <span style="color:#ff3333; font-weight:bold;">:index</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">limit</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">20</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">json</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:json</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@users</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@users</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># GET /users/new</span>
  <span style="color:#008000; font-style:italic;"># GET /users/new.xml                                            </span>
  <span style="color:#008000; font-style:italic;"># GET /users/new.json                                    HTML AND AJAX</span>
  <span style="color:#008000; font-style:italic;">#-------------------------------------------------------------------</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> new
    respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">json</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:json</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user</span> <span style="color:#006600; font-weight:bold;">&#125;</span>   
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># GET /users/1</span>
  <span style="color:#008000; font-style:italic;"># GET /users/1.xml                                                       </span>
  <span style="color:#008000; font-style:italic;"># GET /users/1.json                                     HTML AND AJAX</span>
  <span style="color:#008000; font-style:italic;">#-------------------------------------------------------------------</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> show
    respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">json</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:json</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span>      
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::RecordNotFound</span>
    respond_to_not_found<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:json</span>, <span style="color:#ff3333; font-weight:bold;">:xml</span>, <span style="color:#ff3333; font-weight:bold;">:html</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># GET /users/1/edit                                                      </span>
  <span style="color:#008000; font-style:italic;"># GET /users/1/edit.xml                                                      </span>
  <span style="color:#008000; font-style:italic;"># GET /users/1/edit.json                                HTML AND AJAX</span>
  <span style="color:#008000; font-style:italic;">#-------------------------------------------------------------------</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> edit
    respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">json</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:json</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user</span> <span style="color:#006600; font-weight:bold;">&#125;</span>   
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::RecordNotFound</span>
    respond_to_not_found<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:json</span>, <span style="color:#ff3333; font-weight:bold;">:xml</span>, <span style="color:#ff3333; font-weight:bold;">:html</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># DELETE /users/1     </span>
  <span style="color:#008000; font-style:italic;"># DELETE /users/1.xml</span>
  <span style="color:#008000; font-style:italic;"># DELETE /users/1.json                                  HTML AND AJAX</span>
  <span style="color:#008000; font-style:italic;">#-------------------------------------------------------------------</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> destroy
    <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">destroy</span>!
&nbsp;
    respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">json</span> <span style="color:#006600; font-weight:bold;">&#123;</span> respond_to_destroy<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:ajax</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> head <span style="color:#ff3333; font-weight:bold;">:ok</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> respond_to_destroy<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:html</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>      
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::RecordNotFound</span>
    respond_to_not_found<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:json</span>, <span style="color:#ff3333; font-weight:bold;">:xml</span>, <span style="color:#ff3333; font-weight:bold;">:html</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># POST /users</span>
  <span style="color:#008000; font-style:italic;"># POST /users.xml         </span>
  <span style="color:#008000; font-style:italic;"># POST /users.json                                      HTML AND AJAX</span>
  <span style="color:#008000; font-style:italic;">#-----------------------------------------------------------------</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> create
    <span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">save</span>
      respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
        <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">json</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:json</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">to_json</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">200</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
        <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> head <span style="color:#ff3333; font-weight:bold;">:ok</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
        <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> redirect_to <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:index</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
        <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">json</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:text</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Could not create user&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:unprocessable_entity</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;"># placeholder</span>
        <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> head <span style="color:#ff3333; font-weight:bold;">:ok</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
        <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:new</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:unprocessable_entity</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  ...
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>CanCan provides the class level <strong>accessible_by</strong> method that I am using to retrieve all users that can be viewed by the current user.  The <strong>load_and_authorize_resource</strong> filter provided by CanCan actually does this accessibility filtering for you to keep things DRY and it recognizes collections as of <a href="http://github.com/ryanb/cancan/issues/closed#issue/143">this issue  fix</a>.  I support sorting in my actual code so I didn&#8217;t want to use the CanCan filter to grab my user objects in the index action.</p>
<p>The other two filters &#8211; <strong>get_user</strong> and <strong>accessible_roles</strong> &#8211; are pretty basic:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># Get roles accessible by the current user</span>
  <span style="color:#008000; font-style:italic;">#----------------------------------------------------</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> accessible_roles
    <span style="color:#0066ff; font-weight:bold;">@accessible_roles</span> = Role.<span style="color:#9900CC;">accessible_by</span><span style="color:#006600; font-weight:bold;">&#40;</span>current_ability,:read<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Make the current user object available to views</span>
  <span style="color:#008000; font-style:italic;">#----------------------------------------</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> get_user
    <span style="color:#0066ff; font-weight:bold;">@current_user</span> = current_user
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Finally, <strong>respond_to_not_found</strong> is an application wide helper I use to respond when a requested object is not found.  I stole it from the <a href="http://github.com/michaeldv/fat_free_crm/blob/master/app/controllers/application_controller.rb">Fat Free CRM source code</a>.</p>
<p>Next we&#8217;ll look at the Update action which has some non standard code.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># PUT /users/1</span>
  <span style="color:#008000; font-style:italic;"># PUT /users/1.xml</span>
  <span style="color:#008000; font-style:italic;"># PUT /users/1.json                                            HTML AND AJAX</span>
  <span style="color:#008000; font-style:italic;">#----------------------------------------------------------------------------</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> update
    <span style="color:#9966CC; font-weight:bold;">if</span> params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:password</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">blank</span>?
      <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:password</span>,:password_confirmation,:current_password<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">collect</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">errors</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:base</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;The password you entered is incorrect&quot;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">valid_password</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:current_password</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">errors</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:base</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">empty</span>? <span style="color:#9966CC; font-weight:bold;">and</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">update_attributes</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Your account has been updated&quot;</span>
        <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">json</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:json</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">to_json</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">200</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
        <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> head <span style="color:#ff3333; font-weight:bold;">:ok</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
        <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:edit</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">json</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:text</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Could not update user&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:unprocessable_entity</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;">#placeholder</span>
        <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">errors</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:unprocessable_entity</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
        <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:edit</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:unprocessable_entity</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::RecordNotFound</span>
    respond_to_not_found<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:js</span>, <span style="color:#ff3333; font-weight:bold;">:xml</span>, <span style="color:#ff3333; font-weight:bold;">:html</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h2 style="padding-top: 0;">That was easy!</h2>
<p>The only unusual code we added is to clean up the password request params if the user&#8217;s  password field is blank, and validate the password with Devise&#8217;s <strong>valid_password?</strong> method if it&#8217;s not blank.  That allows us to provide a UI that looks like this:<br />
<img src="http://www.tonyamoyal.com/wp-content/uploads/2010/09/Screen-shot-2010-09-29-at-11.20.17-AM.png" alt="User interface for editing a user with RESTful additions to the Rails Devise authentication gem" title="User interface for editing a user with RESTful additions to the Rails Devise authentication gem" width="518" height="281" class="size-full wp-image-619" /><br />
<strong>Step 3 &#8211; Add you views</strong><br />
It may seem a bit redundant to show all of my view code, but I&#8217;m going to do it anyway so you can see how I&#8217;m using CanCan everywhere.</p>
<p><strong>index.html.erb</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&lt;!-- table header stuff here --&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#0066ff; font-weight:bold;">@users</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>u<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;tr&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to_if<span style="color:#006600; font-weight:bold;">&#40;</span>can?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:read</span>, User<span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">&quot;#{u.name}&quot;</span>, user_path<span style="color:#006600; font-weight:bold;">&#40;</span>u.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#996600;">&quot;#{u.email}&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#996600;">&quot;#{u.roles.collect{|r| r.name}}&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to_if<span style="color:#006600; font-weight:bold;">&#40;</span>can?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:edit</span>, User<span style="color:#006600; font-weight:bold;">&#41;</span>, image_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/images/edit_icon.gif&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, edit_user_path<span style="color:#006600; font-weight:bold;">&#40;</span>u.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to_if<span style="color:#006600; font-weight:bold;">&#40;</span>can?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:delete</span>, u<span style="color:#006600; font-weight:bold;">&#41;</span>, image_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/images/delete_icon.gif&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, u, <span style="color:#ff3333; font-weight:bold;">:confirm</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Are you sure?&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:method</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:delete</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
  &lt;/tr&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Note the heavy use of <strong>link_to_if(can?(:method, object), innerHTML, path, options) </strong>.  It&#8217;s nice to only display links to those who can actually use them.</p>
<p>User registration &#8211; <strong>new.html.erb</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&lt;h2&gt;Register User&lt;/h2&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span>= form_for<span style="color:#006600; font-weight:bold;">&#40;</span>@user<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= error_messages<span style="color:#006600; font-weight:bold;">&#40;</span>@user,<span style="color:#996600;">&quot;Could not register user&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= render <span style="color:#ff3333; font-weight:bold;">:partial</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'user_fields'</span>, <span style="color:#ff3333; font-weight:bold;">:locals</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:f</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> f <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
  &lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
  &lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">password_field</span> <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&nbsp;
  &lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:password_confirmation</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
  &lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">password_field</span> <span style="color:#ff3333; font-weight:bold;">:password_confirmation</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&nbsp;
  &lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">submit</span> <span style="color:#996600;">&quot;Register&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Where the partial &#8211; <strong>_user_fields.html.erb</strong> is the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:first_name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:first_name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&nbsp;
&lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:last_name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:last_name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&nbsp;
&lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">if</span> can? <span style="color:#ff3333; font-weight:bold;">:read</span>, Role <span style="color:#006600; font-weight:bold;">%&gt;</span>
	&lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:role</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
	&lt;ul class=&quot;no-pad no-bullets&quot;&gt;
		<span style="color:#006600; font-weight:bold;">&lt;%</span>= habtm_checkboxes<span style="color:#006600; font-weight:bold;">&#40;</span>@user, <span style="color:#ff3333; font-weight:bold;">:role_ids</span>, <span style="color:#0066ff; font-weight:bold;">@accessible_roles</span>, <span style="color:#ff3333; font-weight:bold;">:name</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
	&lt;/ul&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>This is quite similar to the Devise generated views except we are not using a general &#8220;resource&#8221; object, we are specifying @user.  Owning our views gives us the ability to easily add custom fields.</p>
<p><strong>edit.html.erb</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&lt;h3&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@user</span> == <span style="color:#0066ff; font-weight:bold;">@current_user</span> ? <span style="color:#996600;">&quot;Your Account Settings&quot;</span> : <span style="color:#996600;">&quot;Edit User&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/h3&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span>= form_for<span style="color:#006600; font-weight:bold;">&#40;</span>@user, <span style="color:#ff3333; font-weight:bold;">:html</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:method</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:put</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
	<span style="color:#006600; font-weight:bold;">&lt;%</span>= error_messages<span style="color:#006600; font-weight:bold;">&#40;</span>@user,<span style="color:#996600;">&quot;Could not update user&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
	<span style="color:#006600; font-weight:bold;">&lt;%</span>= render <span style="color:#ff3333; font-weight:bold;">:partial</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'user_fields'</span>, <span style="color:#ff3333; font-weight:bold;">:locals</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:f</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> f <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
	&lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> &lt;i&gt;(leave blank if you don't want to change it)&lt;/i&gt;&lt;/p&gt;
	&lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">password_field</span> <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&nbsp;
	&lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:password_confirmation</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
	&lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">password_field</span> <span style="color:#ff3333; font-weight:bold;">:password_confirmation</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&nbsp;
	&lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:current_password</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> &lt;i&gt;(we need your current password to confirm your changes)&lt;/i&gt;&lt;/p&gt;
	&lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">password_field</span> <span style="color:#ff3333; font-weight:bold;">:current_password</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&nbsp;
  &lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">submit</span> <span style="color:#996600;">&quot;Update&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">&quot;Back&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:back</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p><strong>show.html.erb</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&lt;h3&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/h3&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to_if<span style="color:#006600; font-weight:bold;">&#40;</span>can?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:update</span>,@user<span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">&quot;Edit&quot;</span>, edit_user_path<span style="color:#006600; font-weight:bold;">&#40;</span>@user<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> |
<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to_if<span style="color:#006600; font-weight:bold;">&#40;</span>can?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:delete</span>, <span style="color:#0066ff; font-weight:bold;">@user</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">&quot;Delete&quot;</span>, user_path<span style="color:#006600; font-weight:bold;">&#40;</span>@user<span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#ff3333; font-weight:bold;">:confirm</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Are you sure?&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:method</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:delete</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
&lt;table class=&quot;one-column-emphasis&quot;&gt;
	&lt;tbody&gt;
		&lt;tr&gt;
			&lt;td class=&quot;oce-first&quot;&gt;Email:&lt;/td&gt;
			&lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">email</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td class=&quot;oce-first&quot;&gt;Role:&lt;/td&gt;
			&lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">roles</span>.<span style="color:#9900CC;">first</span>.<span style="color:#9900CC;">name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
		&lt;/tr&gt;
	<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">if</span> can?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:see_timestamps</span>,User<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
		&lt;tr&gt;
			&lt;td class=&quot;oce-first&quot;&gt;Created at:&lt;/td&gt;
			&lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">created_at</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td class=&quot;oce-first&quot;&gt;Last Sign In:&lt;/td&gt;
			&lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">last_sign_in_at</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td class=&quot;oce-first&quot;&gt;Sign In Count:&lt;/td&gt;
			&lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">sign_in_count</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
		&lt;/tr&gt;
	<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
	&lt;/tbody&gt;
&lt;/table&gt;</pre></div></div>

<h2 style="padding-top: 0;">Wait&#8230;WTF is &#8220;see_timestamps&#8221; ???</h2>
<p>This is one thing I love about CanCan &#8211; it&#8217;s easy to add arbitrary permissions.  In my CanCan ability class I can have:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#------------------------------------------------</span>
<span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>user<span style="color:#006600; font-weight:bold;">&#41;</span>
    user <span style="color:#006600; font-weight:bold;">||</span>= User.<span style="color:#9900CC;">new</span> <span style="color:#008000; font-style:italic;"># guest user</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> user.<span style="color:#9900CC;">role</span>? <span style="color:#ff3333; font-weight:bold;">:admin</span>
      can <span style="color:#ff3333; font-weight:bold;">:see_timestamps</span>, User
    <span style="color:#9966CC; font-weight:bold;">elsif</span> user.<span style="color:#9900CC;">role</span>? <span style="color:#ff3333; font-weight:bold;">:normal</span>
      can <span style="color:#ff3333; font-weight:bold;">:see_timestamps</span>, User, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> user.<span style="color:#9900CC;">id</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I&#8217;ll admit that in my actual code I&#8217;m showing the timestamps to users that <strong>can? :manage, :all</strong> but you get the idea.</p>
<p>Between this post and my <a href="http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/">first post on Devise and CanCan</a>, you should be rocking out.<br />
<img src="http://www.tonyamoyal.com/wp-content/uploads/2010/09/rockin_out_funny.jpg" alt="Rockin out with Devise and CanCan ...or a fake guitar" title="Rockin out with Devise and CanCan ...or a fake guitar" width="300" height="380" class="alignnone size-full wp-image-638" /></p>
<div style='clear:both'></div><img src="http://feeds.feedburner.com/~r/TonyAmoyal/~4/caA0Mb8Y7-0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tonyamoyal.com/2010/09/29/rails-authentication-with-devise-and-cancan-part-2-restful-resources-for-administrators/feed/</wfw:commentRss>
		<slash:comments>69</slash:comments>
		<feedburner:origLink>http://www.tonyamoyal.com/2010/09/29/rails-authentication-with-devise-and-cancan-part-2-restful-resources-for-administrators/</feedburner:origLink></item>
		<item>
		<title>Gym Shuffle</title>
		<link>http://feedproxy.google.com/~r/TonyAmoyal/~3/hV8mOzZ0S6w/</link>
		<comments>http://www.tonyamoyal.com/2010/09/14/gym-shuffle/#comments</comments>
		<pubDate>Tue, 14 Sep 2010 16:13:47 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[portfolio]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[SQLite]]></category>

		<guid isPermaLink="false">http://www.tonyamoyal.com/?p=607</guid>
		<description><![CDATA[Gym Shuffle is the world's first workout randomizer for your iPhone.]]></description>
			<content:encoded><![CDATA[Gym Shuffle is the world's first workout randomizer for your iPhone.<img src="http://feeds.feedburner.com/~r/TonyAmoyal/~4/hV8mOzZ0S6w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tonyamoyal.com/2010/09/14/gym-shuffle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.tonyamoyal.com/2010/09/14/gym-shuffle/</feedburner:origLink></item>
		<item>
		<title>Better Human Race</title>
		<link>http://feedproxy.google.com/~r/TonyAmoyal/~3/JpdzZ6LUkBY/</link>
		<comments>http://www.tonyamoyal.com/2010/09/14/better-human-race/#comments</comments>
		<pubDate>Tue, 14 Sep 2010 15:25:56 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[portfolio]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[Conduit]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Facebook API]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[passenger]]></category>
		<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://www.tonyamoyal.com/?p=602</guid>
		<description><![CDATA[Better Human Race is the easiest way to raise money for charity.  Search with Yahoo, Shop with Amazon, Connect with Facebook, and use our toolbar.  I am co-founder and currently the developer of all Better Human Race technology.]]></description>
			<content:encoded><![CDATA[Better Human Race is the easiest way to raise money for charity.  Search with Yahoo, Shop with Amazon, Connect with Facebook, and use our toolbar.  I am co-founder and currently the developer of all Better Human Race technology.<img src="http://feeds.feedburner.com/~r/TonyAmoyal/~4/JpdzZ6LUkBY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tonyamoyal.com/2010/09/14/better-human-race/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.tonyamoyal.com/2010/09/14/better-human-race/</feedburner:origLink></item>
		<item>
		<title>Rails Authentication with Devise and CanCan – Customizing Devise Controllers</title>
		<link>http://feedproxy.google.com/~r/TonyAmoyal/~3/4q-hKsN-XjY/</link>
		<comments>http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 16:34:17 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[can-can]]></category>
		<category><![CDATA[devise]]></category>
		<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[rubyonrails]]></category>
		<category><![CDATA[ryan-bates]]></category>
		<category><![CDATA[user-permissions]]></category>
		<category><![CDATA[user-roles]]></category>

		<guid isPermaLink="false">http://www.tonyamoyal.com/?p=543</guid>
		<description><![CDATA[I&#8217;m tired of spending loads of time creating user authentication systems with permissions or swimming against the current to customize what&#8217;s available. There&#8217;s great open source stuff out there but until now, I haven&#8217;t gotten the full package with really easy customization.
The Devise and CanCan combo for user authentication and permissions in Rails is my [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.tonyamoyal.com%2F2010%2F07%2F28%2Frails-authentication-with-devise-and-cancan-customizing-devise-controllers%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.tonyamoyal.com%2F2010%2F07%2F28%2Frails-authentication-with-devise-and-cancan-customizing-devise-controllers%2F" height="61" width="51" /></a></div><p>I&#8217;m tired of spending loads of time creating user authentication systems with permissions or swimming against the current to customize what&#8217;s available. There&#8217;s great open source stuff out there but until now, I haven&#8217;t gotten the full package with really easy customization.</p>
<p>The <a href="http://github.com/plataformatec/devise">Devise</a> and <a href="http://github.com/ryanb/cancan">CanCan</a> combo for user authentication and permissions in Rails is my combo of choice.<br />
<h2 style="margin-top: 0; padding-top: 0; font-weight: normal;">With Devise and CanCan, you can create a customized authentication and registration process in 15 minutes, and spend another 15 minutes implementing roles and permissions.</h2>
<p><img src="http://www.tonyamoyal.com/wp-content/uploads/2010/07/rails_beauty-300x225.jpg" alt="Rails Beauty" title="Rails Beauty" width="400" height="225" class="alignnone size-medium wp-image-558" /><br />
It&#8217;s pure beauty.</p>
<p><span style="font-size: 80%; line-height: 1.1em"><em>Note that the code here uses Rails 3.  The difference in Rails 3 and Rails 2 code for this purpose should be minimal, but please refer to the documentation for differences.</em></span></p>
<p>Let&#8217;s start with authentication using devise.</p>
<p><strong>Step 1 &#8211; Installation</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">gem install devise
rails generate devise:install
rails generate devise user</pre></div></div>

<p><strong>Step 2 &#8211; Configuration</strong><br />
Configuration is super easy with Devise.  Just choose which of the 11 available modules you would like to include in your authentic model (most up-to-date list <a href="http://github.com/plataformatec/devise">here</a>):</p>
<ol>
<li>Database Authenticatable: encrypts and stores a password in the database to validate the authenticity of an user while signing in. The authentication can be done both through POST requests or HTTP Basic Authentication.</li>
<li>Token Authenticatable: signs in an user based on an authentication token (also known as &#8220;single access token&#8221;). The token can be given both through query string or HTTP Basic Authentication.</li>
<li>Oauthable: adds OAuth2 support</li>
<li>Confirmable: sends emails with confirmation instructions and verifies whether an account is already confirmed during sign in.</li>
<li>Recoverable: resets the user password and sends reset instructions.</li>
<li>Registerable: handles signing up users through a registration process, also allowing them to edit and destroy their account.</li>
<li>Rememberable: manages generating and clearing a token for remembering the user from a saved cookie.</li>
<li>Trackable: tracks sign in count, timestamps and IP address.</li>
<li>Timeoutable: expires sessions that have no activity in a specified period of time.</li>
<li>Validatable: provides validations of email and password. It’s optional and can be customized, so you’re able to define your own validations.</li>
<li>Lockable: locks an account after a specified number of failed sign-in attempts. Can unlock via email or after a specified time period.</li>
</ol>
<p><img src="http://www.tonyamoyal.com/wp-content/uploads/2010/07/easy_button.jpg" alt="easy_button" title="easy_button" width="232" height="249" class="alignnone size-full wp-image-564" /><br />
I chose 5 of the 11 modules and configured with the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># In your model</span>
 <span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
    devise <span style="color:#ff3333; font-weight:bold;">:database_authenticatable</span>, <span style="color:#ff3333; font-weight:bold;">:confirmable</span>, <span style="color:#ff3333; font-weight:bold;">:recoverable</span>, <span style="color:#ff3333; font-weight:bold;">:rememberable</span>, <span style="color:#ff3333; font-weight:bold;">:trackable</span>, <span style="color:#ff3333; font-weight:bold;">:validatable</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># In your migration</span>
create_table <span style="color:#ff3333; font-weight:bold;">:users</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
    t.<span style="color:#9900CC;">database_authenticatable</span>
    t.<span style="color:#9900CC;">confirmable</span>
    t.<span style="color:#9900CC;">recoverable</span>
    t.<span style="color:#9900CC;">rememberable</span>
    t.<span style="color:#9900CC;">trackable</span>
    t.<span style="color:#9900CC;">timestamps</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># In your routes</span>
devise_for <span style="color:#ff3333; font-weight:bold;">:users</span></pre></div></div>

<p><strong>Step 3 &#8211; Use It!</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># In your controllers</span>
before_filter <span style="color:#ff3333; font-weight:bold;">:authenticate_user</span>!, <span style="color:#ff3333; font-weight:bold;">:except</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:some_action_without_auth</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#008000; font-style:italic;"># Access Current User</span>
<span style="color:#9966CC; font-weight:bold;">def</span> index
    <span style="color:#0066ff; font-weight:bold;">@things</span> = current_user.<span style="color:#9900CC;">things</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This simple modular approach to authentication is hot.  Devise also makes it really easy for you to customize views.  The out-of-the-box views are great for prototyping, but if you need more, just generate the views and edit them:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">rails generate devise:views</pre></div></div>

<p>Devise will generate all of the views it is using and place them in an app/views/devise directory.  Now you have complete control over your views.</p>
<p>The next thing you might want to do is customize your controllers.  This is a bit more tricky with devise and we&#8217;ll get to that in a minute.  Right now I want to touch on permissions and then I&#8217;ll tie it all together.</p>
<p>Let&#8217;s consider an example where your website is in Alpha/Beta or maybe an internal tool.  You want to restrict user registration to only an administrator.  Enter <a href="http://github.com/ryanb/cancan">CanCan</a> created by <a href="http://railscasts.com/">Ryan Bates</a>.</p>
<h1 style="padding-top: 0; margin-top: 0;">CanCan</h1>
<p>CanCan is a great gem for implementing model permissions.  The main reasons I chose CanCan are:</p>
<ul>
<li>The code written to check permissions is very readable</li>
<li>The code written to declare permissions is very concise and readable</li>
<li>It keeps permission logic in a single location so it is not duplicated across controllers, views, etc.</li>
<li>Aliasing actions (read = index and show) creates more concise and readable code</li>
</ul>
<h3 style="padding-top: 0; margin-top: 0">Ryan Bates has a great screen cast on using CanCan <a href="http://railscasts.com/episodes/192-authorization-with-cancan">here</a>, but I do not recommend using his roles mask method (mentioned in the screen cast).  It certainly works but it&#8217;s bad database design and you will feel the pain later.</h3>
<p>After you install CanCan (instructions <a href="http://github.com/ryanb/cancan">here</a>),  I recommend you set up a typical users HABTM roles relationship.  So you end up with migrations that look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> CreateRoles <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:roles</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
      t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:name</span>
      t.<span style="color:#9900CC;">timestamps</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    drop_table <span style="color:#ff3333; font-weight:bold;">:roles</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> UsersHaveAndBelongToManyRoles <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:roles_users</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
      t.<span style="color:#9900CC;">references</span> <span style="color:#ff3333; font-weight:bold;">:role</span>, <span style="color:#ff3333; font-weight:bold;">:user</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    drop_table <span style="color:#ff3333; font-weight:bold;">:roles_users</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And your models look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># User Model</span>
<span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_and_belongs_to_many <span style="color:#ff3333; font-weight:bold;">:roles</span>
....
<span style="color:#008000; font-style:italic;"># Role model</span>
<span style="color:#9966CC; font-weight:bold;">class</span> Role <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_and_belongs_to_many <span style="color:#ff3333; font-weight:bold;">:users</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The next step is to create your Ability class that will define permissions.  Mine looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Ability
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">CanCan::Ability</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>user<span style="color:#006600; font-weight:bold;">&#41;</span>
    user <span style="color:#006600; font-weight:bold;">||</span>= User.<span style="color:#9900CC;">new</span> <span style="color:#008000; font-style:italic;"># guest user</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> user.<span style="color:#9900CC;">role</span>? <span style="color:#ff3333; font-weight:bold;">:super_admin</span>
      can <span style="color:#ff3333; font-weight:bold;">:manage</span>, <span style="color:#ff3333; font-weight:bold;">:all</span>
    <span style="color:#9966CC; font-weight:bold;">elsif</span> user.<span style="color:#9900CC;">role</span>? <span style="color:#ff3333; font-weight:bold;">:product_admin</span>
      can <span style="color:#ff3333; font-weight:bold;">:manage</span>, <span style="color:#006600; font-weight:bold;">&#91;</span>Product, Asset, Issue<span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">elsif</span> user.<span style="color:#9900CC;">role</span>? <span style="color:#ff3333; font-weight:bold;">:product_team</span>
      can <span style="color:#ff3333; font-weight:bold;">:read</span>, <span style="color:#006600; font-weight:bold;">&#91;</span>Product, Asset<span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#008000; font-style:italic;"># manage products, assets he owns</span>
      can <span style="color:#ff3333; font-weight:bold;">:manage</span>, Product <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>product<span style="color:#006600; font-weight:bold;">|</span>
        product.<span style="color:#9900CC;">try</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:owner</span><span style="color:#006600; font-weight:bold;">&#41;</span> == user
      <span style="color:#9966CC; font-weight:bold;">end</span>
      can <span style="color:#ff3333; font-weight:bold;">:manage</span>, Asset <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>asset<span style="color:#006600; font-weight:bold;">|</span>
        asset.<span style="color:#9900CC;">assetable</span>.<span style="color:#9900CC;">try</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:owner</span><span style="color:#006600; font-weight:bold;">&#41;</span> == user
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Most of this is application specific but you can see some conveniences right away.  For example, the super admin role &#8220;can manage all&#8221;.  That line is saying &#8220;If the user has the super_admin role, he may perform any action on any model.&#8221;  Easy enough. Also notice that the product team can &#8220;read&#8221; products and assets.  This means that they can access the index or show action of either of those models.  You can pass a block to the can method for more complicated permission checks, but that is beyond the scope of this post and pretty easy to figure out.</p>
<p>Let&#8217;s take a look at the role method.  I store role names as CamelCase strings in the database but I access them with underscores which is more ruby like. The method looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> role?<span style="color:#006600; font-weight:bold;">&#40;</span>role<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> !!<span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">roles</span>.<span style="color:#9900CC;">find_by_name</span><span style="color:#006600; font-weight:bold;">&#40;</span>role.<span style="color:#9900CC;">to_s</span>.<span style="color:#9900CC;">camelize</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h1 style="padding-top: 0; margin-top: 0;">Tying it all together</h1>
<p>Now let&#8217;s go back to the situation I mentioned earlier &#8211; you want to protect user registrations.  This requires us to use CanCan to check for permissions but customize the Devise Registrations controller to restrict access.  </p>
<p>One way to do this is to copy the devise controllers into your controllers directory and start customizing.  That may be the best way to go and it&#8217;s certainly an obvious path, but all I want to do restrict registration.  Should I really have to re-implement the registrations controller to do that?  For now, I will not.  It might make sense when there are more customizations.  Instead I inherit from the Devise Registrations controller.  Here are the steps:</p>
<p><strong>Step 1 &#8211; Create the controller</strong><br />
$ mkdir app/controllers/users<br />
$ touch app/controllers/users/registrations_controller.rb</p>
<p><strong>Step 2 &#8211; Add the custom functionality</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">Users::RegistrationsController</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Devise::RegistrationsController</span>
  before_filter <span style="color:#ff3333; font-weight:bold;">:check_permissions</span>, <span style="color:#ff3333; font-weight:bold;">:only</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:new</span>, <span style="color:#ff3333; font-weight:bold;">:create</span>, <span style="color:#ff3333; font-weight:bold;">:cancel</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  skip_before_filter <span style="color:#ff3333; font-weight:bold;">:require_no_authentication</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> check_permissions
    authorize! <span style="color:#ff3333; font-weight:bold;">:create</span>, resource
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The check permissions method is really simple.  It calls the CanCan method, authorize!, and checks if the current user can create users.  We use resource here because devise uses resource to refer to the model that can be authenticated.  Also notice how I removed the require_no_authentication filter, a Devise filter which allows access to actions without authentication.  </p>
<p><strong>Step 3 &#8211; Tell your routes to go to the new controller</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># replace devise_for :users with:</span>
devise_for <span style="color:#ff3333; font-weight:bold;">:users</span>,  <span style="color:#ff3333; font-weight:bold;">:controllers</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:registrations</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;users/registrations&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p><strong>Step 4 &#8211; Handle the CanCan::AccessDenied exception</strong><br />
At this point if you hit the users/sign_up page when not logged in, you will notice that a CanCan::AccessDenied is thrown.  This exception is thrown anytime permission is denied so you should customize it to your liking.  I put the handler in my ApplicationController:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ApplicationController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>
  ...
  <span style="color:#9900CC;">rescue_from</span> <span style="color:#6666ff; font-weight:bold;">CanCan::AccessDenied</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>exception<span style="color:#006600; font-weight:bold;">|</span>
    flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:error</span><span style="color:#006600; font-weight:bold;">&#93;</span> = exception.<span style="color:#9900CC;">message</span>
    redirect_to root_url
  <span style="color:#9966CC; font-weight:bold;">end</span>
  ...
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I realize I skipped some steps in here but this post + Devise documentation + CanCan documentation should help you set up authentication with roles and permissions very quickly.  Let me know if you have any questions.  Enjoy!</p>
<h3 style="padding-top: 0;">UPDATE</h3>
<p><a href="http://www.tonyamoyal.com/2010/09/29/rails-authentication-with-devise-and-cancan-part-2-restful-resources-for-administrators/">A part 2 of this post is now available</a></p>
<div style='clear:both'></div><img src="http://feeds.feedburner.com/~r/TonyAmoyal/~4/4q-hKsN-XjY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/feed/</wfw:commentRss>
		<slash:comments>73</slash:comments>
		<feedburner:origLink>http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/</feedburner:origLink></item>
		<item>
		<title>Safely turn off Spotlight and remove from menu bar in OS X Snow Leopard</title>
		<link>http://feedproxy.google.com/~r/TonyAmoyal/~3/cBg0RYoZxOQ/</link>
		<comments>http://www.tonyamoyal.com/2010/06/17/safely-turn-off-spotlight-and-remove-from-menu-bar-in-os-x-snow-leopard/#comments</comments>
		<pubDate>Thu, 17 Jun 2010 17:55:13 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[customize snow leopard]]></category>
		<category><![CDATA[disable-spotlight]]></category>
		<category><![CDATA[os x]]></category>
		<category><![CDATA[snow leopard]]></category>
		<category><![CDATA[spotlight]]></category>

		<guid isPermaLink="false">http://www.tonyamoyal.com/?p=532</guid>
		<description><![CDATA[I don&#8217;t like the Spotlight indexing and search that is included in Mac OS X.  I use Quicksilver instead but have been procrastinating turning off Spotlight.
Well I finally got around to disabling Spotlight and wanted to go about it in a safe way.  I came across too many forum posts doing dangerous things [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.tonyamoyal.com%2F2010%2F06%2F17%2Fsafely-turn-off-spotlight-and-remove-from-menu-bar-in-os-x-snow-leopard%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.tonyamoyal.com%2F2010%2F06%2F17%2Fsafely-turn-off-spotlight-and-remove-from-menu-bar-in-os-x-snow-leopard%2F" height="61" width="51" /></a></div><p>I don&#8217;t like the Spotlight indexing and search that is included in Mac OS X.  I use <a href="http://www.versiontracker.com/dyn/moreinfo/macosx/22549" target="_blank">Quicksilver</a> instead but have been procrastinating turning off Spotlight.</p>
<p>Well I finally got around to disabling Spotlight and wanted to go about it in a safe way.  I came across too many forum posts doing dangerous things to disable Spotlight.  I also wanted and easy way to remove it from the menu bar.  Below are aliases I added to my bash profile to conveniently enable and disable spotlight.  I am not a bash scripting master, so feedback is welcome and appreciated.</p>
<p><strong>Step 1 &#8211; Add aliases</strong><br />
Open your bash profile (location: ~/.bash_profile ) and add these lines:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">alias</span> spotlight-off=<span style="color: #ff0000;">'sudo mdutil -a -i off &amp;&amp; sudo mv /System/Library/CoreServices/Search.bundle/ /System/Library/CoreServices/SearchOff.bundle/'</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> spotlight-on=<span style="color: #ff0000;">'sudo mdutil -a -i on &amp;&amp; sudo mv /System/Library/CoreServices/SearchOff.bundle/ /System/Library/CoreServices/Search.bundle/'</span></pre></div></div>

<p><strong>Step 2 &#8211; Source your bash profile</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">source</span> ~<span style="color: #000000; font-weight: bold;">/</span>.bash_profile</pre></div></div>

<p><strong>Step 3 &#8211; Run command</strong><br />
In your shell, run</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ spotlight-off</pre></div></div>

<p><strong>Step 4 &#8211; Restart SystemUIServer Process</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ps</span> aux <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> SystemUIServer.app
<span style="color: #666666; font-style: italic;"># take note of the PID which is the first number you will see</span>
$ <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-HUP</span> <span style="color: #000000; font-weight: bold;">&lt;</span>insert PID here<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>You could potentially restart your computer instead of performing Step 4, but that is inconvenient.  Let me know if you have any comments or a nice way to include everything in one script.  I didn&#8217;t have time to condense this process further.</p>
<div style='clear:both'></div><img src="http://feeds.feedburner.com/~r/TonyAmoyal/~4/cBg0RYoZxOQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tonyamoyal.com/2010/06/17/safely-turn-off-spotlight-and-remove-from-menu-bar-in-os-x-snow-leopard/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.tonyamoyal.com/2010/06/17/safely-turn-off-spotlight-and-remove-from-menu-bar-in-os-x-snow-leopard/</feedburner:origLink></item>
	</channel>
</rss>
