Pflichtenhefte: nach Änderung an Textblöcken Version invalidieren
authorMoritz Bunkus <m.bunkus@linet-services.de>
Fri, 19 Apr 2013 12:53:16 +0000 (14:53 +0200)
committerMoritz Bunkus <m.bunkus@linet-services.de>
Tue, 1 Apr 2014 11:03:19 +0000 (13:03 +0200)
SL/Controller/RequirementSpecTextBlock.pm
SL/DB/RequirementSpec.pm
SL/DB/RequirementSpecTextBlock.pm

index 140a104..ab0b47f 100644 (file)
@@ -16,7 +16,7 @@ use SL::Locale::String;
 
 use Rose::Object::MakeMethods::Generic
 (
-  scalar                  => [ qw(requirement_spec text_block) ],
+  scalar                  => [ qw(text_block) ],
   'scalar --get_set_init' => [ qw(predefined_texts) ],
 );
 
@@ -79,7 +79,8 @@ sub action_ajax_add {
   my $id_base = join('_', 'new_text_block', Time::HiRes::gettimeofday(), int rand 1000000000000);
   my $html    = $self->render('requirement_spec_text_block/_form', { output => 0 }, id_base => $id_base, insert_after => $::form->{id});
 
-  $js->action($::form->{id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($::form->{id} || 'list'))
+  $self->invalidate_version($js)
+     ->action($::form->{id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($::form->{id} || 'list'))
      ->focus('#' . $id_base . '_title')
      ->render($self);
 }
@@ -124,7 +125,7 @@ sub action_ajax_create {
   my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
   my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
 
-  SL::ClientJS->new
+  $self->invalidate_version
     ->replaceWith('#' . $::form->{form_prefix} . '_form', $html)
     ->jstree->create_node('#tree', $insert_after ? ('#tb-' . $insert_after, 'after') : ('#tb-' . ($attributes->{output_position} == 0 ? 'front' : 'back'), 'last'), $node)
     ->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
@@ -148,7 +149,7 @@ sub action_ajax_update {
 
   my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
 
-  SL::ClientJS->new
+  $self->invalidate_version
     ->remove('#' . $prefix . '_form')
     ->replaceWith('#text-block-' . $self->text_block->id, $html)
     ->jstree->rename_node('#tree', '#tb-' . $self->text_block->id, $self->text_block->title)
@@ -170,7 +171,8 @@ sub action_ajax_delete {
 
   $self->text_block->delete;
 
-  $js->jstree->delete_node('#tree', '#tb-' . $self->text_block->id)
+  $self->invalidate_version($js)
+     ->jstree->delete_node('#tree', '#tb-' . $self->text_block->id)
      ->render($self);
 }
 
@@ -205,7 +207,7 @@ sub action_dragged_and_dropped {
 
   # $::lxdebug->dump(0, "form", $::form);
 
-  return $self->render(\'', { type => 'json' }) if $::form->{current_content_type} !~ m/^text-block/;
+  return $self->invalidate_version->render($self) if $::form->{current_content_type} !~ m/^text-block/;
 
   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
   my $new_where     = $self->text_block->output_position;
@@ -246,7 +248,8 @@ sub action_dragged_and_dropped {
     }
   }
 
-  $self->render($js);
+  $self->invalidate_version($js)
+    ->render($self);
 }
 
 sub action_ajax_copy {
@@ -281,19 +284,15 @@ sub action_ajax_paste {
   }
 
   my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
-  $js->jstree->create_node('#tree', $::form->{id} ? ('#tb-' . $::form->{id}, 'after') : ("#tb-${front_back}", 'last'), $node)
-     ->render($self);
+  $self->invalidate_version($js)
+    ->jstree->create_node('#tree', $::form->{id} ? ('#tb-' . $::form->{id}, 'after') : ("#tb-${front_back}", 'last'), $node)
+    ->render($self);
 }
 
 #
 # filters
 #
 
-sub load_requirement_spec {
-  my ($self) = @_;
-  $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
-}
-
 sub load_requirement_spec_text_block {
   my ($self) = @_;
   $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $::form->{id})->load || die "No such requirement spec text block");
@@ -320,4 +319,13 @@ sub init_predefined_texts {
   return SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted;
 }
 
+sub invalidate_version {
+  my ($self, $js) = @_;
+
+  $js           ||= SL::ClientJS->new;
+  my $html        = $self->render('requirement_spec/_version', { output => 0 },
+                                  requirement_spec => SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id} || $self->text_block->requirement_spec_id)->load);
+  return $js->html('#requirement_spec_version', $html);
+}
+
 1;
index e2ac8b1..c25c289 100644 (file)
@@ -173,4 +173,14 @@ sub create_version {
   return $ok ? ($copy, $version) : ();
 }
 
+sub invalidate_version {
+  my ($self, %params) = @_;
+
+  $::lxdebug->message(0, "Invalidate version called for id " . $self->id . " version " . $self->version_id);
+  $::lxdebug->show_backtrace(1);
+
+  return if !$self->id || !$self->version_id;
+  $self->update_attributes(version_id => undef);
+}
+
 1;
index 1db8623..50851ad 100644 (file)
@@ -2,6 +2,10 @@ package SL::DB::RequirementSpecTextBlock;
 
 use strict;
 
+use List::MoreUtils qw(any);
+use Rose::DB::Object::Helpers;
+use Rose::DB::Object::Util;
+
 use SL::DB::MetaSetup::RequirementSpecTextBlock;
 use SL::DB::Manager::RequirementSpecTextBlock;
 use SL::DB::Helper::ActsAsList;
@@ -11,6 +15,9 @@ __PACKAGE__->meta->initialize;
 
 __PACKAGE__->configure_acts_as_list(group_by => [qw(requirement_spec_id output_position)]);
 
+__PACKAGE__->before_save(\  &_before_save_invalidate_requirement_spec_version);
+__PACKAGE__->before_delete(\&_before_delete_invalidate_requirement_spec_version);
+
 sub validate {
   my ($self) = @_;
 
@@ -20,4 +27,26 @@ sub validate {
   return @errors;
 }
 
+sub _before_save_invalidate_requirement_spec_version {
+  my ($self, %params) = @_;
+
+
+  return 1 if !$self->requirement_spec_id;
+
+  my %changed_columns = map { $_ => 1 } (Rose::DB::Object::Helpers::dirty_columns($self));
+
+  $self->requirement_spec->invalidate_version
+    if !Rose::DB::Object::Util::is_in_db($self) || any { $changed_columns{$_} } qw(title text position output_position);
+
+  return 1;
+}
+
+sub _before_delete_invalidate_requirement_spec_version {
+  my ($self, %params) = @_;
+
+  $self->requirement_spec->invalidate_version if $self->requirement_spec_id;
+
+  return 1;
+}
+
 1;