MyDaddyPuzzles and Extreme Makeover Home Edition

written by justin on September 17th, 2008 @ 10:23 AM

MyDaddyPuzzles are being used in the upcoming Season Premier of Extreme Makeover Home Edition. You can read about it here.

rails, nginx, and SSL

written by justin on September 16th, 2008 @ 09:10 AM

This week I decided to buy an ssl certificate to start using with one of my websites. The site is running Rails 2.1.1, behind nginx as a reverse proxy. It was a learning experience to be sure and I'll try to document some of the steps that I had to take to get it working.

The first trick was getting the ca-bundle.crt file to be served by nginx. This is the intermediate ca file that allows browsers to recognize your certificate authority. The trick is to concatonate your certificate file and the ca-bundle.crt file and then place them on the webserver. I found that tidbit here. With Apache, you have a server directive for the ca-bundle file so this step is not necessary.

The next trick was to get nginx to let the rails app know what protocol was being used when requests came in using ssl. The magic is this line in the nginx.conf file

 proxy_set_header X-FORWARDED_PROTO https; 
which I found here.

You also need to install the ssl_requirement plugin: ruby script/plugin install ssl_requirement. Which you can read about here.

Last, but not least, you need to tell nginx to turn on ssl and let it know where your certificates are, which is documented here.

That should about get you going with nginx, rails, and 3rd party ssl certificates.

ical is back

written by justin on September 10th, 2008 @ 10:25 AM

I found a free host for the iCal calendar of the broncodashboard.com iCal. So, it has been added back onto broncodashboard.com. I also added a scoreboard page that auto-updates with some WAC scores as well as some other teams that I like to follow. I used Hpricot, the Ruby library to gather the scores from espn.

rails printer icon helper

written by justin on August 29th, 2008 @ 02:30 PM

I had occasion to need to add a printer icon to some web pages in one of my Rails apps this week. I found a nice free icon at famfamfam.com for the printer. I added the link and javascript and it worked. I decided that it would be a nice idea to make a helper for this so I could easily duplicate it on other pages. Here it is:

# in application_helper.rb
def printer_icon(top=50, left=550)
    "<a href=\"#\" id=\"printer_icon\" style=\"position: absolute; top: #{top}px; left: #{left}px;\" onClick=\"window.print();return false;\">#{image_tag 'printer.png'} </a>"
end

#usage 
<%= printer_icon %>

Works great.

Rails 2.1

written by justin on June 20th, 2008 @ 02:50 PM

Biggest issue for me with the upgrading to Rails 2.1 from 2.0.2 seems to be the eager loading. Where I have used :include => :some_association in my find code and haven't put the full tablename.column syntax in my :conditions SQL, the table doesn't get loaded and errors occur, so I've been busy adding included table names to my conditions strings.

Rails 2.1 also seems to have different behavior when it comes to how dates are handled. If a partial date is input, I end up with some weird dates instead of seeing the multi-param error that I used to see. Not sure I'm liking that very much.

Ruby Business Days in the Future

written by justin on May 7th, 2008 @ 09:38 AM

