Rails on Ubuntu 9.10 with Ruby 1.9.1
Here's a short list of what I needed to get Rails 2.3.5 running on Ubuntu 9.10 with unixodbc support on Ruby 1.9.1
install ubuntu 9.10 sudo apt-get install ssh libssl-dev libreadline5-dev git-core mysql-server pdftk zlib1g zlib1g-dev libmysqlclient16-dev haproxy unixodbc unixodbc-dev freetds-common freetds-dev tdsodbc nginx freetds-bin postfix install ruby1.9.1 from source ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p376.tar.gz update rubygems sudo gem update --system http://wiki.rubyonrails.org/database-support/ms-sql ruby-odbc from source http://www.ch-werner.de/rubyodbc/ruby-odbc-0.9997.tar.gz sudo gem install rails mysql prawn vpim activerecord-sqlserver-adapter dbd-odbc dbi daemons configure unixodbc and freetds, and haproxy and nginx install mongrel gem 1.1.6 sudo gem install mongrel --source http://gems.rubyinstaller.org need to add self signed cert https://help.ubuntu.com/8.04/serverguide/C/certificates-and-security.html add deploy user and certificates for git repo etc.
cap deploy:web:enable
I have finally decided to start putting up a warning page when doing maintenance on one of my sites. I decided that I wanted to use capistrano to get the job done. I checked the cap -T task list and noticed that there was already one there. It was pretty bleak looking so I decided to just roll my own page and put up a task to change the filename when needed and it would be picked up by nginx and displayed. I made up a page and put in on my production server under the system folder in the shared path and named it m.html. I then wrote up a couple of cap tasks to manage the renaming of the file and put them into my deploy.rb file:
desc "Turn on Maintenance Mode"
task :maint_on, :hosts => @web do
run "mv #{shared_path}/system/m.html #{shared_path}/system/maintenance.html"
end
desc "Turn off Maintenance Mode"
task :maint_off, :hosts => @web do
run "mv #{shared_path}/system/maintenance.html #{shared_path}/system/m.html"
end
disclaimer: there is probably already a way to do this using the original deploy:web:enable, but I didn't want to figure it out.
MyDaddyPuzzles and Extreme Makeover Home Edition
MyDaddyPuzzles are being used in the upcoming Season Premier of Extreme Makeover Home Edition. You can read about it here.
rails, nginx, and SSL
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
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
# 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
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
Going the other way now
1 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 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
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 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 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
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
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
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
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
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
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}
Twitter