Technology Agnostic Rubyist : Open Source Lights The Way

Tuesday, November 25, 2008

Write a nationality gem or plugin

This is just a reminder to self to write something that ties in with Ruby inflectors and the country_select helper.

Friday, November 14, 2008

Dirty numbers

/$#@!

This is a pretty basic programming concept but I haven't touched this blog in a while here we go...

Say you're working with numbers in a database field. Those numbers were keyed in by people using a different system, and the other system somehow stored non-numeric values in a numeric field.

Since Ruby does not have strong typing, an easy way to check and see if a field is an integer is to compare its integer value with its actual value.

if (somefield.to_i == somefield)

For example:
n = 1234567
if (n.to_i==n) => true

If the value of n is not keyed properly the equality test returns false
n = "12345x67"
if (n.to_i=="12345x67") => false

Easy!

Monday, March 24, 2008

Web site maintenance - global message

About once a week we upload and update all of the products in the database.

This intensive process takes about 15 minutes, during which time any pages that requires database access may time out.

There has to be a simple way to establish a "web site maintenance" message on the site while these processes run. Additionally cached pages need to be expired if they contain updated products.

I'm still working on a way to achieve this result.

Monday, February 25, 2008

SSL Plugin for Rails

I'm using the acts_as_authenticated plugin for ruby on rails.

Unforunately it falls short in a few key areas for an ecommerce site. An obvious one is that it does not ensure all login pages are SSL encrypted.

So I wrote the simple method (explained below) and put it in the application.rb file.

in application.rb...
def redirect_to_ssl

redirect_to :protocol => "https://" unless (request.ssl? or local_request? or request.server_port.to_i!=80)

end

The reason for the last check of the server port != 80 is twofold. First, if the server port is equal to 80 then I am probably running in production mode. However if the server port is not 80 then I am either running a development box, or I'm already on port 443. Ports 80 and 443 are the standard port used in HTTP and SSL.

Any time I want to ensure that a page is SSL encrypted, I put the following bit of code in my controller as a before_filter.

in my SSL protected controller...
class MySslController < ApplicationController

before_filter :redirect_to_ssl, :except => actions_that_do_not_require_ssl




Voila! (or "Wahla!" as we americans like to say) it works.

Friday, February 22, 2008

State abbreviations in a database

I use an excellent state_select Ruby plugin to generate nice looking dropdown lists for web pages.

Unfortunately this plugin uses full state names instead of abbeviations, both in the view and in the values that are submitted to the database.

I want
<option value="NY">New York

instead of
<option value="New York">New York

The change I want is easy to achieve by modifying /lib/state_select.rb

Comment out the values for US_STATES and add this bit of code instead. Note Puerto Rico and outlying territories have been omitted, since these are considered international locations in my program.
US_STATES=[
[ "Alabama", "AL" ],
[ "Alaska", "AK" ],
[ "Arizona", "AZ" ],
[ "Arkansas", "AR" ],
[ "California", "CA" ],
[ "Colorado", "CO" ],
[ "Connecticut", "CT" ],
[ "Delaware", "DE" ],
[ "District Of Columbia", "DC" ],
[ "Florida", "FL" ],
[ "Georgia", "GA" ],
[ "Hawaii", "HI" ],
[ "Idaho", "ID" ],
[ "Illinois", "IL" ],
[ "Indiana", "IN" ],
[ "Iowa", "IA" ],
[ "Kansas", "KS" ],
[ "Kentucky", "KY" ],
[ "Louisiana", "LA" ],
[ "Maine", "ME" ],
[ "Maryland", "MD" ],
[ "Massachusetts", "MA" ],
[ "Michigan", "MI" ],
[ "Minnesota", "MN" ],
[ "Mississippi", "MS" ],
[ "Missouri", "MO" ],
[ "Montana", "MT" ],
[ "Nebraska", "NE" ],
[ "Nevada", "NV" ],
[ "New Hampshire", "NH" ],
[ "New Jersey", "NJ" ],
[ "New Mexico", "NM" ],
[ "New York", "NY" ],
[ "North Carolina", "NC" ],
[ "North Dakota", "ND" ],
[ "Ohio", "OH" ],
[ "Oklahoma", "OK" ],
[ "Oregon", "OR" ],
[ "Pennsylvania", "PA" ],
[ "Rhode Island", "RI" ],
[ "South Carolina", "SC" ],
[ "South Dakota", "SD" ],
[ "Tennessee", "TN" ],
[ "Texas", "TX" ],
[ "Utah", "UT" ],
[ "Vermont", "VT" ],
[ "Virginia", "VA" ],
[ "Washington", "WA" ],
[ "West Virginia", "WV" ],
[ "Wisconsin", "WI" ],
[ "Wyoming", "WY" ] ] unless const_defined?("US_STATES")
But what happens to the data that has already started been saved with long unabbreviated state names? I need to update that too. I could have written a migration file to do this - but instead just used quick and dirty sql update statements.

