<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description></description><title>Amiel Martin</title><generator>Tumblr (3.0; @amielmartin)</generator><link>http://blog.amielmartin.com/</link><item><title>Using I18n and Draper to Render Database Attributes</title><description>&lt;p&gt;&lt;strong&gt;TL;DR;&lt;/strong&gt;: Check out my additions to &lt;code&gt;ApplicationDecorator&lt;/code&gt; in &lt;a href="https://gist.github.com/1338134"&gt;this gist&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When my models have an attribute that matters to the code (like &lt;code&gt;Admin#role&lt;/code&gt; or &lt;code&gt;User#status&lt;/code&gt;),
I like to store the value as a string that makes sense as an identifier.
For example, &lt;code&gt;User#status&lt;/code&gt; might be ‘active’ or ‘awaiting_approval’.
However, when it comes time to render the admin’s role or the users status in the view,
we want to show ‘Awaiting approval’ instead of ‘awaiting_approval’.
Another example of this sort of thing is the &lt;code&gt;#type&lt;/code&gt; attribute for STI.&lt;/p&gt;

&lt;p&gt;Ok, this isn’t too hard, we can just use &lt;code&gt;#humanize&lt;/code&gt;. But, here’s what happens:&lt;/p&gt;

&lt;!-- more --&gt;

&lt;script src="https://gist.github.com/1340319.js?file=1.html.erb"&gt;&lt;/script&gt;&lt;script src="https://gist.github.com/1340319.js?file=2.rb"&gt;&lt;/script&gt;&lt;p&gt;Ok, let’s be fair. All of these solutions are actually quite fine.
In most cases Ya Ain’t Gonna Need anything more complicated.
The helper version handles most situations just fine.&lt;/p&gt;

&lt;p&gt;However, after a bunch of this I tend to end up with a bunch of methods in my model that seem to be somewhat presentation related,
and/or methods in my helper that seem like they belong to an object and not in the “global” view namespace.&lt;/p&gt;

&lt;h3&gt;Enter decorators&lt;/h3&gt;

