August 2011
1 post
3 tags
Clean up trailing whitespace
One line command to clean up the trailing whitespace for a given project:
find . -name "*.rb" -type f -print0 | xargs -0 sed -i '' "s/[[:space:]]*$//"
This command will search for any files with the “rb” extension (Ruby files) and remove all trailing whitespace. This works in OSX, the command syntax might be different in Linux. Run this command from your project’s root.
July 2011
1 post
The State Open Source Development
My thoughts on the subtext of dealing with some open source project maintainers
June 2011
4 posts
Every pub and cafe should have free wifi and...
Getting a cable connection and sharing over wifi with your customers is a great way to not only have customers all day long but repeat customers. But the real reason why every pub and cafe should today install free wifi is because in a few years they won’t need it and they need to learn how to sell to the customers who can “work from anywhere”.
Tethering is awesome
With my...
ClientSideValidations for Rails 3.1 and 2.3.x...
Today I’ve made two new releases: ClientSideValidations v3.1 for Rails 3.1 and a backport of ClientSideValidations for Rais 2
ClientSideValidations v3.1
ClientSideValidaitons for Rails 3.1 adds the javascript file to the asset pipeline. So you no longer need to run the install generator for each update. Just serve up rails.validaitons.js in your application.js file:
//= require...
ValidAttribute 1.0
I just released ValidAttribute 1.0 So what is ValidAttribute and why should you be using it?
See the README for installation information.
I grew frustrated with how I was writing the specs for my models. Very often I found myself doing this:
describe User do
it { should validate_presence_of :name }
it { should validate_length_of :name, :is => 5 }
end
This felt wrong. I was duplicating...
The Github Follower Problem
Github is awesome. I think most people agree. They have proven the concept of “social coding”. The big flaw in this plan is Github’s relationship system.
Following people on Github is easy. I see somebody interesting, I click follow. The problem is that now my news feed will get every single commit, message, etc… this person does. After following a few active people my...
May 2011
1 post
We choose not to go back to the moon...
We choose not to go back to the moon. We choose not to go back to the moon in this lifetime and not do the other things, not because they are hard, but because they are expensive, because our goals are now self-serving, because the challenge is one that we are unwilling to afford, one that we are willing to postpone, and one which we intend to ignore.
Sad, but true…
April 2011
4 posts
5 tags
Exploring Rails 3.1 - ActiveModel::SecurePassword
Rails 3.1 will have password encryption baked in via ActiveModel::SecurePassword. Any ActiveModel based model will get this functionality. (ActiveRecord, Mongoid)
Your model will need a field called password_digest and then you’ll need to activate secure password:
class User < ActiveRecord::Base
has_secure_password
end
You get a few things from this
attr_accessor :password
...
Setting cookies in the new Capybara
Getting access to the cookie_jar is different for the new Capybara (the one that will have the acceptance test DSL)
You’ll want to add a helper to spec/acceptance/support/cookie_helper.rb
def cookie_jar
Capybara.current_session.driver.browser.current_session.instance_variable_get(:@rack_mock_session).cookie_jar
end
Now you can access the cookies at any time:
#in some...
Why I love Rails and Github
Today a pull request to r\Ruby on Rails was opened. In short, it added a few methods that allowed an alternative use of Array#include? I happened to notice that Jose Valim commented on it in my Github news feed, so I checked it out. I weighed in as I questioned the necessity for the added methods. Others began to weigh in too, and in short time we had a good conversation going.
Other language or...
valid_attribute
valid_attribute on Github
I’ve been writing model tests for years. Like most I started out with Shoulda which was great. As I moved over to RSpec thankfully Shoulda moved over as well.
After a while I began to hit some walls with Shoulda, specifically when I wanted to use some of the matchers for non-ActiveRecord models. I previously wrote a gem called remarkable_mongoid that provided...
March 2011
1 post
Client Side Validations 3.0
client_side_validations on Github
The README and the Wiki are very detailed. Please check them out as well!
Client Side Validations 3.0 is out! Adding client side validations to an existing Rails 3 project is as simple as: (assuming you are already using jQuery >= 1.4.1)
requiring the gem…
# Your Gemfile
gem 'client_side_validations'
running the install...
November 2010
1 post
5 tags
Convert Prawn generated PDFs to an image with...
You’ll see some complaining from RMagick about how the Prawn generated PDF is not 100% up to Adobe standards but that can be ignored.
pdf = Prawn::Document.new { text 'This is only a test.' }
base64 = Base64.encode64(pdf.render)
image = Magick::Image.read_inline(base64_pdf).first
image.write('test.png')
July 2010
3 posts
Rails Camp New England #3
Registration is open!
When: Sept 10 - Sept 13
Where: Stowe, Vt Ski-Adventure Lodge http://ski-adventure.com/
How much: $140 (+ eventbrite fees)
Why: Hacking and fun
Join your other New England Rubyists for the 3rd Rails Camp New England!
http://rcne3.eventbrite.com
More information Rails Camp
remarkable_mongoid
I just released remarkable_mongoid. A set of Association and Validation RSpec matchers to use with Mongoid.
Check out the README for instructions.
Please note that this gem requires the 2.0 beta of Mongoid and installs the 4.0 alpha of Remarkable/ActiveModel. Despite their beta/alpha statuses I’ve been using both for over 2 weeks now and they feel very stable.
Rails Camp New England # 3
The time has come to start planning for another Rails Camp NE. Here are my thoughts on the next camp:
Where: Vermont
When: Sometime After Labor Day (dependent upon scheduling a venue and other conflicting Ruby events)
While our last venue was fantastic it maybe have been too big for the size group that we attract. So I’d like to find a venue similar to the first RCNE (basically a large...
June 2010
2 posts
3 tags
Custom Subdomains in Rails 3
Rails 3 supports subdomains out of the box, which is great. But did you know that the constraints in the Router can actually match patterns too? This means instead of hardcoding each subdomain into your routes you can allow your customers to decide their own subdomains.
However, we have to be careful with pattern matching on the subdomain. There are obvious subdomains we don’t want to...
4 tags
RVM Gemsets + Bundler == Awesome
So I’m going to assume that you’re already familiar with RVM, Gemsets and Bundler. If you don’t know about them please follow the links and read up.
Mixing RMV Gemsets and Bundler together is a kickass way to maintain environment isolation for your projects. First you want to create a .rvmrc file in your project’s root directory. RVM will traverse up the directory...
May 2010
3 posts
7 tags
A cry for standardized versioning in Ruby (pretty...
Yesterday Rails 2.3.6 was released, today 2.3.7 was released. This morning the team I am on attempted to update our Rails 2.3.5 apps (we have several) to 2.3.7. It’s been several hours and we’re still not updated yet.
When I see a version update of 2.3.5 to 2.3.6 as a developer I should be able to assume that the update is fairly marginal. In fact, in almost every case it should be a...
Moved from Blogspot to Tumblr
The move was incredibly easy thanks to this tool:
http://terrymhung.com/jtran/tumblr/import-blogger-to-tumblr.php
Clean up your OSX paths
If you’re anything like me you’ve got a ton of paths in your .bash_profile. I defined them with some variables based upon applications then appended them onto $PATH at the bottom: Recently I discovered that OSX has a cool feature to help clean this up. It’s called path_helper Basically, it will read every file in /etc/paths.d and append each line of the file onto $PATH. For...
April 2010
3 posts
RubyNation 2010 Live Blog Day 2
RubyNation 2010 Live Blog Day 2 April 9 - 10 Reston, VA RubyNation 2010 Agenda Saturday 4pm Block, Procs and Lambdas, Oh My! Functional Programming in Ruby by Paul Barry From RubyNation2010 All programming languages are opinionated First-Class Functions And Higher-Order Functions First-Class Function: Procs Immutability: value > reference Basically a bunch of stuff has been happening involving...
RubyNation 2010 Day 1
RubyNation 2010 Live Blog Day 1 April 9 - 10 Reston, VA Day 2 RubyNation 2010 Agenda Friday 6pm ruByBQ! 5pm Keynote by Dave Thomas From RubyNation2010 Dave is leading a humors keynote on some of Ruby’s more notorious WTFs Dave still loves Ruby even though he’s been using it since ‘98! Now he’s covering some of the new stuff in Ruby 1.9 Rails changed the frontier land of...
TDD is Like Communism
A co-worker of mine who isn’t a big fan of strict TDD practices tried to mock it by comparing it to communism. (I work in politics, so it was supposed to be a huge insult) But the more that I thought about it the more I realized he was correct, only for an entirely different reason. TDD is like Communism Communism fails to live to its ideals when one person doesn’t play ball. The...
January 2010
1 post
Rails 2 & Rails 3 side by side
If you’d like to use Rails 3.0.pre and also maintain Rails 2.x you’ll need to do the following: (my binaries are in /usr/local/bin so yours might be different) > mv /usr/local/bin/rails /usr/local/bin/rails2 Edit the rails binary and change: version = ">= 0" to: version = "~> 2.0" Save, now install Rails 3.0 Rename rails to rails3: > mv /usr/local/bin/rails...
December 2009
2 posts
RailsCamp 2010 Venue Change
Just a quick update, I just got off the phone with the Venue that we had booked. Very long story short: we got bumped and I’m not OK with trying another weekend with them. What this means: We are still having RailsCamp. I will find another venue. I will do my best to secure that same weekend. If the weekend changes and you have already paid but cannot make the event I will...
I need to following
1.) Macbook with extremely extended battery life. Something like 15 - 20 hours 2.) Extremely light-weight and water-proof Macbook 3.) A non-reflective screen that maintained the gloss and sharp color of the glossy screen 4.) Offshore WiMax 5.) A sailboat “office”. At least a 35 footer, probably an X42, so I can travel comfortably across the Atlantic
November 2009
2 posts
Write your congressman
DNC idea: allows you to print out a customized letter to your local rep that is formatted properly and includes your specific information. Also tells you where the closest mailing location.
Some company ideas
1.) Local internet radio ads
Internet radio suffers from only being able to use national or global ads. Which means these programs are losing the usual money that syndicated shows could reap. Let’s say a show charged $100 for a 1 minute national ad. They could charge $25 for a localized ad but have 20 different localized ads play. Localizing based upon IP address is easy enough, the issue...
October 2009
4 posts
Domain Names Suck
Domain Names Suck because they really limit the number of potential startups. Further “.com” *really* sucks because it seems you must be forced into the .com namespace to be successful. The internet needs a better naming system.
Problems Long names are far less marketable than short names TLDs are controlled by governments and not available to everybody’ Squatters sit on the...
Mocha stub_chain with AnyInstance
There is a module floating around called stub_chain: Stub Chain In short, it allows you to do: >> Object.stub_chain(:this, :is, :my, :chain).rerturns(true) >> Object.this.is.my.chain => true It cleans up a bunch of mocking, stubbing code. Very cool. So in Rails I found myself with the following chain in a controller: current_user.profile.photos.build So naturally I tried: ...
Rounding a Float to a Fraction
I had a need for more rounding precision than the regular Ruby Float#round method. For example, if I wanted to round to the nearest half Ruby does: >> (0.6).round => 1 What I needed was: >> (0.6).round_to_fraction(0.5) => 0.5 So I wrote up this monkey patch for the Float class: Float#round_to_fraction(fraction = 0.5) This works with any fractional value: >>...
Rails Filter Matchers in RSpec
This is pretty granular TDD but I’ve found it useful: Rails Filter Matchers for RSpec I wrote these out of necessity for testing out the inheritance of certain filters I was writing in a recent app. Just save the file in: RailsRoot/spec/support/active_record/have_filter.rb It’s pretty simple to use: class HomeController before_filter :require_user, :except => [:action_1,...
September 2009
1 post
jferris-mocha and Rspec
I ran into a small issue when using Joe Ferris’ fork of Mocha with Rspec. In the spec helper you have “ config.mock_with :mocha I’m on version 0.9.5.0.1241126838 of jferris-mocha and the library load was failing. The reason is because Mocha::Standalone has been changed to Mocha::API. To fix this, create the file spec/support/mocha.rb fill it with: module Mocha module API ...
Decoder
Decoder I just released Decoder. So what is Decoder? It is a simple library for postal code lookups. For example: >> country = Decoder::Countries[:US] => <Decoder::Country @name = "United States", @code = "US", @states = { "MA" => "Massachusetts", ... } > So you do a code lookup and get back a Country object. If the country has states then you can lookup via a state code: ...
July 2009
1 post
Need a Help desk? Try Zendesk!
This is a shameless self-promotional post. I just started working at Zendesk.com, we provide SAAS Help Desk software that is dead simple to use. It costs nothing to try. Learn not to hate answering tickets! :) http://zendesk.com
April 2009
1 post
1 tag
Ruby 1.8 and 1.9 living together on Mac OSX
Lately I’ve been playing with Ruby 1.9.1 But for my professional life I still need 1.8.6 So here is how I have them setup to live happily together. What you first want to do is, of course, install Ruby 1.9.1 (your tarball name might differ) > wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p0.tar.gz > tar xvf ruby-1.9.1-p0.tar.gz > cd ruby-1.9.1-p0 > autoconf >...
January 2009
5 posts
6 tags
Going back on Mocking & Stubbing with RR (kind of)
Almost 2 months ago I wrote an article called A case against Mocking and Stubbing. In it I outlined why I think mocking & stubbing is a bad idea. I still maintain some of those beliefs but since I’ve been playing with the mocking framework RR I have changed my tune some. RR allows for a much cleaner and faster way to write mocks and stubs. There is a whole lot more to the framework that...
One Month No TV Challenge
So, continuing with my productivity thread I’ve decided to completely cut out TV for an entire month. I’ve given this some thought and here are my current TV viewing habits: I would say that most of the TV I watch consists of pre-recorded shows. Something that I could easily catch up on at a later time. With such services as Hulu and NetFlix I really shouldn’t have to bother...
Increasing My Productivity II
More productivity increasing: 1.) The multi-touch MacBook touchpad is VERY useful. Learn all of the finger strokes. 2.) (this is from Bryan Liles) This kind of goes against #1 but don’t use the mouse for an entire day. Try to memorize all OSX keyboard shortcuts You’ll be surprised how much faster you’ll be working. 3.) Purchase and watch the Peepcode Productivity Screencast.
Increasing my Productivity
In my never ending attempt to increase my productivity I’ve started printing cheatsheets and hanging them up around me. I have a terrible memory and spend a good amount of time bouncing between RDocs looking up methods that I’ve used 100 times before. Next step: master Textmate… then probably go back to VIM. (or Emacs) Rails Testing Shoulda 2.0 TextMate for Rails Please let me...
3 tags
Testing Your Helpers Sucks (but necessary)
Lately I’ve been really focusing on getting 100% code coverage and keeping my views free of all logical statements. This means moving all of the logic that would be in a view into a helper and testing that helper. In a previous post I spoke about testing your helpers. Here are some things I’ve learned since then: 1.) Helper testing is very much like Unit Testing (hell, it *is* unit...
December 2008
2 posts
Laces = Shoes + Rails
This is a recent lightning talk I gave at the Boston Ruby Group. Thanks to Josh Nichols for recording the video and posting it to Vimeo. Laces = Shoes + Rails from Josh Nichols on Vimeo.
A case against Mocking and Stubbing
I used to be a big advocate of mocking and stubbing back when I was doing a lot of Rspec. To the point that when I interviewed at Thoughtbot I brought that up as my reason for using Rspec over Shoulda. (note: if you’re interviewing for a company, it might be in your best interest to be using their stuff and be familiar with it) However, since I’ve been TATFT with TDD and some BDD...
November 2008
3 posts
Testing Your Helpers with Autotest
So I’ve been writing an app lately and I am really trying to go for 100% test code coverage. When I ran into the issue of TDDing my helpers I recalled a post by Josh Nichols: Helper Testing Using ActionView::TestCase. This was a really good start but it felt somewhat incomplete. I didn’t like the idea of putting my helper tests into test/unit/helpers. So let me go through my process: I...
Is God Dead?
nietzsche.rake
Better rake:stats Test Code Ratio Metrics
It doesn’t make much sense to me why rake:stats will only return 0.1 precision on the test code ratio. So, if you’d like a more precise metric make a change to the code in Rails: rails/lib/code_statistics.rb from: def print_code_test_stats code = calculate_code tests = calculate_tests puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio:...
October 2008
1 post
acts_as_solr forked
I have forked acts_as_solr on GitHub Why? Well, because I wasn’t too happy with the way acts_as_solr was indexing associations. class Person acts_as_solr :include => [:dog] has_one :dog end acts_as_solr would index this as: first_name_t:John last_name_t:Doe id_t:1 ... dog_t:name=Rover color=brown sex=male Do you see how all of the associated fields are lumped into one string? So...
September 2008
3 posts
3 tags
Instantize Keys in Rails
If you find yourself doing something similar to this quite often: @name = parms[:organization][:name] @url = params[:organization][:url] You can save yourself a lot of time with this method: application.rb private def instantize_keys(hash) hash.each { |key, value| eval("@#{key} = #{value}") } end Now just call this private method in your controller method and pass the appropriate hash. ...
6 tags
Importing and Exporting Very Large Data Sets in...
A recent project has been me pushing beyond the regularly used ActiveRecord methods in Rails. The goal was to import and export very large CSV files with Rails. The example data set I was given was over 850,000 rows. Uncompressed it came out to 46MBs. So let’s explore the process to do this. So let’s say for example I want to import a set of users. I expect the import CSV to have the...