Ruby Reload! Method in Non Rails IRB Sessions

I love the Rails reload! function when in the console. I need it in Irb. To get it back this is what I did. If you don’t already have an .irbrc file in your home directory, just create it.

vim ~/.irbrc
or textmate if you prefer
mate ~/.irbrc

Add this little snippet to it…

unless defined?(reload!)
  $files = []
  def load!(file)
    $files << file
    load file
  end
  def reload!
    $files.each { |f| load f }
  end
end

Usage : To load files in irb just use the method we defined in the .irbrc file “load!” notice the bang “!”. I don’t want to overwrite the actual load method. This load! method will just put the file in an array before loading it, so when running reload! it will iterate over this collection and load them again with whatever changes have since taken place. The unless conditional is so that you don’t overwrite the reload! method if you’re actually in the rails console.

How to Recover a Mistakenly Deleted Branch

Workflow

git checkout -b _new_branch_name
# do some work and commit changed
git checkout master
git branch -d _new_branch_name
# doh... i meant to merge first

Fortunately, you can easily recover from this mistake.

git reflog
395b1ea HEAD@{0}: checkout: moving from _master_cleanup_akismet to _master_cleanup
bd7df04 HEAD@{1}: commit: spam handling using akismet for form submissions on contact_submission and applicant models
395b1ea HEAD@{2}: checkout: moving from _master_cleanup to _master_cleanup_akismet
395b1ea HEAD@{3}: commit: cleaning up and adding some basic features
a828ef3 HEAD@{4}: checkout: moving from master to _master_cleanup

Should show you a list of commits across all branches in desc chronological order.

To merge the branch you just deleted you can give merge the sha.

git merge bd7df04

Which would match the commit at HEAD@{1}

Aliasing Attributes in Ruby on Rails

Alias an attribute with alias_attribute method.

The first argument is the name for the new attribute, while the second argument is to identify the attribute that is already defined.

class User < ActiveRecord::Base
  alias_attribute :username, :login
end

Rails 3 Config Auto Load Paths in Application.rb

In Rails 3 files in lib/ are no longer loaded by default. It’s a snap to auto load these classes by adding the following line to config/application.rb

    config.autoload_paths += %W(#{config.root}/lib)

This is commented out around line 16. Either replace it or uncomment it out to reflect the location where your lib classes are located.

How To Get A List of All Registered Mime Types in Rails

When mime types are registered they are placed in a hash constant EXTENSION_LOOKUP in the module Mime. For reference, the file with the relevant code is in rails/action_pack/lib/action_dispatch/http/mime_type.rb available on Github at https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/http/mime_type.rb

You can check for existence of certain mime types with

Mime::Type.lookup_by_extension(:json)

which will return the object or nil if it’s not registered.
You can also iterate over all registered mime types by cycling through the constant like so

Mime::EXTENSION_LOOKUP.each { |m| puts m}

Api documentation is here: http://api.rubyonrails.org/classes/Mime/Type.html#method-c-lookup_by_extension

Git Feature Branch Naming Strategy

There are only two hard things in Computer Science: cache invalidation and naming things.
– Phil Karlton

Typically, we have three main branches at any given time in a project lifecycle.

master, development and staging.

Master is production ready code, development is actively being worked on and staging is pre-flight testing before deploying master.

Features are branched off of development and use underscores to indicate distance from originating branch. For instance

_development_users
__development_users_reset_password

indicates that __development_users_reset_password branched off of _development_users branch.

___development_users_reset_password_experiment

(3 underscores away) would indicate that I was experimenting with a branch off of development users reset password branch.

This provides a nice visual hierarchy when running git branch from the command line. It’s also helpful when you have multiple features being worked on by several people and you want to know where these branches are in relation to each other without needing to ask anyone. Here is a more complex example that illustrates the usefulness in using underscores in naming feature branches

  __development_people_importing_csv
* __development_people_importing_xml
  __development_users_password_reset
  _development_people_importing
  _development_users
  development
  master
  staging

My First Ruby Gem: Hashed_Attributes

I just wrote and released my first Ruby Gem, Hashed Attributes https://rubygems.org/gems/hashed_attributes. It is a very simple ActiveRecord extension for saving variables through a serialized hash. The Gem will let you declare getter and setter methods that use a hash to store assigned data. For instance, instead of doing something like

@user.preferences[:favorite_color] = 'orange'

you can do

@user.favorite_color = 'orange'

The value for favorite color will be kept in a serialized hash.

  person = Person.new(:favorite_color=>"orange")
  {
               :id => nil,
      :preferences => {
          :favorite_color => "orange",
      },
       :created_at => nil,
       :updated_at => nil
  }

Setup in the model is very straight forward. The first argument is the column name you want to use followed by a list of methods used as keys on the hash.

# Example model
class Person < ActiveRecord::Base
  hashed_attributes :preferences, :favorite_color, :theme, :plan # etc...
end

ActiveRecord is required to use this gem. Additionally, you need to add a column in your database.

# Sample migration
create_table :people do |t|
   t.text :preferences
end

To install

gem install hashed_attributes

Or place in your Rails Gemfile

gem 'hashed_attributes'

The project code is hosted on Github: https://github.com/bseanvt/hashed_attributes and https://rubygems.org/gems/hashed_attributes

Releasing this gem was a lot of fun and I’m looking forward to writing more gems!

Rails Send_File in Production Delivers an Empty File

If you’re running Rails in production it will by default be configured to let apache or nginx send files for you. If you’re handling file downloads yourself with send_file

send_file("path/to/file.txt")

you will notice that the downloaded files are empty. To get around this just comment out the following in your config/environments/production.rb file.

  # comment out for production because apache/nginx are not doing this for us
  #config.action_dispatch.x_sendfile_header = "X-Sendfile"

Git Untrack Already Tracked Files

To remove files that are currently being tracked by git, you have to remove them from the “cache”. Note, doing this will NOT delete the file on your local machine. It will still be there but not be tracked.

 git rm -r --cached supersecretpasswords.txt 

You then need to add the file to the .gitignore file in the root of the project so that it isn’t tracked again on your next commit.


vim .gitignore
supersecretpasswords.txt

.gitignore files are tracked so remember to check in these changes.

git commit -am'my super secret passwords are safe!'

If you want to completely delete the file, on your local machine and from git

git rm supersecretpasswords.txt

If you’re working with a directory remember to add the -r flag for recursive removal!

Rails Find All by Birthday: How to Find Upcoming Birthdays with ActiveRecord

There are a few ways to solve this problem. However, I think the easiest is to cache the day of the year that the user is born on as an integer. If stored alongside the timestamp we can quickly get a list but properly handle the full birthday elsewhere, such as in the view. You don’t want to rely on just the cached day of year because leap year is not accounted for.

The model will need both born_at and birthday columns.

create_table :users do |t|
  t.timestamp :born_at  # full timestamp
  t.integer :birthday 	# just the day of year
end

The user model also needs a callback (before_save) to set and or update the cached birthday column based on the full timestamp. For convenience, a named scope can be added to the model which will let you call User.birthdays.

class User
  # User.birthdays
  scope :birthdays, lambda { where('birthday in(?)', 7.times.map{|i| Time.now.yday + i}) }

  def before_save
  	self.birthday = born_at.yday
  end
end

You could also use the week in year (1 – 52) for the cache. Using the day you can look an arbitrary number of days ahead.