Archive for the ‘code’ Category

|

Raising the Dead

Friday, September 18th, 2009

If you run in to the situation where you (or someone you’re working with) has deleted files that shouldn’t have been deleted (like a stylesheet that gets used in only one place within a very large project, say), you can recover it easily (assuming you’re using Git, of course). Simply find when the file was deleted (this is just one way to do it, of course):

git log -- path/to/file

This method will show all commits for that file – sort of like a log-view of git blame

Then, when you have the commit you want, find a commit prior to that (it’s easiest if you use a tool like GitX or GitK, which comes with git, to view the tree’s history), grab that commit’s SHA, and run the following command:

git checkout [SHA] -- path/to/file

Then commit, and you’re good to go!

Note that you can solve this problem using git rebase, but that’ll flatten your tree. This adds a new commit at the head of your tree, and should be less likely to cause any conflicts or weird history issues.

Permalink: http://panpainter.com/p/8

Tags:
Posted in Tips and Tricks, code | No Comments »

Git Demote

Friday, August 28th, 2009

I’m a big fan of Trevor Squires’ Git Promote script, but one thing that’s bothered me about it since I started using it is that it tends to clutter up my .git/config file. So, I made a counter-script for it that I’m calling, appropriately, “git demote”

    #!/bin/sh
    #
    # Counter-script to Travis Squires' git-promote script
    # (http://hoth.entp.com/2008/11/10/improving-my-git-workflow)
    # Removes a 'promoted' branch from the git config file.

