1 Mar
2008
Chris came up with this excellent little snippet, try, as seen on Ruby Inside.
That inspired me to a create a similarly sugary method for an idiom I use time and again:
class Object
##
# @person.name rescue nil
# vs
# @person.do_or_do_not(:name)
def do_or_do_not(method)
send method rescue nil
end
end
Comments : 11 Comments »
Categories : Ruby
1 Jan
2008
Thanks to the contributions of a couple of people on the mailing list, and thanks to a client who wanted some Lorem Ipsum text to be generated, Faker 0.3.0 has been released.
Here are the changes:
- Added Lorem to generate fake Latin
- Added secondary_address to Address, and made inclusion of secondary address in street_address optional (false by default).
- Added UK address methods [Caius Durling]
Go get it.
Comments : 1 Comment »
Categories : Ruby
22 Nov
2007
My first gem! :) This is a simple one—a port of Perl’s Data::Faker. It’s useful for generating fake data, like names, addresses, etc. Check out Faker’s home page or install via gem install faker.
Comments : 4 Comments »
Categories : Ruby
3 Oct
2007
If you’d like a quick and dirty UPS rate calculator to use in your Ruby/Rails projects, check out this bit of code I did, based on the great shipping gem:
http://pastie.caboo.se/103505
Changes from the shipping gem include replacing REXML with Hpricot and adding a method to get a list of available methods and rates for a particular package. It uses the UPS Online Tools, so you’ll need to get a shipping account with UPS to use it, but you can get one of those for free and fairly painlessly.
I’ve used this code in a few e-commerce projects now, so hopefully it will work pretty well for you, too. Use it as you wish, but you get no warranty. :)
Comments : 1 Comment »
Categories : E-Commerce, Ruby, Ruby on Rails
7 Feb
2007
Here’s a fun one that’s been discussed a little bit here and there. One scenario I encounter pretty regularly is needing to have two different ways to load data from a controller action:
- Load all data (a list of orders, let’s say) when the user loading the action is an admin
- Load a subset of data (a list of just one user’s orders) when the user loading the action is not an admin
There are a few ways you can handle this scenario, but here’s my favorite way (for the time being, at least):
class OrdersController < ApplicationController
def index
@orders = order_finder.find(:all, :order => ‘name’)
end
protected
def order_finder
current_user.admin? ? Order : current_user.orders
end
end
This is a simplified snippet, but it demonstrates an easy way to change the data being pulled out based on who is using the application. And for no extra charge you can still keep your controller code lean and mean. :)
Comments : 6 Comments »
Categories : Ruby, Ruby on Rails
Recent Comments