Download: JiftyX-ModelHelpers

It has been a while since audreyt first mentioned the namespace "JiftyX" for Jifty plugins.

Few month ago I had this hack to make my life coding in Jifty easier (in the aspect of typing):

sub M { return Jifty->app_class(Model => $_[0])->new; }

This lets you do something like:

$book = M("Book"); $book->load(42);

Instead of

$book = Jifty->app_class(Model => "Book")->new;

Jifty always want to be polite, prevent global namespace clobbering. That is an absolutely good convention to follow. But it does not make life of developer easier. I decided to see if I can push this M hack further and came up with something like this:

$book = Book(42);

I think this is likely the shortest way you can do to load a record with id 42 from Book model. In RoR, you do this:

book = Book.find(42)

Or, we might need to load a record with other info instead of ID:

$book = Book(name => "Harry Potter");

This is like calling Jifty::DBI::Record::load_by_cols(), but shorter

To do this in Rails, you need to say:

book = Book.find(:first, :conditions => ["name = ?", "Harry Potter"])

OK, now I am very happy ;)

After discussed on jifty-dev mailing list, I put my currently progress of this work on svn.jifty.org here: JiftyX-ModelHelpers . It has no doc in there, but currently what's implemented is really all described in this blog entry. So feel free to try it in your Jifty app.

Sometimes in View you will failed to use JiftyX::ModelHelpers, perl will tell you there are some syntax error. In that case, please say:

require JiftyX::ModelHelpers; JiftyX::ModelHelpers->import();

This makes me feels especially good writing tests. Here's a piece of test setup code in one of my recently work in progress:

use Retail::Test::Fixtures qw(providers commodities consumers);

my $good_company = Provider(name => "good company"); my $good_consumer = Consumer(name => "good consumer");

my $a = Commodity(name => "Hello Kitty A");

is($a->quantities_in_stock, 0, "Initially its stockless");

add_supply($a, 10, 0);

is($a->quantities_in_stock, 10, "Added 10");

add_supply($a, 15, 0);

is($a->quantities_in_stock, 25, ".. added another 15");

# Draft supply.

add_supply($a, 15, 1); is($a->quantities_in_stock, 25, "A draft supply shouldn't count.");

Sold 15 items

add_sale($a, 15, 0); is($a->quantities_in_stock, 10, "Just sold 10.");

sub add_supply { my $record = shift; my $quantity = shift; my $draft = shift;

my $s = Supply->create(
    provider => $good\_company,
    draft => $draft
);

SupplyCommodity->create(
    supply => $s,
    commodity => $record,
    quantity => $quantity
);

}

sub add_sale { my $record = shift; my $quantity = shift; my $draft = shift;

my $s = Sale->create(
    consumer => $good\_consumer,
    draft => $draft
);

SaleCommodity->create(
    sale => $s,
    commodity => $record,
    quantity => $quantity
);

}

Or see it here: t/commodity-quantities-in-stock.t. Also take a look how I load fixtures in Fixtures.pm

Since it simplyfies the code to create new records on-the-fly. Now I feel much better to write more tests in my Jifty app. :)

Labels: Jift-dbi, jifty, perl