rework crud_getProperties. Update tests.
This commit is contained in:
parent
447fe27d5f
commit
07cde4d696
2 changed files with 25 additions and 62 deletions
|
|
@ -365,19 +365,24 @@ sub crud_dropTable {
|
|||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 crud_getProperties ( session )
|
||||
=head2 crud_getProperties ( )
|
||||
|
||||
A management class method that returns just the 'properties' from crud_definition().
|
||||
|
||||
=head3 session
|
||||
|
||||
A reference to a WebGUI::Session.
|
||||
A management class method that returns just the 'properties' from the Crud'd definition.
|
||||
These properties have limited use, as you really need a full object to get access to a
|
||||
session.
|
||||
|
||||
=cut
|
||||
|
||||
sub crud_getProperties {
|
||||
my ($class, $session) = @_;
|
||||
return $class->meta->get_all_property_list;
|
||||
my @property_names = $class->meta->get_all_property_list();
|
||||
my $properties = {};
|
||||
foreach my $property_name (@property_names) {
|
||||
my $property = $class->meta->find_attribute_by_name($property_name);
|
||||
my $form_properties = $property->form;
|
||||
$properties->{$property_name} = $form_properties;
|
||||
}
|
||||
return $properties;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
|
@ -884,56 +889,16 @@ sub reorder {
|
|||
|
||||
=head2 update ( properties )
|
||||
|
||||
Updates an object's properties. While doing so also validates default data and sets the lastUpdated date.
|
||||
|
||||
=head3 properties
|
||||
|
||||
A hash reference of properties to be set. See crud_definition() for a list of the properties available.
|
||||
|
||||
B<WARNING:> As part of it's validation mechanisms, update() will delete any elements from the properties list that are not specified in the crud_definition().
|
||||
Extend the base method to update the lastUpdated property.
|
||||
|
||||
=cut
|
||||
|
||||
#sub update {
|
||||
# my ($self, $data) = @_;
|
||||
# my $session = $self->session;
|
||||
#
|
||||
# # validate incoming data
|
||||
# my $properties = $self->meta->get_all_property_list($session);
|
||||
# my $dbData = { $self->meta->tableKey($session) => $self->getId };
|
||||
# foreach my $property (keys %{$data}) {
|
||||
#
|
||||
# # don't save fields that aren't part of our definition
|
||||
# unless (exists $properties->{$property} || $property eq 'lastUpdated') {
|
||||
# delete $data->{$property};
|
||||
# next;
|
||||
# }
|
||||
#
|
||||
# # set a default value if it's empty or undef
|
||||
# if ($data->{$property} eq "") {
|
||||
# $data->{$property} = $properties->{$property}{default};
|
||||
# }
|
||||
#
|
||||
# # serialize if needed
|
||||
# if ($properties->{$property}{serialize} && $data->{$property} ne "") {
|
||||
# $dbData->{$property} = JSON->new->canonical->encode($data->{$property});
|
||||
# }
|
||||
# else {
|
||||
# $dbData->{$property} = $data->{$property};
|
||||
# }
|
||||
# }
|
||||
#
|
||||
# # set last updated
|
||||
# $data->{lastUpdated} ||= WebGUI::DateTime->new($session, time())->toDatabase;
|
||||
#
|
||||
# # update memory
|
||||
# my $refId = id $self;
|
||||
# %{$objectData{$refId}} = (%{$objectData{$refId}}, %{$data});
|
||||
#
|
||||
# # update the database
|
||||
# $session->db->setRow($self->meta->tableName($session), $self->meta->tableKey($session), $dbData);
|
||||
# return 1;
|
||||
#}
|
||||
around update => sub {
|
||||
my ($orig, $self, $data) = @_;
|
||||
delete $data->{lastUpdated};
|
||||
$self->lastUpdated($self->_now);
|
||||
$self->$orig($data);
|
||||
};
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
|
|
|
|||
14
t/Crud.t
14
t/Crud.t
|
|
@ -45,10 +45,6 @@ my $session = WebGUI::Test->session;
|
|||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
plan tests => 55; # Increment this number for each test you create
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
|
||||
# check table structure
|
||||
WebGUI::Cruddy->crud_createTable($session);
|
||||
WebGUI::Test->addToCleanup(sub { WebGUI::Cruddy->crud_dropTable($session); });
|
||||
|
|
@ -123,7 +119,7 @@ is($copyOfRecord3->sequenceNumber, '2', "deletion of record 2 moved record 3 to
|
|||
is($copyOfRecord4->sequenceNumber, '3', "deletion of record 2 moved record 4 to sequence 3");
|
||||
|
||||
# updating
|
||||
sleep 1;
|
||||
$copyOfRecord4->dateCreated(WebGUI::DateTime->new($session, WebGUI::Test->webguiBirthday)->toMysql);
|
||||
ok($copyOfRecord4->update, "update returns success");
|
||||
isnt($copyOfRecord4->lastUpdated, $copyOfRecord4->get('dateCreated'), "updates work");
|
||||
|
||||
|
|
@ -158,8 +154,10 @@ while (my $object = $iterator->()) {
|
|||
|
||||
#crud management stuff
|
||||
is(ref WebGUI::Cruddy->crud_getProperties($session), 'HASH', 'properties work');
|
||||
is(WebGUI::Cruddy->crud_getTableKey($session), 'id', 'default key is id');
|
||||
is(WebGUI::Cruddy->crud_getTableName($session), 'some_crud_table', 'default table is some_crud_table');
|
||||
is(WebGUI::Cruddy->crud_getSequenceKey($session), undef, 'default sequence key is blank');
|
||||
is(WebGUI::Cruddy->crud_getTableKey(), 'id', 'default key is id');
|
||||
is(WebGUI::Cruddy->crud_getTableName(), 'some_crud_table', 'default table is some_crud_table');
|
||||
is(WebGUI::Cruddy->crud_getSequenceKey(), undef, 'default sequence key is blank');
|
||||
|
||||
done_testing();
|
||||
|
||||
#vim:ft=perl
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue