Remove Class::InsideOut from Shop/Vendor.t Update migration notes, tests and POD.

This commit is contained in:
Colin Kuskie 2010-07-02 15:16:43 -07:00
parent e0177dc666
commit c159067c36
3 changed files with 186 additions and 128 deletions

View file

@ -183,3 +183,8 @@ Asset API
----------
->get will still work, but will be slightly slower since inside it calls the direct Moose accessor. Similarly,
getId is slightly slower than ->assetId.
WebGUI::Shop::Vendor
====================
Object properties are no longer written to the database when an object is created from scratch. The write method needs
to be called.

View file

@ -1,7 +1,93 @@
package WebGUI::Shop::Vendor;
use strict;
use Class::InsideOut qw{ :std };
use Scalar::Util qw/blessed/;
use Moose;
use WebGUI::Definition;
property 'name' => (
is => 'rw',
noFormPost => 1,
default => '',
);
property 'userId' => (
is => 'rw',
noFormPost => 1,
default => '',
);
property 'url' => (
is => 'rw',
noFormPost => 1,
default => '',
);
property 'paymentInformation' => (
is => 'rw',
noFormPost => 1,
default => '',
);
property 'preferredPaymentType' => (
is => 'rw',
noFormPost => 1,
default => '',
);
has 'dateCreated' => (
is => 'ro',
);
has [ qw/session vendorId/ ] => (
is => 'ro',
required => 1,
);
around BUILDARGS => sub {
my $orig = shift;
my $class = shift;
if (ref $_[0] eq 'HASH') {
##Need same db code as below here.
##Session check goes here?
##Build a new one
my $session = $_[0]->{session};
if (! (blessed $session && $session->isa('WebGUI::Session')) ) {
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session.");
}
my ($vendorId, $dateCreated) = $class->_init($session);
my $properties = {};
$properties->{vendorId} = $vendorId;
$properties->{dateCreated} = $dateCreated;
return $class->$orig($properties);
}
my $session = shift;
if (! (blessed $session && $session->isa('WebGUI::Session'))) {
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session.");
}
my $argument2 = shift;
if (!defined $argument2) {
WebGUI::Error::InvalidParam->throw( param=>$argument2, error=>"Need a vendorId.");
}
if (ref $argument2 eq 'HASH') {
##Build a new one
my ($vendorId, $dateCreated) = $class->_init($session);
my $properties = $argument2;
$properties->{session} = $session;
$properties->{vendorId} = $vendorId;
$properties->{dateCreated} = $dateCreated;
return $class->$orig($properties);
}
else {
##Look up one in the db
my $vendor = $session->db->quickHashRef("select * from vendor where vendorId=?", [$argument2]);
if ($vendor->{vendorId} eq "") {
WebGUI::Error::ObjectNotFound->throw(error=>"Vendor not found.", id=>$argument2);
}
$vendor->{session} = $session;
return $class->$orig($vendor);
}
};
use WebGUI::Shop::Admin;
use WebGUI::Exception::Shop;
use WebGUI::International;
@ -21,7 +107,7 @@ Keeps track of vendors that sell merchandise in the store.
use WebGUI::Shop::Vendor;
my $vendor = WebGUI::Shop::Vendor->new($session, $vendord);
my $vendor = WebGUI::Shop::Vendor->new($session, $vendorId);
=head1 METHODS
@ -29,35 +115,35 @@ These subroutines are available from this package:
=cut
readonly session => my %session;
readonly properties => my %properties;
#-------------------------------------------------------------------
=head2 _init ( session )
Builds a stub of object information in the database, and returns the newly created
vendorId, and the dateCreated fields so the object can be initialized correctly.
=cut
sub _init {
my $class = shift;
my $session = shift;
my $vendorId = $session->id->generate;
my $dateCreated = WebGUI::DateTime->new($session)->toDatabase;
$session->db->write("insert into vendor (vendorId, dateCreated) values (?, ?)",[$vendorId, $dateCreated]);
return ($vendorId, $dateCreated);
}
#-------------------------------------------------------------------
=head2 create ( session, properties )
Constructor. Creates a new vendor.
=head3 session
A reference to the current session.
=head3 properties
A hash reference containing the properties for this vendor. See update() for details.
Constructor. Creates a new vendor. Really an alias for WebGUI::Shop::Vendor->new($session, $properties)
=cut
sub create {
my ($class, $session, $properties) = @_;
unless (defined $session && $session->isa("WebGUI::Session")) {
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session.");
}
my $id = $session->id->generate;
$session->db->write("insert into vendor (vendorId, dateCreated) values (?, now())",[$id]);
my $self = $class->new($session, $id);
$self->update($properties);
return $self;
return $class->new($session, $properties);
}
#-------------------------------------------------------------------
@ -70,51 +156,20 @@ Deletes this vendor.
sub delete {
my ($self) = @_;
$self->session->db->deleteRow("vendor","vendorId",$self->getId);
}
#-------------------------------------------------------------------
=head2 get ( [ property ] )
Returns a duplicated hash reference of this objectÕs data. See update() for details.
=head3 property
Any field returns the value of a field rather than the hash reference.
=head3 Additional properties
=head4 dateCreated
The date this vendor was created in the system.
=head4 vendorId
The id of this vendor from the database. Use getId() instead.
=cut
sub get {
my ($self, $name) = @_;
if (defined $name) {
return $properties{id $self}{$name};
}
my %copyOfHashRef = %{$properties{id $self}};
return \%copyOfHashRef;
$self->session->db->deleteRow("vendor", "vendorId", $self->vendorId);
}
#-------------------------------------------------------------------
=head2 getId ()
Returns the unique id of this item.
Returns the unique id of this item. You should use $self->vendorId instead.
=cut
sub getId {
my $self = shift;
return $self->get("vendorId");
return $self->vendorId;
}
#-------------------------------------------------------------------
@ -147,7 +202,7 @@ sub getPayoutTotals {
my %totals = $self->session->db->buildHash(
'select vendorPayoutStatus, sum(vendorPayoutAmount) as amount from transactionItem as t1, transaction as t2 '
.'where t1.transactionId = t2.transactionId and t2.isSuccessful <> 0 and vendorId=? group by vendorPayoutStatus ',
[ $self->getId ]
[ $self->vendorId ]
);
# Format the payout categories and calc the total those.
@ -204,10 +259,10 @@ sub isVendorInfoComplete {
my $self = shift;
my $complete =
defined $self->get( 'name' )
&& defined $self->get( 'userId' )
&& defined $self->get( 'preferredPaymentType' )
&& defined $self->get( 'paymentInformation' );
defined $self->name
&& defined $self->userId
&& defined $self->preferredPaymentType
&& defined $self->paymentInformation;
return $complete
}
@ -216,7 +271,12 @@ sub isVendorInfoComplete {
=head2 new ( session, vendorId )
Constructor. Returns a WebGUI::Shop::Vendor object.
=head2 new ( session, properties )
=head2 new ( hashref )
Constructor. Returns a WebGUI::Shop::Vendor object, either by fetching information from the database,
or using passed in properties.
=head3 session
@ -229,26 +289,44 @@ A unique id for a vendor that already exists in the database. If the vendorId i
in, then a WebGUI::Error::InvalidParam Exception will be thrown. If the requested Id cannot
be found in the database, then a WebGUI::Error::ObjectNotFound exception will be thrown.
=head3 properties
A hashref of properties to assign to the object when it is created.
=head3 hashref
A classic Moose-style hashref of options. It must include a WebGUI::Session object.
=head3 Attributes
=head4 name
The name of the vendor.
=head4 userId
The unique GUID of the vendor.
=head4 url
The vendor's url.
=head4 vendorId
A unique identifier for this vendor. This option may be included in the properties for the new object, but it will
be ignored.
=head4 dateCreated
The date this vendor was created, in database format. This option may be included in the properties for the new object,
but it will be ignored.
=head4 paymentInformation
=head4 preferredPaymentType
=cut
sub new {
my ($class, $session, $vendorId) = @_;
unless (defined $session && $session->isa("WebGUI::Session")) {
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session.");
}
unless (defined $vendorId) {
WebGUI::Error::InvalidParam->throw( param=>$vendorId, error=>"Need a vendorId.");
}
my $vendor = $session->db->quickHashRef("select * from vendor where vendorId=?",[$vendorId]);
if ($vendor->{vendorId} eq "") {
WebGUI::Error::ObjectNotFound->throw(error=>"Vendor not found.", id=>$vendorId);
}
my $self = register $class;
my $id = id $self;
$session{ $id } = $session;
$properties{ $id } = $vendor;
return $self;
}
#-------------------------------------------------------------------
@ -262,7 +340,7 @@ A reference to the current session.
=head3 userId
A unique userId. Will pull from the session if not specified.
A unique userId. Will pull from the session if not specified.
=cut
@ -289,44 +367,16 @@ Returns a reference to the current session.
#-------------------------------------------------------------------
=head2 update ( properties )
=head2 write ( )
Sets properties of the vendor
=head3 properties
A hash reference that contains one of the following:
=head4 name
The name of the vendor.
=head4 userId
The unique GUID of the vendor.
=head4 url
The vendor's url.
=head4 paymentInformation
????
=head4 preferredPaymentType
????
Serializes the object's properties to the database
=cut
sub update {
my ($self, $newProperties) = @_;
my $id = id $self;
my @fields = (qw(name userId url paymentInformation preferredPaymentType));
foreach my $field (@fields) {
$properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field};
}
$self->session->db->setRow("vendor","vendorId",$properties{$id});
sub write {
my ($self) = @_;
my $properties = $self->get();
$self->session->db->setRow("vendor", "vendorId", $properties);
}
#-------------------------------------------------------------------
@ -447,7 +497,7 @@ sub www_manage {
.WebGUI::Form::formHeader($session, {extras=>'style="float: left;"' })
.WebGUI::Form::hidden($session, { name => "shop", value => "vendor" })
.WebGUI::Form::hidden($session, { name => "method", value => "delete" })
.WebGUI::Form::hidden($session, { name => "vendorId", value => $vendor->getId })
.WebGUI::Form::hidden($session, { name => "vendorId", value => $vendor->vendorId })
.WebGUI::Form::submit($session, { value => $i18n->get("delete"), extras => 'class="backwardButton"' })
.WebGUI::Form::formFooter($session)
@ -455,12 +505,12 @@ sub www_manage {
.WebGUI::Form::formHeader($session, {extras=>'style="float: left;"' })
.WebGUI::Form::hidden($session, { name => "shop", value => "vendor" })
.WebGUI::Form::hidden($session, { name => "method", value => "edit" })
.WebGUI::Form::hidden($session, { name => "vendorId", value => $vendor->getId })
.WebGUI::Form::hidden($session, { name => "vendorId", value => $vendor->vendorId })
.WebGUI::Form::submit($session, { value => $i18n->get("edit"), extras => 'class="normalButton"' })
.WebGUI::Form::formFooter($session)
# Append name
.' '. $vendor->get("name")
.' '. $vendor->name
.'</div>';
}

View file

@ -31,14 +31,14 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
plan tests => 49;
plan tests => 50;
#----------------------------------------------------------------------------
# put your tests here
my $loaded = use_ok('WebGUI::Shop::Vendor');
my ($vendor, $guard, $numberOfVendors);
my ($vendor);
my ($fence, $fenceCopy);
my $fenceUser = WebGUI::User->new($session, 'new');
$fenceUser->username('fence');
@ -46,8 +46,6 @@ my $guardUser = WebGUI::User->new($session, 'new');
$guardUser->username('guard');
WebGUI::Test->addToCleanup($fenceUser, $guardUser);
$numberOfVendors = scalar @{ WebGUI::Shop::Vendor->getVendors($session) };
#######################################################################
#
# new
@ -125,15 +123,17 @@ cmp_deeply(
my $now = WebGUI::DateTime->new($session, time);
eval { $fence = WebGUI::Shop::Vendor->create($session, { userId => $fenceUser->userId, }); };
WebGUI::Test->addToCleanup($fence);
$e = Exception::Class->caught();
ok(!$e, 'No exception thrown by create');
ok(!$e, 'No exception thrown by create') ||
diag $@;
isa_ok($vendor, 'WebGUI::Shop::Vendor', 'create returns correct type of object');
WebGUI::Test->addToCleanup($fence);
$fence->write;
ok($fence->get('dateCreated'), 'dateCreated is not null');
my $dateCreated = WebGUI::DateTime->new($session, $fence->get('dateCreated'));
my $deltaDC = $dateCreated - $now;
cmp_ok( $deltaDC->seconds, '<=', 2, 'dateCreated is set properly');
cmp_ok( $deltaDC->in_units('seconds'), '<=', 2, 'dateCreated is set properly');
#######################################################################
#
@ -144,12 +144,15 @@ cmp_ok( $deltaDC->seconds, '<=', 2, 'dateCreated is set properly');
ok($session->id->valid($fence->get('vendorId')), 'get: vendorId is a valid guid');
is($fence->getId, $fence->get('vendorId'), 'get: getId is an alias for get vendorId');
is($fence->get('userId'), $fenceUser->userId, 'get: userId');
is($fence->get('name'), undef, 'get: by default, no name is set');
is($fence->get('name'), '', 'get: by default, no name is set');
$fence->update({name => 'Bogs Diamond'});
is($fence->get('name'), 'Bogs Diamond', 'get: get name');
is($fence->get('userId'), $fenceUser->userId, 'get: updating name did not affect userId');
my $fence_fresh = WebGUI::Shop::Vendor->new($session, $fence->vendorId);
is($fence->name, 'Bogs Diamond', 'update wrote to the db');
my $newProps = {
name => 'Warden Norton',
url => 'http://www.shawshank.com',
@ -170,7 +173,6 @@ cmp_deeply(
paymentInformation => ignore(),
vendorId => ignore(),
preferredPaymentType => ignore(),
paymentAddressId => ignore(),
dateCreated => ignore(),
url => 'http://www.shawshank.com',
userId => $fenceUser->userId,
@ -246,10 +248,11 @@ my $defaultVendor = WebGUI::Shop::Vendor->newByUserId($session, 3);
#
#######################################################################
$guard = WebGUI::Shop::Vendor->create($session, { userId => $guardUser->userId, name => q|Warden Norton|});
my $guard = WebGUI::Shop::Vendor->create($session, { userId => $guardUser->userId, name => q|Warden Norton|});
$guard->write;
WebGUI::Test->addToCleanup($guard);
my $vendorsList = WebGUI::Shop::Vendor->getVendors($session);
cmp_deeply(
cmp_bag(
$vendorsList,
[ $guard, $fence, $defaultVendor, ],
'getVendors returns all 3 vendors as an array ref'