Jun 25
RJS redirect_to params contain &
I came across this today when I added a parameter to a RJS page.redirect_to call. It appears that prototype or rails is not properly handling the parameters when there are more than 2. I got around it by using a defined route in the routes.rb that looks like this:
map.route_name 'controller_name/action_name/:foo_id', :controller => 'controller_name', :action => 'action_name'
I had to change the redirect_to in my RJS file from this:
page.redirect_to page_recrawl_path(:foo_id => @foo_id, :bar_id => @bar_id, :bang => true)
To this:
page.redirect_to page_recrawl_path(@foo_id, {:bar_id => @bar_id, :bang => true})
Jun 20
Jailbroken iPhone 3g 3.0 firmware
Thanks to the iPhone Dev-Team for releasing the Pwnage Tool 3.0, I was able to upgrade to the jailbroken 3.0 firmware. The install process went very smoothly. I followed a similar procedure to what they demo’d on their youtube video. Some of my favorite jailbroken apps don’t work with the new firmware. Here are my findings:
Apps
- Cycorder: Yes
- OpenSSH: Yes
- SBS Settings: Yes
- Icy: Yes
- Cydia: Stability issues
- PdaNet: Yes, but possible stability issues
- Winterboard: Mostly, some themes weren’t stable
- Pwnplayer: no
- Snapture: no
- NES: no
Overall the new 3.0 features were well worth upgrading. The only app I will really miss is Pwn player and I hope there will be a similar replacement (it’s no longer maintained). As for the other apps, I’m sure they’ll be getting updates soon. Please let me know what apps you have working.
1 commentJun 2
Ruby on Rails background process: MySQL server has gone away
Using workling and starling to handle background tasks is very nice. Check out this link for info on setting it up. However, I encountered the error: “Mysql::Error: MySQL server has gone away” when accessing an external DB tables like users and roles. I found that someone recommended using verify_active_connections!, however the table the connection error occurred on was a HABTM table ‘user_roles’. I found that verify_active_connections will use the scope of the model/object you invoke it on. So the final solution was to do the following:
user.roles.verify_active_connections!
This will make sure users, user_roles, and roles all have valid connections. This error occurred using rails 2.0.1. It may be fixed in newer versions of ActiveRecord.
No commentsApr 3
Passing around arguments in rake tasks
So I had the need to pass arguments from one rake task to another. This is easy to do from the command line, but gets a bit tricking when its being called from within another rake task. So here is the task that is being called:
1 namespace :database do 2 desc %{Dump DB to create restore data in case of error} 3 task :dump, :tables, :needs => [:setup] do |t, args| 4 filename = "#{DB_NAME}#{'-'+args.tables.values.join('-') unless args.tables.nil?}-#{DB_DUMP_TIME}.sql.gz" 5 puts "Dumping the database to '#{filename}'" 6 7 cmd = "" 8 cmd << "echo 'SET AUTOCOMMIT=0;'|gzip> #{filename};" 9 cmd << "echo 'SET FOREIGN_KEY_CHECKS=0;'|gzip>> #{filename};" 10 cmd << "mysqldump -u #{DB_USER} -p'#{DB_PASSWORD}' " 11 cmd << "-h #{DB_HOST} #{DB_NAME} " 12 cmd << "--tables #{args.tables.values.join(' ')} " unless args.tables.nil? 13 cmd << "|gzip>> #{filename};" 14 cmd << "echo 'SET FOREIGN_KEY_CHECKS=1;'|gzip>> #{filename};" 15 cmd << "echo 'COMMIT;'|gzip>> #{filename};" 16 cmd << "echo 'SET AUTOCOMMIT=1;'|gzip>> #{filename};" 17 18 if system(cmd) 19 puts "Successfully ran databse:dump to restore run:" 20 puts "rake database:reload DB_DUMP_TIME='#{DB_DUMP_TIME}'" 21 else 22 puts "rake database:dump was unsuccessful" 23 end 24 end
So this task is used to dump the database for our app. I added the ability to pass in an optional hash to allow it to only dump the specified tables. Here are the important lines to note.
task :dump, :tables, :needs => [:setup] do |t, args|
The “tables” argument specifies what arguments are being passed in. The “t” and “args” block defines the task and the arguments. Your “tables” hash is now available through “args” values.
Now when I call this task from within another rake task, I can do something like this.
tables = {:table1 => 'users', :table2 => 'roles'} Rake::Task["database:dump"].invoke(tables)
Note, I define the hash outside of the invoke because it is used by two other tasks, but it could be directly passed to the task as an argument.
No commentsNov 15
Ubuntu Intrepid Sound Juicer MP3 support
After upgrading to Intrepid Kaudiocreator was no longer available, so I decided to give Sound Juicer a try. When I went to the preferences to change the encoding to MP3, I found that it was missing. So I clicked edit and found that the option was there for MP3 output. Turns out the option will not show up until you install this package - gstreamer0.10-plugins-ugly-multiverse. So just type the following and your set.
sudo apt-get install gstreamer0.10-plugins-ugly-multiverse
Nov 6
Rails, Time Zones, and Scheduling
The time zone support in rails has vastly improved since the integrated TZInfo into Rails 2.1. However if you have any scheduled tasks stored in your DB, you’ll notice it quickly breaks down. Here is a short and sweet explanation:
I have an app that delivers system notification to users at a specified time they set. I have a field in my “reminders” table called “deliver_at” which is a datetime field. The data is stored as a UTC timestamp. Everything works as expected until daylight savings time, or the user changes their time zone. This is because the “deliver_at” timestamp has no context of what offset was used when the record was created. So when daylight saving rolls around your messages are now delivered an hour off. Or if the user moves to a new timezone, it will be off by x hours. Here is the solution I came up with.
I added a field to the “reminders” table called “offset”.
add_column :reminders, :offset, :decimal, :precision =>; 4, :scale => 2, :null =>; false, :default => 0.0
This field is recorded when the record is first created. You can override the attribute using write_attribute and read_attribute, but I was already doing a seporate update call, so I just stuck it there. *Important, make sure you are setting the Time Zone on login.
self.update_attributes!(:delivered_at => Time.zone.now, :next_reminder_at => date, :offset => user_utc_offset)
Here is my user_utc_offset method:
def user_utc_offset TimeZone[self.user.timezone].utc_offset.to_f / 1.hour.to_f end
I’m using floats because not all the offsets are whole numbers. Same reason I used a decimal field in the DB. Then when I calculate my next reminder delivery, I basically do this:
offset_adjustment = adjust_for_difference_in_offsets case self.reminder.repeat when "daily" next_reminder = reminder.deliver_at + 1.day - offset_adjustment.hours when "weekly" next_reminder = reminder.deliver_at + 7.days - offset_adjustment.hours when "monthly" next_reminder = reminder.deliver_at + 1.month - offset_adjustment.hours end
I have a case statement to calculate daily, weekly, and monthly reminders. Here is the method to calculate the offset difference.
def adjust_for_difference_in_offsets user_utc_offset - self.offset.to_f end
I use the above for the mailer, but we need to do the same for displaying the datetime stamp to the user. So I created a helper method that does like so:
def reminder_repeat_datetime(reminder) offset_adjustment = reminder.adjust_for_difference_in_offsets case reminder.repeat when "monthly" repeat = "#{reminder.deliver_at.strftime('%d')}th" when "weekly" repeat = "#{reminder.deliver_at.strftime('%A')}s" when "daily" repeat = "Daily" when "once" repeat = "#{reminder.deliver_at.strftime('%B %d, %Y')}" end "#{repeat} at #{(reminder.deliver_at - offset_adjustment.hours).strftime('%I:%M %p')}" end
A lot of the above is just pretty formatting of the datetime for the user. You could most likely just get away with something like the last line. That is called from my index view in my reminders controller like so:
<%= reminder_repeat_datetime(reminder) %>
So now no matter if the offset changes because of DST or the user changes their timezone, the offset will be calculated and applied to the datetime field.
No commentsNov 4
My simple ruby backup
After using a Linux dev laptop for almost a year, I went back to a MacBook Pro for work. I had forgotten the pains of finding a good backup solution for a Mac. I needed something that would incrementally back up my work laptop and my home Linux box to the same external hard drive. After trying various solutions(Time Machine, CCC, iBackup, etc), they all seemed to either not work at all or hog up way too much room on the drive. So I decided to just write my own backup solution using Ruby and Rsync.
This initial release is very basic and only requires you have ruby and rsync installed. It supports Mac or Linux and can back up to any drive that supports hard links (ext2, ext3, hfs, hfs+). Future releases will include integration with s3 as well as Cron scheduling. For now I just wanted to share this with others having similar gripes.
Steps to getting up and running:
1. Download Ruby Backup Zip or Ruby Backup tarball.
2. Extract the contents of the above package.
3. Edit the backup_settings.yml.sample file and save it as backup_settings.yml
4. run the script via “ruby ruby_backup.rb” or “./ruby_backup.rb”
Oct 23
Gem cache search depricated in boot.rb
I was sick of getting the following deprication warning running rubygems version 1.3.0 and a rails 1.2.6 project.
“Gem::SourceIndex#search support for String patterns is deprecated
./script/../config/boot.rb:20 is outdated”
So I made some modifications to the boot.rb file so that it checks for the rails gem similar to the rails 2.x apps. The gem itself is still loaded in the 1.2 fashoin. Here is a diff on the changes
20,29c20,25 < rails_gem = Gem.cache.search('rails', "=#{rails_gem_version}.0").sort_by { |g| g.version.version }.last < < if rails_gem < gem "rails", "=#{rails_gem.version.version}" < require rails_gem.full_gem_path + '/lib/initializer' < else < STDERR.puts %(Cannot find gem for Rails =#{rails_gem_version}.0: < Install the missing gem with 'gem install -v=#{rails_gem_version} rails', or < change environment.rb to define RAILS_GEM_VERSION with your desired version. < ) --- > begin > gem "rails", rails_gem_version > gem_dir = Gem.path.select{|p| File.directory?("#{p}/gems/rails-#{rails_gem_version}")}.first > require "#{gem_dir}/gems/rails-#{rails_gem_version}/lib/initializer" > rescue Gem::LoadError => load_error > $stderr.puts %(Missing the Rails #{rails_gem_version} gem. Please `gem install -v=#{rails_gem_version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
Hope this helps with those annoying warnings.
4 commentsOct 16
VIM tricks: comment block of code
A friend of mine (Shovan) was asking how to comment out a block of code using VIM. This can easily be done using the “Visual Block” feature of VIM. To enter visual block mode use “Ctrl + v”. At this point you can use “j” or “k” to highlight the lines in which you want to comment out. Then use “Shift + i” to enter insert mode. Type the respective comment character (# for ruby and bash, // for php), then hit “Esc” to exit insert mode.
1 commentOct 9
Generate a Rails version specific project
If you have rails 2.x installed but need to generate a rails 1.x project, you can use the version tag as follows:
rails _1.2.6_ project
Just replace the 1.2.6 with whatever version you need, and make sure you use underscores before and after the version.
No comments