Going the other way now


    1 def business_days_future(num,start_date=nil)
    2   # takes the number of days in the past you are looking for
    3   # like 10 business days ago
    4   start_date ||= Date.today
    5   start_day_of_week = start_date.cwday #Date.today.cwday
    6   ans = 0
    7   # find the number of weeks
    8   weeks = num / 5.0
    9   #puts "yields #{weeks} weeks"
   10     
   11   temp_num = num > 5 ? 5 : num
   12   #puts "first temp num #{temp_num}"
   13   
   14   begin
   15     
   16     ans += days_to_adjust_f(start_day_of_week,temp_num)
   17     #puts "ans in loop #{ans}"
   18     
   19     weeks -= 1.0
   20     #puts "weeks in loop #{weeks}"
   21     
   22     temp_num = (weeks >= 1) ? 5 : num % 5
   23     #puts "temp_num after loop #{temp_num}"
   24   end while weeks > 0
   25   
   26   #puts "#{start_date} - #{num} - #{ans}"
   27   days_ago = start_date + num + ans
   28   
   29 end
   30 
   31 
   32 
   33 def days_to_adjust_f(start_day_of_week,num)
   34   ansr = 0
   35   case start_day_of_week
   36   when 1
   37     if 5 == num then ansr += 2 end
   38   when 2
   39     if (4..5).include?(num) then ansr += 2 end
   40   when 3
   41     if (3..5).include?(num) then ansr += 2 end
   42   when 4
   43     if (2..5).include?(num) then ansr += 2 end
   44   when 5
   45     if (1..5).include?(num) then ansr += 2 end
   46   when 6
   47     if (1..5).include?(num) then ansr += 1 end
   48   when 7
   49     #do nothing 
   50   end
   51   return ansr
   52 end

Ruby Business Days ago

written by justin on May 7th, 2008 @ 08:57 AM

My attempt at writing a solution to finding the number of business days ago from a specific date. I am using this in a Rails App.


    1 def business_days_ago(num,start_date=nil)
    2   # takes the number of days in the past you are looking for
    3   # like 10 business days ago
    4   start_date ||= Date.today
    5   start_day_of_week = start_date.cwday #Date.today.cwday
    6   ans = 0
    7   # find the number of weeks
    8   weeks = num / 5.0
    9   #puts "yields #{weeks} weeks"
   10 
   11   temp_num = num > 5 ? 5 : num
   12   #puts "first temp num #{temp_num}"
   13 
   14   begin
   15 
   16     ans += days_to_adjust(start_day_of_week,temp_num)
   17     #puts "ans in loop #{ans}"
   18 
   19     weeks -= 1.0
   20     #puts "weeks in loop #{weeks}"
   21 
   22     temp_num = (weeks >= 1) ? 5 : num % 5
   23     #puts "temp_num after loop #{temp_num}"
   24   end while weeks > 0
   25 
   26   #puts "#{start_date} - #{num} - #{ans}"
   27   days_ago = start_date - num - ans
   28 
   29 end
   30 
   31 
   32 
   33 def days_to_adjust(start_day_of_week,num)
   34   ansr = 0
   35   case start_day_of_week
   36   when 1
   37     if (1..5).include?(num) then ansr += 2 end
   38   when 2
   39     if (2..5).include?(num) then ansr += 2 end
   40   when 3
   41     if (3..5).include?(num) then ansr += 2 end
   42   when 4
   43     if (4..5).include?(num) then ansr += 2 end
   44   when 5
   45     if 5 == num then ansr += 2 end
   46   when 6
   47     # no adj
   48   when 7
   49     if (1..5).include?(num) then ansr += 1 end
   50   end
   51   return ansr
   52 end

Capistrano

written by justin on April 23rd, 2008 @ 07:21 AM

I finally broke down and purchased the screencast for Capistrano from peepcode.com yesterday, and voila, today all of my internal apps have been redeployed using Capistrano. $9 well spent.

Broncodashboard updated for 2008 --mostly

written by justin on April 19th, 2008 @ 04:54 PM

I spent the day today updating Broncodashboard.com for the 2008 season. This entailed adding support for multiple seasons, redoing the widget and the feed that supplies info to the widget and adding logos and some details on the new schools on the schedule.

I still need to add all of the schedules for the opponents, so the full schedule link is still showing the 2007 season. All in all a good day.

jRuby Rails mssql server

written by justin on April 15th, 2008 @ 09:41 AM

Database.yml for connecting to a MS Sql Server using jdbc


development:
  adapter: jdbc
  driver: com.microsoft.jdbc.sqlserver.SQLServerDriver
  url: jdbc:microsoft:sqlserver://someserver.domain.local:1433;databaseName=some_db;AutoCommit=false;
  username: someuser
  password: somepassword