&lt;p&gt;A decorator (or presenter) is an object that holds the presentation logic for a model,
so that the model can stick to the business logic.
I’ve been using a great gem called &lt;a href="https://github.com/jcasimir/draper"&gt;Draper&lt;/a&gt;.
I won’t go into too much detail about how to use Draper
(check out the &lt;a href="https://github.com/jcasimir/draper"&gt;Github readme&lt;/a&gt; or &lt;a href="http://railscasts.com/episodes/286-draper"&gt;Railscast&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Here’s how you would implement the above pattern with Draper:&lt;/p&gt;

&lt;script src="https://gist.github.com/1340319.js?file=3.rb"&gt;&lt;/script&gt;&lt;p&gt;Then this is our view:&lt;/p&gt;

&lt;script src="https://gist.github.com/1340319.js?file=4.html.erb"&gt;&lt;/script&gt;&lt;h3&gt;Bonus&lt;/h3&gt;

&lt;script src="https://gist.github.com/1337448.js?file=application_helper.rb"&gt;&lt;/script&gt;&lt;p&gt;Now:&lt;/p&gt;

&lt;script src="https://gist.github.com/1337448.js?file=examples.rb"&gt;&lt;/script&gt;&lt;h3&gt;My Abstractions&lt;/h3&gt;

&lt;p&gt;And now the reason for this post. I find that I use this pattern frequently, so I generalized it to ApplicationDecorator.
It adds a class method &lt;code&gt;ApplicationDecorator.humanizes&lt;/code&gt; that can be used in each decorator to define attributes that need automatic humanization.&lt;/p&gt;

&lt;p&gt;The full source can be found here: &lt;a href="https://gist.github.com/1338134"&gt;&lt;a href="https://gist.github.com/1338134"&gt;https://gist.github.com/1338134&lt;/a&gt;&lt;/a&gt; (&lt;a href="#show_gist_1338134"&gt;Show inline&lt;/a&gt;)&lt;/p&gt;

&lt;div id="hide_gist_1338134" style="display: none;"&gt;
&lt;script src="https://gist.github.com/1338134.js"&gt; &lt;/script&gt;&lt;script type="text/javascript"&gt;
setTimeout(function() {
  $('a[href="#show_gist_1338134"]').click(function() {
    $('#hide_gist_1338134').toggle();
    return false;
  });
}, 2000); // Wait for jquery to load (hopefully)
&lt;/script&gt;&lt;/div&gt;

&lt;p&gt;Here’s how you would use it:&lt;/p&gt;

&lt;script src="https://gist.github.com/1340319.js?file=5.rb"&gt;&lt;/script&gt;&lt;p&gt;And in the view:&lt;/p&gt;

&lt;script src="https://gist.github.com/1340319.js?file=6.html.erb"&gt;&lt;/script&gt;&lt;h3&gt;To Conclude&lt;/h3&gt;

&lt;p&gt;I like this because each layer is really simple and really focuses on only what it needs to.&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;The view doesn’t have to know that that data is not user-friendly.&lt;/li&gt;
&lt;li&gt;The model isn’t polluted with methods designed for the view.&lt;/li&gt;
&lt;li&gt;There isn’t much complexity or black-magic to make this abstraction simple.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;If this pattern works out in my current project I will probably pull this out into a gem.
Would anyone else find this useful? If I do I’ll be looking for name suggestions…&lt;/p&gt;</description><link>http://blog.amielmartin.com/post/12341219947</link><guid>http://blog.amielmartin.com/post/12341219947</guid><pubDate>Fri, 04 Nov 2011 15:15:00 -0700</pubDate><category>draper</category><category>I18n</category><category>decorators</category><category>ruby-on-rails</category></item><item><title>I Can Haz Disqus</title><description>&lt;p&gt;I’ve added comments to this blog. So, if you wanted to comment on my posts, now you can!&lt;/p&gt;</description><link>http://blog.amielmartin.com/post/12312419573</link><guid>http://blog.amielmartin.com/post/12312419573</guid><pubDate>Thu, 03 Nov 2011 19:52:13 -0700</pubDate></item><item><title>How to get GetBundles working with RVM</title><description>&lt;p&gt;Ever since I set rvm to use ruby 1.9.2 as my default, GetBundles hasn’t been working.&lt;/p&gt;

&lt;p&gt;A quick peek at TextMateGetBundles.log revealed that there were syntax errors in Support/getBundles.rb. Here’s what I did to make GetBundles use ruby 1.8.7&lt;/p&gt;

&lt;p&gt;This is how I got GetBundles to work with rvm.
&lt;!-- more --&gt;&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;Open the Bundle Editor (Bundles -&gt; Bundle Editor -&gt; Show Bundle Editor).&lt;/li&gt;
&lt;li&gt;Find GetBundles and select the Get Bundles command.&lt;/li&gt;
&lt;li&gt;Paste the following to the text area.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;&lt;img src="https://gist.github.com/raw/920507/228d4745f86d093c705fb0db1d8c58f5c6b059c9/finding_getbundles.png" alt="Finding the Get Bundles command in the Bundle Editor" title="Finding the Get Bundles command in the Bundle Editor"/&gt;&lt;/p&gt;

&lt;script src="https://gist.github.com/920507.js?file=GetBundles.sh"&gt;&lt;/script&gt;</description><link>http://blog.amielmartin.com/post/4614430278</link><guid>http://blog.amielmartin.com/post/4614430278</guid><pubDate>Thu, 14 Apr 2011 13:59:00 -0700</pubDate></item><item><title>"Do you practice specific skills with repetition and intent?  Athletes do drills.  Musicians hone..."</title><description>“Do you practice specific skills with repetition and intent?  Athletes do drills.  Musicians hone difficult passages.  What do you do?”&lt;br/&gt;&lt;br/&gt; - &lt;em&gt;&lt;a href="http://worldofsu.com/philipsu/?page_id=193"&gt;http://worldofsu.com/philipsu/?page_id=193&lt;/a&gt;&lt;/em&gt;</description><link>http://blog.amielmartin.com/post/1401251448</link><guid>http://blog.amielmartin.com/post/1401251448</guid><pubDate>Mon, 25 Oct 2010 15:34:40 -0700</pubDate></item><item><title>Delete action in Rails 3</title><description>&lt;p&gt;At Carnes Media, we’ve been really happy with using a delete action as a confirmation step before calling destroy.&lt;/p&gt;

&lt;p&gt;This solves a bunch of problems:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;No more hacky inline javascripts that create a DELETE form.&lt;/li&gt;
&lt;li&gt;We render the delete form in a modal instead of a confirm dialog.&lt;/li&gt;
&lt;li&gt;It’s a completely unobtrusive solution and easy to test with cucumber.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Here are a couple of bits and pieces that help make this pattern easier:&lt;/p&gt;

&lt;!-- more --&gt;

&lt;h3&gt;app/views/shared/_delete.html.erb&lt;/h3&gt;

&lt;p&gt;This partial can be stashed in a shared view folder and rendered as the delete form for most views. It uses &lt;a href="http://github.com/justinfrench/formtastic"&gt;formtastic&lt;/a&gt;, but could easily be rewritten without it.&lt;/p&gt;

&lt;script src="http://gist.github.com/605064.js?file=_delete.html.erb"&gt;&lt;/script&gt;&lt;p&gt;NOTE: For the rendering of actions in modals without the layout getting in the way, you might want to check out &lt;a href="http://github.com/amiel/can_haz_modal" title="Can Haz Modal"&gt;another poorly named plugin&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;lib/add_delete_to_resource_routes.rb&lt;/h3&gt;

&lt;p&gt;I know, I’m terrible at naming things.&lt;/p&gt;

&lt;p&gt;This little file adds a delete action to the default set of actions for “resources” routes. So when you put &lt;code&gt;resources :widgets&lt;/code&gt; in your routes.rb file, a delete action (GET) will be added to your routes along with index, new, create, destroy, etc…&lt;/p&gt;

&lt;p&gt;Just put this in lib and require it in your Application class in config/application.rb.&lt;/p&gt;

&lt;script src="http://gist.github.com/605064.js?file=add_delete_to_resource_routes.rb"&gt;&lt;/script&gt;&lt;p&gt;If there is demand, I might work this into an actual plugin…&lt;/p&gt;</description><link>http://blog.amielmartin.com/post/1216720481</link><guid>http://blog.amielmartin.com/post/1216720481</guid><pubDate>Thu, 30 Sep 2010 11:54:00 -0700</pubDate></item><item><title>Konami jQuery Plugin</title><description>&lt;p&gt;Back when I worked at Tatango, Adrian wrote a very concise implementation for the konami code.&lt;/p&gt;

&lt;p&gt;So, I figured I would make a nice little jQuery plugin. This is how you would use it:&lt;/p&gt;

&lt;script src="http://gist.github.com/483453.js"&gt; &lt;/script&gt;&lt;!-- more --&gt;&lt;p&gt;The end result is nice, small, and efficient. Internally it triggers the “konami” event, so it’s easy to trigger multiple functions after the konami code has been fired. Here is the entire plugin, uncompressed. You can also find the whole project, including a Readme and compressed version here: &lt;a href="http://github.com/amiel/konami"&gt;http://github.com/amiel/konami&lt;/a&gt;&lt;/p&gt;

&lt;script src="http://gist.github.com/483472.js"&gt; &lt;/script&gt;&lt;p&gt;This wasn’t good enough for Adrian though. No, he wanted to be able to register multiple functions and that one of them would get triggered at random on the “konami” event. So, for extra credit, here is the random konami addition:&lt;/p&gt;

&lt;script src="http://gist.github.com/483480.js"&gt; &lt;/script&gt;</description><link>http://blog.amielmartin.com/post/837563917</link><guid>http://blog.amielmartin.com/post/837563917</guid><pubDate>Tue, 20 Jul 2010 13:06:00 -0700</pubDate></item><item><title>A Couple of Handy Cucumber Transforms</title><description>&lt;p&gt;I’m writing some cucumber tests for a billing system, and I figured I’d try cucumber transforms for parsing CreditCard and Money object out of steps. Here’s what I came up with:&lt;/p&gt;

&lt;script src="http://gist.github.com/371747.js"&gt;&lt;/script&gt;</description><link>http://blog.amielmartin.com/post/534233659</link><guid>http://blog.amielmartin.com/post/534233659</guid><pubDate>Mon, 19 Apr 2010 15:41:32 -0700</pubDate></item><item><title>Bellingham Atlas for Google Fiber</title><description>&lt;a href="http://bellinghamatlas.com"&gt;Bellingham Atlas for Google Fiber&lt;/a&gt;: &lt;p&gt;We’re putting together an interactive atlas of Bellingham, and we need your help. We want to show Google that Bellingham is the perfect mix of big city and small town, urban and rural — it’s just right. (Yes, we are going for a Goldilocks thing here.)&lt;/p&gt;</description><link>http://blog.amielmartin.com/post/473442941</link><guid>http://blog.amielmartin.com/post/473442941</guid><pubDate>Thu, 25 Mar 2010 16:26:54 -0700</pubDate></item><item><title>"If we accept that code is design — a creative process rather than a mechanical one — the software..."</title><description>“If we accept that code is design — a creative process rather than a mechanical one — the software crisis is explained. We now have a design crisis: The demand for quality, validated designs exceeds our capacity to create them. The pressure to use incomplete design is strong.”&lt;br/&gt;&lt;br/&gt; - &lt;em&gt;&lt;a href="http://programmer.97things.oreilly.com/wiki/index.php/Code_Is_Design"&gt;Code Is Design - Programmer 97-things&lt;/a&gt; (via &lt;a href="http://blog.duwanis.com/" class="tumblr_blog"&gt;duwanis&lt;/a&gt;)&lt;/em&gt;</description><link>http://blog.amielmartin.com/post/416700637</link><guid>http://blog.amielmartin.com/post/416700637</guid><pubDate>Sat, 27 Feb 2010 19:30:54 -0800</pubDate></item><item><title>Another jQuery plugin</title><description>&lt;p&gt;A friend recently was asking about scoping a bunch of jquery calls to a particular container. So… I wrote this plugin: &lt;a href="http://github.com/amiel/jquery_scope"&gt;&lt;a href="http://github.com/amiel/jquery_scope"&gt;http://github.com/amiel/jquery_scope&lt;/a&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are some contrived usage examples:&lt;/p&gt;

&lt;script src="http://gist.github.com/311672.js"&gt;&lt;/script&gt;</description><link>http://blog.amielmartin.com/post/405755967</link><guid>http://blog.amielmartin.com/post/405755967</guid><pubDate>Mon, 22 Feb 2010 15:47:29 -0800</pubDate><category>jquery</category></item><item><title>less/more and Heroku</title><description>&lt;p&gt;I’ve been using the less gem and the more plugin together on heroku. Recently, the more maintainers changed the plugins concept and stylesheets are not generated on the request anymore. I haven’t found any explanations as to why this is and I still like the old version, so I forked the project and reset —hard back to what they had before.&lt;/p&gt;

&lt;p&gt;If you want the old way (with the latest heroku fix):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;./script/plugin install git://github.com/amiel/more.git --force
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;EDIT: I don’t use the more plugin anymore. I use rails 3.1 with the new less gem. This plugin has since been deleted.&lt;/p&gt;</description><link>http://blog.amielmartin.com/post/398066564</link><guid>http://blog.amielmartin.com/post/398066564</guid><pubDate>Thu, 18 Feb 2010 21:59:00 -0800</pubDate><category>heroku</category></item><item><title>"Rails 3 is DRY like a well-aged scotch :-)"</title><description>“Rails 3 is DRY like a well-aged scotch :-)”&lt;br/&gt;&lt;br/&gt; - &lt;em&gt;Overheard in the Rails Online Conference Chat&lt;/em&gt;</description><link>http://blog.amielmartin.com/post/396883678</link><guid>http://blog.amielmartin.com/post/396883678</guid><pubDate>Thu, 18 Feb 2010 09:49:39 -0800</pubDate></item><item><title>"If you are not embarrassed by the first version of your product, you’ve launched too late"</title><description>“If you are not embarrassed by the first version of your product, you’ve launched too late”&lt;br/&gt;&lt;br/&gt; - &lt;em&gt;Reid Hoffman, founder of LinkedIn&lt;/em&gt;</description><link>http://blog.amielmartin.com/post/371951522</link><guid>http://blog.amielmartin.com/post/371951522</guid><pubDate>Thu, 04 Feb 2010 22:49:44 -0800</pubDate></item><item><title>"I mean, really, if Java had proper garbage collection, most programs would self-delete on execution."</title><description>“I mean, really, if Java had proper garbage collection, most programs would self-delete on execution.”&lt;br/&gt;&lt;br/&gt; - &lt;em&gt;&lt;ul&gt;&lt;li&gt;slashdot post &lt;a href="http://slashdot.org/comments.pl?sid=40887&amp;cid=4347983"&gt;http://slashdot.org/comments.pl?sid=40887&amp;cid=4347983&lt;/a&gt; by Jucius Maximus&lt;/li&gt;
&lt;/ul&gt;&lt;/em&gt;</description><link>http://blog.amielmartin.com/post/348687520</link><guid>http://blog.amielmartin.com/post/348687520</guid><pubDate>Fri, 22 Jan 2010 23:39:36 -0800</pubDate></item><item><title>heroku with lesscss more plugin</title><description>&lt;p&gt;Something just happened at heroku. They used to provide an environment variable HEROKU_ENV, but for some reason its not there anymore.&lt;/p&gt;

&lt;p&gt;This is a problem only because the more plugin for lesscss used the presence of that environment variable to test for heroku. Now, all my apps on Heroku using more are failing.&lt;/p&gt;

&lt;p&gt;so, for those of you with the same problem, here is a quick fix:&lt;/p&gt;

&lt;p&gt;heroku config:add HEROKU_ENV=production&lt;/p&gt;</description><link>http://blog.amielmartin.com/post/347068263</link><guid>http://blog.amielmartin.com/post/347068263</guid><pubDate>Thu, 21 Jan 2010 22:40:53 -0800</pubDate></item><item><title>Great write-up of the future of persistant relational data</title><description>&lt;a href="http://www.igvita.com/2009/12/07/future-of-rdbms-is-ram-clouds-ssd/"&gt;Great write-up of the future of persistant relational data&lt;/a&gt;</description><link>http://blog.amielmartin.com/post/273641633</link><guid>http://blog.amielmartin.com/post/273641633</guid><pubDate>Mon, 07 Dec 2009 13:53:50 -0800</pubDate></item><item><title>"Programs that write programs are the happiest programs in the world."</title><description>“Programs that write programs are the happiest programs in the world.”&lt;br/&gt;&lt;br/&gt; - &lt;em&gt;&lt;a href="http://www.xaprb.com/blog/2005/12/12/javascript-closures-for-runtime-efficiency"&gt;http://www.xaprb.com/blog/2005/12/12/javascript-closures-for-runtime-efficiency&lt;/a&gt;&lt;/em&gt;</description><link>http://blog.amielmartin.com/post/245739991</link><guid>http://blog.amielmartin.com/post/245739991</guid><pubDate>Sun, 15 Nov 2009 22:43:00 -0800</pubDate></item><item><title>Using I18n in cucumber</title><description>&lt;p&gt;I know this defeats part of the concept of cucumber, but for my purposes, it means less work down the road when our copywriter comes in to edit my en.yml file.&lt;/p&gt;

&lt;p&gt;&#13;
&lt;script src="http://gist.github.com/219920.js"&gt;&lt;/script&gt;&lt;/p&gt;</description><link>http://blog.amielmartin.com/post/235855133</link><guid>http://blog.amielmartin.com/post/235855133</guid><pubDate>Thu, 29 Oct 2009 15:05:00 -0700</pubDate><category>cucumber</category></item><item><title>javascript flash of unstyled content</title><description>&lt;p&gt;I’ve been trying to put &lt;script&gt; tags near &lt;/body&gt; for performance. However, this causes a “flash of unstyled content” while content that is hidden by javascript is loaded but hasn’t been taken over by javascript. One option is use display:none to hide the content, but that is not very unobtrusive.&lt;/p&gt;

&lt;p&gt;Put this snippet in &lt;head&gt;, and the rest of your javascripts just before &lt;/body&gt;.&lt;/p&gt;

&lt;script src="http://gist.github.com/199161.js"&gt;&lt;/script&gt;</description><link>http://blog.amielmartin.com/post/235854581</link><guid>http://blog.amielmartin.com/post/235854581</guid><pubDate>Wed, 30 Sep 2009 12:51:00 -0700</pubDate><category>javascript</category></item></channel></rss>

