Tuesday, August 28. 2007Improving upon Rails' validates_inclusion_of
I'm fairly new to the Ruby language and the Ruby on Rails framework, but it worked nicely for quickly building my website BirdSite.
I was using validates_inclusion_of fairly regularly to make sure the data in my models was valid. But then, watching the development log, I noticed extra SQL queries being made when I accessed my main model. It seems that validates_inclusion_of was forcing the model to make queries to the related models even when I didn't need data from them. Some Googling led to a blog post that did the validation a different way. It worked well, until I added the code to a new model today and it broke. The code was translating the model field name into a string from which to call the model itself. But the code assumed that the model would be a single word. I added a change to strip the spaces out, and it worked again. I contacted the company that hosts the blog and they confirmed that it was a problem, and they didn't know of a nicer Ruby way to correct it short of dropping the spaces with search/replace. They updated their code. I'm glad I could help out. It really works better than the standard validates_inclusion_of. Tuesday, August 14. 2007Rails acts_as_ferret without DRb
I wanted to add a search feature to my Ruby on Rails application BirdSite. There is a plugin called "acts_as_ferret" which allows Rails applications to use the Ferret search engine (based on Lucene).
Using this tutorial, I was able to get search up an running. It worked great on my development system. But there was a warning that you needed to run the indexer using a DRb server instead of directly from the Rails app. This is because the index cannot handle multiple processes writing to it simultaneously. (I also ran into this problem using PHP with another engine called Xapian). I was hoping my new site was low traffic enough to avoid problems. Since I'm hosting it on a shared server, I can't run a DRb server there anyway. But I got my first indexing exceptions less than 24 hours after I loaded my stuff onto the live server. So I decided to try periodic indexing by a cron job. This would allow me to update the index once an hour, from a single process. The downside is that the search index is only updated hourly, but I decided I could live with that. The first step is telling your Rails app to not index the content when updating. My model already had a before_save method, so I added this code:
# in the model def before_save # other stuff goes here # disable automatic ferret indexing...move it to a cron job self.disable_ferret(:always) end Then I had to create a rake task which would build the index:
# ferret_index.rake desc "Updates the ferret index for the application." task :ferret_index => [ :environment ] do | t | MyModel.rebuild_index # here I could add other model index rebuilds puts "Completed Ferret Index Rebuild" end This task is simplified: I'm telling it to rebuild the entire index each hour. I'm guessing when my dataset gets big enough, this will be really slow. In that case I'll need to track all the model instances that got updated in the past hour and just index those. Finally, I needed a cron job to run the rake task, making sure to set the environment to "production":
cd /rails_app && rake ferret_index RAILS_ENV=production
So far this is working well, and I haven't received any indexing exceptions since. Tuesday, July 31. 2007Northeast Minnesota
Check the photo album
Well, I've been back a week, but it took that long to recover and get through lots of photos. I went up to northeast Minnesota for the BugGuide Gathering. I had a great time and it was fun to meet several of the BugGuide members I've known online for years. The meetup was at Wolf Ridge Environmental Learning Center, which was the perfect place for it. They are up a small mountain with lots of wilderness, but they've got a sizable meeting room with a PC projector and WiFi internet. One minute you're looking at moths attracted to blacklights and the next you're trying to ID it on BugGuide. Friday the 20th I flew up to Duluth. I didn't realize that I sat next to one of the BugGuiders on the flight from Detroit. I did a little sightseeing along Lake Superior on my way north to Wolf Ridge. In the evening we met up and chatted about dragonflies and damselflies with Kurt Mead and folks who were attending the Minnesota Dragonfly Gathering simultaneously with us. After dark we set up blacklights and tried to attract some moths to the Science Building. The first few moth arrivals were subjected to papparazi-like photography. It was quite a scene. Eventually with the arrival of more moths in various places and folks going to bed, the flash gun rate dropped to a less blinding level. I was excited to see my first Polyphemus moth. Saturday the whole group loaded up into several cars and headed for a boggy area between Finland and Isabella. It was the first time I've walked into a bog and it was fun. The mossy ground is squishy and in some places a footstep will sink just enough to flood your sandals. The bugs weren't as busy as we thought they might have been, but we still found several species of butterflies and a couple dragonflies. And I can't forget the deer fly that bit my knee. We moved on to lunch at a lake, my first of Minnesota's 10,000 lakes, or maybe the second if you include Lake Superior. At a creek with some dry brush, I did some photo hunting of beautiful red dragonflies, the white-faced meadowhawks. Just before we left I got a good photo. At the next stop we had to fix a flat on John VanDyk's car and I saw a female white-face. The final stop was the treat of the day, a beautiful large bog and a pond, with lots of damselflies. In the evening I got my first chance to try rock climbing on a very nice indoor climbing wall. That night we did some more moth blacklighting, but a cool breeze kept their numbers down. We did see a nice Laurel Sphinx, and some of the night owls were treated to views from Wolf Ridge's telescope and the sound of wolves howling in the distance. Sunday the group started to break up as some folks had to be on their way. But several stayed and did their own exploring of Wolf Ridge. I joined up with a former Wolf Ridge guide and a forest service worker. We decided to go canoe Wolf Lake. I agreed to let them paddle and I would take pictures from the middle of the boat. Despite being an experienced paddler who should have known better, I slipped on the dock trying to get in and soaked my pants and shoes. At least the camera gear stayed dry! I eventually got in properly and we circumnavigated the lake, not seeing any bugs due to the cool weather. But we did spot a pair of beavers. After the paddle we made an easy hike up Marshall Mountain, which gave us a view of Wolf Lake, plus Raven Lake and even Lake Superior, which was a little tricky to see, since the lake was the same color as the hazy sky. After lunch I went with several folks to Raven Lake, where I made a much better showing in the canoe and did some paddling myself. Kurt Mead took the bow and captured a few damselflies right from the boat. He did a good job explaining how he made his IDs using a hand lens and a well worn field guide. Later a small group of us hiked into the wetlands on one side of Raven Lake. That evening, I enjoyed a great meal with Kurt and his family and a few other Odonata enthusiasts, and spent the night in a treehouse! Monday I was up a little early, driving up Highway 1 looking for moose. I struck out in this endeavour, but I did get a few bugs on a poorly maintained walking trail. From there I started my run south, stopping at points along Lake Superior. First I drove up to the Palisade Head cliffs. It's precarious up there (reminds me of Jumping Off Rock), but I was careful and got some good photos. Then it was down to the Split Rock Lighthouse, a very picturesque subject, though it's probably a better photo in winter with snow all over the place and fewer tourists climbing around. From there I went to Two Harbors and had a chance to shoot the lighthouses there before returning to Duluth and the flight home. Great trip! Friday, July 6. 2007Check out the BirdSite!
I recently published a new website: birdsite.org.
This site was inspired by BugGuide, where I've been
a participant for several years, both as a photographer and a developer. I'd been
thinking about doing a similar site for other fauna that I photograph.
This forum thread made it clear
that others were interested in such a site for birds. I decided to build it. For my
own programming amusement I created this site as my first project with
Ruby on Rails. I still have lots of features to
add but I wanted to get the bare bones up and running.
Thursday, June 7. 2007Vote on the New SC License Plate
Apparently South Carolina is letting people vote online for the design of the new license plate. I think that's a neat idea, but I'm sort of disappointed with the three designs. Two of the designs feature a centered palmetto tree, just like the current one, but I like the current one better than either of those. The other design is the state flag tucked into the corner of the plate. It looks decent but I can see that one getting covered up by plate frames. My vote goes to the palmetto tree at sunrise/sunset, but I'd be happy to keep my current palmetto tree plate.
Update 2007-08-28: Looks like they picked the plate that I voted for (PDF warning). I still like the current plate better, and I don't care for the cheesy .com advertisement on the new plate. Plus, the new plate will aggravate SC Gamecock fans as it leans toward Clemson colors! Wednesday, May 30. 2007Drupal Anti-spam Registration Question
Last year I had to add an anti-spam question to my PHPBB forum to prevent spam bots from signing up and wreaking havoc. Lately the same bot problem has crept up on a site I have running Drupal 4.7.x. So I resorted to the same solution, though this one is implemented as a Drupal module. It is very simple...the only thing you configure is the question and answer you want to use. Click here to see it, and follow the instructions in the comments. I hope it helps you.
Monday, May 14. 2007One More Book
The Kaufman Field Guide to Insects of North America has been out since February, but I finally got my copy today. My photo of this wasp was published in it. I submitted that photo to them a couple years ago. I'm glad the book is finally out, and congrats to the many BugGuiders who have photos in it.
Thursday, April 26. 2007Guitar Chord Matcher
Today I was trying to figure out the chords in a song (Katie Reider's awesome "Slowing Down for the Silence"). I figured out a couple chords, but was having trouble guessing some of the others.
So I whipped up this little web application. You tell it a chord in your song and it will tell you all the major keys where you can find that chord. Using that knowledge, you can try the other chords in the key and see if they fit the song you're playing. I'm pretty sure Katie's song is in D major.
Enjoy! Wednesday, April 25. 2007Cedar Waxwings
The Cedar Waxwings really seem to be hanging around this year. Normally I will see a lot of them in February each year, and then they're gone. Again this year, I saw some in February, but I continued to see them on and off through March. Now in April I have seen a sizable group of them quite frequently. They have been raiding berry-filled bushes at my and my neighbor's houses. Perhaps the birds are hanging around because of the cooler spring we've been having.
This week they've been coming to the Mahonia bush right outside my office window. This is the same place where I got my great tanager photo last year. The waxwings are a lot more skittish than the tanager was, so I haven't gotten the perfect shot yet, but these two are pretty good. Here's hoping the summer tanager and rose-breasted grosbeak from last spring will still stop by! The grosbeak owes me a clear picture. Lots of bird photos in the photo album. Saturday, April 21. 2007PHPBB Anti-spam Registration Question
If you are not comfortable installing this on your own, I'll do it for you. If you're using PHPBB 2.0.23 and the SubSilver template, I will install it for $25 via Paypal. (If you're using a different template or need an older site updated to the latest PHPBB, I can do that too, but it will take longer and cost extra). Contact me.
I've been meaning to document this for a while now, and I finally made myself write it up. The simple measure I will describe has been working for my PHPBB website since last December. It has kept spam accounts from registering and posting anything on my site. This idea involves asking a question during registration that spam bots do not know how to answer. You can choose any question and answer that you want. The website that I run PHPBB on is a sailing website, so I asked a question that sailors could answer. For this example I chose another question. You have to change two files, templates/(your template)/profile_add_body.tpl and includes/usercp_register.php. In templates/(your template)/profile_add_body.tpl, add this after the <!-- END switch_confirm --> line (about line number 68):
<!-- BEGIN switch_add_profile -->
In includes/usercp_register.php, add this after else if ( $mode == 'register' ) { (about line number 275):<tr> <td class="row1"><span class="gen">Anti-Spam Question:</span></td> <td class="row2"> <span style="font-size: small;">Enter the name Luke Skywalker's father. Check your spelling! Requiring this question to be answered will hopefully limit spammers who try to sign up.</span><br /> <input type="text" class="post" style="width: 200px" name="bonusq" size="25" maxlength="255" value="" /> </td> </tr> <!-- END switch_add_profile -->
// mod by MB to require human data to prevent spam bots
So there you have it. I'm sure there are some sophisticated PHPBB mods out there that will do the same and more for you, but this simple change has saved me a lot of trouble. I'm keeping my fingers crossed that it will last.
if(trim(strtolower($_POST['bonusq']))!='darth vader') { $error = TRUE; $error_msg .= ( ( isset($error_msg) ) ? '<br />' : '' ) . 'You did not answer the Anti-Spam question correctly...' . 'please try again.'; } // end mod Update 2007-05-23: I omitted one other mod to make this work. You also need to edit your includes/usercp_register.php file. Find the line if ( $mode == 'editprofile' ) (about line number 941) and modify that block to look like this:
if ( $mode == 'editprofile' )
{ $template->assign_block_vars('switch_edit_profile', array()); } else { // Else block is Mod by MB 2006-08-11 $template->assign_block_vars('switch_add_profile', array()); } Update 2007-06-24: Several folks wrote in expressing confusion about where these blocks of code belonged. I apologize, and I amended the article to give approximate line numbers where the mods belong. I used PHPBB 2.0.22 as my reference. Note that if you have made other modifications to your PHPBB files, the line numbers might not be quite right. Update 2007-06-27: Bug fix. See comment #7 below. Update 2007-09-29: A lot of people have written in having trouble with this modification when it comes to users editing their profile. To avoid problems, I have applied the modification to a new copy of PHPBB 2.0.22 and tested it. I made copies of the modified files and they are available to download. The modification as it appears in this blog post is what I used, and it works fine for me. I did not make any of the other changes from the comments (except the bug fix from June which has already been incorporated into this post). Update 2008-02-27: The update works just fine for me in PHPBB 2.0.23 as well. Neither profile_add_body.tpl nor usercp_register.php were changed between 2.0.22 and 2.0.23.
« previous page
(Page 3 of 17, totaling 170 entries)
» next page
|
CategoriesQuicksearchSyndicate This Blog |