Warning: Creating default object from empty value in /homepages/39/d161420129/htdocs/p373.net/wp/wp-content/themes/p373b/admin/functions.php on line 183
Category Archives: Ruby/Rails


Stub a ruby method and conditionally raise an exception based upon state of object

(If you want to skip my fancy and hilarious writing, skip straight to the SO post) Here’s the use case: You’re building  a mass email system for your globo-mega-corp’s 90,000 employees.  Your corp’s smtp server can only handle batches of 450 recipients at a time.  Your boss tells you your corp’s SMTP server is finicky […]

Also posted in Coding, Technology | Tagged , , | Comments closed

Adventures in Grunt-Land Part II: How to dynamically update files with Grunt-Replace

In the previous post, I went over how to setup Grunt so that you can have environment based builds.  A build for your development environment would have a destination of builds/development, builds for staging would go to builds/staging, and builds for production go to builds/production.  You get the idea. In this post, let’s take our […]

Also posted in Coding, Technology | Tagged , , | Comments closed

How to create environment based builds with Grunt

Grunt is pretty neat when it comes to creating builds of javascript applications.  However, I found it quite difficult to create environment based builds, ie, builds for development, staging, and production. First off, let me say that I’m not a javascript developer.  In reality, what I”m trying to do is very simple: create a deployment […]

Also posted in Coding, Technology | Tagged , , | Comments closed

Recursively generate nested errors in json with Rails

See this gist: https://gist.github.com/synth/8511262 def nice_errors(object, errors) object.errors.each do |key| error_obj = object.send(key) if error_obj.kind_of?(Array) errors[key] = {} error_obj.each {|item| nice_errors(item, errors[key])} elsif error_obj.kind_of?(ActiveRecord::Base) errors[error_obj.id] = {} nice_errors(error_obj, errors[error_obj.id]) else errors[key] = object.errors[key] end end return errors end end # call it with nice_errors(resource, {}) # will generate something like: {"errors":{"user_recipients":{"email":["may not be the same as […]

Also posted in Coding, Technology | Comments closed

An algorithm to generate fake but realistic stock price graph data that trends

Sometimes you want to generate data. Generating random data is pretty easy, but that doesn’t represent real user behavior. Real user behavior probably follows patterns. Here is an approach to generating some data that approximates stock price fluctuations. The below algorithm will generate a string of code that can be input into Google charts for […]

Also posted in Coding, Technology | Comments closed

How to customize Http Digest Authentication error pages in Ruby on Rails

Say you are working on a site that has a splash/landing page that is public, but the site rest of the site needs to be locked down to a small group of people to alpha test. You could build this into your authentication model, or you could do it quickly and easily using HTTP Digest […]

Also posted in Coding, Technology | Comments closed

Carrierwave custom validations

The use case here is when you need to conditionally validate the extensions you validate.  In our case, we only wanted to validate the extension white list when a local file was uploaded.  This is because our users can signup via Yammer during which we suck in their mugshot images.  Yammer’s mugshot urls do not […]

Also posted in Coding, Technology | Tagged , | Comments closed

Forget pretty urls, how about vain gorgeous urls

In Rails, resource based urls are by default not pretty.  For example, say you have the following routing resources :projects do resources :users end This yields urls /projects /projects/1 /projects/1/users /projects/1/users/1 So to make pretty urls, its pretty trivial.  Just implement #to_param on the project model and user model #app/models/project.rb class Project < ActiveRecord::Base … […]

Also posted in Coding, Technology | Comments closed

Rails deployments from 4 minutes to 40 seconds

Currently my deployment(which is pretty close to a default setup) takes about 4 minutes.  Currently we take the server offline into a “maintenance” mode for the entire deployment process.  That’s 4 minutes of downtime.  No bueno. Here’s what we’re going to do: 1. Move the maintenance mode (think of it like a mutex) to wrap […]

Also posted in Coding, Technology | Tagged , , , | Comments closed

Capistrano Callback Sequence

