Skip to content

Commit

Permalink
Refactor node/object assignment to keep the provided order
Browse files Browse the repository at this point in the history
  • Loading branch information
melmothx committed Feb 5, 2024
1 parent 3d750b9 commit e9efb4d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 22 deletions.
36 changes: 33 additions & 3 deletions lib/AmuseWikiFarm/Schema/Result/Node.pm
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,40 @@ sub update_from_params {

if (defined $params->{attached_uris}) {
my $res = $site->validate_node_attached_uris($params->{attached_uris});
# we're in a transaction
my $sorting_pos = 0;
foreach my $rel (qw/node_titles node_categories node_aggregations node_aggregation_series/) {
$self->$rel->delete;
}
foreach my $obj (@{$res->{objects}}) {
# here it will call set_aggregations, set_series_aggregations, set_categories, set_titles
my $method = $obj->{method};
$self->$method($obj->{list});
my %common = (sorting_pos => ++$sorting_pos);
if ($obj->isa('AmuseWikiFarm::Schema::Result::Title')) {
$self->node_titles->create({
%common,
title => $obj,
});
}
elsif ($obj->isa('AmuseWikiFarm::Schema::Result::Category')) {
$self->node_categories->create({
%common,
category => $obj,
});
}
elsif ($obj->isa('AmuseWikiFarm::Schema::Result::Aggregation')) {
$self->node_aggregations->create({
%common,
aggregation => $obj,
});
}
elsif ($obj->isa('AmuseWikiFarm::Schema::Result::AggregationSeries')) {
$self->node_aggregations->create({
%common,
aggregation_series => $obj,
});
}
else {
die "Shouldn't happen $obj";
}
}
}
# we need to change the linkage between the record and the set and
Expand Down
14 changes: 4 additions & 10 deletions lib/AmuseWikiFarm/Schema/Result/Site.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4408,9 +4408,7 @@ sub serialize_site {

sub validate_node_attached_uris {
my ($self, $string) = @_;
my @list = ref($string)
? (@$string)
: (grep { length($_) } split(/\s+/, $string));
my @list = grep { length($_) } map { split(/\s+/, $_) } (ref($string) ? @$string : ($string));

# all of them have a shared api, so we can loop
my @objects = (
Expand All @@ -4437,27 +4435,23 @@ sub validate_node_attached_uris {
);
my %done;
my @missing;
my @out;
STRING:
foreach my $str (@list) {
OBJECT:
foreach my $obj (@objects) {
if (my $found = $obj->{rs}->by_full_uri($str)) {
my $u = $found->full_uri;
$done{$u}++;
push @{$obj->{list}}, $found if $done{$u} == 1;
push @out, $found if $done{$u} == 1;
next STRING;
}
}
push @missing, $str;
log_info { "Ignored $str while validating node uris $string" };
}
Dlog_debug { "Validation: $_ " }
+{
objects => [ map { $_->{method} . ' ' . scalar(@{$_->{list}}) } @objects ],
fail => \@missing,
};
return +{
objects => \@objects,
objects => \@out,
fail => \@missing,
};
}
Expand Down
20 changes: 11 additions & 9 deletions t/nodes.t
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,23 @@ ok $site->categories->count;
for my $id (0..1) {
my $uri = "$u-$id";
my $node = $site->nodes->create({ uri => $uri });
$node->update_from_params({
title_en => ucfirst($uri) . ' (en)',
body_en => ucfirst($uri) . ' body EN',
title_it => ucfirst($uri) . ' (it)',
body_it => ucfirst($uri) . ' body IT',
parent_node_uri => $parent ? $parent->uri : undef,
});
my %params = (
title_en => ucfirst($uri) . ' (en)',
body_en => ucfirst($uri) . ' body EN',
title_it => ucfirst($uri) . ' (it)',
body_it => ucfirst($uri) . ' body IT',
parent_node_uri => $parent ? $parent->uri : undef,
);
# test both styles.
if ($id) {
my @texts = $site->titles->search({ uri => { -like => '%' . $u . '%' } })->all;
$node->set_titles(\@texts);
$params{attached_uris} = [ map { $_->full_uri } @texts ];
}
else {
my @cats = $site->categories->search({ uri => { -like => '%' . $u . '%' } })->all;
$node->set_categories(\@cats);
$params{attached_uris} = join(' ', map { $_->full_uri } @cats);
}
$node->update_from_params(\%params);
if ($id) {
$parent = $node;
}
Expand Down

0 comments on commit e9efb4d

Please sign in to comment.