Git rollback to previous commit

So you’ve pushed a few (or more) bad commits in a row, and you want to throw away those changes. Sometimes it helps to jump straight back to a previous commit.

Try it out:

# Check that "git status" is clean:
$ git status

# Set the index (staging area) to be as it was at commit db0bc:
$ git read-tree db0bc

# Create a commit based on that index:
$ git commit -m "Going back to the state at commit db0bc"

# Your working tree will still be as it was when you started, so
# you'll want to reset that to the new commit:
$ git reset --hard

Code courtesy of Mark Longair over at Stack Overflow.

XCode 4 style tab views

We needed tab views that look and feel just like the ones in XCode 4. Our first attempt trying to override NSTabView’s styles failed. Instead, we found it easier to make NSTabView tabless and control it using a customized NSSegmentedControl. What’s nice is we can set it all up inside Interface Builder.

Instructions:

  • Open the nib file that will contain the tab view
  • Drag a NSTabView from the Object Library onto the stage
  • Change its class from NSTabView to WILLTabView
  • Add however many tab items you want and edit their labels to match up with an image filename


Check it out:

https://github.com/aaroncrespo/WILLTabView/

GCD Delayed Block Invocation using dispatch_after

I needed to run some code after a period of time after an asynchronous remoting call completed. Normally you’d use NSObject’s performSelector:withObject:afterDelay:, but I wanted to use blocks instead so the code could be inline. Grand Central Dispatch’s dispatch_after method did exactly what I needed.

Try it out:

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), 
                   dispatch_get_main_queue(), ^{
       // This gets called after 1 second
    }); 

Sinatra ActiveRecord Mock Server

So you’ve built your awesome shiny new RESTful Rails app. You’ve been developing the client and find it pretty tough to unit test it against your service. The problem is that data on your service persists causing inconsistent test results. The solution is to usually mock requests on the client which can be sometimes difficult. When that’s the case, I’ve found it easier to mock the service instead using Sinatra — a light-weight web server. This easy-to-setup server can leverage your existing ActiveRecord models using a temporary in-memory database enabling you to mock requests to responses consistently in any manner required.

/MyRailsApp/spec/Gemfile:

source 'http://rubygems.org'
gem 'sinatra'
gem 'sinatra-activerecord'
gem 'sqlite3'
gem 'require_all'

/MyRailsApp/spec/mock-server.rb:

require 'sinatra'
require 'sinatra/activerecord'
require 'require_all'

# Require ActiveRecord models
require_all '../app/models/*.rb'

# Use UTC timezone
Time.zone = "UTC"
ActiveRecord::Base.default_timezone = :utc

# Disable Concurrency
set :lock, true

before do
  ActiveRecord::Base.logger.level = Logger::ERROR

  # Setup in-memory Database
  ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
  ActiveRecord::Migration.verbose = false
  ActiveRecord::Migrator.up('../db/migrate')
  ActiveRecord::Base.logger.level = Logger::DEBUG

  # Any additional setup
end

after do
  #nothing yet
end

post '/' do
  "Hello World!"
end

# Example POST User
post '/users' do
  post = JSON.parse(request.body.read)
  puts post.inspect

  begin
    raise if User.find_by_id(post["user"]["id"])
    @user = User.create!(post["user"])
    content_type 'application/json'
    status 201
    response = @user.to_json
    puts response
    response
  rescue
    status 400
  end
end

Install Gems:

$ cd /MyRailsApp/spec
$ bundle install

Start Server:

$ ruby -rubygems mock-server.rb

Check out the gist:
https://gist.github.com/1439361