don't redirect to /data/index needlessly
[MojoFacets.git] / lib / MojoFacets / Data.pm
index 4cdf334..c2283b8 100644 (file)
@@ -226,7 +226,6 @@ sub load {
        $self->_load_path( $path );
 
        $self->session( 'path' => $path );
-       $self->session( 'modified' => $loaded->{$path}->{modified} );
 
        my $redirect_to = '/data/items';
 
@@ -254,7 +253,7 @@ sub _loaded {
        my $path = $self->session('path') || $self->param('path');
        $self->redirect_to('/data/index') unless $path;
 
-       if ( $loaded->{$path}->{modified} > 1 ) {
+       if ( defined $loaded->{$path}->{modified} && $loaded->{$path}->{modified} > 1 ) {
                my $caller = (caller(1))[3];
                if ( $caller =~ m/::edit/ ) {
                        warn "rebuild stats for $path ignored caller $caller\n";
@@ -268,13 +267,18 @@ sub _loaded {
        if ( ! defined $loaded->{$path}->{$name} ) {
                warn "$path $name isn't loaded\n";
                $self->_load_path( $path );
-               $self->redirect_to('/data/index')
-                       unless defined $loaded->{$path}->{$name};
                if ( ! defined $loaded->{$path}->{stats} ) {
                        warn "rebuild stats for $path\n";
                        $loaded->{$path}->{stats} = __stats( $loaded->{$path}->{data}->{items} );
                }
+               if ( ! defined $loaded->{$path}->{$name} ) {
+                       warn "MISSING $name for $path\n";
+                       $self->redirect_to('/data/index')
+               }
        }
+
+       $self->session( 'modified' => $loaded->{$path}->{modified} );
+
        return $loaded->{$path}->{$name};
 }
 
@@ -529,8 +533,12 @@ sub _data_sorted_by {
 sub items {
        my $self = shift;
 
+       if ( my $show = $self->param('id') ) {
+               $self->param('show', $show);
+               warn "show $show\n";
+       }
+
        my $path = $self->session('path');
-       $self->redirect_to('/data/index') unless defined $loaded->{ $path };
 
        my @columns = $self->_param_array('columns');
        $self->redirect_to('/data/columns') unless @columns;
@@ -741,7 +749,7 @@ sub facet {
 }
 
 
-sub _invalidate_path_column {
+sub __invalidate_path_column {
        my ( $path, $name ) = @_;
 
        if ( defined $loaded->{$path}->{sorted}->{$name} ) {
@@ -755,6 +763,14 @@ sub _invalidate_path_column {
        }
 }
 
+sub __path_modified {
+       my ( $path, $value ) = @_;
+       $value = 1 unless defined $value;
+       
+       $loaded->{$path}->{modified}  = $value;
+
+       warn "# __path_modified $path $value\n";
+}
 
 sub edit {
        my $self = shift;
@@ -808,12 +824,11 @@ sub edit {
                        warn "# change $path $i $old -> $new\n";
                        $loaded->{$path}->{data}->{items}->[$i]->{$name} = $v;
 
-                       _invalidate_path_column( $path, $name );
+                       __invalidate_path_column( $path, $name );
 
                        $status = 201; # created
                        # modified = 2 -- force rebuild of stats
-                       $loaded->{$path}->{modified}  = 2;
-                       $self->session( 'modified' => 2 );
+                       __path_modified( $path, 2 );
        
                        $new_content = join("\xB6",@$v);
 
@@ -839,8 +854,7 @@ sub save {
        my $self = shift;
        my $path = $self->_param_or_session('path');
        my $dump_path = $self->_save( $path );
-       $loaded->{$path}->{modified} = 0;
-       $self->session( 'modified' => 0 );
+       __path_modified( $path, 0 );
 
        $self->redirect_to( '/data/items' );
 }