[Jifty-commit] r4995 - in jifty/trunk: . lib/Jifty/Plugin lib/Jifty/Plugin/Attributes/Mixin lib/Jifty/Plugin/Attributes/Model t/TestApp-Plugin-Attributes t/TestApp-Plugin-Attributes/bin t/TestApp-Plugin-Attributes/doc t/TestApp-Plugin-Attributes/etc t/TestApp-Plugin-Attributes/lib t/TestApp-Plugin-Attributes/lib/TestApp t/TestApp-Plugin-Attributes/lib/TestApp/Plugin t/TestApp-Plugin-Attributes/lib/TestApp/Plugin/Attributes t/TestApp-Plugin-Attributes/lib/TestApp/Plugin/Attributes/Action t/TestApp-Plugin-Attributes/lib/TestApp/Plugin/Attributes/Model t/TestApp-Plugin-Attributes/share t/TestApp-Plugin-Attributes/share/po t/TestApp-Plugin-Attributes/share/web t/TestApp-Plugin-Attributes/share/web/static t/TestApp-Plugin-Attributes/share/web/templates t/TestApp-Plugin-Attributes/t

Jifty commits jifty-commit at lists.jifty.org
Tue Feb 5 11:54:39 EST 2008


Author: sartak
Date: Tue Feb  5 11:54:38 2008
New Revision: 4995

Added:
   jifty/trunk/lib/Jifty/Plugin/Attributes/
   jifty/trunk/lib/Jifty/Plugin/Attributes.pm
   jifty/trunk/lib/Jifty/Plugin/Attributes/Mixin/
   jifty/trunk/lib/Jifty/Plugin/Attributes/Mixin/Attributes.pm
   jifty/trunk/lib/Jifty/Plugin/Attributes/Model/
   jifty/trunk/lib/Jifty/Plugin/Attributes/Model/Attribute.pm
   jifty/trunk/lib/Jifty/Plugin/Attributes/Model/AttributeCollection.pm
   jifty/trunk/t/TestApp-Plugin-Attributes/
   jifty/trunk/t/TestApp-Plugin-Attributes/Makefile.PL
   jifty/trunk/t/TestApp-Plugin-Attributes/bin/
   jifty/trunk/t/TestApp-Plugin-Attributes/bin/jifty   (contents, props changed)
   jifty/trunk/t/TestApp-Plugin-Attributes/doc/
   jifty/trunk/t/TestApp-Plugin-Attributes/etc/
   jifty/trunk/t/TestApp-Plugin-Attributes/etc/config.yml
   jifty/trunk/t/TestApp-Plugin-Attributes/lib/
   jifty/trunk/t/TestApp-Plugin-Attributes/lib/TestApp/
   jifty/trunk/t/TestApp-Plugin-Attributes/lib/TestApp/Plugin/
   jifty/trunk/t/TestApp-Plugin-Attributes/lib/TestApp/Plugin/Attributes/
   jifty/trunk/t/TestApp-Plugin-Attributes/lib/TestApp/Plugin/Attributes/Action/
   jifty/trunk/t/TestApp-Plugin-Attributes/lib/TestApp/Plugin/Attributes/Model/
   jifty/trunk/t/TestApp-Plugin-Attributes/share/
   jifty/trunk/t/TestApp-Plugin-Attributes/share/po/
   jifty/trunk/t/TestApp-Plugin-Attributes/share/web/
   jifty/trunk/t/TestApp-Plugin-Attributes/share/web/static/
   jifty/trunk/t/TestApp-Plugin-Attributes/share/web/templates/
   jifty/trunk/t/TestApp-Plugin-Attributes/t/
   jifty/trunk/t/TestApp-Plugin-Attributes/t/00-basic.t
Modified:
   jifty/trunk/   (props changed)

Log:
 r51361 at onn:  sartak | 2008-02-05 11:53:49 -0500
 Checkpoint in the new Attributes plugin, a port of RT::Attribute


