New admin UI functionality.
[catagits/App-IdiotBox.git] / lib / App / IdiotBox / Video.pm
index 99b7c38..cd12c20 100644 (file)
@@ -17,4 +17,38 @@ sub url_path {
   join('/', $_[0]->bucket->slug, $_[0]->slug);
 }
 
+sub update {
+       my ($self, %args) = @_;
+
+       my %update = map { $_ => delete $args{$_} } grep { exists $args{$_} } $self->fields;
+
+       if (%args) {
+               return (undef, "Unknown fields in update: " . join(',', keys %args));
+       }
+
+       unless (%update) {
+               return (undef, "No changes requested");
+       }
+
+       for my $k (qw(slug bucket_slug name author details)) {
+               my @bad;
+
+               if (exists $update{$k} && $update{$k} =~ /^\s+$/) {
+                       push @bad, $k;
+               }
+
+               return (undef, "Empty fields in update: ", join(',', @bad)) if @bad;
+       }
+
+       for my $k (qw(slug bucket_slug)) {
+               $update{$k} =~ s/\s+/-/g if exists $update{$k};
+       }
+
+       my $new = $self->clone;
+
+       $new->{$_} = $update{$_} for keys %update;
+
+       return $new;
+}
+
 1;