February 2, 2012 § 2 Comments
GitHub is awesome. No doubt about it. It has made code fun and exciting again. I know that I wouldn’t be involved in the open source community without it. I probably wouldn’t be writing this blog without it. I mainly started this blog as a way to get out some more info out about some of my gems that I had written. Being able to link to repos and have a repo’s landing page be a good landing page has been huge. As I have be working on this blog I find myself more fascinated by snippets of code than with whole repositories.
Which is why I love gists.
Gists are great ways to share pieces of code that don’t need their own full GitHub repo. The only problem is that there is no good way to find all of that awesome code. There really needs to be a way to browse through gists. Sort them by language type or allow people to tag them. GitHub could even intelligently autotag a bunch of stuff. Right now gists are only really useful if you link to them from an outside location or embed them on in your blog.
There is a lot of great and useful code out there. We just need to be able to find it easier.
January 30, 2012 § 2 Comments
Last Rails 3.2 post. I swear.
ActiveRecord::Relation#pluck in a previous post because it was one I was particularly excited about. After reading through the rest of the release notes I wanted to highlight a few of the other smaller changes I thought were interesting.
is now written
You can also revert the uniqueness with
Don’t user LOWER for case insensitive searches on MySQL
When querying against our databases there are a bunch of times we don’t care about case. In the case of searching against an email address, a case insensitive search does exactly what we want it to. When you add the
:case_sensitive => true to a
validates_uniqueness_of it use to wrap that part of the query in a
LOWER(). This sucked because it prevents MySQL from using an index. When you put this restriction on your email column and have to do this query every time you save a record. Since MySQL does case insensitive searches, ActiveRecord doesn’t worry about adding the
LOWER() around the columns. Time saved all around!
rake db:drop drops both dev and test
rake db:create creates both the development and test databases. r
ake db:drop now drops the both development and test databases instead of just the development database.
When you have multiple forms on one page for the same type of objects, you can get conflicting ids form elements. Problem solved with form namespaces.
<%= form_for(@offer, :namespace => 'namespace') do |f| %> <%= f.label :version, 'Version' %>: <%= f.text_field :version %> <% end %>
The ids of the form elements have the namespace prepended with and underscore to the front of the original id.
One of the things that I love most about Rails is ActiveSupport. And one of the things I love most about ActiveSupport is how it makes dealing with Time awesome and easy. They have added a few new convenience functions to help with date ranges:
You can use these with ActiveRecord queries like so:
Event.where(:created_at => Time.now.all_week) Event.where(:created_at => Time.now.all_day)
Obviously there are a lot of other additions in 3.2, these are just the few that I know that I am going to have a use for. Enjoy.
January 23, 2012 § Leave a comment
Rails 3.2 has been released and there are a few things in it that I am excited about. I mentioned in a previous post that a few of the things coming in 3.2 were things that I had thought about doing or had already done myself. Turns out I missed one.
This is the exact same functionality provided by my
select-column gem. It allows you to get an array of column values from the database without having to instantiate an ActiveRecord object for each one. So instead of having to do this:
Post.where(:published => true).collect(&:id)
you can simply do:
Post.where(:published => true).pluck(:id)
The place I see this having a lot of nice applications is in caching. I know that I wrote my gem so that I could use it to speed up some methods that would find appropriate records and then cache the ids for quicker look ups later. I am really excited to do a find and replace of collect for pluck in some of my projects.
January 19, 2012 § Leave a comment
PayPal can be a pain in the butt to integrate in with your website. I sit next the guy on our team tasked with doing PayPal integration. I swear I can see the wrinkles and gray hair starting in on him. One of the biggest pains you’ll run into is testing the callbacks and web hooks. If you are doing anything more than putting a little donate button on you site, you’ll be dealing with IPN callbacks to keep your database in sync with PayPal. Typically your doing your development on a local machine that isn’t exposed to the outside world so you can’t have them post directly your dev machine. Bummer. There are a number of ways one can go about dealing with, I’ll mention two that we have tried before we found the third.
Test it on Prod
Nothing ever goes wrong when you do this right? A better solution here is to set up a separate staging machine that is hooked up to the PayPal sandbox, but setting up boxes just to do some sporadic testing really sucks. What if there was a service you could just point PayPal at to and just showed you what PayPal was posting, would that be better?
postbin.org is a nice little service that provides web hook endpoints. It’ll give you a url to give PayPal as a callback url. When PayPal post to this url you’ll be able to inspect the request it sends. Unfortunately you’ll then have to manually parse it and figure out a way to post that info locally on your machine. But hey, no extra machines floating around in your cloud just wasting space and money! But there still has to be a better way to do this. I really would rather all of this just go to my local dev machine. Please tell me this can be done!
Enter localtunnel. localtunnel is a ruby gem that does what the name implies. It hooks your local server up to the world wide web by tunneling to a subdomain of localtunnel.com. You simply install and run the gem giving it your public key and a port and BOOM.
http://as2js.localtunnel.com now points to your local dev machine. The PayPal IPN’s can now trigger all that nice sync code you have written.
I am currently working on some GitHub continuous integration web hook stuff, so this tool has been invaluable. Priceless even. The only additional thing I would like to see with this gem would be a way to deploy the server side to your own machine. While localtunnel.com is fine and dandy, I’d feel better if I controlled all the stops of the data. Anyways, get you localtunnel up and you web hooks on!
January 17, 2012 § 2 Comments
I was working on a project a while ago that required the use of custom variable subdomains. If you have ever had to develop a site that used user specified subdomains, you quickly realize that editing your local
/etc/hosts file over and over again sucks. A lot. You end up setting up half a dozen subdomains on a dummy domain that points to
localhost. Not an ideal setup. Thanks to Railscast I tracked down a pretty cool solution to the problem.
The basic idea is to set up a live domain to point
127.0.0.1 and let DNS take care of everything for you. Tim Pope wrote about it nearly two years ago using
smackaho.st. In the Railscast Ryan Bates mentions another domain setup for this,
lvh.me, which was registred by Matthew Hutchinson.
lvh.me do the same thing, just on different domains. And because two isn’t enough, I went and registered
lvho.st and configured it to do the same thing as well. The
lvho.st domain comes from local virtual host, which I took from
lvh.me and the fact that I liked the thought of using a
.st domain like
smackaho.st so I could use
ho.st. Shove the two together and you get
Even though I don’t work with subdomains currently, I still use
lvho.st for local development. Anyways, feel free to use if you have the need.
Interesting side note, this same project is the one that inspired me to write my first ruby gem, tld-cookies. It’s a nice little gem that makes it easy to set a cookie that works across all subdomains instead of just the current one.
January 12, 2012 § Leave a comment
Here is a great little Chrome extension for developers, Sight. It’s a syntax highlighter for source files that you find on the web. It’s very nifty when you find yourself perusing the source of some open source project you are thinking of using.
Tips: ‘L’ will toggle line numbers and ‘G’ will take you to a specific line number. There are lots of font and themes to choose from as well.
January 9, 2012 § Leave a comment
If you are using the observer pattern with your ActiveRecord models, you have to register the observers. I don’t see why this can’t be done automatically. I place all of my observers in
app/observers so I can easily register all of them with a few line in my
# Register all the observers in the observers folder. observers =  pattern = File.join(Rails.application.config.root, 'app', 'observers', '**', '*.rb') Dir.glob(pattern).each do |filename| observers << File.basename(filename, '.rb').to_sym end config.active_record.observers = observers