Added: jifty/trunk/lib/Jifty/Plugin/Attributes.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Plugin/Attributes.pm	Tue Feb  5 11:54:38 2008
@@ -0,0 +1,8 @@
+use strict;
+use warnings;
+
+package Jifty::Plugin::Attributes;
+use base 'Jifty::Plugin';
+
+1;
+

Added: jifty/trunk/lib/Jifty/Plugin/Attributes/Mixin/Attributes.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Plugin/Attributes/Mixin/Attributes.pm	Tue Feb  5 11:54:38 2008
@@ -0,0 +1,135 @@
+#!/usr/bin/env perl
+package Jifty::Plugin::Attributes::Mixin::Attributes;
+use strict;
+use warnings;
+use Jifty::Plugin::Attributes::Model::Attribute;
+use Jifty::Plugin::Attributes::Model::AttributeCollection;
+
+use base 'Exporter';
+
+our @EXPORT = qw/attributes first_attribute add_attribute set_attribute
+                 delete_attribute/;
+
+=head2 attributes
+
+Returns an AttributeCollection limited to the invoking object.
+
+=cut
+
+sub attributes {
+    my $self = shift;
+    my $attrs = Jifty::Plugin::Attributes::Model::AttributeCollection->new;
+    $attrs->limit_to_object($self);
+}
+
+=head2 first_attribute name
+
+Returns the first attribute on this object with the given name.
+
+=cut
+
+sub first_attribute {
+    my $self = shift;
+    my $name = shift;
+
+    $self->attributes->named($name)->first;
+}
+
+=head2 add_attribute PARAMHASH
+
+Adds the given attribute to this object. The following fields must be
+provided:
+
+=over 4
+
+=item name
+
+The name of the attribute
+
+=item description
+
+A description of the attribute
+
+=item content
+
+The attribute's value
+
+=back
+
+=cut
+
+sub add_attribute {
+    my $self = shift;
+    my %args = @_;
+
+    my $attr = Jifty::Plugin::Attributes::Model::Attribute->new;
+    $attr->create(
+        object      => $self,
+        name        => $args{name},
+        description => $args{description},
+        content     => $args{content},
+    );
+}
+
+=head2 set_attribute PARAMHASH
+
+Sets the given attribute on the object. Note that all existing attributes of
+that name will be removed. The following fields must be provided:
+
+=over 4
+
+=item name
+
+The name of the attribute
+
+=item description
+
+A description of the attribute
+
+=item content
+
+The attribute's value
+
+=back
+
+=cut
+
+sub set_attribute {
+    my $self = shift;
+    my %args = @_;
+
+    my $attrs = $self->attributes->named($args{name});
+    if ($attrs->count == 0) {
+        return $self->add_attribute(%args);
+    }
+
+    my $saved = $attrs->first;
+
+    while (my $attr = $attrs->next) {
+        $attr->delete;
+    }
+
+    $saved->set_content($args{content});
+    $saved->set_description($args{description});
+
+    return $saved;
+}
+
+=head2 delete_attribute name
+
+Deletes all attributes of the given name
+
+=cut
+
+sub delete_attribute {
+    my $self = shift;
+    my $name = shift;
+
+    my $attrs = $self->attributes->named($name);
+    while (my $attr = $attrs->next) {
+        $attr->delete;
+    }
+}
+
+1;
+

