<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Nicolás Hock Isaza]]></title>
  <link href="http://blog.nhocki.com/atom.xml" rel="self"/>
  <link href="http://blog.nhocki.com/"/>
  <updated>2013-03-07T21:33:07-05:00</updated>
  <id>http://blog.nhocki.com/</id>
  <author>
    <name><![CDATA[Nicolás Hock Isaza]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Tower of Hanoi with a stack]]></title>
    <link href="http://blog.nhocki.com/2013/02/05/tower-of-hanoi-with-a-stack/"/>
    <updated>2013-02-05T14:37:00-05:00</updated>
    <id>http://blog.nhocki.com/2013/02/05/tower-of-hanoi-with-a-stack</id>
    <content type="html"><![CDATA[<p>I&#39;ve recently subscribed to <a href="http://codingforinterviews.com/">Coding for Interviews</a>
and received my first email today. The content is great and you should join. 
Even if you&#39;re not looking for a job, it&#39;s a short read with nice topics to 
refresh your memory.</p>

<p><a href="http://us2.campaign-archive2.com/?u=cadc6c448cd083a0aeed7f864&amp;id=c550bc59d5&amp;e=5694567579">Today&#39;s assignment</a> was with stacks. I&#39;ve used stacks to
<a href="http://blog.nhocki.com/2012/12/27/reverse-a-list-in-o-n/">reverse a list in O(n)</a>
but today&#39;s challenge was to solve the <a href="http://en.wikipedia.org/wiki/Tower_of_Hanoi">Tower of Hanoi</a> problem without
recursion.</p>

<p>Here&#39;s my solution:</p>

<div><script src='https://gist.github.com/4716874.js?file='></script>
<noscript><pre><code>Hanoi = Struct.new(:disk, :source, :dest, :spare)

def move(hanoi)
  &quot;Move #{hanoi.disk.abs} from #{hanoi.source} to #{hanoi.dest}&quot;
end

class Stack
  def initialize
    @data = []
  end

  def push(hanoi)
    @data.unshift(hanoi)
  end

  def pop
    @data.shift
  end

  def peek
    @data.first
  end

  def empty?
    @data.empty?
  end
end


# Resolve the Tower of Hanoi problem with a Stack. You can't use recursion
# in it's traditional way, but adding everything to a stack works the same.
#
# Notice you have to invert the order of the calls to the recursive function
# since the stack is **Last In - First Out**.
def stacker hanoi
  stack = Stack.new
  stack.push(hanoi)

  until stack.empty? do
    hanoi = stack.pop

    if hanoi.disk &lt;= 1
      puts move(hanoi)
    else
      left  = Hanoi.new(hanoi.disk - 1, hanoi.source, hanoi.spare, hanoi.dest)
      right = Hanoi.new(hanoi.disk - 1, hanoi.spare, hanoi.dest, hanoi.source)
      hanoi.disk = -hanoi.disk # force printing it next time it appears
      stack.push right
      stack.push hanoi
      stack.push left
    end
  end
end


# Resolve the Tower of Hanoi problem with recursion.
def recursive hanoi
  if hanoi.disk == 1
    puts move(hanoi)
  else
    left  = Hanoi.new(hanoi.disk - 1, hanoi.source, hanoi.spare, hanoi.dest)
    right = Hanoi.new(hanoi.disk - 1, hanoi.spare, hanoi.dest, hanoi.source)
    recursive(left)
    puts move(hanoi)
    recursive(right)
  end
end

h = Hanoi.new(3, 'A', 'B', 'C')

recursive(h)
puts &quot;\n\n&quot;
stacker(h)
</code></pre></noscript></div>

<p>There&#39;s another iterative solution but I haven&#39;t implemented it. So, if you did
or have some other way to solve this, I&#39;d love to know on
<a href="https://twitter.com/nhocki">Twitter @nhocki</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Stop using config files for secret data]]></title>
    <link href="http://blog.nhocki.com/2013/01/25/stop-using-config-files-for-secret-data/"/>
    <updated>2013-01-25T11:02:00-05:00</updated>
    <id>http://blog.nhocki.com/2013/01/25/stop-using-config-files-for-secret-data</id>
    <content type="html"><![CDATA[<p>Seriously, it&#39;s <strong>your</strong> fault that Github exposed your keys. If you want to
add everything to your repo (not that this is a good idea though), at least
make it private.</p>

<p>If you want to have <strong>per-environment variables, use the environment variables</strong>.
<em>But my development env would be full of crap!</em> some of you may cry&#8230; well,
there&#39;s a simple solution for that. Use <a href="https://github.com/bkeepers/dotenv">Dotenv</a>.</p>

<p>With <em>Dotenv</em> you only need to add a <code>.env</code> file to your application root directory
and you&#39;re done. Here&#39;s a quick example:</p>
<div class="highlight"><pre><code class="sh"><span class="c"># .env</span>
<span class="nv">S3_BUCKET</span><span class="o">=</span>some_development_bucket
<span class="nv">S3_SECRET</span><span class="o">=</span>a_secret_key
</code></pre>
</div>

<p>Then in your code, simply use <code>ENV[]</code> and be happy (and safe, but mostly happy)</p>
<div class="highlight"><pre><code class="ruby"><span class="c1"># some config file</span>
<span class="n">config</span><span class="o">.</span><span class="n">fog_directory</span>  <span class="o">=</span> <span class="no">ENV</span><span class="o">[</span><span class="s1">&#39;S3_BUCKET&#39;</span><span class="o">]</span>
</code></pre>
</div>

<p>And just like that, you can setup your server&#39;s config environments and not
worry about <em>supposed-to-be-private</em> public information.</p>

<p>One really cool thing about this is that this variables will only be loaded
in that specific application. It won&#39;t change <strong>your</strong> environment. So when
you stop the servers, your environment won&#39;t have the <code>.env</code> variables.</p>

<p>You can read more about <a href="http://opensoul.org/blog/archives/2012/07/24/dotenv/">Dotenv here</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Easily add versions to your Gemfile]]></title>
    <link href="http://blog.nhocki.com/2013/01/15/easily-add-versions-to-your-gemfile/"/>
    <updated>2013-01-15T22:31:00-05:00</updated>
    <id>http://blog.nhocki.com/2013/01/15/easily-add-versions-to-your-gemfile</id>
    <content type="html"><![CDATA[<p>Since <a href="http://gembundler.com/" title="Bundler">bundler</a> came out, managing dependencies
on ruby applications has been amazing. Combined with <a href="http://37signals.com/svn/posts/2998-setting-up-a-new-machine-for-ruby-development" title="37 Signals bootstrap script">some</a> <a href="http://zachholman.com/talk/unsucking-your-teams-development-environment/" title="Zach Holman&#39;s talk on development environment">bootstrap</a> <a href="http://ngauthier.com/2012/08/one-click-development.html" title="One click development">script</a>,
it&#39;s really easy to download some source code, install, and run an application.</p>

<p>There is still some room for improvement though. I&#39;ve always supported the use
of <a href="http://semver.org/" title="SemVer: Semantic Versioning">SemVer</a> and pessimistic locking (the <code>~&gt;</code>). This will allow you to
update applications without pain. And, with all this rails vulnerabilities,
you need just that.</p>

<p>But, since people don&#39;t always have the versions on their <code>Gemfile</code>, <a href="http://tenderlovemaking.com/2012/12/18/rails-4-and-your-gemfile.html" title="Rails 4 and your Gemfile">this can get
a little messy</a>. So, I&#39;ve created a simple gem to help you with that.</p>

<h3 id="toc_73">Welcome version_gemfile</h3>

<ul>
<li>1. Simply install it: <code>$ gem install version_gemfile</code></li>
<li>2. Go to your rails application: <code>$ cd ~/my/ruby/application</code></li>
<li>3. And add the versions to your <em>Gemfile</em>: <code>$ version_gemfile</code></li>
</ul>

<h2 id="toc_74">How does it work?</h2>

<p>This gem will go through each line in your <code>Gemfile</code> looking for dependencies
that have no versions. Then, will look into your <code>Gemfile.lock</code> for the version
you are currently using and add a pessimistic lock on that version.</p>

<p>The code can be found on <a href="https://github.com/nhocki/version_gemfile" title="Version Gemfile source code">Github</a>. If you have any problems, open an issue there
or ping me on <a href="https://twitter.com/nhocki" title="My twitter profile">Twitter</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Reverse a list in O(n)]]></title>
    <link href="http://blog.nhocki.com/2012/12/27/reverse-a-list-in-o-n/"/>
    <updated>2012-12-27T16:53:00-05:00</updated>
    <id>http://blog.nhocki.com/2012/12/27/reverse-a-list-in-o-n</id>
    <content type="html"><![CDATA[<p>For about a year, I&#39;ve always had a question I ask someone I&#39;m interviewing for
a programming position. I&#39;ve asked this around 3 times and got some nice
answers, but I had never implemented it. The question is really simple:</p>

<p><strong>Given a linked list, how would you reverse it in <code>O(n)</code>?</strong></p>

<p>Here&#39;s my solution:</p>

<div><script src='https://gist.github.com/4392454.js?file='></script>
<noscript><pre><code>Node = Struct.new(:id, :next)

# Build the list
first = Node.new(1, nil)
nodes = 10.times.map{|x| Node.new(x + 2, nil)}
nodes.each_with_index{|node, index| node.next = nodes[index + 1] }
first.next = nodes.first

def reverse_list(node)
  prox = node.next  # Get the next node on the list.
  node.next = nil   # Make the new tail point to nil (end of the new list).

  last = node       # Save that new tail on a tmp variable (to point to it later)
  node = prox       # Change the `current` node to the `next` one. Move ahead.

  while node          # If I'm not at the end of the list.
    prox = node.next  # Save the next node on the original list.
    node.next = last  # Reverse the list (point back from `current`)
    last = node       # Save the current node to point later.
    node = prox       # Move ahead once.
  end
  last
end

def print_list(node)
  while node
    puts node.id
    node = node.next
  end
end

print_list(first)
puts &quot;\n&quot;
print_list(reverse_list(first))
</code></pre></noscript></div>

<p>How would you do it? There&#39;s a really simple solution using a
<a href="https://gist.github.com/nhocki/4392454#comment-676651">stack</a>, but if you
find another one, I&#39;d love to hear about it on
<a href="https://twitter.com/nhocki">Twitter @nhocki</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Using Liquid `include` with DB templates]]></title>
    <link href="http://blog.nhocki.com/2012/09/05/using-liquid-include-with-db-templates/"/>
    <updated>2012-09-05T17:37:00-04:00</updated>
    <id>http://blog.nhocki.com/2012/09/05/using-liquid-include-with-db-templates</id>
    <content type="html"><![CDATA[<p>I am working on a new application that will use some <a href="http://liquidmarkup.org/" title="Liquid Markup">Liquid</a> templates to
allow our clients to edit some templates.</p>

<p>We will store these templates in the Database, load them, parse them and render
them. So far, no magic.</p>

<p>But, I needed to have some &quot;partials&quot; for these. I wanted some common elements
(like image galleries) to be available for the client to use in that specific
template.</p>

<p>The way you normally accomplish this is by using an <code>include</code> tag, but I needed
a little bit more control. Not all the clients will have the same partials and
I also wanted the clients to be able to edit these partials.</p>

<p>So after reading the <a href="https://github.com/Shopify/liquid/blob/master/lib/liquid/tags/include.rb#L49-61" title="Liquid include source code">Liquid source code</a>, I found a really easy way to do
this.</p>

<p>You just need an object that responds to <code>read_template_file</code> and it can take
1 or 2 arguments (please, go for 2). This method must return the Liquid <strong>source</strong>
of the partial (not a parsed template).</p>

<p>So, here&#39;s a really simple one that will work:</p>

<div><script src='https://gist.github.com/3645632.js?file='></script>
<noscript><pre><code># Class to be used as a Liquid File System for the `include` tag
class LiquidTemplateSystem
  attr_reader :user

  def initialize(user)
    @user = user
  end

  # Return a valid liquid template string
  def read_template_file(template_path, context)
    user.partials.find_by_name(template_path).liquid_source
  end
end


# In the view:
# This `template` has an {% include %} tag.
&lt;%= template.render(obj, registers: LiquidTemplateSystem.new(user)) %&gt;</code></pre></noscript></div>

<h2 id="toc_71">Default FileSystem</h2>

<p>You could also set the default <code>FileSystem</code> with
<code>Liquid::Template.file_system = MyNewFileSystem.new</code>. This may be useful if you
want the partials to be shared on the database but not different for every user.</p>

<h2 id="toc_72">Any other way?</h2>

<p>Even though I find this to be <em>really</em> easy, another way may exist. If you know
of a better way to do this, please <a href="https://twitter.com/nhocki">let me know</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Seriously just do it]]></title>
    <link href="http://blog.nhocki.com/2012/07/26/seriously-just-do-it/"/>
    <updated>2012-07-26T01:07:00-04:00</updated>
    <id>http://blog.nhocki.com/2012/07/26/seriously-just-do-it</id>
    <content type="html"><![CDATA[<p>I have finished my first &quot;working season&quot; at <a href="http://mit.edu">MIT</a>. I got here
on October 4. It&#39;s been an amazing and weird journey.</p>

<p>But I have to admit that at first I was a bad employee. I was <strong>really</strong> used
to have somebody telling me what to do all the time. When I arrived at MIT that
didn&#39;t happen. <strong>At all</strong>. I was given some &quot;long term&quot; tasks and that was it.
I just had to do them.</p>

<p>Since no one was all over me checking my progress or asking me how I was
doing on everything, I was waisting a lot (*most*) of my time.
<a href="http://www.espndeportes.com">ESPNDeportes</a> got a lot of visits from me.
I started using Facebook on regular basis. <a href="http://www.threadless.com">Threadless</a>
didn&#39;t have enough T-shirts for me. <a href="http://twitter.com/nhocki">Twitter</a> was
not &quot;real time enough&quot;&#8230;</p>

<p>But, with time, I started working and I can&#39;t say how grateful I am for <a href="http://tomayko.com/writings/management-style">this
way of managing</a>. I&#39;ve learned
to be much more productive on my own. To manage my time. To be much more
responsible. But, I guess the best thing about this was that I learned that
you just need to <strong>shut up, close all the meaningless things you have open and
get shit done</strong>.</p>

<p>Sure, if you need help, ask. If what you&#39;re doing is wrong, try again. But you
won&#39;t know something doesn&#39;t work if it&#39;s just an idea.</p>

<p>I became much more productive in everything. Not only MIT work stuff. Personal
(disposable) things I wanted to try.
<a href="http://shop.oreilly.com/product/0636920021810.do">Things</a> I wanted to learn.
<a href="http://shop.oreilly.com/product/0636920019664.do">Things I</a>
<a href="http://designinghypermediaapis.com/">wanted</a> <a href="http://objectsonrails.com/">to</a>
<a href="http://pragprog.com/book/warv/the-rails-view">read</a> (even my
<a href="http://instapaper.com">Instapaper</a> queue went down. By a lot!).</p>

<p>So, just try it out. It may take more than a week to get used to it, but just
try it. <strong>Close this post and every other distracting thing and do something</strong>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Use the community for something more]]></title>
    <link href="http://blog.nhocki.com/2012/07/10/use-the-community-for-something-more/"/>
    <updated>2012-07-10T23:25:00-04:00</updated>
    <id>http://blog.nhocki.com/2012/07/10/use-the-community-for-something-more</id>
    <content type="html"><![CDATA[<p>For a while, this idea has been wondering around in my head.</p>

<p>I would love to have some web-development community in Medellín, but I would
like it to be &quot;something more&quot;. I love the idea of learning from others,
teaching and getting people involved. I know there are
<a href="http://coffeegrid.org/">more people</a> that are looking forward for this.</p>

<p>Sadly, we don&#39;t have a community like the one in Boston. It&#39;s amazing how
many events there are here. <a href="http://bostonrb.org/">Boston.rb</a> meets up to
3 times a month! It&#39;s just crazy! You just need to go to
<a href="http://www.meetup.com/">Meetup</a> to find an event of your interest! All
filled with people eager to share and help!</p>

<p>But I would love to put all that lovin&#39; to some &quot;charity&quot; work. Get real
projects for people that need it. <strong>It doesn&#39;t have to be 100% free</strong>,
but not charging nearly as much as you would normally do it!</p>

<p>For example, I know a <a href="http://www.bomberosenvigado.org/"><strong>Volunteer</strong> Fire Fighter Department</a>
that needs to build a simple learning system to distribute knowledge because
<strong>ignorance is taking a lot of lives</strong> (from firefighters and people they&#39;re
trying to save). Hell, they also need a new website!</p>

<p>I also know about a guy that needs a website for an organisation that helps
small kids with cancer in Africa.</p>

<p>All these projects could use the expertise of &quot;old&quot; developers and enthusiasm
from the ones that are just learning. I&#39;ve found that working on a real
project is way better than learning on your backyard. So why not take something
that could use more man-power and pair program on these websites?</p>

<p>I&#39;m not saying we should not have presentations! Presentations should still
be the center of this. Sharing new stuff is the most important part for the
community. But maybe having some <em>hackathons</em> where we work on these projects.</p>

<p>So, what do you think? Would something like this work? Would people be
interested? Those organizations could, for example, sponsor the meetings
giving some beer and food! Developing companies could also sponsor this
and get some mention on the created websites. I know
<a href="http://www.zinergia.co">we</a> would do it!</p>

<p>I can&#39;t do it right now. I&#39;m 2.5K miles away! And I don&#39;t think it&#39;s an
easy thing to organize, but I would like to know if people is actually
interested before I throw myself sending emails and talking to people,
so please contact me if you are, either by leaving a comment or
<a href="http://twitter.com/nhocki">pinging me on Twitter(@nhocki)</a>.</p>

<p>If you liked this idea, you might one to <a href="http://news.ycombinator.com/item?id=4227721">up vote it in Hackernews</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Fix Xcode Path]]></title>
    <link href="http://blog.nhocki.com/2012/05/09/fix-xcode-path/"/>
    <updated>2012-05-09T19:38:00-04:00</updated>
    <id>http://blog.nhocki.com/2012/05/09/fix-xcode-path</id>
    <content type="html"><![CDATA[<p>I updated my Xcode version this week and to my surprise they moved it from <code>/Developer</code> to <code>/Applications/Xcode</code>, the problem is that now all the developer tools went missing. Here&#39;s the easy solution for this:</p>

<p><code>sudo /usr/bin/xcode-select -switch /Applications/Xcode.app/Contents/Developer/</code></p>

<p>That&#39;s it. Just let Xcode know where to look for the developer tools again.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Mixing Presenters and Helpers]]></title>
    <link href="http://blog.nhocki.com/2012/05/08/mixing-presenters-and-helpers/"/>
    <updated>2012-05-08T18:28:00-04:00</updated>
    <id>http://blog.nhocki.com/2012/05/08/mixing-presenters-and-helpers</id>
    <content type="html"><![CDATA[<p>I just finished <a href="http://pragprog.com/book/warv/the-rails-view">The Rails View</a> and the best chapter for me was about presenters. I&#39;ve been <a href="http://blog.nhocki.com/2012/01/31/thoughts-on-decorators-and-presenters/">working with this</a> for a while and I have to admit I love the approach presented on the view.</p>

<p>All the presenters I&#39;ve done were <a href="http://blog.nhocki.com/2012/01/10/simple-presenters--decorators-on-rails/">instantiated in the controller</a>. In the book they suggest that <strong>if the presenter doesn&#39;t depend on params or session variables it&#39;s the view responsibility to create it</strong>. This is extremely flexible specially when we&#39;re presenting various objects on the same view. Here&#39;s an example to present users in our application, let&#39;s use the same presenter for the <em>index</em> and <em>show</em> actions, but let&#39;s make it in a really simple way!</p>

<h3 id="toc_68">The Helper &amp; View</h3>

<p>Let&#39;s create the helper method first. It&#39;s really simple and will take a block that we can use to build a custom markup &quot;on the fly&quot;.</p>

<div><script src='https://gist.github.com/2640442.js?file=user_helpers.rb'></script>
<noscript><pre><code># app/helpers/user_helpers.rb
def basic_information_for(user = @user, options = {})
  presenter = UserBasicPresenter.new(user, self, options)
  if block_given?
    yield(presenter)
  else
    presenter
  end
end</code></pre></noscript></div>

<p>Now, let&#39;s take a look at the views. In the <strong>show</strong> action, we&#39;re going to present only one user, and we can easily build the custom view for this. In the <strong>index</strong> action, we&#39;ll present all a &quot;basic info&quot; for the each user. We have that in a partial because this is used for all the collections of users in our application. Here&#39;s the view code:</p>

<div><script src='https://gist.github.com/2640442.js?file=user_views.html.erb'></script>
<noscript><pre><code># app/views/show.html.erb
&lt;% basic_information_for do |user|  %&gt;
  &lt;p&gt;&lt;%= user.name %&gt;&lt;/p&gt;
  &lt;p&gt;&lt;%= user.status %&gt;&lt;/p&gt;
  ...
&lt;% end %&gt;

# app/views/index.html
&lt;% @users.each do |user| %&gt;
  &lt;%= basic_information_for(user) %&gt;
&lt;% end %&gt;</code></pre></noscript></div>

<h3 id="toc_69">The Presenter, A.K.A What&#39;s happening here?</h3>

<p>What makes this <strong>really</strong> clean is that we can actually use <code>&lt;%=  %&gt;</code> directly on our helper method. This works because we&#39;ll define the <code>to_s</code> method in our presenter! In this method we <strong>render our partial in the context of the helper</strong>, which is amazing. Here&#39;s the code for that:</p>

<div><script src='https://gist.github.com/2640442.js?file=user_basic_presenter.rb'></script>
<noscript><pre><code># app/presenters/user_basic_presenter.rb
class UserBasicPresenter
  def initialize(user, template, options = {})
    @user     = user
    @options  = options
    @template = template
  end

  # more methods ...

  def to_s
    @template.render partial: 'user/basic', object: self
  end
end</code></pre></noscript></div>

<p>Is <strong>that</strong> simple! We have a unique interface (the helper) that will work great in both places! We could obviously make the presenter more complex, otherwise, why use it?</p>

<p>Even though I haven&#39;t used this approach a lot, I&#39;m really happy with it. I felt the difference instantly and it made a lot of sense to me. Being able to mix this with a <a href="http://blog.nhocki.com/2012/03/29/flexible-and-friendly-layouts-on-rails/">flexible layout</a> is really powerful!</p>

<h3 id="toc_70">Feedback</h3>

<p>I&#39;m also really interested in how people are using presenters. I&#39;d love to gain more feedbacks and points of view about this. Feel free to post a comment (here on in the gist) or ping me on Twitter, <a href="https://twitter.com/nhocki">@nhocki</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Flexible and friendly layouts on rails]]></title>
    <link href="http://blog.nhocki.com/2012/03/29/flexible-and-friendly-layouts-on-rails/"/>
    <updated>2012-03-29T23:16:00-04:00</updated>
    <id>http://blog.nhocki.com/2012/03/29/flexible-and-friendly-layouts-on-rails</id>
    <content type="html"><![CDATA[<p>I&#39;ve been working on a &quot;new&quot; project (I&#39;m new to the project, the project itself is &#39;old&#39;) and at the beginning I had to edit a bunch of stuff in the views.</p>

<p>A friend needed to change the header of the page on <strong>one</strong> action of the <code>UsersController</code>. He is new to rails and was struggling with this, so he asked me for help.</p>

<p>It&#39;s pretty common to have a <code>header</code> partial rendered on the view. If you&#39;re a lovely developer you would have <code>render &quot;header&quot;</code> and not something like <code>render &quot;layouts/header&quot;</code>. The reason behind this is simple. <a href="http://railscasts.com/episodes/269-template-inheritance" title="Railscasts Template Inheritance">Template Inheritance</a>. This means that if you&#39;re working on the <code>UsersController</code>, when rails looks for the <code>header</code> partial, it will first look for <code>users/header</code> moving up the parents chain, ending in <code>application/header</code>. So, next time, put all your <em>layout</em> stuff in <code>application/_partial</code> so you can easily override it!</p>

<p>But this is a <em>controller wide</em> solution, since all the actions from that controller will render the same partial. So, how do you achieve this on a single action? My solution is a 3 line helper:</p>

<div><script src='https://gist.github.com/2246251.js?file=application_helper.rb'></script>
<noscript><pre><code># This method will allow a section to be rendered form a view or
# to render a partial instead.
# 
# It's really useful to have 'flexible' sections in the layout. E.g
# add this to your layout file:
#
# content_or_partial(:header)
#   =&gt; renders the content_for(:header) or the &quot;header&quot; partial
#
# content_or_partial(:header, 'users/hello')
#   =&gt; renders the content_for(:header) or the &quot;users/hello&quot; partial
#
# application.html.erb
#   &lt;header class=&quot;main-header wrapper&quot;&gt;&lt;%= content_or_partial(:header) %&gt;&lt;/header&gt;
#
# users/show.html.erb
#   &lt;%= content_for :header do %&gt;
#     My action specific content
#   &lt;% end %&gt;
#
def content_or_partial(content, partial = content)
  content_for?(content) ? content_for(content) : render(partial.to_s)
end</code></pre></noscript></div>

<p>Now all you have to do is fill your layouts with &quot;content boxes&quot; that can be changed on an controller or action-specific need, giving great flexibility.</p>

<p>How would you get around this?</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Thoughts on Decorators & Presenters]]></title>
    <link href="http://blog.nhocki.com/2012/01/31/thoughts-on-decorators-and-presenters/"/>
    <updated>2012-01-31T18:12:00-05:00</updated>
    <id>http://blog.nhocki.com/2012/01/31/thoughts-on-decorators-and-presenters</id>
    <content type="html"><![CDATA[<p>I have been developing a new app using a lot of <a href="http://blog.nhocki.com/2012/01/10/simple-presenters--decorators-on-rails/">decorators and presenters</a>. I&#39;ve been highly influenced by <a href="http://about.avdi.org/">Avdi</a> (both his <a href="http://avdi.org/devblog/">blog</a> and his <a href="http://avdi.org/devblog/2011/11/15/early-access-beta-of-objects-on-rails-now-available-2/">Objects in Rails book</a>) and <a href="http://steveklabnik.com">Steve Klabnik</a>, but now I have a problem. My decorators are starting to get big. I&#39;ll have to use <code>concerns</code> for that. Expect another post about it (maybe).</p>

<p><strong>I want to warn you that I am not by any means an expert on the subject. So I&#39;m more than open to any feedback you can give me.</strong></p>

<blockquote>
<p>All presenters are decorators, but not all decorators are presenters.</p>

<p>A decorator is a class that adds some sort of functionality to another
class. A presenter is a class that adds some sort of presentation
formatting functionality to another class.</p>

<p><a href="https://groups.google.com/forum/#!msg/objects-on-rails/htAopf3k_dM/qJMq6QAfMvsJ">Steve Klabnik</a></p>
</blockquote>

<p>I think that what a decorator <em>should</em> do is give a standard API for the processed data stored somewhere (probably a database). I don&#39;t think a decorator should be in charge of the markup of the presented data. For me, it makes no sense that the decorator (that is processed data for me), knows <em>how</em> it is being shown to the user.</p>

<p>That said, I think the <strong>same</strong> decorator should be used to build the JSON response and the HTML response. The HTML markup and JSON structure are <em>just</em> \&#39;markup\&#39;. So, they can be treated as templates.</p>

<h3 id="toc_65">So, where do the logic in the template goes?</h3>

<p>I had this discussion with a <a href="http://mheroin.com/">friend</a>, and he asked me that, with this approach, how would he add elements to a view depending on a condition, for example, the famous \&#39;***You are logged in as nhocki***\&#39; if there&#39;s a user logged in.</p>

<p>I think this should be created inside helper methods that takes a block. Inside that block you can use the <strong>decorator</strong> methods to show the needed data. You can use those methods in your JSON builder too (try out the new <a href="https://github.com/rails/jbuilder">Jbuilder</a>). You could bundle up these methods and re-use them in your projects too.  </p>

<p>Here&#39;s an example:</p>

<div><script src='https://gist.github.com/1714913.js?file='></script>
<noscript><pre><code># app/models/user.rb
#
# User model has name &amp; username
#
# Only the username is required (signup with Github for example)
class User &lt; ActiveRecord::Base
  validates :username, presence: true
end



# app/decorators/user_decorator.rb
#
# Let's use the Draper gem for the decorators
class UserDecorator &lt; ApplicationDecorator
  decorates :user

  def display_name
    user.name.presence || user.username
  end

  # Formatting the data is OK. I'm against adding the markup
  def salary
    h.number_to_currency(user.salary)
  end
end


# app/views/users/index.html.erb

# current_user is a presenter
&lt;%= logged_in_area do %&gt;
  You are logged in as &lt;%= current_user.display_name %&gt;
&lt;% end %&gt;

# viewing @some_users's profile (which is also a presenter)
&lt;%= admin_area do %&gt;
  &lt;span class=&quot;salary&quot;&gt;&lt;%= @some_user.salary %&gt;&lt;/span&gt;
&lt;% end %&gt;


# app/helpers/application_helpers.rb
#
# We all hate helpers, but this time, they're useful.
def admin_area(&amp;block)
  content_tag(:div, capture(&amp;block), class: 'admin') if current_user.admin?
end</code></pre></noscript></div>

<h3 id="toc_66">Maybe I have the names wrong</h3>

<p>As I mentioned before, I&#39;m no expert on design patterns. And I&#39;m pretty <em>new</em> to presenters and decorators. So, maybe my approach has a different name. I don&#39;t really care how this is called, I feel it&#39;s a more natural approach and it keeps the template and the processed data in different parts.</p>

<h3 id="toc_67">Please provide feedback</h3>

<p>It is also possible that I have it all wrong, that&#39;s why I&#39;m really interested in your opinion. I&#39;m eager to learn about everything from anyone, so I would love to have some feedback about this. You can post a comment below or send me a message on <a href="http://twitter.com/nhocki">twitter</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[From Tumblr to Octopress]]></title>
    <link href="http://blog.nhocki.com/2012/01/23/from-tumblr-to-octopress/"/>
    <updated>2012-01-23T00:00:00-05:00</updated>
    <id>http://blog.nhocki.com/2012/01/23/from-tumblr-to-octopress</id>
    <content type="html"><![CDATA[<p>I wrote an article yesterday about URL design and I spent about an hour fighting with <a href="http://tumblr.com/">Tumblr</a> to format my <code>code inside a pre (or code) tag</code>. I knew I was done with them. Today, I&#39;m giving <a href="http://octopress.org/">Octopress</a> a try. I honestly wanted to try <a href="http://jekyllrb.com/">Jekyll</a> but I was really lazy to code my HTML. So, for now, I&#39;m welcoming Octopress.</p>

<p>I really love the fact that I can write in <a href="http://daringfireball.net/projects/markdown/">Markdown</a>, it&#39;s really powerful yet simple that makes me focus on writing and not formatting. I know I can use Markdown in Tumblr but I don&#39;t have Textmate there, so this is a big +1 for Jekyll/Octopress.</p>

<h2 id="toc_63">Moving Posts From Tumblr</h2>

<p>There&#39;s a <a href="https://github.com/mojombo/jekyll/wiki/Blog-Migrations">great script</a> to move posts from Tumblr. They&#39;ll even be downloaded as markdown, but I still had to edit them to add the Gist snippets. Thankfully I don&#39;t have a lot of posts so it didn&#39;t took a lot.</p>

<h2 id="toc_64">Deploying</h2>

<p>I&#39;ve deployed my blog to <a href="http://heroku.com">Heroku</a> now. It was supposed to be really simple. It was, after I realized that the <code>public</code> folder is ignored by the git. I spent an hour of my life there :-( !</p>

<pre>Remove the public folder from your .gitignore</pre>

<p>I&#39;ll probably post more about Octopress when I get to know it better. There are a bunch of plugins and stuff that may be useful, or maybe, it&#39;ll suck and I&#39;ll have to go and find something else!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Beautiful URLs in Rails. The easy way.]]></title>
    <link href="http://blog.nhocki.com/2012/01/22/beautiful-urls-in-rails-the-easy-way/"/>
    <updated>2012-01-22T00:00:00-05:00</updated>
    <id>http://blog.nhocki.com/2012/01/22/beautiful-urls-in-rails-the-easy-way</id>
    <content type="html"><![CDATA[<p>I&#39;ve always been in love with <a href="http://warpspire.com/posts/url-design/">beautiful URL design</a>. Specially for &#39;public&#39; parts of the site. In Rails, it&#39;s pretty common to use the ID of the object in the URL. Well, that <strong>really</strong> sucks.</p>

<p>My first approach to solve this problem was started by <a href="http://mheroin.com/">Federico Builes</a> a long time ago with a plugin (<a href="https://github.com/nhocki/make_permalink">which I now maintain</a> as a gem) called <a href="http://rubygems.org/gems/make_permalink">Make Permalink</a>. It&#39;s really easy to use and will (up to some point) solve the problem. I know <a href="http://rubygems.org/gems/friendly_id">FriendlyID</a> exists, but I <em>hate</em> having the permalink field in my database (I <em>always</em> forget it) so that&#39;s why I like Make Permalink&#39;s approach.</p>

<div><script src='https://gist.github.com/1660555.js?file=make_permalink_example.rb'></script>
<noscript><pre><code>class Post &lt; ActiveRecord::Base
  make_permalink :title

  # Override this method to have nice looking
  # URLs. You still use `Post.find` in the controller
  # and will get URL like this:
  #
  #    /posts/1-look-mom-i-rock
  def to_param
    permalink
  end
end
</code></pre></noscript></div>

<p>The good thing about having <code>/users/1-nhocki</code> as the URL is that is way better than <code>/users/1</code> <strong>AND</strong> you don&#39;t have to change any of your app for that (you can still use <code>find</code> in your controllers).</p>

<p>I was really glad with this but not too long ago, someone asked me for something similar but for Mongoid. And hell! Mongoid ids are ugly as shit! So, they wanted to have <em><strong>just</strong></em> the username. I said, well, why don&#39;t you just change the <code>to_param</code> method to return the username and use <code>User.find_by_username</code> everywhere?</p>

<p>Even though this actually works, I would be very tired from writing <code>find_by_username</code> over and over again. So I told him to write a <code>fetch</code> method where he could get an user either by the <code>id</code> or the <code>username</code>. I think this is pretty convenient and nice, but I would <em><strong>hate</strong></em> to write that fetch method in every model. So, with some metaprogramming we can easily do this.</p>

<p>First, I&#39;ve created a <code>fetchable_on</code> method to use it in your models. This will define a class method called <code>fetch</code> that will let you get the object by querying on the <code>attribute</code> <strong>OR</strong> on the <code>id</code>. Right now, it won&#39;t work if your attribute is a numeric value (like the number of bathrooms you have in your house), but most of the times, it&#39;s ok. Here&#39;s the code. Place it in your initializers folder.</p>

<div><script src='https://gist.github.com/1657758.js?file='></script>
<noscript><pre><code># config/initializers/fetchable.rb

# encoding: UTF-8

module Extensions
  module Fetchable
    # Adding `fetchable_on` to a model will define the `fetch` method
    # that will allow you to query that model with the `attribute` or `id`.
    #
    # Having the following:
    #
    #    class User &lt;&lt; ActiveRecord::Base
    #      fetchable_on :username
    #    end
    #
    # Will let you fetch users by username or id.
    #
    #    User.fetch('nhocki').id # =&gt; 1
    #    User.fetch(1).username # =&gt; nhocki
    def fetchable_on(attribute)
      define_singleton_method(&quot;fetch&quot;) do |*args|
        looked_value = args.shift
        begin
          find(looked_value.to_i, *args)
        rescue ActiveRecord::RecordNotFound
          send(&quot;find_by_#{attribute}!&quot;, looked_value.to_s, *args)
        end
      end
    end
  end
end

ActiveRecord::Base.extend(Extensions::Fetchable)</code></pre></noscript></div>

<p>That way, you just need to call <code>fetchable_on :attribute</code> in your model and it&#39;ll adjust the <code>fetch</code> method to query on that <code>attribute</code> or the <code>id</code>. Here&#39;s an example:</p>

<div><script src='https://gist.github.com/1660555.js?file=fetchable_example.rb'></script>
<noscript><pre><code>class User &lt; ActiveRecord::Base
  fetchable_on :username

  # URL Style: /users/nhocki
  def to_param
    username
  end
end

# Gives you the `fetch` method.
# It takes the username or the id.
# Use fetch in your controller

User.fetch('nhocki').id # =&gt; 1
User.fetch(1).username  # =&gt; 'nhocki'
</code></pre></noscript></div>

<p><strong>Note that I use <code>find_by_attribute!</code> to throw an exception if the object is not found. Using the method without a <code>!</code> will return <code>nil</code> if there&#39;s no object in the DB.</strong></p>

<p>How would you implement this? Any good solutions?</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Simple Presenters & Decorators on Rails]]></title>
    <link href="http://blog.nhocki.com/2012/01/10/simple-presenters--decorators-on-rails/"/>
    <updated>2012-01-10T00:00:00-05:00</updated>
    <id>http://blog.nhocki.com/2012/01/10/simple-presenters&#8211;decorators-on-rails</id>
    <content type="html"><![CDATA[<p>There&#39;s been a <a href="http://blog.steveklabnik.com/posts/2011-09-06-the-secret-to-rails-oo-design">lot</a> of <a href="http://blog.steveklabnik.com/posts/2011-09-09-better-ruby-presenters">talk</a> <a href="http://avdi.org/devblog/2011/11/15/early-access-beta-of-objects-on-rails-now-available-2/">about</a><a href="http://robots.thoughtbot.com/post/14825364877/evaluating-alternative-decorator-implementations-in">decorators</a> and <a href="http://robots.thoughtbot.com/post/13641910701/tidy-views-and-beyond-with-decorators">presenters</a> (<a href="https://groups.google.com/forum/#!msg/objects-on-rails/htAopf3k_dM/qJMq6QAfMvsJ">which are almost the same</a>) in Rails and how they help you with a &quot;real&quot; OO code. There are great gems and there&#39;s even a <a href="http://railscasts.com/episodes/287-presenters-from-scratch">Railscast</a> (pro) on how to build a presenter from scratch. But, in the new project I&#39;m working on, we (&lt;- <a href="https://github.com/febuiles">he</a>) has this <strong>really</strong> simple setup which I feel kind of cleaner.</p>

<p>What we wanted to have was a <code>present</code> method in the controller and build the view from its methods. So, we created an <a href="http://api.rubyonrails.org/classes/ActiveSupport/Concern.html">ActiveSupport::Concern</a> called <code>Presentable</code> and included it in our application controller. Here&#39;s our (once again, his) code for that module.</p>

<div><script src='https://gist.github.com/1592754.js?file=presentable.rb'></script>
<noscript><pre><code># app/concerns/controllers/presentable.rb

# Presentable Concern
# Include this module in the controllers where
# you need the `present` method.
module Presentable
  # This method returns a Presenter Object to work
  # with in the view.
  #
  # Attributes:
  #   object:   The object to present
  #
  # Supported Options
  #    as:   The presenter class. If this is not suplied 
  #          the method will try to guess the class
  #          e.g: UserPresenter for a User object.
  def present(object, options = { })
    if object.respond_to?(:first)
      return [] if object.empty?
      presenter = &quot;#{object.first.class.to_s.pluralize}Presenter&quot;
    else
      presenter = &quot;#{object.class}Presenter&quot;
    end

    presenter = options.fetch(:as) { eval(presenter) }
    presenter.new(object)
  end
end
</code></pre></noscript></div>

<p>Now, we just need to create each presenter. But, since there&#39;s a lot of shared logic between presenters we (he) just created a parent <code>Presenter class</code> with all that. Notice that our presenter takes an object as parameter, so we just delegated all the method calls to our record (thank you <code>method missing</code>) if it responded to that method. You could also send the view context to access Rails helpers inside your presenter. Here&#39;s our <code>Presenter base class</code>.</p>

<div><script src='https://gist.github.com/1592754.js?file=presenter.rb'></script>
<noscript><pre><code># Base presenter class
# All presenters should inherit from this one.
class Presenter
  # Include all the modules you need or
  # send the view context to access rails helpers.
  include Rails.application.routes.url_helpers
  # ...

  include Presentable

  attr_reader :record

  def initialize(record)
    @record = record
    @klass = record.class
  end

  def method_missing(name, *arguments, &amp;block)
    super unless record.respond_to?(name)
    record.send(name, *arguments, &amp;block)
  end

  def translate_field_name(name)
    @klass.human_attribute_name(name)
  end

  # UrlHelper was being an ass, easier to go commando.
  def link_to(name, url, options={})
    href = { :href =&gt; url }
    content_tag(:a, name, options.merge(href))
  end
end
</code></pre></noscript></div>

<p>Then, we just need to inherit from the <code>Presenter</code> class and add our custom, clean methods. Using <code>delegate and method missing</code> will help us keep the class clean. I won&#39;t show you a presenter here, they&#39;ll look a lot like Ryan&#39;s one in his Railscasts so check that out first.</p>

<p>I will show you our controller though. Using the decent_exposure gem we managed to get super tiny controllers. Here&#39;s 1 of them:</p>

<div><script src='https://gist.github.com/1592754.js?file=requests_controller.rb'></script>
<noscript><pre><code>class RequestsController &lt; ApplicationController
  expose(:filter) { params.fetch(:filter) { :all} }
  expose(:filtered_requests) { Request.filter(filter) }
  expose(:requests) { present(filtered_requests) }

  before_filter :find_request, :only =&gt; [:show, :destroy]

  def index
  end

  def show
    extend(RequestRedirector) # Another concern
    redirect_to_request
  end

  def destroy
    @request.destroy
    redirect_to requests_path(:filter =&gt; &quot;new_requests&quot;), :notice =&gt; &quot;Se ha eliminado la solicitud&quot;
  end

  private
  def find_request
    @request = Request.find(params[:id])
  end
end
</code></pre></noscript></div>

<p>Combining <code>presenters</code> and <code>form builders</code> will make your views even cleaner, giving a lot of pleasure to the future you! So please, just pretty-please, start using presenters and builders and leave helpers as simple as possible. Or, leave them for good.</p>

<p>You&#39;ll be amazed how different you&#39;ll feel when reading some old code or some co-worker&#39;s code.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[But free web services are not like free software]]></title>
    <link href="http://blog.nhocki.com/2011/12/06/but-free-web-services-are-not-like-free-software/"/>
    <updated>2011-12-06T00:00:00-05:00</updated>
    <id>http://blog.nhocki.com/2011/12/06/but-free-web-services-are-not-like-free-software</id>
    <content type="html"><![CDATA[<blockquote>
<p>But free web services are not like free software. If your free software project suddenly gets popular, you gain resources: testers, developers and people willing to pitch in. If your free website takes off, you lose resources. Your time is spent firefighting and your money all goes to the nice people at Linode. </p>
</blockquote>

<p>Don&#39;t be a Free User - <a href="http://blog.pinboard.in/2011/12/don_t_be_a_free_user/">Pinboard Blog</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Colorize your rake output]]></title>
    <link href="http://blog.nhocki.com/2011/11/30/colorize-your-rake-output/"/>
    <updated>2011-11-30T00:00:00-05:00</updated>
    <id>http://blog.nhocki.com/2011/11/30/colorize-your-rake-output</id>
    <content type="html"><![CDATA[<p>I&#39;ve been writing a lot of rake tasks lately. I like to have a lot of output (feedback) from my tasks, at least while I&#39;m writing them. I have colors in my tests, but my output is really ugly, so I took <a href="https://github.com/andmej/chatte/blob/master/client.rb#L15-24">some code</a> from <a href="https://github.com/andmej/chatte/">Chatte</a>, - a simple chat application a <a href="http://andr.esmejia.com/">friend</a> (and awesome <a href="https://github.com/andmej/">developer</a>, btw) wrote for college- and made some modifications for it. Here&#39;s my module right now…</p>

<div><script src='https://gist.github.com/1410582.js?file='></script>
<noscript><pre><code>module Colors
  def colorize(text, color_code)
    &quot;\033[#{color_code}m#{text}\033[0m&quot;
  end

  {
    :black    =&gt; 30,
    :red      =&gt; 31,
    :green    =&gt; 32,
    :yellow   =&gt; 33,
    :blue     =&gt; 34,
    :magenta  =&gt; 35,
    :cyan     =&gt; 36,
    :white    =&gt; 37
  }.each do |key, color_code|
    define_method key do |text|
      colorize(text, color_code)
    end
  end
end

# green &quot;Hello&quot;
#   =&gt; &quot;\e[32mHello\e[0m&quot;</code></pre></noscript></div>

<p>Now, you just need to <code>include Colors</code> in your tasks and enjoy your life.</p>

<p>I know there&#39;s a <a href="https://github.com/fazibear/colorize">colorize</a> gem out there, but I don&#39;t like their syntax. I prefer Andres&#39; approach much more, so maybe I&#39;ll turn this into a really simple gem. Do you think it&#39;s worth it?</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Algorithms are on the cutting edge of entrepreneurship.]]></title>
    <link href="http://blog.nhocki.com/2011/11/09/algorithms-are-on-the-cutting-edge-of-entrepreneurship/"/>
    <updated>2011-11-09T00:00:00-05:00</updated>
    <id>http://blog.nhocki.com/2011/11/09/algorithms-are-on-the-cutting-edge-of-entrepreneurship</id>
    <content type="html"><![CDATA[<blockquote>
<p>Algorithms are on the cutting edge of entrepreneurship. If you&#39;re talking
about just re-implementing stuff that people did ten years ago, performance
isn&#39;t that important at some level. But, if you&#39;re talking about doing stuff
that nobody has done before, one of the reasons often that they haven&#39;t done
it is because it&#39;s too time-consuming.</p>
</blockquote>

<p>By Charles Leiserson</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Compile assets and push them - no pain involved!]]></title>
    <link href="http://blog.nhocki.com/2011/09/28/compile-assets-and-push-them---no-pain-involved/"/>
    <updated>2011-09-28T00:00:00-04:00</updated>
    <id>http://blog.nhocki.com/2011/09/28/compile-assets-and-push-them&#8212;no-pain-involved</id>
    <content type="html"><![CDATA[<p>This week, I&#39;ve been working on a side project with <a href="http://andr.esmejia.com/">Andrés
Mejía</a> (<a href="http://twitter.com/andmej">@andmej</a>) and
for some really really sad reason <a href="http://heroku.com">Heroku</a> is not working
as expected (a simple git push should do the trick, but we&#39;re having a really
<a href="https://github.com/ddollar/rails_log_stdout/issues/4">weird issue</a>).</p>

<p>At first, I thought it had something to do with the assets. Even-though
<a href="http://heroku.com">Heroku</a> has <a href="h%0Attp://devcenter.heroku.com/articles/rails31_heroku_cedar#the_asset_pipeline">asset compiling tasks on their Cedar stack</a>,
it didn&#39;t really worked for us at first. So I just decided to create a small
rake task to compile the assets and push them to Github.</p>

<p>The good thing about this rake task is that it will only push the assets. This
means, that <strong>you&#39;ll need a clean git tree to run this task or it will die</strong>.
This ensures that the automatic push <strong>won&#39;t commit any non-related change to
the compilation of the assets</strong>.</p>

<p>Now, you only have to run <strong>rake assets:compile</strong> and sleep well at night.
Here&#39;s the code. Hope it helps.</p>

<div><script src='https://gist.github.com/1241135.js?file='></script>
<noscript><pre><code>namespace :assets do
  desc &quot;Compile all the assets named in config.assets.precompile and push them&quot;
  task :compile do
    AssetsCompiler.new.compile
  end

  class AssetsCompiler
    def compile
      ensure_clean_git
      removed_previous_assets
      compile_assets
      commit_compiled_assets
      push
    end

    def ensure_clean_git
      raise &quot;Can't deploy without a clean git status.&quot; if git_dirty?
    end

    def removed_previous_assets
      run &quot;bundle exec rake RAILS_ENV=production assets:clean&quot;
    end

    def compile_assets
      run &quot;bundle exec rake RAILS_ENV=production assets:precompile&quot;
    end

    def commit_compiled_assets
      run &quot;git add -u &amp;&amp; git add . &amp;&amp; git commit -am 'Compiled assets'&quot;
    end

    def push
      run &quot;git push&quot;
    end

    private

    def run(command)
      puts &quot;+ Running: #{command}&quot;
      puts &quot;-- #{system command}&quot;
    end

    def git_dirty?
      `[[ $(git diff --shortstat 2&gt; /dev/null | tail -n1) != &quot;&quot; ]]`
      dirty = $?.success?
    end
  end
end</code></pre></noscript></div>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Simple scrollbar synchronization with jQuery]]></title>
    <link href="http://blog.nhocki.com/2011/09/21/simple-scrollbar-synchronization-with-jquery/"/>
    <updated>2011-09-21T00:00:00-04:00</updated>
    <id>http://blog.nhocki.com/2011/09/21/simple-scrollbar-synchronization-with-jquery</id>
    <content type="html"><![CDATA[<p>I&#39;m working on a really cool project on my University. It&#39;s a web application
to compare book editions and help professors build what is know as the
Collatzio. The Collatzio is simply the &quot;original&quot; version of the book. That
means, to have the book the author wanted to write and not the one that the
editors wanted to publish.</p>

<p>I needed to have two versions of the book aligned side by side. So, the
HTML/CSS was pretty simple. Just have two simple divs with <strong>overflow-y:
auto;</strong> to make the scrollbar appears on each div.</p>

<p>The coffeescript code I used is really simple, here it is:</p>

<div><script src='https://gist.github.com/1233545.js?file='></script>
<noscript><pre><code>jQuery ($) -&gt;
  ($ &quot;.scroll-text&quot;).scroll (event) -&gt;
    trigger = ($ @)
    element = trigger.data(&quot;scrolls&quot;)
    $(element).scrollTop(trigger.scrollTop())
    event.preventDefault()
    return false</code></pre></noscript></div>

<p>Hope it helps someone. I struggled a little bit with the &quot;common&quot; way of
scrolling things. People usually use the <code>animate</code> method to scroll the body,
but I couldn&#39;t use it because it kept triggering the scroll event on the
elements, so it was an infinite loop.</p>
<div class="highlight"><pre><code class="javascript"><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;html, body&quot;</span><span class="p">).</span><span class="nx">animate</span><span class="p">({</span> <span class="nx">scrollTop</span><span class="o">:</span> <span class="mi">0</span> <span class="p">},</span><span class="err"> </span><span class="s2">&quot;slow&quot;</span><span class="p">);</span>
</code></pre>
</div>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[TimeHub, the first winner!]]></title>
    <link href="http://blog.nhocki.com/2011/08/26/timehub-the-first-winner/"/>
    <updated>2011-08-26T00:00:00-04:00</updated>
    <id>http://blog.nhocki.com/2011/08/26/timehub-the-first-winner</id>
    <content type="html"><![CDATA[<p>I just wanted to &quot;personally&quot; say thanks to everyone who voted for my entry on
the <a href="http://rallyonrails.com">Rally on Rails</a> (Latinamerican Rails Rumble).</p>

<p><a href="http://timehub.net">Timehub</a> won! And this couldn&#39;t be possible without the
people who voted.</p>

<p>I would like to thank the Rally on Rails team &amp; judges. They made an excellent
job and even though there are somethings to improve (like the voting rules),
<strong>I think that the contest was great!</strong></p>

<p>We had already posted a &quot;<a href="http://blog.timehub.net/post/9404894926/from-medellin-with-love">thank you
note</a>&quot;, in
the <a href="http://timehub.net">Timehub</a> blog, but I really wanted to write one
myself.</p>

<p>I was pretty spammer during these last 3 days! That will stop now!</p>

<p><strong>Thanks again!</strong> Hope to see you in <a href="http://timehub.net">Timehub</a> v2!</p>
]]></content>
  </entry>
  
</feed>
