SyntaxHighlighter

Tuesday, July 05, 2011

Set a default expires_in value for memcache

I bet there is an easier way to do this, but here's what I came up with. I didn't want to cache items forever (which is the default with memcache and rails). I wanted to cache items for 6 hours, max. To do this, I wrote a patch and if the :expires_in option is missing, it will set it to 21600. I would love to hear if there is a better way...


module ActiveSupport
module Cache
class Store

private

# Original code from ActiveSupport::Cache::Store
#
# def expires_in(options)
# expires_in = options && options[:expires_in]
# raise ":expires_in must be a number" if expires_in && !expires_in.is_a?(Numeric)
# expires_in || 0
# end

def expires_in(options)
expires_in = options && options[:expires_in]
raise ":expires_in must be a number" if expires_in && !expires_in.is_a?(Numeric)
expires_in || 6.hours
end

end
end
end

Monday, May 02, 2011

Brew/RVM/PSQL/Bundler Installation for a MacBook Pro

Install XCode - if you do this from disc, you'll need to upgrade later (version 3.2 that comes on snow leopard disc has bugs). You can download and start with the new one. It's 3.2 GB and could take a while to download. Do this first.

Install Brew. Follow the instructions here (https://github.com/mxcl/homebrew). I used the ruby script. If you have permission errors, you may need to do the following:

sudo chown -R $USER /usr/local


If you're OK, then install git

brew install git


If you have errors at this point, it may be due to XCode not being installed properly.

brew install postgresql

If you have an older system you may run into conflicts. Try 'brew doctor'

Follow all the instructions that show up after the postgres build. This will force the db to start up right away and set the psql function so you can access the db from command line.

# install rvm

bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head ) # The first time you install RVM, you must put the following line into your ~/.bash_profile at the very end, after all path loads etc: [[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session. # Add ruby rvm install 1.9.2 rvm install 1.8.7 rvm install ree For each project, create a .rvmrc file - this goes in the root of the rails app directory. More Show hidden files on your mac defaults write com.apple.Finder AppleShowAllFiles YES restart launcher

RVM configuration


.rvmrc files - add ruby version and gemset per project

gem install bundler - bundler should be a gem that you can access from any rvm configuration - used to generate Gemfile and Gemfile.lock.

* rvm instructions for 10.10 http://giantflyingsaucer.com/blog/wp-trackback.php?p=1792

Monday, April 11, 2011

Very true! Makes a change to see someone spell it out like that

I just got this 'comment' on a wordpress blog I write (unrelated to web development). After doing a quick search on google for this phrase: Makes a change to see someone spell it out like that, you can see a lot of other blogs have received the same post.

There's an interesting little code in front of it. It's random, and never the same on the blog. Mine was 'TRuxcI'.

Makes me think someone is out there testing to see if they can get you to add a special code to your blog. If so, they'll probably spam you.

Just delete them.

Sunday, April 03, 2011

String Truncate

Here's a truncate method that's a little smarter than the one that comes with rails. It won't cut off in the middle of a word. Just put this code in a file (like string_initializers.rb) and put that your initializers folder under config.



class String

def truncate length
if length > 0
self.strip!
if self.length > length
out_array = []
words = self.split(' ')
for word in words
if ((out_array + [word]).join(' ').length) < length
out_array << word
else
break
end
end
string_to_return = out_array.join(' ')

# in case truncate is too short
if string_to_return.length == 0
string_to_return = self[0..length]
end

# remove any trailing spaces.
string_to_return.strip!

# special last characters
if string_to_return[-1,1] == '.'
string_to_return += '..'
elsif string_to_return[-1,1] == ','
string_to_return.chop!
string_to_return += '...'
else
string_to_return += '...'
end
return string_to_return
else
return self
end
end
end
end

Wednesday, January 05, 2011

Deleting objects in Rails - using deleted_at

I wanted to allow a user to delete an object, but also to keep the object in the database so other elements could continue to refer to it. Basically, I didn't want ActiveRecord to destroy the object, just hide it. Forever.

This was in a content management project where a site may have many pages. Users were able to delete the page, and I wanted to keep the "activity log" complete. So I could refer to the page, but not show it. "Jim deleted the page 'welcome'", for example. Where welcome is the @page.name.

The page name was unique and I didn't want to do anything too complicated to ensure the names were unique - if there was an existing name already, don't allow another. But, if a deleted page had the name 'Welcome', I didn't want to see a validation error.

This is an easy situation to handle...

First, add a mark_deleted method in the Model.


def mark_deleted
self.deleted_at = Time.now
end

def mark_deleted!
mark_deleted
self.save
end


For the name, set the uniqueness validation to have a scope



validates_uniqueness_of :name, :scope => [:parent_id, :deleted_at]



You'll need to add the deleted_at column via a migration




class AddDeletedAtToObject < ActiveRecord::Migration
def self.up
add_column :objects, :deleted_at, :datetime
end

def self.down
add_column :objects, :deleted_at
end
end



When you call the 'destroy' method in the controller, instead of object.destroy!, call object.mark_deleted!

And when referring to parent.objects, call parent.objects.not_deleted, instead.


scope :not_deleted, where('objects.deleted_at is null')