Added: jifty/trunk/lib/Jifty/Plugin/Attributes/Model/Attribute.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Plugin/Attributes/Model/Attribute.pm	Tue Feb  5 11:54:38 2008
@@ -0,0 +1,101 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+package Jifty::Plugin::Attributes::Model::Attribute;
+use base 'Jifty::Record';
+
+use Jifty::DBI::Schema;
+use Jifty::Record schema {
+    column name =>
+        type is 'text',
+        is mandatory;
+
+    column description =>
+        type is 'text';
+
+    column content =>
+        type is 'blob',
+        filters are 'Jifty::DBI::Filter::Storable';
+
+    column object_type =>
+        type is 'text',
+        is mandatory;
+
+    column object_id =>
+        type is 'int',
+        is mandatory;
+};
+
+=head2 before_create
+
+Let users pass in object instead of object_type and object_id.
+
+=cut
+
+sub before_create {
+    my $self = shift;
+    my $args = shift;
+
+    if (my $object = delete $args->{object}) {
+        $args->{object_type} = ref($object);
+        $args->{object_id}   = $object->id;
+    }
+
+    return 1;
+}
+
+=head2 current_user_can
+
+If you can read the original object, you can read its attributes. If you can
+update the original object, you can create, update, and delete its attributes.
+
+=cut
+
+sub current_user_can {
+    my $self  = shift;
+    my $right = shift;
+
+    # get a copy of the object
+    my ($type, $id);
+
+    if ($right eq 'create') {
+        my %args = @_;
+        ($type, $id) = $args{object}
+                     ? (ref($args{object}), $args{object}->id)
+                     : ($args{object_type}, $args{object_id});
+    }
+    else {
+        ($type, $id) = ($self->__value('object_type'), $self->__value('object_id'));
+    }
+
+    Carp::confess "No object given!" if !defined($type);
+
+    my $object = $type->new;
+    $object->load($id);
+
+    if ($right ne 'read') {
+        $right = 'update';
+    }
+
+    return $object->current_user_can($right, @_);
+}
+
+=head2 object
+
+Returns the object that owns this attribute.
+
+=cut
+
+sub object {
+    my $self = shift;
+    my ($type, $id) = ($self->object_type, $self->object_id);
+
+    my $object = $type->new;
+    $object->load($id);
+
+    return $object;
+}
+
+1;
+

Added: jifty/trunk/lib/Jifty/Plugin/Attributes/Model/AttributeCollection.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Plugin/Attributes/Model/AttributeCollection.pm	Tue Feb  5 11:54:38 2008
@@ -0,0 +1,52 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+package Jifty::Plugin::Attributes::Model::AttributeCollection;
+use base 'Jifty::Collection';
+
+=head2 record_class
+
+Is this even required any more?
+
+=cut
+
+sub record_class { 'Jifty::Plugin::Attributes::Model::Attribute' }
+
+=head2 named name
+
+Limits to attributes with the given name.
+
+=cut
+
+sub named {
+    my $self = shift;
+    my $name = shift;
+
+    $self->limit(column => 'name', value => $name);
+    return $self;
+}
+
+=head2 limit_to_object object
+
+Limits to attributes modifying the given object.
+
+=cut
+
+sub limit_to_object {
+    my $self = shift;
+    my $object = shift;
+
+    my $type = ref($object); # should this check be smarter?
+    return undef unless $type && $object->can('id');
+
+    my $id = $object->id;
+
+    $self->limit(column => 'object_type', value => $type);
+    $self->limit(column => 'object_id', value => $id);
+
+    return $self;
+}
+
+1;
+

Added: jifty/trunk/t/TestApp-Plugin-Attributes/Makefile.PL
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp-Plugin-Attributes/Makefile.PL	Tue Feb  5 11:54:38 2008
@@ -0,0 +1,7 @@
+use inc::Module::Install;
+
+name        'TestApp::Plugin::Attributes';
+version     '0.01';
+requires    'Jifty' => '0.71129';
+
+WriteAll;

Added: jifty/trunk/t/TestApp-Plugin-Attributes/bin/jifty
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp-Plugin-Attributes/bin/jifty	Tue Feb  5 11:54:38 2008
@@ -0,0 +1,11 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+use File::Basename qw(dirname); 
+use UNIVERSAL::require;
+
+use Jifty;
+use Jifty::Script;
+
+local $SIG{INT} = sub { warn "Stopped\n"; exit; };
+Jifty::Script->dispatch();