Considering how well Capistrano is documented otherwise, I’m surprised there isn’t better documentation on the load order or sequence of the default capistrano tasks. There Capistrano Deploy Recipe doc, which if you read(and who likes to do that), you can kind of figure it out.  But where are the pictures!  Google didn’t seem to turn up […]

Also posted in Coding, Technology | Tagged , , | Comments closed

Patterns: Exception Factory(or is it a Proxy?)

Let’s say you are writing a library that wraps around an API, say Yammer.  Your library probably uses a lower level library to manage the http requests, say Faraday.  There are a multitude of problems that may arise while communicating with their api.  For example, the user could have revoked their token, or there could […]

Also posted in Coding, Technology | Tagged , , | Comments closed

Capybara custom matcher alternative

Sometimes when writing specs, you want to test a high level case multiple times across multiple spec tests.  This test may involve asserting multiple things on the page. There is probably a proper way to do this, like see this gist , or there is my way, which seems way simpler. So the objective is to […]

Also posted in Coding, Technology, Uncategorized | Tagged , | Comments closed

Ec2, Elastic LoadBalancers, Passenger, forcing ssl and how not to drive yourself crazy

Say you are using ec2, elastic load balancers and passenger to run your rails app, and then say you want to force ssl for all requests.  If you’re running a Rails 3.1+ app you just need to specify force_ssl, right?  nope. Do these things and save yourself some time, money, and hair follicles: Change the […]

Also posted in Coding, Technology | Comments closed

ProTip: Don’t name your namespaced controllers “ApplicationController”

Don’t do this: class Admin::ApplicationController < ApplicationController layout "admin" end class Admin::IndexController < Admin::ApplicationController end This works for the most part except that the layout is not chosen properly.  Rails just chooses the default application.html.erb layout. Instead, rename to something like this: class Admin::BaseController < ApplicationController layout "admin" end class Admin::IndexController < Admin::BaseController end you’re […]

Also posted in Coding, Technology | Tagged , , , , | Comments closed

Rails Nested Resource Controllers – what not to do when testing

Testing nested resource controllers is pretty easy and awesome.  It is not easy and awesome, however, if the name of your nested controller differs from the route you want to use. Examine the following resources :posts do resources :comments, controller: "UserComments" end Did you catch whats wrong with the above?  You may not actually run […]

Also posted in Coding, Technology | Comments closed

Capybara, ajax, requirejs and how to pull your hair out in 8 easy hours…

Editors Note:  I wrote this post after a full day of hair pulling and shotgun debugging, so it may be a bit incoherent…but I wanted to get the situation documented as soon as possible.  Read at your own risk Capybara is awesome for rails acceptance testing. However, I do have a love/hate relationship with it. […]

Also posted in Coding, Technology | Comments closed

Ubuntu 10.04, Rvm, and Rails 3.1.3 from scratch

Assumptions:(swap these out  in the below notes as you see fit) database: mysql scm: bazaar testing: capybara(has a libqt dependency) do basic install ubuntu 10.04 do full software update apt-get install build-essential curl git-core ruby zlib1g-dev libxml2-dev libxslt1-dev emacs mysql-client mysql-server libmysqlclient-dev bzr libqt4-core libqt4-dev rvm pkg install zlib openssl curl readline rvm install 1.9.2-p180 […]

Also posted in Coding, Technology | Comments closed

ActiveScaffold and Rails 3.1.x without Asset pipeline

We have recently upgraded an app to rails 3.1.3 and to keep things simple(or so we thought) we avoided jumping into the asset pipeline. As expected, with rails, whenever you go against the grain, expect problems. The app was working mostly, until we tested ActiveScaffold.  First off, I despise AS.  It has definitely caused me […]

Also posted in Coding, Technology | Comments closed

How to *Not Really* delete a ruby class from memory

Alright, alright…(first blog post here, cut me some slack…) So, how can you delete a class from memory in Ruby?  First, you might be asking….why in the hell would you do that. Well, as it happens, in my case, I wrote some base classes that other developers will derive off of.  So in this base […]

Also posted in Coding, Technology | Tagged , , | Comments closed