[Jifty-commit] r6991 - wifty/trunk/lib/Wifty/Model

Jifty commits jifty-commit at lists.jifty.org
Tue May 12 02:18:05 EDT 2009


Author: ruz
Date: Tue May 12 02:18:05 2009
New Revision: 6991

Added:
   wifty/trunk/lib/Wifty/Model/BlackList.pm

Log:
* add BlackList model

Added: wifty/trunk/lib/Wifty/Model/BlackList.pm
==============================================================================
--- (empty file)
+++ wifty/trunk/lib/Wifty/Model/BlackList.pm	Tue May 12 02:18:05 2009
@@ -0,0 +1,95 @@
+package Wifty::Model::BlackList;
+use warnings;
+use strict;
+
+use List::Compare;
+
+use base qw/Wifty::Record/;
+use Jifty::DBI::Schema;
+use Wifty::Model::User;
+use Wifty::Model::RevisionCollection;
+
+use Jifty::Record schema {
+    column type =>
+        type is 'varchar(32)',
+        label is 'value',
+        is mandatory,
+    ;
+
+    column value =>
+        type is 'varchar(255)',
+        label is 'value',
+        is mandatory,
+    ;
+
+    column created =>
+        type is 'timestamp',
+    ;
+
+    column created_by =>
+        refers_to Wifty::Model::User,
+    ;
+};
+
+sub since { '0.0.23' }
+
+sub create {
+    my $self = shift;
+    my %args = (@_);
+    my $now  = DateTime->now();
+    $args{'created'}    ||= $now->ymd . " " . $now->hms;
+    $args{'created_by'} ||= $self->current_user? $self->current_user->user_object : undef;
+    return $self->SUPER::create(%args);
+}
+
+=head2 current_user_can ACTION
+
+=cut
+
+sub current_user_can {
+    my $self = shift;
+    my $type = shift;
+
+    return 1 if $self->current_user->is_superuser;
+    return 0 unless $self->current_user->id;
+    return 1 if $self->current_user->user_object->admin;
+    return 0;
+}
+
+sub update_list {
+    my $self = shift;
+    my %args = (@_);
+
+    my $current = Jifty->app_class('Model::BlackListCollection')->new;
+    $current->limit( column => 'type', value => $args{'type'} );
+
+    my $values = delete $args{'values'} || [];
+    unless ( @$values ) {
+        while ( my $e = $current->next ) {
+            my ($status, $msg) = $e->delete;
+            return ($status, $msg) unless $status;
+        }
+        return (1, "Done");
+    }
+
+    my $now  = DateTime->now();
+    $args{'created'}    ||= $now->ymd . " " . $now->hms;
+    $args{'created_by'} ||= $self->current_user? $self->current_user->user_object : undef;
+
+    my %current = map { $_->value => $_ } @$current;
+
+    my $lc = List::Compare->new(
+        '--unsorted', $values, [keys %current]
+    );
+    foreach my $e ( map $current{$_}, $lc->get_Ronly ) {
+        my ($status, $msg) = $e->delete;
+        return ($status, $msg) unless $status;
+    }
+    foreach my $e ( $lc->get_Lonly ) {
+        my ($status, $msg) = $self->create( %args, value => $e );
+        return ($status, $msg) unless $status;
+    }
+    return (1, 'Done');
+}
+
+1;


More information about the Jifty-commit mailing list