August 2010
1 post
validates_presence_of
A handy little method to ensure that one field, another or both are present. def self.validates_presence_of_either(*attrs) options = { :message => "One of #{attrs.to_sentence} must be set", :on => :save, :error_key => attrs.collect { |a| a.to_s }.join("_") } options.update(attrs.pop) if attrs.last.is_a?(Hash) send(validation_method(options[:on])) do |record| ...
Aug 1st
March 2010
1 post
Updated breadcrumbs [+ better titles!]
Just a quick post, I’ve been tweaking my breadcrumbs mixin, and I think I’ve got it as good as it’s gonna get (hah!).. I’ll just give you guys the code: module BreadcrumbHelper def self.included receiver receiver.extend ClassMethods end module ClassMethods def breadcrumb name, options = {} before_filter options do |controller| ...
Mar 19th
January 2010
1 post
I recently had to include a breadcrumb style navigation system on a site I’m working on. You know the score… Like on a forum: Home > Random Crap > My new pet narwhal Anyway, I found a few gems and plugins out there which help out with this, but nothing really nailed it for what I needed. My requirements were: Simple to implement Smartness. I don’t want to write config...
Jan 31st
September 2009
2 posts
Adding markdown to your project
A quick one this time. I’ve just gone about implementing the following setup, which is a super easy way to add Markdown support in your views (and, well, anywhere). We’ll be using the RDiscount gem for this. There are other gems like Bluecloth, but RDiscount has been consistently good for a long time now. config/environment.rb add: config.gem "rdiscount" then install the new gem...
Sep 28th
Validating images with Paperclip
If you don’t already use it, check out Thoughtbot’s Paperclip first. Doing this is always a huge pain, so I wrote a mixin which makes it super easy. It’s based upon some model code I referenced online before, but with a crucial bug fix relating to a situation where an exception would be raised if you posted a form without selecting an image. So, without further ado: ...
Sep 27th
March 2009
1 post
Damn
The itegration between Rails and (I hate to say it) AJAX is really awesome. I’ve created something that would take an afternoon to make in PHP in about 2 minutes. Wow.
Mar 11th
February 2009
6 posts
MacRuby
This looks interesting [Apple.com] Develop OS X-native apps with Ruby + Cocoa bindings? Sweet.
Feb 20th
Another Challenge
Okay, so I found another challenge I could do, this time on RosettaCode which deals with file I/O in Ruby. Basically, there’s a 10,000 line log file to parse and gather some data from it. This one was really, really simple and took about 5 minutes to do. license_out = 0 record_out = 0 record_times = [] File.open("licenselog.txt").each do |line| if line.include?("OUT") license_out +=...
Feb 15th
Yield!
Wow, this is cool: def test_block puts "Beginning...." yield if block_given? yield if block_given? puts "Ended!" end test_block do puts "I am being called from within test_block!" end
Feb 15th
More Euler..
Just finished Project 4. This isn’t the most concice code, because i functionised it, but it works quite well. class Numeric def is_palindrome? s_num = self.to_s half = s_num[0..(s_num.length/2)-1].to_s s_num == half + half.reverse or (s_num.length%2 == 1 and s_num == half + s_num[s_num.length/2,1].to_s + half.reverse) end end def find_largest_palindrome...
Feb 15th
Project Euler
So, I’m gonna do 4 project Euler challenges to get me the hang of this Ruby thing. Well, get the hang of dealing with numbers, anyway. I’ve finished the first 3 challenges already, so here you go: Problem 1 The first challenge is really easy. You have to add the sum of the multiples of 3 and 5 below 1000. # Establish minimum and maximum minimum = 1 maximum = 1000 counter =...
Feb 15th
Hello, world.
Hi everyone. I am a PHP developer with about 5 years experience. I’ve built everything from online stores to forums, but yesterday I realised that all this time I had been fighting PHP to do what I feel it should do naturally. I was spending more time trying to wrangle with the terrible Zend Framework documentation than I was working on the rest of my project. I’d had enough. Then I...
Feb 15th