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

  <title><![CDATA[Entreprevelopment adventures]]></title>
  <link href="http://luisalima.github.com/atom.xml" rel="self"/>
  <link href="http://luisalima.github.com/"/>
  <updated>2013-01-09T16:02:42+00:00</updated>
  <id>http://luisalima.github.com/</id>
  <author>
    <name><![CDATA[Luisa Lima]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[How I test part IV -- testing controllers in rails]]></title>
    <link href="http://luisalima.github.com/blog/2013/01/09/how-i-test-part-iv/"/>
    <updated>2013-01-09T11:21:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2013/01/09/how-i-test-part-iv</id>
    <content type="html"><![CDATA[<p>First and foremost, I advise you to <a href="http://everydayrails.com/2012/04/07/testing-series-rspec-controllers.html">read this post from Everyday Rails</a>. It gives a very nice overview, by controller method, of what controller tests should look like. Like Aaron, I had read about testing and noone advises you to test controllers &#8211; rather, they would say &#8220;test models and do integration testing, and controllers will be covered by those tests&#8221;. But as you can see in <a href="http://solnic.eu/2012/02/02/yes-you-should-write-controller-tests.html">&#8220;Yes, you should write controller tests!&#8221;</a>, controllers should be tested. I&#8217;ll post their reasons here and add a few reasons extra why to test controllers:</p>

<ul>
<li>Controllers are models, too;</li>
<li>Controller specs can be written and ran more quickly than integration tests;</li>
<li>(My own reason #1) It&#8217;s very easy to write them using the basic scaffholding provided;</li>
<li>(My own reason #2) If they are not easy to write, it&#8217;s because you are doing some nasty complex not-that-CRUDdy stuff. Uh oh. Which is what was happening in my case.</li>
</ul>


<p>But mainly, I don&#8217;t see tests as testing instruments per se. I see tests as a way to reflect on your code, see how complex it is, and an opportunity for refactoring as well.</p>

<p>That being said&#8230; What I did was to pick up the scaffholding and adapt it as needed, and here is the trouble that I ran into when testing controllers.</p>

<p>First, run the scaffholding routing tests. Yeah, just go ahead and do it. And now correct them to account for all the nested resource paths that you have, as well as the paths that you don&#8217;t want to allow. You are going to need it.</p>

<p>Now open your scaffholding controller tests. Each example refers to a single method of the controller, and you have tests for each action that the method must take. If you run them and they don&#8217;t behave as expected, there can be several reasons, but from my (very limited experience), here are the most common:</p>

<ol>
<li>you didn&#8217;t set your valid_attributes properly &#8211; including having nested attributes that are required and are not being set;</li>
<li>you are using authentication and need a current_user (modifying valid_session);</li>
<li>you have nested resources and so need to set the id(s) of the parent resource(s).</li>
</ol>


<p>Let&#8217;s go through each of these in order&#8230;</p>

<h2>Attributes</h2>

<p>First and foremost, have you tested your factories in your models? Good.</p>

<p>What I usually do is to make use of the <em>FactoryGirl attributes_for</em> method and just stick it in valid_attributes:</p>

<pre><code>def valid_attributes
  FactoryGirl.attributes_for(:my_model)
end
</code></pre>


<p>However, this won&#8217;t work when you have <em>validates_presence_of</em> for a has_one association. Say that my model has:</p>

<pre><code>
    has_one :banana
    validates_presence_of :banana
    accepts_nested_attributes_for :banana
</code></pre>


<p>Then, even if you have the association with the banana model in your &#8220;my_model&#8221; Factory, you&#8217;ll get an error because attributes_for does not include associations and thus banana is not there. To fix that, we do:</p>

<pre><code>def valid_attributes
    banana_attribs = FactoryGirl.attributes_for(:banana)
  FactoryGirl.attributes_for(:my_model).merge({:banana_attributes => banana_attribs})
end
</code></pre>


<p>Finally&#8230; if your model belongs_to another model, let&#8217;s say that it looks like this (can you tell I&#8217;m hungry?):</p>

<pre><code>
    has_one :banana
    validates_presence_of :banana
    accepts_nested_attributes_for :banana
    belongs_to :strawberry
</code></pre>


<p>Assuming that you set up this association correctly in FactoryGirl, you&#8217;ll have a strawberry_id in your model&#8217;s attributes. You have two options: either to set it up as an attr_accessible, or to get rid of it in the valid_attributes in order not to get a security error:</p>

<pre><code>def valid_attributes
    banana_attribs = FactoryGirl.attributes_for(:banana)
  FactoryGirl.attributes_for(:my_model).except(:strawberry_id).merge({:banana_attributes => banana_attribs})
end
</code></pre>


<p>Disclaimer: I was going to write a method go go through all the associations and do this automagically, but then I figured that it was kind of cheating because it is really valuable, like I said, to reflect on the way that you are writing your code. At least for me, it is.</p>

<h2>Authentication and authorization &#8211; you need valid session data and a current_user variable</h2>

<p>If you are using any kind of authentication, you need to set up a valid current user and a valid session. Here&#8217;s <a href="https://github.com/plataformatec/devise/wiki/How-To%3a-Controllers-and-Views-tests-with-Rails-3-%28and-rspec%29">how to do it if you are using Devise</a>:</p>

<pre><code>before(:each) do
  @request.env["devise.mapping"] = Devise.mappings[:user]
  user = FactoryGirl.create(:user)
  user.add_role(:master_of_all_fruits) # don't forget that if you are doing authorization, you need to set up the correct role as well
    user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the confirmable module
  sign_in user
end

it "should have a current_user" do
  subject.current_user.should_not be_nil
end
</code></pre>


<p>Also, don&#8217;t forget to set up the session:</p>

<pre><code>def valid_session
  {"warden.user.user.key" => session["warden.user.user.key"]}
end
</code></pre>


<p>The errors that are given are not clear at all in this case, so make sure to check that you didn&#8217;t forget to set up the valid session and the current_user!</p>

<h2>Nested resources</h2>

<p>If my_model is a nested resource, you need to adapt your controller spec in order to account for that. Suppose that we have</p>

<pre><code>resources :strawberries do
  resources :my_model do
</code></pre>


<p>When you run the scaffholding for my_model controller tests, you&#8217;ll get a &#8220;No route matches&#8230;&#8221; followed by a hash of my_model.</p>

<p>So what you have to do is to:
* add the strawberry_id to the actions that require a /strawberries/:strawberry_id/my_model route;
* add the @strawberry to the redirects that require the same type of routes.</p>

<p>What I do is to use a before(:each) that creates the required @strawberry:</p>

<pre><code>before(:each) do
    @strawberry = FactoryGirl.create(:strawberry)
  end

it "redirects to the created my_model" do
post :create, {:strawberry_id => @strawberry.id, :my_model => valid_attributes}, valid_session
response.should redirect_to([@strawberry, MyModel.last])
end

it "redirects to the my_models list" do
  my_model = MyModel.create! valid_attributes
  delete :destroy, {:strawberry_id => @strawberry.id, :id => my_model.to_param}, valid_session
  response.should redirect_to(strawberry_my_models_url(@strawberry))
end

# Don't forget to create my_models for @strawberry in the nested route!
describe "GET index" do
  it "assigns all my_models as @my_models" do
    my_model = MyModel.create! valid_attributes
    @strawberry.my_models << my_model
    get :index, {:strawberry_id => @strawberry.id}, valid_session
    assigns(:my_models).should eq([my_model])
  end
end
</code></pre>


<p>What if you also have a route like this:</p>

<pre><code> resources :my_models, :only => [:index, :show]
</code></pre>


<p>What I do is that I divide the tests into two contexts:</p>

<pre><code> context "directly via /my_models" do
  #...
end

context "via /strawberries/:strawberry_id/my_models" do
  #...
end
</code></pre>


<p>and place the &#8220;before(:each)&#8221; that creates @strawberry in the nested route context.</p>

<h2>Conclusion</h2>

<p>I managed to detect many bugs in my controllers doing this (especially variables that I had assumed not to be nil and that were not set). Testing controllers is easy and fast, so I strongly advise you to test your controllers!</p>

<p>Eventually, I&#8217;ll also do a post on integration testing with rspec, but for now, <a href="http://everydayrails.com/2012/04/24/testing-series-rspec-requests.html">here&#8217;s another resource from Everyday Rails that covers the topic very nicely</a>. Enjoy!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Understanding large systems on the web - interesting links]]></title>
    <link href="http://luisalima.github.com/blog/2013/01/05/understanding-large-systems-on-the-web-interesting-links/"/>
    <updated>2013-01-05T15:58:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2013/01/05/understanding-large-systems-on-the-web-interesting-links</id>
    <content type="html"><![CDATA[<p>Back when I was in undergrad, web programming was something considered to be a tad boring, unchallenging and chaotic. How things have changed during my PhD :-) I assume, largely thanks to Google and Apple, the advent of smartphones and mobile. But I digress, yet again.</p>

<p><a href="http://martinfowler.com/">Martin Fowler</a>, the father of software patterns, has <a href="http://martinfowler.com/articles/nosql-intro.pdf">an amazing introduction to nosql</a> (or, rather, &#8220;polyglot persistence&#8221;) which should remind us of why we have nosql systems: to facilitate horizontal scaling. That means that so many startups that are using nosql just because it&#8217;s fashionable should probably just use a traditional sql model and follow the &#8220;Premature optimization is the root of all evil&#8221; motto by Knuth.</p>

<p>And now jumping from backend to frontend, he also has another amazing overview of <a href="http://martinfowler.com/articles/multiMobile/">developing software for multiple mobile devices</a>. The most obvious downside to writing multiple native apps is the cost of building several copies of the same application and then, especially, maintaining them. In my case, I started learning iOS programming some time ago, but didn&#8217;t pursue it actively at the time, and am now wondering whether learning multiple platforms is a good idea, or whether I should go with a cross-platform toolkit. The overview has a list of the problems of going down that road. The solution is again&#8230; mobile web apps:</p>

<p>&#8220;Don&#8217;t try to make a web app work like a native app. This is the path to the Uncanny Valley. Instead focus on making it a usable web app, building on the familiarity of how the web works on devices.&#8221;</p>

<p>In the end, the slideshow provides a nice algorithmic way to determine whether you should go the native route, the mobile web app route, or something in between, depending on whether the app is part of your core product or just a side channel to reach more customers. Highly recommended!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Random notes on Factory Girl Part II]]></title>
    <link href="http://luisalima.github.com/blog/2013/01/04/random-notes-on-factory-girl-part-ii/"/>
    <updated>2013-01-04T20:25:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2013/01/04/random-notes-on-factory-girl-part-ii</id>
    <content type="html"><![CDATA[<p>More random/tutorialish aspects of my usage of the FactoryGirl gem. I do advise you to read their <a href="https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#associations">getting started guide</a>, it helps a lot. This is more of a reference of the things I found most useful. If you have any questions/corrections/observations, let me know in the comments.</p>

<h2>FactoryGirl and Polymorphic named associations</h2>

<p>If you have polymorphic associations with different names, here&#8217;s how to go about it in factories:</p>

<pre><code> factory :category, aliases: [:category_one, :category_two] </code></pre>


<p>There are other approaches to this problem <a href="http://stackoverflow.com/questions/7747945/factorygirl-and-polymorphic-associations">here</a>, but I find this one to be the simplest and so far it hasn&#8217;t gotten me into trouble (perhaps yet :-) ).</p>

<h2>Attributes_for</h2>

<p>This is probably very obvious, but I find it useful to use attributes_for from FactoryGirl to build the valid attributes for use in controller specs.</p>

<pre><code> def valid_attributes
    FactoryGirl.attributes_for(:my_model)
    end
</code></pre>


<p>One thing that is useful to know is that attributes_for ignores associations, and thus does not include attributes that are related to associations even if you have an association in the factory. <a href="http://stackoverflow.com/questions/10290286/factorygirl-why-does-attributes-for-omit-some-attributes">Here is a post at stackoverflow</a> that provides you with the foreign keys in the attributes if you need them.</p>

<h2>Has_and_belongs_to_many associations</h2>

<p>I&#8217;m just adding this for my own reference. It&#8217;s in the <a href="https://github.com/thoughtbot/factory_girl/wiki/Usage">FactoryGirl getting started wiki</a>.</p>

<pre><code> FactoryGirl.define do
  factory :person do
    first_name { 'James' }
    books {
      Array(5..10).sample.times.map do
        FactoryGirl.create(:book) # optionally add traits: FactoryGirl.create(:book, :book_description)
      end
    }
  end
end
</code></pre>


<p>There is more in FactoryGirl associations <a href="https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#associations">here</a>.</p>

<h2>DRYing up: Traits</h2>

<p>I just discovered traits, which are ways to add state to a factory (read: create factories whose attributes have different values without having to go through aliases). I am <a href="http://robots.thoughtbot.com/post/23673635798/remove-duplication-with-factorygirls-traits">shamelessly copying Thoughtbot&#8217;s example</a> because I think it&#8217;s the clearest one to have here for your reference:</p>

<pre><code> FactoryGirl.define do
  factory :todo_item do
    name 'Pick up a gallon of milk'

    trait :completed do
      complete true
    end

    trait :not_completed do
      complete false
    end
  end
end
</code></pre>


<h2>DRYing up: inheritance</h2>

<p>Something that I wasn&#8217;t making much use of &#8211; you can nest factories, which allows you to create multiple factories for the same class without repeating common attributes:</p>

<pre><code>
factory :user do
    name "testing"

  factory :super_user do
    super_powers true
  end
end
</code></pre>


<h2>Tracking factories</h2>

<p>I followed the advice <a href="https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#activesupport-instrumentation">here</a> to track factories. I think it&#8217;s useful to know how factories are being used.</p>

<h2>If you want to go really in depth&#8230;</h2>

<p>You have a <a href="http://www.arailsdemo.com/posts/39">very nice post on code reading of the factorygirl gem here</a>. You can even learn something about how to do cool stuff in ruby/rails like:</p>

<pre><code> def set(attribute, value)
    @instance.send(:"#{attribute}=", value)
  end
</code></pre>


<p>And that&#8217;s all, folks (for now&#8230;)!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Random pseudo-tutorialish notes on Factory Girl, part I]]></title>
    <link href="http://luisalima.github.com/blog/2013/01/04/random-notes-on-factory-girl/"/>
    <updated>2013-01-04T18:39:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2013/01/04/random-notes-on-factory-girl</id>
    <content type="html"><![CDATA[<p>The guys at Thoughtbot do an excellent job. I especially love shoulda and high_voltage. I have a love/hate relationship with factory_girl, so I decided to write this post trying to detail some of the ways I&#8217;ve been using it. Maybe it will help someone. Hope it does!</p>

<p>First of all, what is a factory and what do you need it for? Factories are a (healthier) alternative to fixtures, and fixtures are simply a &#8220;fixed state&#8221; for models, used as baseline for tests. In Rails, fixtures get automatically loaded into the test database before tests are run, and you define them in a YAML file. However, they can become awkward pretty easily, so Factories were created &#8211; frameworks designed to allow a quick creation of fully featured objects for testing.</p>

<p>And now you ask, why don&#8217;t we just create the object directly before each test? Think required attributes and associations &#8211; if your model has 10 attributes and 5 different associations you might want to have somewhere where you can create valid instances of that model upfront and then reuse them.</p>

<p>So, we have (somewhat) established what factories are for. <a href="http://blog.steveklabnik.com/posts/2012-07-14-why-i-don-t-like-factory_girl">Just beware of the downsides of using factories</a> (quoting the blog in the link, which is highly recommended):
&#8220;A big feature of tests is to give you feedback on your code. Tests and code have a symbiotic relationship. Your tests inform your code. If a test is complicated, your code is complicated. Ultimately, because tests are a client of your code, you can see how easy or hard your code&#8217;s interfaces are.&#8221;</p>

<p>Yup, I feel your pain, bro. My latest project has tons of models, polymorphic interfaces, STI&#8217;s, etc. Ultimately, flexibility creates complexity, and it&#8217;s better to avoid extra features to keep simplicity. But I digress.</p>

<p>So, how do we use these lil&#8217; bastards? :-)</p>

<h2>Configuration</h2>

<p>The <a href="https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md">Getting started</a> guide provides what you need. Except&#8230; I don&#8217;t think it&#8217;s that clear that if you are using Rspec, those lines</p>

<pre><code>
    require 'factory_girl'
    FactoryGirl.find_definitions
</code></pre>


<p>go in the spec_helper.rb file. More precisely, <em>FactoryGirl.find_definitions</em> goes inside the <em>RSpec.configure do |config|</em> block.</p>

<p><em>But:</em> if you are using Spork, you need to reload Factory definitions at each run, otherwise you get a &#8220;factory not registered&#8221; error. So, place the following inside the <em>Spork.each_run do</em> block:</p>

<pre><code>
    Spork.each_run do
    # ....
        FactoryGirl.definition_file_paths << File.join(File.dirname(__FILE__), 'factories')
      FactoryGirl.find_definitions
    # etc...
</code></pre>


<p>There you go! Everything nicely configured :-) Also, you can use FactoryGirl in rails console! Just use the code <a href="http://blog.thefrontiergroup.com.au/2011/03/reloading-factory-girl-factories-in-the-rails-3-console/">in this post</a>, which I&#8217;ll shamelessly copy here for posterity and for my own reference:</p>

<pre><code>
    ActionDispatch::Callbacks.after do
      # Reload the factories
      return unless (Rails.env.development? || Rails.env.test?)

      unless FactoryGirl.factories.blank? # first init will load factories, this should only run on subsequent reloads
        FactoryGirl.factories.clear
        FactoryGirl.find_definitions
      end
    end
</code></pre>


<p>You can also just run this code directly in the console. If you do have sequences defined, you&#8217;ll need to add the following extra:</p>

<pre><code>
FactoryGirl.factories.clear
FactoryGirl.sequences.clear
FactoryGirl.find_definitions
</code></pre>


<p>(and if you are using <a href="http://robots.thoughtbot.com/post/23673635798/remove-duplication-with-factorygirls-traits">traits</a>, apparently you need to add FactoryGirl.traits.clear as well).</p>

<h2>In unit testing</h2>

<p>So first things first&#8230; You can have a file for testing factories but I&#8217;d rather have the test for the validity of the factory in the model test file itself &#8211; this to avoid incurring errors when you are focusing on testing a particular model, all because your factory was ill-defined. So go ahead and test the factory itself:</p>

<pre><code>
    it "has a valid factory" do
        m = FactoryGirl.build(:my_model)
        m.should be_valid
    end
</code></pre>


<p>What do we do if this goes wrong and we can&#8217;t sort out the error? Go to the console, write</p>

<pre><code>
m = FactoryGirl.build(:my_model)
m.valid?  # duh, it'll be false
m.save    # just to trigger all validations callbacks etc that we might have
m.errors
</code></pre>


<p>And now go ahead and complete your factory with the necessary information. Simple enough, right? The syntax for defining factories is explained pretty well <a href="https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md">in the starting guide</a> so I won&#8217;t go to the trouble, except for the case when we are using Faker.</p>

<h2>With Faker</h2>

<p>Faker is a cool gem for&#8230; you guessed it, generating fake data. And here&#8217;s how you do it:</p>

<pre><code>
  factory :company do
    name { Faker::Company.name }
    phone {Faker::PhoneNumber.cell_phone}
  end
</code></pre>


<p>Yup, just a normal block, not too bad.</p>

<h2>Sequences</h2>

<p>Sequences are used for attributes that must be unique, such as email addresses. You can define sequences &#8220;outside&#8221; of the factory definition, or inside as a block:</p>

<pre><code>
  sequence :email do |n|
    "test#{n}@example.com"
  end

    factory :user do
        email
    password "password"
    password_confirmation "password"
    phone {Faker::PhoneNumber.cell_phone}
    end
</code></pre>


<p>is the same as:</p>

<pre><code>
    factory :user do
        sequence(:email) { |n| "test#{n}@example.com"}
    password "password"
    password_confirmation "password"
    phone {Faker::PhoneNumber.cell_phone}
    end
</code></pre>


<h2>Associations</h2>

<p>Associations in factories are simply created by adding the factory name for the association in the other factory block. For example, if my user has_one location:</p>

<pre><code>
    factory :user do
        sequence(:email) { |n| "test#{n}@example.com"}
    password "password"
    password_confirmation "password"
    phone {Faker::PhoneNumber.cell_phone}
    location   # ta-da!
    end
</code></pre>


<p>More detail about associations with FactoryGirl <a href="https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#associations">here</a>.</p>

<p>More about this tomorrow!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Good & somewhat random but focused advice for programmers]]></title>
    <link href="http://luisalima.github.com/blog/2013/01/04/good-advice-for-programmers/"/>
    <updated>2013-01-04T10:48:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2013/01/04/good-advice-for-programmers</id>
    <content type="html"><![CDATA[<p>First, the non-technical one: <a href="http://www.kalzumeus.com/2011/10/28/dont-call-yourself-a-programmer/">Don&#8217;t call yourself a programmer, and other career advice, by Patrick from Kalzumeus</a>. Although it does seem to me that some of this advice has changed very recently due to the lack of people in the field (somehow there is somewhat of a mystique about being a programmer right now, don&#8217;t you think?), the article is amazing and is filled with great advice. And it&#8217;s also filled with very funny observations about academia&#8230; Spoiler alert: &#8220;(After you’ve escaped the mind-warping miasma of academia, you might rightfully question whether Published In A Journal is really personally or societally significant as opposed to close approximations like Wrote A Blog Post And Showed It To Smart People.)&#8221;. Yup, been wondering about that myself.</p>

<p>And <a href="http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html">this is scary</a>. I wonder whether this is really true? I think that all the developers I know would pass the FizzBuzz test. Now I feel very much tempted to have my students code this in the next lesson&#8230; Mmmmmhmmmm&#8230;</p>

<p>On the same lines, from the creator of Rails himself, <a href="http://37signals.com/svn/posts/3375-the-five-programming-books-that-meant-most-to-me">here is a short list of (5) books</a> that help you program better. I think I&#8217;m going to order some of those. Probably the Smalltalk Best Practice Patterns. Look, <a href="http://web.cecs.pdx.edu/~black/OOP/slides/Best%20Practice%20I.pdf">here is an amazing set of slides based on the book to help me make up my mind</a>: &#8220;Remember: the purpose of education is to save you from having to think&#8221; ROFL!</p>

<p>Yes, 70% of the time is spent on finding the intent of other programmers. And that&#8217;s probably because we don&#8217;t read enough code, because it&#8217;s more fun to just jump ahead at the problem and play with code than to try to figure out how things are done (guilty&#8230;). Along those lines, <a href="http://blog.mojotech.com/post/38317154050/code-with-clarity">here is a cool article on &#8220;Code with Clarity&#8221;</a>, where you have a step-by-step deconstruction of a very small function. And somewhat along those lines as well, <a href="http://rubysource.com/cracking-gems-reading-ruby-code/">here is a cool article on cracking gems and reading ruby code</a>.</p>

<p>And finally, lately I have met programmers that fit into two sets: the first set is the linux hacker set. These guys are extremely fluent in all things linux-y and could probably code a whole OS in shell scripting. The second set tends to stay away from the shell like it&#8217;s the devil, they love IDEs, and think that man pages are ugly. So here is a little something for the second set: <a href="http://net.tutsplus.com/tutorials/tools-and-tips/essential-command-line-tools-for-web-developers/">essential command line tools for web developers</a> (and not just web developers). Terribly useful to know your way around a shell, you&#8217;ll need it someday.</p>

<p>And I think that&#8217;s it for the day. Now on to read &amp; write some code&#8230;</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Raspberry pi GPIO for beginners]]></title>
    <link href="http://luisalima.github.com/blog/2013/01/03/raspberry-pi-gpio-for-beginners/"/>
    <updated>2013-01-03T10:23:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2013/01/03/raspberry-pi-gpio-for-beginners</id>
    <content type="html"><![CDATA[<p>Me and my boyfriend offered his father (an amateur electronics afficcionado) this Christmas the best gift ever (we think) &#8211; a Raspberry Pi!</p>

<p>But now I&#8217;ve been struggling with finding some true beginner tutorials for people who are new to Linux, don&#8217;t know that much English, and have an interest on using the Pi for more advanced controller stuff. So here is the list that I found so far &#8211; I&#8217;ll be adding more links as time comes (and possibly translating some stuff to Portuguese, so if you are interested in that, stay tuned)!</p>

<h2>From Adafruit learning system</h2>

<p>They have extremely nice (I mean <em>extremely</em> nice) step-by-step tutorials, exactly with the focus I wanted. Just <a href="http://learn.adafruit.com/search?q=raspberry">perform a search by &#8220;raspberry&#8221; on their site</a> and you&#8217;ll find:</p>

<ul>
<li><a href="http://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-gpio">GPIO setup on the Raspberry Pi</a></li>
<li><a href="http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds">Raspberry Pi E-mail notifier using LEDs</a></li>
<li><a href="http://learn.adafruit.com/reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi">Reading analog inputs with the Raspberry Pi</a></li>
<li><a href="http://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-pi">Playing sounds and using buttons with the Raspberry Pi</a></li>
</ul>


<h2>Other sources</h2>

<p>This <a href="http://www.raspberrypi.org/archives/2953">video tutorial from Geek Gurl Diaries</a> is also pretty neat to learn something more.</p>

<p>I&#8217;ll be doing some of these projects myself and translating them to PT as I have the time. My ultimate goal is to be able to control the lights and blinders in my house with the Pi and to do some sort of home automation smarts but for now, I think I&#8217;ll stick to LEDs and not to 220V stuff :-) I also want to do some sort of intrusion detection system. Stay tuned!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Random cool links]]></title>
    <link href="http://luisalima.github.com/blog/2013/01/03/random-cool-links/"/>
    <updated>2013-01-03T00:23:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2013/01/03/random-cool-links</id>
    <content type="html"><![CDATA[<p>An assortment of random cool links I&#8217;ve been gathering during the holiday break (even in my glorious horsey days in Alentejo).</p>

<h2>Iphone and app stuffs</h2>

<ul>
<li><p><a href="https://www.brewster.com">This app</a>, if it lives up to its promises, has everything that I&#8217;ve been wishing for and more. Basically, it&#8217;s a smarter contact manager that can aggregate not only contacts from your contact list, but also from social networks, it keeps track of how much you communicate with your contacts, spots trends, can create smart lists and take advantage of the information that you have in social networks to perform smarter searches. The interface is pretty awesome as well. My only complaint is that for now, I cannot add my Facebook contacts. When I do it, it completely resets the settings in the app (that&#8217;s the iphone app). Maybe that&#8217;s just a problem with my particular system, but I&#8217;m looking forward to seeing this working in my iphone, ipad (app hasn&#8217;t been released yet) and desktop (ahhh&#8230; automatic lists for sending e-mails in Xmas)!</p></li>
<li><p>If your iphone home button is unresponsive (mine becomes from time to time, randomly so), <a href="http://howto.cnet.com/8301-11310_39-57524891-285/four-ways-to-fix-an-unresponsive-iphone-home-button/">here are four ways to fix it by our friends at c|net</a>.</p></li>
</ul>


<h2>Web development tutorials and stuffs</h2>

<p><a href="http://net.tutsplus.com/articles/general/what-was-hot-in-2012-month-by-month/">This link</a> has an assortment of the &#8220;best&#8221; nettuts tutorials, released month by month. The most interesting for me seem to be the &#8220;learn jQuery in 30 days&#8221;, closures with javascript, mobile-first responsive web design, javascript design patterns, and gem creation with bundler. For those that want to know more about topics such as agile development and vagrant (highly recommended) there&#8217;s something for every taste as well! I&#8217;ve been procrastinating learning more on frontend development because I find the backend problems way more interesting&#8230; I hope to follow these tutorials to learn more about these topics.</p>

<h2>Fourier transform</h2>

<p>Yup, I wish I had taken a signal processing class during undergrad. But I didn&#8217;t. And this <a href="http://betterexplained.com/articles/an-interactive-guide-to-the-fourier-transform/">interactive guide to the Fourier transform</a> is a lovely start for those like me who didn&#8217;t have the wisdom to figure out the stuff that they needed on their own during undergrad. Amazing explanation!</p>

<h2>Three Crowdsourcing Projects that are Advancing the Medical Field</h2>

<p>I really really believe in the power of crowdsourcing for advancing our knowledge of medicine and of the human body. Medicine as it is practiced nowadays seems so&#8230; retrogad. As a friend of mine put it, the best doctors for ourselves are ourselves. There are so many things that doctors just cannot know, so many new weird conditions&#8230; So why not harness the power of the people? That&#8217;s what <a href="http://dailycrowdsource.com/crowdsourcing/projects/1059-3-crowdsourcing-projects-that-are-advancing-the-medical-field">these crowdsourcing projects</a> aim to to.</p>

<h2>Security random stuffs</h2>

<p>A very <a href="http://spectrum.ieee.org/podcast/computing/software/computers-its-time-to-start-over">cool article/podcast</a> interviewing Robert Watson about the shifts in computer security, especially from an OS security and exploits point of view.</p>

<p>An app that attempts to mitigate the side effects of these problems (such as accessing the data in your phone) is <a href="http://www.remotium.com">remotium</a>, where a good friend of mine (José Luís Pereira) is working. Do check it out &#8211; they have a cool video of an android exploit being performed live.</p>

<h2>Clipboard buffering for OS X</h2>

<p>How can you live without it?</p>

<p>I chose <a href="https://itunes.apple.com/us/app/flycut-clipboard-manager/id442160987?mt=12">flycut</a>, which is based on <a href="http://jumpcut.sourceforge.net">jumpcut</a> but seems to be updated more often (as we can see by the choice of github and not sourceforge :-) ). Highly recommended, you&#8217;ll never know how you could live without it.</p>

<h2>Really assorted links</h2>

<ul>
<li><p>One topic that really comes to my mind often, especially now that I teach at the University, is how much college is worth exactly. <a href="http://threads2.scripting.com/2012/december/siliconValleyIsWrongAboutCollege">This article</a> gives a nice blunt opinion on why college still matters (but to be honest, I do think that we should not go straight to college out of high school &#8211; not because we don&#8217;t know what we want exactly, but also because we don&#8217;t know how to appreciate and select the most important topics for us to focus on. I view my undergraduate experience completely differently as of today!).</p></li>
<li><p>Another proof that only execution matters is <a href="http://www.forbes.com/sites/jjcolao/2012/10/15/hounded-by-recruiters-coders-put-themselves-up-for-auction/">DeveloperAuction</a>. I had something similar to this idea years ago&#8230; but of course, never executed on it, and it doesn&#8217;t mean that I would be any successful if I tried ;-) The article is very interesting. The &#8220;world&#8221; is very different for developers engineers in the US!</p></li>
</ul>


<h2>Call for help</h2>

<p>If anyoone has any idea of why the code in my gists appears centered and not nicely indented as nature intended it to be, please let me know. I am using Octopress and Slash, and the gists are stored in Github (where they do indeed appear nicely indented).</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Intriguing behavior: validating presence of a boolean field]]></title>
    <link href="http://luisalima.github.com/blog/2012/12/20/intriguing-behavior/"/>
    <updated>2012-12-20T20:15:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2012/12/20/intriguing-behavior</id>
    <content type="html"><![CDATA[<p>So, for those who don&#8217;t know it (I just discovered about it), if you want to validate the presence of a boolean field, you can&#8217;t do it with validates_presence_of.</p>

<p>So, suppose that we have a model like</p>

<pre><code>
# == Schema Information
# Table name: Bla
#  test       :boolean
class Bla < ActiveRecord::Base
  attr_accessible :test
</code></pre>


<p>For example, if I had used validates_presence_of :test in the example above, it would have failed miserably because false.blank? is true &#8211; so in practice, we could have never set bla.test to false, since it would make the validation fail (<a href="http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates_presence_of">more details here</a>).</p>

<p>So I set out to try this out:</p>

<pre><code>
validates_inclusion_of :test, :in => [true, false]
</code></pre>


<p>and test it like this:</p>

<pre><code>
it {should_not allow_value("bla").for(:completed)}
</code></pre>


<p>and it still failed miserably.</p>

<p>Now I was puzzled&#8230; So I tried this one out:</p>

<div><script src='https://gist.github.com/4348179.js?file=bla.rb'></script>
<noscript><pre><code># == Schema Information
#
# Table name: Bla
#  test       :boolean

class Bla &lt; ActiveRecord::Base
  attr_accessible :test
  before_validation :test_present
  def test_present
    puts &quot;TEST PRESENT&quot;
    puts self.test
    errors.add(:test, &quot;Must be set to true or false&quot;) if !([true, false].include?(self.test))
  end
  
# Same behavior with validate, before_save, before_validation:
b = Bla.new
b.test = &quot;bla&quot;
b.save
# prints
# TEST PRESENT
# false

# found out about this while testing
#   it {should_not allow_value(:other).for(:completed)}
# I know that I don't need to test that the boolean will be true or false, the db does it for me...
# but I'm wondering... does the test attribute ever get to be &quot;bla&quot;?
</code></pre></noscript></div>


<p>I was thinking that at some point the test attribute would be set to &#8220;bla&#8221;, but since the field is a boolean, inserting other values will always evaluate to false. So we don&#8217;t have to go to all this trouble: this way we can validate the presence, but we never need to validate whether the value itself is valid. More about this <a href="http://stackoverflow.com/questions/5170008/rails-validating-inclusion-of-a-boolean-fails-tests/5171074#5171074">here</a>. Living and learning!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How I test part III: Why rspec?]]></title>
    <link href="http://luisalima.github.com/blog/2012/12/20/how-i-test-part-iii/"/>
    <updated>2012-12-20T16:48:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2012/12/20/how-i-test-part-iii</id>
    <content type="html"><![CDATA[<p>Why RSpec? That is a good question. My main reason for using RSpec right now is because I want to have a testing + documentation + TODO bundle, all in one, like a shampoo:</p>

<ul>
<li>testing &#8211; ok, RSpec does make things a tad more complicated for testing. I believe that testing actually makes you learn more about what you code, and RSpec, being a DSL, can make things esoteric enough that sometimes you don&#8217;t know exactly what you&#8217;re testing (I confess that I like Test::Unit more because it&#8217;s simpler for developers to read &#8211; assert that this gives this result). But, on the other hand, you have shoulda! :-)</li>
<li>documentation &#8211; the exact same thing that makes testing a bit more esoteric, makes documentation a piece of cake. You want to know how something works? Just read the tests. And RSpec does make that side of testing much clearer.</li>
<li>TODO &#8211; yup, that&#8217;s right. If you have features that you want to implement later on, just add a &#8220;pending&#8221; spec with the description of what you want to do. Of course that you can do that in Test::Unit as well, but it&#8217;s not as beautiful and immediate as in RSpec.</li>
</ul>


<p>Of course that I&#8217;m really just beginning and I might change my mind eventually. <a href="http://www.rubyinside.com/dhh-offended-by-rspec-debate-4610.html">Even the creator of Rails argues against RSPec</a>, and there are quite a few arguments against it (if you missed the link, here&#8217;s an <a href="http://objectreload.com/articles/2010/09/thoughts-on-testing-part-1.html">excellent blog post on that</a>).</p>

<p>By the way, I&#8217;m abiding by this rule from the last post:</p>

<p>&#8221;- Keep things flat — deeply nested contexts are ugly as they hinder readability of your test cases. Split your tests up into different files if they’re getting too long.&#8221;</p>

<p>I was trying to make things DRY in the tests, and then I thought&#8230; why bother, exactly? It&#8217;s better if they are more readable, for the sake of documentation as stated above. Amen, brothers.</p>

<h2>Why not Cucumber for request specs?</h2>

<p>All the reasons are in this <a href="http://epimetrics.com/entries/3129">excellent post</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How I test part II - testing models]]></title>
    <link href="http://luisalima.github.com/blog/2012/12/19/how-i-test-part-ii/"/>
    <updated>2012-12-19T10:17:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2012/12/19/how-i-test-part-ii</id>
    <content type="html"><![CDATA[<h2>Testing models</h2>

<p>From <a href="http://guides.rubyonrails.org/testing.html">rails guides</a>:</p>

<p>&#8220;Ideally, you would like to include a test for everything which could possibly break. It’s a good practice to have at least one test for each of your validations and at least one test for every method in your model.&#8221;</p>

<p>Which leads me to&#8230;</p>

<h2>Why I use shoulda</h2>

<p>I think that the best thing is just to take a peek at <a href="http://cheat.errtheblog.com/s/rspec_shoulda/">The shoulda cheat sheets</a>.</p>

<p>You actually get a guide of what to test by looking at the cheat sheet! It&#8217;s too bad that it&#8217;s not up to date (actually I think that the whole site is not up to date &#8211; even the gem doesn&#8217;t work anymore, which I think is too bad :-( )</p>

<p>Furthermore, it&#8217;s great for lazy people and it&#8217;s definitely easier on the eyes. Just check the following example:</p>

<div><script src='https://gist.github.com/4339197.js?file=shoulda_model.rb'></script>
<noscript><pre><code># without shoulda... tons of code :-)
it &quot;should belong to person&quot; do
    Model.reflect_on_association(:person).macro.should == :belongs_to
end

# with shoulda... one-liner!
it{should belong_to(:person)}
</code></pre></noscript></div>


<p>Convinced? ;-)</p>

<h2>What I test exactly with shoulda</h2>

<p>I go through the whole list of shoulda macros for models, and I just use and abuse them. Since the cheat sheet is not up to date, I created a new one (just for models for now) at:</p>

<div><script src='https://gist.github.com/4339490.js?file=shoulda_cheat_model.rb'></script>
<noscript><pre><code># updated from the original @ http://cheat.errtheblog.com/s/rspec_shoulda
# just a subset -- models -- is included here. I'll update this, and create cheat sheets for others, as I go along.
# I marked the ones I added with NEW and also added the links to the corresponding code, as I think it's useful.
# Any comments/corrections are welcome!

# =================  Data and Associations =======================
# https://github.com/thoughtbot/shoulda-matchers/tree/master/lib/shoulda/matchers/active_record

it { should_not have_db_column(:admin).of_type(:boolean) }
it { should have_db_column(:salary).
      of_type(:decimal).
      with_options(:precision =&gt; 10, :scale =&gt; 2) }
it { should have_readonly_attributes(:password) }
it { should belong_to(:parent) }
it { should have_db_index(:id) } # NEW

it { should have_many(:friends) }
it { should have_many(:enemies).through(:friends) }
it { should have_many(:enemies).dependent(:destroy) }

it { should have_one(:god) }

it { should have_and_belong_to_many(:posts) }

it {should accept_nested_attributes_for(:user)} # NEW

# ================== Validation Matchers ============================
# https://github.com/thoughtbot/shoulda-matchers/tree/master/lib/shoulda/matchers/active_model

it { should validate_uniqueness_of(:keyword) }
it { should validate_uniqueness_of(:keyword).with_message(/dup/) }
it { should validate_uniqueness_of(:email).scoped_to(:name) }
it { should validate_uniqueness_of(:email).
              scoped_to(:first_name, :last_name) }
it { should validate_uniqueness_of(:keyword).case_insensitive }

it { should validate_presence_of(:name) }
it { should validate_presence_of(:name).
              with_message(/is not optional/) }

it { should validate_numericality_of(:age) }

it { should validate_format_of(:name).
              with('12345').
              with_message(/is not optional/) }
it { should validate_format_of(:name).
              not_with('12D45').
              with_message(/is not optional/) }

it { should validate_acceptance_of(:eula) }

it { should ensure_length_of(:password).
              is_at_least(6).
              is_at_most(20) }
it { should ensure_length_of(:name).
              is_at_least(3).
              with_short_message(/not long enough/) }
it { should ensure_length_of(:ssn).
              is_equal_to(9).
              with_message(/is invalid/) }

it { should ensure_inclusion_of(:age).in_range(0..100) }
it { should_not allow_mass_assignment_of(:password) }
it { should allow_mass_assignment_of(:first_name) }
it { should validate_format_of(:first_name).with(&quot;Carl&quot;) }

it { should validate_confirmation_of(:password) } # NEW</code></pre></noscript></div>


<p>Yes, it&#8217;s a bit tedious. But (1) you make sure that you are actually testing all these details, which is extremely useful when you are constantly refactoring your code, and (2) it&#8217;s not that slow with the multiple cursors of Sublime Text 2. You just need to have a scaffold.</p>

<h2>What I test without shoulda</h2>

<p>Apart from model methods, is there that much to test without shoulda? I test:</p>

<ul>
<li>Any model methods, extensively</li>
<li>Callbacks, extremely extensively</li>
<li>Some validations - but just to make sure I coded them right. See more of this perspective in <a href="http://www.christopherbloom.com/2011/07/12/testing-model-validations-in-rspec-the-short-and-sweet-way/">Testing model validations in rspec the short and sweet way</a></li>
</ul>


<p>&#8230; and for now, that&#8217;s about it.</p>

<h2>Regarding generating fake data</h2>

<p>Like I mentioned before, I do use FactoryGirl extensively. Maybe that means that <a href="http://blog.steveklabnik.com/posts/2012-07-14-why-i-don-t-like-factory_girl">my models are complicated</a>? I don&#8217;t know, but for now, I am sort of using good sense and stuff. Let&#8217;s see what happens. I do use <a href="http://faker.rubyforge.org">Faker</a> as well. Come to think about it, will do a post on that eventually.</p>

<h2>Random issues in testing models</h2>

<p>When everything seems to be absolutely correct in the models and in the tests and you are still getting unexplainable errors, do restart guard. I already spent several hours messing around with the tests because guard didn&#8217;t refresh the models. <a href="http://stackoverflow.com/questions/5855587/spork-is-really-great-but-how-can-i-get-it-to-refresh-validations-and-other-cod">There are automatic solutions</a>, but I didn&#8217;t feel like hacking away the spork/guard configurations again&#8230; yet.</p>

<h2>Random resources</h2>

<ul>
<li><p><a href="http://everydayrails.com/2012/03/19/testing-series-rspec-models-factory-girl.html">Everyday Rails: How I learned to test my Rails applications, Part 3: Model specs</a> - this has a very nice overview of the actual steps required to test models. Now I just cheat using shoulda, but it&#8217;s very nice and useful to get a</p></li>
<li><p><a href="http://www.christopherbloom.com/2011/07/12/testing-model-validations-in-rspec-the-short-and-sweet-way/">Testing model validations in rspec the short and sweet way</a> - it&#8217;s a bit outdated, but describes how to test validations in a short way.</p></li>
</ul>


<hr />

<p>Again, as I already mentioned, I am a beginner to rails and, consequently, in testing rails, so I might be doing/saying terribly wrong things. Any input is very much appreciated :-)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Inspiring TED talk]]></title>
    <link href="http://luisalima.github.com/blog/2012/12/18/inspiring-ted-talk/"/>
    <updated>2012-12-18T11:06:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2012/12/18/inspiring-ted-talk</id>
    <content type="html"><![CDATA[<p>For anyone who works in business (especially startups), this is most probably (or most definitely) the most important TED talk that you&#8217;ll ever get to watch. Or the most important talk/piece of advice that you&#8217;ll ever get, I believe.</p>

<p><a href="http://www.ted.com/talks/simon_sinek_how_great_leaders_inspire_action.html">How great leaders inspire action</a></p>

<p>The fact is that this is what attracts me (and many people, probably) to startups. It&#8217;s just easier to find this sharing of common beliefs &#8211; and if it&#8217;s not, it&#8217;s the wrong startup.</p>

<p><strong>People don&#8217;t buy what you do, they buy why you do it.</strong></p>

<p>People buy what you believe in. Somehow this makes me believe in mankind a bit more :-)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Analytics js & a mash of interesting rails stuff]]></title>
    <link href="http://luisalima.github.com/blog/2012/12/14/analytics-js/"/>
    <updated>2012-12-14T13:34:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2012/12/14/analytics-js</id>
    <content type="html"><![CDATA[<h2>Analytics.js</h2>

<p>I don&#8217;t know whether this library is very common, but it definitely shows a lot of promise:</p>

<p><a href="http://segmentio.github.com/analytics.js/">Segmentio&#8217;s Analytics.js</a></p>

<p>It provides provider-agnostic hooks to route to analytics services. They also have a hosted version where the analytics services can be changed without touching the code. Looks really clean and neat.</p>

<h2>Nice video: to_proc</h2>

<p><a href="http://www.youtube.com/watch?v=aISNtCAZlMg">This video by CooperPress</a> explains in a very clear fashion how to use the &#8220;to_proc&#8221; feature of Ruby (and understand some weird code you might come across). It is also well worth it to subscribe to <a href="http://www.youtube.com/user/CooperPress?feature=watch">their other videos</a>.</p>

<h2>Faster Rails partial rendering and caching</h2>

<p>I sure will try out <a href="http://ninjasandrobots.com/rails-faster-partial-rendering-and-caching/">this gem (multi_fetch_fragments)</a> to optimize Rails when I get to that point of my work&#8230;</p>

<h2>Indexes in Rails databases</h2>

<p><a href="http://tomafro.net/2009/08/using-indexes-in-rails-index-your-associations">Extremely good article by Tom Ward on using indexes in rails associations</a>. An excellent guide to determine what indexes your app should include (no more, no less).</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How I test - Part I: Environment]]></title>
    <link href="http://luisalima.github.com/blog/2012/12/14/how-i-test/"/>
    <updated>2012-12-14T11:43:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2012/12/14/how-i-test</id>
    <content type="html"><![CDATA[<p>Not that I am an authority in testing&#8230; this is just a compilation of the so-called &#8220;best practices&#8221; that I&#8217;ve been seeing around, as well as some extra tips from my (very limited) experience. I&#8217;d love to hear the feedback from more experienced people&#8230; I&#8217;m going to do several of these as I go along.</p>

<h2>Gems I use</h2>

<p>I test using <a href="http://rspec.info">rspec</a> and <a href="https://github.com/thoughtbot/shoulda">shoulda</a>, which dramatically reduces the size of the tests. But beware &#8211; rspec is a DSL, and I think that I only got a bit more intuition on what I was exactly testing after doing some tests in Test::Unit. I also use <a href="https://github.com/thoughtbot/factory_girl">FactoryGirl</a> for setting up the test data, instead of fixtures. Last but not least, the <a href="https://github.com/colszowka/simplecov">simplecov</a> gem is amazing, and although you shouldn&#8217;t rely on it to determine when to stop testing, it really is good to get a (very precise) sense of what&#8217;s covered and what&#8217;s not in their beautiful html reports.</p>

<h2>General tips</h2>

<p>When all your tests are red for no apparent reason, it&#8217;s because you were messing around with the db and then forgot to do</p>

<pre><code>rake db:test:clone</code></pre>


<p>Yup, the reason I am mentioning it here even though it seems completely obvious is because I am distracted and do that a lot&#8230; :-)</p>

<h2>Tips for running guard/spork in vagrant</h2>

<p>I use vagrant to keep my environment all neatly in one place. However, there are downsides. When running guard inside the guest OS, use</p>

<pre><code>bundle exec guard -p</code></pre>


<p>Otherwise, guard won&#8217;t detect changes in the guest filesystem.</p>

<h2>FactoryGirl tips</h2>

<p>You can keep all factories in one file, for me that is more intuitive (and also, I don&#8217;t have to keep opening files while I&#8217;m testing, it&#8217;s all in one place.)</p>

<p>To try FactoryGirl methods in the console, run:</p>

<pre><code>FactoryGirl.find_definitions</code></pre>


<p>At least in my case, I would get a <a href="http://stackoverflow.com/questions/8409787/factorygirl-factory-not-registered-user-argumenterror">factory not registered</a> error when I didn&#8217;t do that.</p>

<h2>Better Errors</h2>

<p>Not exactly related to testing, but <a href="https://github.com/charliesome/better_errors">this gem</a> has been a life changer! When there is an error, the standard error page gets replaced by a much better and useful error page, which has a full trace, the local and instance variables that are set at the time, and a terminal that you can use to inspect and make changes to the variables in the models and controllers in real time (or code whatever you want). I know I already mentioned it in another post, but I can&#8217;t stress enough how cool this gem is!</p>

<p>In the next posts, I&#8217;ll give an overview of how I test the models and controllers, which I only started testing this week!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Rails tip #2 - less Rspec in more color]]></title>
    <link href="http://luisalima.github.com/blog/2012/12/14/rails-tip-number-2-less-rspec-in-color/"/>
    <updated>2012-12-14T11:04:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2012/12/14/rails-tip-number-2-less-rspec-in-color</id>
    <content type="html"><![CDATA[<p>I usually have Guard running in the background with Spork, but when I&#8217;m writing tests I like to do it the old fashioned way and be able to do rspec some_test | less. The problem with that approach is that we get a monochrome version of our tests. In order to redirect the color in the pipe, edit spec_helper and add the following:</p>

<pre><code> # spec/spec_helper.rb
Rspec.configure do |config|
    config.tty = true
    config.color_enabled = true
    # ... whatever configurations you might have here...
end
</code></pre>


<p>And enjoy a more colorful life ;-)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Rails tip #1 - environment variables]]></title>
    <link href="http://luisalima.github.com/blog/2012/12/14/rails-tip-number-1-environment-variables/"/>
    <updated>2012-12-14T10:24:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2012/12/14/rails-tip-number-1-environment-variables</id>
    <content type="html"><![CDATA[<p>This isn&#8217;t exactly my tip (well, since this is a learning / work in progress blog, none of them are), but when an article is already as comprehensive as this one, there&#8217;s nothing left to say except:</p>

<p><a href="http://railsapps.github.com/rails-environment-variables.html">Here&#8217;s a very nice article on the options for setting environment variables in Rails</a></p>

<p>Why would you want to set environment variables in rails? There are some examples in the article, but for instance, setting passwords to access services. They should not be in any file (nor tracked in git), so that other people cannot access them. One way is to set Unix environment variables, the other way &#8211; which actually sounds like a &#8220;cleaner way&#8221; of organizing things &#8211; is to set them from Rails itself by taking the values from a yaml file.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Controlling a Rapberry Pi via VNC without keyboard]]></title>
    <link href="http://luisalima.github.com/blog/2012/12/14/controlling-a-rapberry-pi-via-vnc-without-keyboard/"/>
    <updated>2012-12-14T09:45:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2012/12/14/controlling-a-rapberry-pi-via-vnc-without-keyboard</id>
    <content type="html"><![CDATA[<p>I don&#8217;t have a USB keyboard, so my only option for controlling my Raspberry Pi is via SSH. That turned out to work quite well, since the customized image of Debian provided by Raspberry Pi (<a href="http://www.raspberrypi.org/downloads">2012-10-28-wheezy-raspbian.img</a>) already has ssh enabled by default. It suffices to discover the IP address that was assigned by the router, and connect :-)</p>

<p>(By the way, for those with Mac OS, the command line instructions <a href="http://elinux.org/RPi_Easy_SD_Card_Setup#Copying_an_image_to_the_SD_Card_in_Mac_OS_X_.28command_line.29">here</a>) are very nice and comprehensive.</p>

<p>So now on to the cool stuff&#8230; Initally I had installed TightVNC, but could not connect to the same session as being displayed via HDMI (which is console 0). I don&#8217;t know whether that&#8217;s possible with TightVNC, but I sort of got tired of trying. So <a href="http://www.raspberrypi.org/phpBB3/viewtopic.php?p=108862#p108862">here are the steps to make it work</a>, which basically consist of installing x11vnc instead of TightVNC and having your desktop manager autostart it:</p>

<pre><code>
sudo apt-get install x11vnc
11vnc -storepasswd

# to create the autostart entry, do:

cd .config
mkdir autostart
cd autostart
nano x11vnc.desktop

# paste following text:

[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=X11VNC
Comment=
Exec=x11vnc -forever -usepw -display :0 -ultrafilexfer
StartupNotify=false
Terminal=false
Hidden=false

</code></pre>


<p>&#8230; aaand it works :-) For more details <a href="http://www.raspberrypi.org/phpBB3/viewtopic.php?p=108862#p108862">check the original link</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Installable Web Apps]]></title>
    <link href="http://luisalima.github.com/blog/2012/12/13/installable-web-apps/"/>
    <updated>2012-12-13T17:08:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2012/12/13/installable-web-apps</id>
    <content type="html"><![CDATA[<p><a href="http://www.technologyreview.com/view/508741/installable-web-apps-will-be-the-next-tech-battleground/">Very interesting remark from MIT Technology Review</a> on how installable web apps will be the next tech background. To be honest&#8230; Hope not. This should be the next big thing, especially for developers, who can make their apps ubiquitous with a little extra effort&#8230; It will be interesting to see Apple&#8217;s reaction, though.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Rack protection - another cool gem]]></title>
    <link href="http://luisalima.github.com/blog/2012/12/13/rack-protection-another-cool-gem/"/>
    <updated>2012-12-13T14:01:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2012/12/13/rack-protection-another-cool-gem</id>
    <content type="html"><![CDATA[<p>This one I haven&#8217;t evaluated yet, but looks promising: <a href="http://rkh.github.com/rack-protection/">Rack Protection</a>, to prevent typical web attacks such as <a href="https://www.owasp.org/index.php/Cross-Site_Request_Forgery">cross site request forgery</a>, <a href="https://www.owasp.org/index.php/Cross_site_scripting">cross site scripting</a>, <a href="https://www.owasp.org/index.php/Clickjacking">clickjacking</a>, directory traversal, <a href="https://www.owasp.org/index.php/Session_hijacking_attack">session hijacking</a> and IP spoofing.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Grooveshark media center with raspberry pi]]></title>
    <link href="http://luisalima.github.com/blog/2012/12/13/grooveshark-media-center-with-raspberry-pi/"/>
    <updated>2012-12-13T12:09:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2012/12/13/grooveshark-media-center-with-raspberry-pi</id>
    <content type="html"><![CDATA[<p>&#8230; and better yet: in the command line. No more messing around with slooow browsers in the Pi.</p>

<p>First, install mplayer if you don&#8217;t already have it:</p>

<pre><code> sudo apt-get install mplayer </code></pre>


<p>Then, open ~/.mplayer/config and <a href="http://martin.ankerl.com/2006/06/10/pimp-my-mplayer/">pimp your player</a> by adding the following settings:</p>

<pre><code> cache=8192
cache-min=4 </code></pre>


<p>Next, clone the <a href="https://github.com/drakedevel/gsclient">gsclient plugin</a> by <a href="https://github.com/drakedevel">drakedevel</a>:</p>

<pre><code> 
git clone https://github.com/drakedevel/gsclient.git</code></pre>


<p>And now&#8230; RoCk and RoLL :-)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Better errors gem]]></title>
    <link href="http://luisalima.github.com/blog/2012/12/13/better-errors-gem/"/>
    <updated>2012-12-13T11:29:00+00:00</updated>
    <id>http://luisalima.github.com/blog/2012/12/13/better-errors-gem</id>
    <content type="html"><![CDATA[<p>Yes, today I am in &#8220;documentation&#8221; mode, which means &#8220;bloggation&#8221; mode :-)</p>

<p>Just installed this gem &#8211; <a href="https://github.com/charliesome/better_errors">Better errors</a>. It is a gem meant to help you out in development mode that replaces the standard Rails error page with a wayyyy better and more useful error page, featuring full stack trace, source code inspection for all stack frames, local and instance variable inspection, etc.</p>

<p>I&#8217;m going to try it out today, but definitely beats the time spent looking at development logs (even if they are as clean and contained as mine after doing the changes described in <a href="http://luisalima.github.com/blog/2012/12/13/disabling-nagging-logs-in-rails">this post</a>).</p>
]]></content>
  </entry>
  
</feed>
