forked from lix-project/hydra
Merge pull request #933 from cole-h/boolean-json
Serialize `enabled` and `hidden` columns as boolean
This commit is contained in:
commit
fa5811eabf
|
@ -5,6 +5,7 @@ use strict;
|
|||
use warnings;
|
||||
|
||||
use base 'DBIx::Class';
|
||||
use JSON;
|
||||
|
||||
sub TO_JSON {
|
||||
my $self = shift;
|
||||
|
@ -17,6 +18,14 @@ sub TO_JSON {
|
|||
$json{$column} = $self->get_column($column);
|
||||
}
|
||||
|
||||
foreach my $column (@{$hint->{string_columns}}) {
|
||||
$json{$column} = $self->get_column($column) // "";
|
||||
}
|
||||
|
||||
foreach my $column (@{$hint->{boolean_columns}}) {
|
||||
$json{$column} = $self->get_column($column) ? JSON::true : JSON::false;
|
||||
}
|
||||
|
||||
foreach my $relname (keys %{$hint->{relations}}) {
|
||||
my $key = $hint->{relations}->{$relname};
|
||||
$json{$relname} = [ map { $_->$key } $self->$relname ];
|
||||
|
|
|
@ -247,15 +247,17 @@ __PACKAGE__->many_to_many("usernames", "projectmembers", "username");
|
|||
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:+4yWd9UjCyxxLZYDrVUAxA
|
||||
|
||||
my %hint = (
|
||||
columns => [
|
||||
string_columns => [
|
||||
"name",
|
||||
"displayname",
|
||||
"description",
|
||||
"homepage",
|
||||
"enabled",
|
||||
"hidden",
|
||||
"owner"
|
||||
],
|
||||
boolean_columns => [
|
||||
"enabled",
|
||||
"hidden"
|
||||
],
|
||||
relations => {
|
||||
jobsets => "name"
|
||||
}
|
||||
|
|
38
t/Controller/projects.t
Normal file
38
t/Controller/projects.t
Normal file
|
@ -0,0 +1,38 @@
|
|||
use feature 'unicode_strings';
|
||||
use strict;
|
||||
use Setup;
|
||||
use JSON qw(decode_json);
|
||||
|
||||
my %ctx = test_init();
|
||||
|
||||
require Hydra::Schema;
|
||||
require Hydra::Model::DB;
|
||||
require Hydra::Helper::Nix;
|
||||
use HTTP::Request::Common;
|
||||
|
||||
use Test2::V0;
|
||||
require Catalyst::Test;
|
||||
Catalyst::Test->import('Hydra');
|
||||
|
||||
my $db = Hydra::Model::DB->new;
|
||||
hydra_setup($db);
|
||||
|
||||
my $project = $db->resultset('Projects')->create({name => "tests", displayname => "", owner => "root"});
|
||||
|
||||
my $projectinfo = request(GET '/project/tests',
|
||||
Accept => 'application/json',
|
||||
);
|
||||
|
||||
ok($projectinfo->is_success);
|
||||
is(decode_json($projectinfo->content), {
|
||||
description => "",
|
||||
displayname => "",
|
||||
enabled => JSON::true,
|
||||
hidden => JSON::false,
|
||||
homepage => "",
|
||||
jobsets => [],
|
||||
name => "tests",
|
||||
owner => "root"
|
||||
});
|
||||
|
||||
done_testing;
|
Loading…
Reference in a new issue