Capistrano deploy rails spin file

written by justin on February 29th, 2008 @ 04:11 PM

I had an issue where my spin file wouldn't work in my capistrano deploy recipe. It was a permissions issue, no execute permissions on my spin file. So to change the file permissions on the repository version you do the following.

To make your spin file work if it is not executable in subversion


# Set an svn property (svn:executable) on the script
svn propset svn:executable ON script/spin

Broncodashboard - more data

written by justin on October 4th, 2007 @ 10:01 PM

This evening I added something to Broncodashboard.com. I am always interested to know how the competition is doing. I decided that one way to monitor the competition is to show the combined record of all of the BSU opponents for the year. Because of my accounting background, I knew that I'd need backup for that combined record, so I listed all records of the competition below it.

It doesn't really look too good so far. The wins are only 23 versus 31 losses. Teams like Weber State, Utah State, and University of Idaho are really killing that strength of schedule (1 - 13). Hopefully the body bag games are mostly over and it will start to balance out a little more in the weeks to come.

Validations, :if and Proc.new

written by justin on September 17th, 2007 @ 03:44 PM

I learned something new today about dealing with conditional validations in Ruby on Rails. I have an Active Record (AR) class that has multiple states during its life cycle. It is initially created with a bare minimum of information. Essentially the record is created to be a place holder for future activity relating to an application that may or may not be returned.

I set up my initial validations for the information that would be available when the application is given out. That is pretty basic stuff and I've done it a number of times. It worked as expected, so I moved on to the next step.

When the application is returned we need to find that record, and update it with some additional information, and this new information needs to be validated as well. I have multiple steps of updates, so I couldn't really use the :on => (created|updated) parameter.

My first thought was that I'd have to put that validation into the Controller. I immediately got a bad taste in my mouth at the thought of what that would look like. I thought to myself, "self, we'd better check Agile Web Development with Rails" before we do anything.

What I found was the :if parameter of the Validation Helpers in Rails. The :if parameter causes the validation to be run only if the boolean returned is true. The :if can be either:

  • a method call using a Symbol, which is passed the AR object as the parameter i.e. :if => :some_method_name
  • a string, which is evaluated i.e. :if => %q{"a small sentence".length}
  • a proc object, which is called with the AR object as the parameter i.e. :if => Proc.new {|u| u.classify}
I set a flag on my object telling it what step I'm on and then my conditional validations queue on that variable and the correct information is validated at the correct time.

BroncoDashboard.com gets WAC Info

written by justin on September 4th, 2007 @ 09:05 AM

Over the weekend I added functionality to BroncoDashboard to show the WAC standings and also the upcoming games for this week.

Rails select options

written by justin on August 24th, 2007 @ 03:48 PM

I am developing an application where I have a dropdown list of hours worked on a project. The spec is that it should be in quarter hour increments using a decimal notation. So, 0, 0.25, 0.50, 0.75, etc. The select helper in rails takes an array of arrays that would look like this: [[0,0],[.25,.25], etc...]. I needed to generate that array and this is what I came up with:

  def labor_hours
    ar = []
    (0..39).each do |n| 
    	a = n.to_f
    	b = a + 0.25
    	c = a + 0.50
    	d = a + 0.75
    	ar.push([sprintf('%0.2f', a), a],[sprintf('%0.2f', b), b],[sprintf('%0.2f', c), c],[sprintf('%0.2f', d), d])
    end
    ar.push(["40.00",40])
  end

I start by creating the array that I'm going to return. I then use a range and a block to loop over 0 to 39. I have to made sure that I convert the integers to floats and add my partial hours. I then create my arrays for this integer and add them to the final array. At the last second, I add the last array for a nice even 40 hours. Works pretty well. I used sprintf to format the floats so that everything has 2 decimal places instead of some with one and some with two.

So, my view code looks like this:

<%= form.select 'labor', labor_hours, :include_blank => true %>