Added: jifty/trunk/t/TestApp-Plugin-Attributes/etc/config.yml
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp-Plugin-Attributes/etc/config.yml	Tue Feb  5 11:54:38 2008
@@ -0,0 +1,76 @@
+--- 
+framework: 
+  AdminMode: 1
+  ApplicationClass: TestApp::Plugin::Attributes
+  ApplicationName: TestApp-Plugin-Attributes
+  ApplicationUUID: CCAA95B8-D400-11DC-990A-966E8CB3492A
+  ConfigFileVersion: 3
+  Database: 
+    AutoUpgrade: 1
+    CheckSchema: 1
+    Database: testapp_plugin_attributes
+    Driver: SQLite
+    Host: localhost
+    Password: ''
+    RecordBaseClass: Jifty::DBI::Record::Cachable
+    User: ''
+    Version: 0.0.1
+  DevelMode: 1
+  L10N: 
+    PoDir: share/po
+  LogLevel: INFO
+  Mailer: Sendmail
+  MailerArgs: []
+
+  Plugins: 
+    - 
+      LetMe: {}
+
+    - 
+      SkeletonApp: {}
+
+    - 
+      REST: {}
+
+    - 
+      Halo: {}
+
+    - 
+      ErrorTemplates: {}
+
+    - 
+      OnlineDocs: {}
+
+    - 
+      CompressedCSSandJS: {}
+
+    - 
+      AdminUI: {}
+
+    - 
+      Attributes: {}
+  PubSub: 
+    Backend: Memcached
+    Enable: ~
+  SkipAccessControl: 0
+  TemplateClass: TestApp::Plugin::Attributes::View
+  View: 
+    FallbackHandler: Jifty::View::Mason::Handler
+    Handlers: 
+      - Jifty::View::Static::Handler
+      - Jifty::View::Declare::Handler
+      - Jifty::View::Mason::Handler
+  Web: 
+    BaseURL: http://localhost
+    DataDir: var/mason
+    Globals: []
+
+    MasonConfig: 
+      autoflush: 0
+      default_escape_flags: h
+      error_format: text
+      error_mode: fatal
+    Port: 8888
+    ServeStaticFiles: 1
+    StaticRoot: share/web/static
+    TemplateRoot: share/web/templates

Added: jifty/trunk/t/TestApp-Plugin-Attributes/t/00-basic.t
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp-Plugin-Attributes/t/00-basic.t	Tue Feb  5 11:54:38 2008
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+use lib 't/lib';
+use Jifty::SubTest;
+use Jifty::Test tests => 10;
+
+my $song = TestApp::Plugin::Attributes::Model::Song->new;
+my ($ok, $msg) = $song->create(
+    name   => 'Arco Arena',
+    artist => 'Cake',
+    album  => 'Comfort Eagle',
+);
+ok($ok, $msg);
+
+can_ok($song, qw/attributes first_attribute add_attribute set_attribute delete_attribute/);
+
+is($song->first_attribute('instrumental'), undef, "unknown attributes return undef for ->first_attribute");
+
+my $attrs = $song->attributes;
+isa_ok($attrs, "Jifty::Plugin::Attributes::Model::AttributeCollection", "->attributes returns an AttributeCollection");
+can_ok($attrs, qw/named limit_to_object/);
+
+ok($song->set_attribute(
+    name        => 'is_instrumental',
+    description => 'Is this song an instrumental?',
+    content     => 1,
+));
+
+my $attr = $song->first_attribute('is_instrumental');
+can_ok($attr, qw/name description content object_type object_id object/);
+
+is($attr->name, 'is_instrumental', "name of the attribute was saved");
+is($attr->description,  'Is this song an instrumental?', "description of the attribute was saved");
+is($attr->content, 1, "content of the attribute was saved");
+


More information about the Jifty-commit mailing list