On the fly field encryption/decryption
"ActsAsSecure adds an ability to store ActiveRecord model's fields encrypted in a DB. When a model is marked with acts_as_secure, the :binary type fields are recognized as needed to be stored encrypted. The plugin does before_save/after_save/after_find encryption/decryption thus making it transparent for a code using secured models.
The plugin supports a master key approach as well as individual records encryption keys. It does not contain any crypto provider but allows to plug in any external one as long as it supports encrypt/decrypt methods."
http://revolutiononrails.blogspot.com/2007/04/plugin-release-actsassecure.html
The site has some other goodies too.
Posted in Rails | no comments |
Message queues in ruby
Sometimes you run across neat and interesting libraries when you are reading up on something completely different. Case in point, via another blog, I stumbled on Reliable Messaging with Ruby. This looks to solve a problem I haven’t yet run into, but I know I’ll find a use for it in the future (maybe for a game I’m slowly working on, or a work project). As of today, there haven’t been any commits to the project for two years, but it’s entirely possible there are no bugs (except for the one I found below) and the author considers it feature complete.
Current feature list:
- Simple API.
- Transction processing.
- Disk-based and MySQL message stores.
- Best effort, repeated and once-only delivery semantics.
- Priority queues, message expiration, dead-letter queue.
- Message selectors.
- Local and remote queue managers using DRb.
Installation was a breeze
$ sudo gem install reliable-msg -yFor testing, I chose a disk queue, but the library also supports mysql for the message queue.
Note: I used sudo here as my normal user has no access to write the config file - were I to care about this install, I’d spend the time figuring out how to run the queue manager as a non-privileged user.
$ sudo queues install disk foo
Created queues configuration file: /Library/Ruby/Gems/1.8/gems/reliable-msg-1.1.0/queues.cfgStart up the queue manager:
$ sudo queues manager start
Loaded queues configuration from: /Library/Ruby/Gems/1.8/gems/reliable-msg-1.1.0/queues.cfg
Using message store: disk
Accepting requests at: druby://localhost:6438I had some issues with drb ACL’s that appear to be due to ACL ordering. After changing line 238 of queue-manager.rb from:
@drb_server = DRb::DRbServer.new drb_uri, self, :tcp_acl=>ACL.new(drb["acl"].split(" "), ACL::ALLOW_DENY)to:
@drb_server = DRb::DRbServer.new drb_uri, self, :tcp_acl=>ACL.new(drb["acl"].split(" "), ACL::DENY_ALLOW)I was able to have my test program run. Not sure why I had to make the change, but the queue manager is still denying non localhost connections after it, so I’m not terribly concerned.
$ ruby q.rb
Local: Hello queue master
Queue: Hello queue masterAnd the contents of q.rb:
require 'rubygems'
require 'reliable-msg'
world = String.new "Hello queue master"
queue = ReliableMsg::Queue.new 'my-queue'
queue.put world
msg = queue.get
puts "Local: #{world}"
puts "Queue: #{msg.object}"I’m going to let this library permeate my subconscious and find a use for it sometime in the near future.
There is a doc for integrating this library with rails at Reliable Messaging with Rails. Also, AP4R appears to use Reliable Messaging for it’s queueing and looks like a well polished Rails plugin. I don’t have time to look at AP4R now, so I’ll leave a post on that for another day.
Using Active Record from outside of Rails
I’ve read a bunch of conflicting advice on how to use active record models defined in a Rails application from outside Rails.
The following is the result of experimenting with others’ advice plus my own digging around:
require 'rubygems'
require 'active_record'
require '/path/to/rails/config/environment'This should allow you to use your Rails models just like you would in a Rails controller:
#!/usr/local/bin/ruby
require 'rubygems'
require 'active_record'
require '/var/www/railsapp/config/environment'
m = MyModel.new
m.find_all.each { |row|
puts row.some_column
}
Treating gems as plugins
This is specific to rails 1.2 as I believe Edge Rails has this built in.
There’s been some amount of debate on various blogs on whether packaging up gems in your Rails application is a good choice. In my case, I decided that for me, the deployment convenience (a single application on a single server) far outweighs the maintenance aspects.
I found the ‘gems’ plugin over at techno-weenie.net that does exactly what I want with little effort. It’s a standard script/plugin install and installs a rake task (rake gems:freeze) which takes an argument of the gem you want to freeze. From the README
$ rake gems:freeze GEM=tzinfoinstalls the tzinfo gem in vendor/tzinfo-x.y.z (where x.y.z is the version of tzinfo that was frozen). If needed, you can freeze a specific gem version with:
$ rake gems:freeze GEM=tzinfo VERSION=0.1.2Posted in Rails | no comments |
Textmate plugin for rails
I’m trying to make more and better use out of TextMate for my rails development and have been bitching about having to find the code a rails backtrace points to when my apps blow up. Enter the footnotes plugin. This plugin will give you some links in the footer of your page that allow you to go directly to the controller, view, layout, stylesheets, javascripts, etc in TextMate. It also hyperlinks your rails backtraces so you can go direct to the line of code that’s erroring out.
Note the underlined lines in this backtrace - those are hyperlinks.
To install the plugin just run this command from your RAILS_ROOT.
script/plugin install http://macromates.com/svn/Bundles/trunk/Bundles/Ruby%20on%20Rails.tmbundle/Support/plugins/footnotesPosted in Rails | no comments |
Multiple class initialization methods
Here’s a quick run-down on the development of multiple initialize methods for Ruby classes: Multiple Initialize Methods.
The author looks at the subject from a domain language idiom perspective. I used the information to provide multiple creation interfaces for a class.
Quick example:
Class Archive
def Archive.create( archive_name )
return self.new( archive_name, :create )
end
def Archive.open( archive_name )
return self.new( archive_name, :open )
end
def initialize( archive_name, mode )
# do interesting stuff
end
end
arch = Archive.open("some_existing_file")
arch = Archive.create("some_new_file")
Posted in Ruby, Rails | no comments |
tabnav rails plugin
While working on a rails app a couple days ago, I found myself needing to implement a tabbed interface. I hacked it together, but meant to go back to it later and clean it up. I stumbled upon the tabnav plugin in the meantime. Haven’t had the time to try it out, but based on reading it’s examples and a few blog entries here and there about it, I think it’ll solve my problem very nicely.
Posted in Rails | no comments |
Subversion setup rake task for rails
Found this nice little rake task to setup your rails app in a new subversion repository over at DZone Snippets the other day.
desc "Configure Subversion for Rails"
task :configure_for_svn do
system "svn remove log/*"
system "svn commit -m 'removing all log files from subversion'"
system 'svn propset svn:ignore "*.log" log/'
system "svn update log/"
system "svn commit -m 'Ignoring all files in /log/ ending in .log'"
system 'svn propset svn:ignore "*.db" db/'
system "svn update db/"
system "svn commit -m 'Ignoring all files in /db/ ending in .db'"
system "svn move config/database.yml config/database.example"
system "svn commit -m 'Moving database.yml to database.example to provide a template for anyone who checks out the code'"
system 'svn propset svn:ignore "database.yml" config/'
system "svn update config/"
system "svn commit -m 'Ignoring database.yml'"
system "svn remove tmp/*"
system "svn commit -m 'Removing /tmp/ folder'"
system 'svn propset svn:ignore "*" tmp/'
end
desc "Add new files to subversion"
task :add_new_files do
system "svn status | grep '^\?' | sed -e 's/? *//' | sed -e 's/ /\ /g' | xargs svn add"
end
desc "shortcut for adding new files"
task :add => [ :add_new_files ]Posted in Rails | no comments |
Vlad the Deployer
Vlad the Deployer is a rails deployment tool built to be simpler than Capistrano. Having spent many moons trying to figure out Capistrano, I’m all for checking out a replacement.
Posted in Rails | no comments |