This is the code that I used. It's very straightforward, but I'm posting it here anyway with the hope that someone can copy/paste it and save themself the hassle of writing 50+ updates from scratch.
update addresses set state = 'AL' where [state]= 'Alabama'
update addresses set state = 'AK' where [state]= 'Alaska'
update addresses set state = 'AZ' where [state]= 'Arizona'
update addresses set state = 'AR' where [state]= 'Arkansas'
update addresses set state = 'CA' where [state]= 'California'
update addresses set state = 'CO' where [state]= 'Colorado'
update addresses set state = 'CT' where [state]= 'Connecticut'
update addresses set state = 'DE' where [state]= 'Delaware'
update addresses set state = 'DC' where [state]= 'District Of Columbia'
update addresses set state = 'FL' where [state]= 'Florida'
update addresses set state = 'GA' where [state]= 'Georgia'
update addresses set state = 'HI' where [state]= 'Hawaii'
update addresses set state = 'ID' where [state]= 'Idaho'
update addresses set state = 'IL' where [state]= 'Illinois'
update addresses set state = 'IN' where [state]= 'Indiana'
update addresses set state = 'IA' where [state]= 'Iowa'
update addresses set state = 'KS' where [state]= 'Kansas'
update addresses set state = 'KY' where [state]= 'Kentucky'
update addresses set state = 'LA' where [state]= 'Louisiana'
update addresses set state = 'ME' where [state]= 'Maine'
update addresses set state = 'MD' where [state]= 'Maryland'
update addresses set state = 'MA' where [state]= 'Massachusetts'
update addresses set state = 'MI' where [state]= 'Michigan'
update addresses set state = 'MN' where [state]= 'Minnesota'
update addresses set state = 'MS' where [state]= 'Mississippi'
update addresses set state = 'MO' where [state]= 'Missouri'
update addresses set state = 'MT' where [state]= 'Montana'
update addresses set state = 'NE' where [state]= 'Nebraska'
update addresses set state = 'NV' where [state]= 'Nevada'
update addresses set state = 'NH' where [state]= 'New Hampshire'
update addresses set state = 'NJ' where [state]= 'New Jersey'
update addresses set state = 'NM' where [state]= 'New Mexico'
update addresses set state = 'NY' where [state]= 'New York'
update addresses set state = 'NC' where [state]= 'North Carolina'
update addresses set state = 'ND' where [state]= 'North Dakota'
update addresses set state = 'OH' where [state]= 'Ohio'
update addresses set state = 'OK' where [state]= 'Oklahoma'
update addresses set state = 'OR' where [state]= 'Oregon'
update addresses set state = 'PA' where [state]= 'Pennsylvania'
update addresses set state = 'RA' where [state]= 'Rhode Island'
update addresses set state = 'SC' where [state]= 'South Carolina'
update addresses set state = 'SD' where [state]= 'South Dakota'
update addresses set state = 'TN' where [state]= 'Tennessee'
update addresses set state = 'TX' where [state]= 'Texas'
update addresses set state = 'UT' where [state]= 'Utah'
update addresses set state = 'VT' where [state]= 'Vermont'
update addresses set state = 'VA' where [state]= 'Virginia'
update addresses set state = 'WA' where [state]= 'Washington'
update addresses set state = 'WV' where [state]= 'West Virginia'
update addresses set state = 'WI' where [state]= 'Wisconsin'
update addresses set state = 'WY' where [state]= 'Wyoming'

Visually impaired software development

This is my work blog.

I will present ideas (very) informally, as well as shortcuts and tips that I learn and use for application development.

Since working at Lighthouse International during 2006 and 2007 I have become enamored with Ruby as a programming language. Mainly I love the fact that it's open source, though I'm still using Microsoft SQL Server for the databased operations and have been happy with this database server for a long time.

The idea for Ruby on Braille came to me when as a team, we three developers, (Mike Hill, Rathnavel Kandaswami, and myself) were toying with the idea of building something new, a program that had never been built before. As is often the case with open source software there are a lot of free resources, but it can be hard to separate signal from noise and often as developers we must simply "roll our own" code, modify existing programs, and evaluate the effectiveness and reliability of free solutions.

So while I don't plan on offering any free solutions I will tell you about my experience as a software developer.

Tweets

Contributors