Archive for May, 2008

Became a Licensed Private Pilot


Fixing rails Cookie Overflow error

It looks like the rails session only stores 4k of data. If you have a large form, it might not work.

Looks like I?ll probably have to change to the database session

  1. this guy has a solution
  2. then you need to do this

he changed to database session helpful forum answer link

Generate PDFs

To generate pdfs:

Rails date manipulation / styling

To style dates in rails use strftime function.

For example: <%= comment.created_at.strftime(?%Y?) %>

Though, this might technically be ruby code not rails.

Importing CSV in rails with fastercsv

UPDATE: Here is updated code

Definitely, use fastercsv

The best way to explain this is with code. Here it is. Copy and paste. Copy and paste.

restful_authentication

Ryan Bates is the man and so is Rick Olson who created the plugin. Actually Mr. Olson is even more the man.

Exporting as csv in rails

UPDATE: I made some code changes that seemed to work better for my needs. Plus, it places everything in the respond_to .csv block. Check the pastie

Recipe 35 of rails recipes does not seem to work any longer on rails 2.0. I believe you have to now strictly use FasterCSV - which is supposed to be much better anyhow.

fastercsv docs

fastercsv project page

Here is a great tutorial on how to get it working

to install fastercsv, you must install the gem. Do the command: sudo gem install fastercsv

Date Range SQL clause in ruby

Here is how to do it:

Date range to SQL clause

(6.months.ago.to_date..1.year.ago.to_date).to_s(:db) => ?BETWEEN ?2005-07-27? AND ?2005-01-22??

taken from here

Gruff graphs

The following are some good resource for how to use Gruff graphs:

The last one was HUGELY helpful:

def stats g = Gruff::Line.new(?580x210?) g.theme = { :colors => [?#ff6600?, ?#3bb000?, ?#1e90ff?, ?#efba00?, ?#0aaafd?], :marker_color => ?#aaa?, :background_colors => [?#eaeaea?, ?#fff?] }

g.hide_title = true
g.font = File.expand_path('path/to/font.ttf', RAILS_ROOT)

range = "created_at #{(12.months.ago.to_date..Date.today).to_s(:db)}"
@users = User.count(:all, :conditions => range, :group => "DATE_FORMAT(created_at, '%Y-%m')", :order =>"created_at ASC")
@votes = Vote.count(:all, :conditions => range, :group => "DATE_FORMAT(created_at, '%Y-%m')", :order =>"created_at ASC")
@bookmarks = Bookmark.count(:all, :conditions => range, :group => "DATE_FORMAT(created_at, '%Y-%m')", :order =>"created_at ASC")

# Take the union of all keys & convert into a hash {1 => "month", 2 => "month2"...}
# - This will be the x-axis.. representing the date range
months = (@users.keys | @votes.keys | @bookmarks.keys).sort
keys = Hash[*months.collect {|v| [months.index(v),v.to_s] }.flatten]

# Plot the data - insert 0's for missing keys
g.data("Users", keys.collect {|k,v| @users[v].nil? ? 0 : @users[v]})
g.data("Votes", keys.collect {|k,v| @votes[v].nil? ? 0 : @votes[v]})
g.data("Bookmarks", keys.collect {|k,v| @bookmarks[v].nil? ? 0 : @bookmarks[v]})

g.labels = keys

send_data(g.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "site-stats.png")

end

Sort by date - year, month, and day

Here is how to sort by date thanks to Ryan Bates

Also see episode 70