~rgrjr/bookworm

06cc315b871f957a6ed1491c6b3ae267516004f5 — Bob Rogers 1 year, 5 months ago 1905bba moving-hacks
Fix two location content sorting bugs

* Bookworm/Location.pm:
   + (_cmp_stackable):  Helper function, used as the field comparator
     so that the resulting order matches what you get by comparing the
     database encoding.  Previously, the stackability search order was
     different for location content than for database search results.
   + Also, don't try to sort on total number of books when searching
     the database, since n_total_books isn't available to SQL.
1 files changed, 24 insertions(+), 3 deletions(-)

M Bookworm/Location.pm
M Bookworm/Location.pm => Bookworm/Location.pm +24 -3
@@ 198,6 198,26 @@ sub html_link {

### Web interface.

my @stackable_values = qw(yes no never);

sub _cmp_stackable {
    # This compares stackable values.  The "stackable" field uses it as the
    # "comparator" so that the resulting order matches what you get by
    # comparing the database encoding.
    my ($a, $b) = @_;

    my $encode = sub {
	my ($x) = @_;
	for my $i (0 .. @stackable_values-1) {
	    return $i
		if $stackable_values[$i] eq $x;
	}
	return @stackable_values + 1;
    };

    $encode->($a // '') <=> $encode->($b // '');
}

my @local_display_fields
    = ({ accessor => 'name', pretty_name => 'Location',
	 type => 'self_link', class => 'Bookworm::Location' },


@@ 216,15 236,16 @@ my @local_display_fields
	 verbosity => 2, show_total_p => 1 },
       { accessor => 'stackable', pretty_name => 'Stack?',
	 skip_if_not => \&has_stackable_p,
	 type => 'enumeration',
	 values => [ qw(yes no never) ],
	 type => 'enumeration', values => \@stackable_values,
	 comparator => \&_cmp_stackable,	# q.v.
	 search_multiple => 1,
	 search_field => 'location.stackable',
	 search_default => [ qw(yes no never) ] },
	 search_default => \@stackable_values },
       { accessor => 'bg_color', pretty_name => 'Background',
	 type => 'enumeration',
	 values => \@background_colors },
       { accessor => 'n_total_books', pretty_name => 'Books',
	 sort_p => 0,	# because it's not a database column.
	 type => 'integer', read_only_p => 1, show_total_p => 1 },
       { accessor => 'parent_location_id', pretty_name => 'Parent location',
	 edit_p => 'find-location.cgi',