Archive for the ‘Tips & Tricks’ Category

Implementing a Mixin for Tagging

Monday, May 18th, 2009 by john.beppu

Recently, I’ve been asked to add tagging to the guided reports section. I used this as an opportunity to implement a mixin that’s similar in spirit to acts_as_taggable for Ruby’s ActiveRecord ORM.

I called this class, “C4::Taggable”, and it will inject tagging-related methods to any class that uses it. Normally, it’s bad form to use Exporter in OO code, but that mainly applies to when you’re defining a class. However, when you’re defining a mixin, I believe the use of Exporter is justified, because it’s one of the easiest ways to inject methods into a namespace.

To illustrate this technique, here is an outline of how C4::Taggable is implemented. (For brevity’s sake, the method bodies were omitted.)

C4::Taggable

  package C4::Taggable;
  use strict;
  use warnings;
  use base 'Exporter';
  use C4::Context;
  our @EXPORT_OK   = qw( add_tag remove_tag tags search_by_tags                );
  our %EXPORT_TAGS =   ( mixin => [qw(add_tag remove_tag tags search_by_tags)] );

  sub add_tag        { }
  sub remove_tag     { }
  sub tags           { }
  sub search_by_tags { }

  1;

Notice how C4::Taggable exports tagging-related methods.

Next, C4::Report is outlined. Note that by using C4::Taggable, C4::Report became taggable by virtue of having tagging-related methods for C4::Taggable being mixed into it.

C4::Report

  package C4::Report;
  use strict;
  use warnings;
  use C4::Context;
  use C4::Taggable ':mixin';

  sub new            { }
  sub id             { }
  sub borrowernumber { }
  sub date_created   { }
  sub last_modified  { }
  sub savedsql       { }
  sub last_run       { }
  sub report_name    { }
  sub type           { }
  sub notes          { }
  sub update         { }

  sub table          { }

  1;

Isn’t that simple? Now any time you want to add tagging to a class, you just use C4::Taggable.

What’s the catch?

The catch is that C4::Taggable will have expectations about the host class and the database schema. The host class has to provide a few methods so that C4::Taggable can introspect enough to generate the right SQL. There will also be a table you have to create that has a predefined structure and naming pattern. I won’t go into any more detail here, but suffice it to say that for C4::Taggable to work, certain conventions have to be followed.

To me, it’s a small price to pay, because it makes it ridiculously easy to add tagging to other classes should that ever be necessary or desired.

Tips & Tricks from Koha Developers

Sunday, April 19th, 2009 by Nicole C. Engard

I’m at the Developers’ Meeting at KohaCon 2009 and they just went around the table with Tips & Tricks - here are my notes (sorry it’s not cleanly written):

Chris - Git has a built in garbage collector $ git gc - if you run it after creating a branch and before checking it out, makes switch branches much faster (make a branch, run this and then checkout the branch).  Also cluster ssh http://sourceforge.net/projects/clusterssh/ is a handy tool

Jesse - has a simple shell script that builds the environment

Joe - has a script he makes on different servers that he uses to get his shell to where he wants it to be for testing, it sets his self created variables and standard values (http://blogs.liblime.com/developers/2009/04/19/simple-shell-trick/).  Also he posted a good tip on the LibLime blog: http://blogs.liblime.com/developers/2009/04/10/simple-git-trick-for-bash/

Thomas - it’s important to remember that the real world of records is much more complicated than the record you see most frequently, so when testing things trying the really hard MARC records (Josh has a test file of really tough MARC records) will give you a better test base.  In the real world, you will find typos in ISBNs printed in the book - ISBNs that can’t possible be right or match our matching rules.  And also remember that records will lie about their encoding all of the time - and they seem to prefer it that way. 

Galen - One of the things that he does since he can’t claim to have the visual design skills that Owen does - he has become a real stickler about the HTML that is on the OPAC & Staff client - making it appear as XHTML.  There are tools in Firefox that can make for a good development environment: Fire Bug, for validation the HTML validator plugin in excellent at doing it quickly without submitting your site to an online validator, Firefox accessibility plugin lets you run automated tests against your site to meet requirements for ADA, Also the web developer plugin, Yahoo! Dev tools are slow, but they provide valuable info.  Something that is useful, but not a dev tool, is the Zotero plugin (citation manager) www.zotero.org — geared toward people to do lots of research online.  

Corey - Hasn’t done much dev work, but as far as install goes, if you install on red hat linux make sure you have lots of caffeine and the RPM packages

Agnes - Adds to Corey’s comment by saying she repeats his comment for 64 bit Zebra

Brendan - Fetch & Pull do not to the same thing!  He likes the graphical SQL interface and uses Cocoa (http://sourceforge.net/projects/freshmeat_mysql-cocoa/)

Everyone - Use Screen: http://www.gnu.org/software/screen/

Danny - took notes too: http://dbouman.blogspot.com/2009/04/general-koha-tips-and-tricks.html

Technorati Tags:

Simple Shell Trick

Sunday, April 19th, 2009 by atz

Here’s a file I use to get myself into the correct environment for a given Koha installation. I create one of these for each Koha I’m using or testing, then just source the file into my shell to put myself in the right directory, with the right ENV variables set.

#!/bin/bash
#
# source this file, e.g.:
# . ./standard_export.sh
export KOHA_CONF=/home/atz/koha/etc/etc/koha-conf.xml
export KOHA_LOG=/home/atz/koha/log/production-intranet-error_log
export OPAC_LOG=/home/atz/koha/log/production-opac-error_log
export PERL5LIB=/home/atz/koha/production/koha
echo KOHA_LOG  = $KOHA_LOG
echo OPAC_LOG  = $OPAC_LOG
echo KOHA_CONF = $KOHA_CONF
echo PERL5LIB  = $PERL5LIB
cd $PERL5LIB

Simple Git Trick for Bash

Friday, April 10th, 2009 by atz

Chris Cormack shared this useful trick for keeping track of what git repo you are currently using/editing. Stick it in your .bash_profile to have your prompt adapt to the repo you are in!

function parse_git_branch {
  ref=$(git-symbolic-ref HEAD 2> /dev/null) || return
  echo "("${ref#refs/heads/}")"
}
RED    ="\[\033[0;31m\]"
YELLOW ="\[\033[0;33m\]"
GREEN  ="\[\033[0;32m\]"
PS1="$RED\$(date +%H:%M) \w$YELLOW \$(parse_git_branch)$GREEN\$ "