From: Sven Schöling Date: Thu, 6 Oct 2011 10:20:43 +0000 (+0200) Subject: _bool_yn accessor X-Git-Tag: release-2.7.0beta2~5^2~14 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=8944e41547df93c8b25943938bb42ddec101fc16;p=kivitendo-erp.git _bool_yn accessor --- diff --git a/SL/DB/Helper/Attr.pm b/SL/DB/Helper/Attr.pm index 94b53dd7f..f7fabfe89 100644 --- a/SL/DB/Helper/Attr.pm +++ b/SL/DB/Helper/Attr.pm @@ -33,6 +33,7 @@ sub _make_by_type { _as_percent($package, $name, places => 2) if $type =~ /numeric | real | float/xi; _as_number ($package, $name, places => 0) if $type =~ /int/xi; _as_date ($package, $name) if $type =~ /date | timestamp/xi; + _as_bool_yn($package, $name) if $type =~ /bool/xi; } sub _as_number { @@ -104,6 +105,23 @@ sub _as_date { return 1; } +sub _as_bool_yn { + my ($package, $attribute, %params) = @_; + + no strict 'refs'; + *{ $package . '::' . $attribute . '_as_bool_yn' } = sub { + my ($self) = @_; + + if (@_ > 1) { + die 'not an accessor'; + } + + return !defined $self->$attribute ? '' + : $self->$attribute ? $::locale->text('Yes') + : $::locale->text('No'); + } +} + 1; diff --git a/t/helper/attr.t b/t/helper/attr.t index 3fc0607d5..5d6238ed9 100644 --- a/t/helper/attr.t +++ b/t/helper/attr.t @@ -1,4 +1,4 @@ -use Test::More tests => 29; +use Test::More tests => 32; use lib 't'; @@ -50,3 +50,13 @@ $i->amount(12); $i->netamount(10.34); is($i->taxamount_as_number, '1,66'); +$o->closed(1); +is $o->closed_as_bool_yn, 'Ja', 'bool 1'; +$o->closed(0); +is $o->closed_as_bool_yn, 'Nein', 'bool 2'; + +# undef test: this only works for columns without default, rose will set +# defaults according to the database +$i->taxincluded(undef); +is $i->taxincluded_as_bool_yn, '', 'bool 3'; +