curr_branch=$(git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||')

git config --remove-section "branch.${curr_branch}"

To install it, simply create a file called $HOME/bin/git-demote (assuming you have $HOME/bin in your PATH, otherwise place it somehwere that does exist in your PATH), then run the following from the terminal:

chmod 755 $HOME/bin/git-demote

Now, when you’re done with a branch, you can clean up your .git/config file by running:

git checkout topic-branch
git demote

And you’re good to go.

You can fork the Gist, if you don’t want to type all that.

Shortlink: http://panpainter.com/p/7

Tags: ,
Posted in code, development | No Comments »

Stubbing/Mocking for ThinkingSphinx and will_paginate using Flexmock

Friday, August 28th, 2009

One of the things that I love about Rails development is the emphasis on Test Driven Design (and BDD, but that’s another topic entirely). As someone relatively new to Rails, however, it’s occasionally frustrating to attempt to build tests when you don’t full know what to expect back (not knowing what to expect as a returned value, being dependent on an outside resource like an API, whatever).

Nothing has been as frustrating for me as learning about mocking/stubbing.  It’s one of those things that everyone else just seems to know, but rarely talks about.  Maybe I’m just looking in the wrong places; I’m not sure.  Either way, I was recently working on adding in ThinkingSphinx to a project that I was working on, and I wanted to make sure that my implementation of the plugin was being tested (not, of course, testing ThinkingSphinx itself, because it’s already tested in its own right).

Of course, anything that runs through the plugin is eventually going to want to hit the search daemon, which is a dependency that our test suite shouldn’t have.  This is easy enough to overcome with a quick mock (if you know the right magic incantation, that is).

After some searching, I settled on Flexmock as the framework of choice, if for no more reason than the fact that it has better documentation than some of the others I’ve found.  Here’s the test that I wrote that just made sure that search would occur given a few different situations. (Note that this is done using Test/Unit.)

First I had to make sure that will_paginate would cooperate with me, so I created a view_helper.rb file in /test/mocks/test/ to set the total_pages_for_collection method to just return “1″ (because we really don’t care how many pages there are in the collection, and running anything against a view without this being set throws an error):

# /test/mocks/view_helpers

require 'will_paginate/core_ext'

module WillPaginate
  module ViewHelpers
    def self.total_pages_for_collection(collection) #:nodoc:
      return 1
    end
  end
end

The only slightly odd thing here is the #:nodoc: statement; we just don’t want to have this method show up in any of the documentation (probably not really necessary, but I feel better having it in place). Now that will_paginate is getting pre-empted, we can test the SearchController:

# /test/functional/search_controller_test.rb

require 'test_helper'
require 'flexmock/test_unit'
require 'mocks/view_helpers'

class SearchControllerTest < ActionController::TestCase

  test "should search events" do

    flexmock(Location).should_receive(:search).once.with_any_args.and_return([locations(:one)])
    flexmock(Event).should_receive(:search).once.with_any_args.and_return([events(:one)])

    get :index, :q => 'hello world'
    assert_response :success

    assert_match '<p>There is 1 result for "hello world"</p>', @response.body
end

test "get with no query paramter searches everything" do

    flexmock(Event).should_receive(:search).once.with_any_args.and_return([events(:one)])

    get :index
    assert_response :success
    assert_match 'There is 1 result for ""</p>', @response.body
  end
end

I’ve got two tests here, one that tests that search works if a query is passed to the search daemon, and one that tests what happens if no query is actually passed (the expected function for the latter test is that all Events would be displayed).

I also like Flexmock because of the readability of the tests – the method names are quite literally what they seem to be. The really important thing to note though, and what took me forever to find, was the .once.with_any_args.and_return(#...) phrasing. That’s what’s really key here – I was getting all manner of weird errors back until I set up my stub this way. What I’m doing is short-circuiting the search function – I’m telling my tests that the search parameters and responses are unimportant, all that we really want is to make sure that the search method is getting invoked.

Hopefully this will help save someone else time. If there are better ways to solve this problem, I would love to hear about it – like I mentioned above, mocking/stubbing seems to be one of those topics that no one really discusses at any length, so the more discussion we can have, the better.

Shortlink: http://panpainter.com/p/6

Tags: , , , , ,
Posted in code | No Comments »

A High Point

Thursday, August 27th, 2009

Last weekend I participated in Rails Rumble ‘09 (along with some really awesome teammates), and today we found out that we made it in to the Public Voting stage!

It really makes that weekend of hard work worth it (although we all really wanted to build Quippopotamus anyway). Congrats to my teammates, and to everyone else – go vote for us! :)

You’ll also notice that I’ve added something new to the sidebar (over there on the right) – that would be the by-product of last weekend’s endeavors, an embedded widget from Quippopotamus cycling through a collection of quips that I’ve started to build.

Shortlink: http://panpainter.com/p/5

Tags: , , ,
Posted in code, development, onehub | No Comments »

Dynamic Page Tabs in IE6

Friday, January 2nd, 2009

Note: This is a re-posting/summary of a post that was written and released on the Onehub blog; I’m just posting up a snippet so that you can see what it’s about.  If you want to read the full text, please visit: http://www.onehub.com/past/2009/1/2/dynamic_page_tabs_in_ie6/

While working to ensure that the Onehub experience was of the best possible quality for our Internet Explorer 6 users, I ran in to a unique challenge: we have page tabs that need to be completely flexible. I had no guarantee that they would be a certain width, height, or even how many tabs there would be.

Anyone who has worked with IE6 for long enough will know what a headache scenario that can be, but I needed to make sure that these tabs were as usable in IE6 as they were in any other browser. After doing no small amount of research, I realized that very few people have written about how they have solved this problem; in fact, I found very few examples of tabs that were as complex as ours were. So, I had to solve the problem for myself, and this is what I did.

Read the full content of this post…

Shortlink: http://panpainter.com/p/4

Tags: , , ,
Posted in Shameless Self Promotion, code, onehub | No Comments »

Featherdust: A Simple Twitter Gem

Sunday, December 28th, 2008

Not too long ago, I released a Ruby gem on GitHub.  You can read more about it over at the gem’s site, but in a nutshell it’s a gem that’s designed to be added in to your website, and it will aggregate however many status updates you want from your Twitter account.  It works similarly to many of the javascript Twitter aggregators, except that it doesn’t require the end user to have Javascript installed.

Shortlink: http://panpainter.com/p/3

Tags: , ,
Posted in Shameless Self Promotion, code, development | No Comments »

Something new and shiny

Monday, December 15th, 2008

Just to keep everyone in the loop; I’m in the process of working on a Ruby gem that will be released in the next week or so on RubyForge. When there’s more to know, I’ll post it here.  Or, you can keep an eye onThe project’s site (not much there now, of course).

This is being released as a part of the UW course that I’m taking focusing on Ruby, but is something that I plan on maintaining well after the course is over.

Shortlink: http://panpainter.com/p/2

Tags: , , , ,
Posted in code, development | No Comments »

|