Extending the Garden

14 views
Skip to first unread message

Ian Bottomley

unread,
May 13, 2014, 1:48:22 PM5/13/14
to cataly...@googlegroups.com
have trnasferred this query here from RDBO group.

I'm experimenting with extending the CRUD application generated by mk_garden. 

I have successfully added §Role/Action austorisation - and role sensitive menu's

Now looking at Role sensitive forms.

My first foray is to simply try to add other forms and switch between them

I have tried to evaluate this in a simple way: changing the name of a form and seeing what I must do to get this form to 'work'
However despite my best efforts - I can make it sing. The CRUD ecosystem doesnt recognise the form as 'one of its own' and throws errors

I have read other posts here which suggest I may be barking up the wrong tree in that there is a table=form=controller model here and what I am trying to do conflicts with this 

I have outlined a simple test case below.

If you have a working Rose Garden app - jump straight to the JUMP HERE marker :)

any comments would be appreciated

cheers



MY TEST:



Create a small database - mine is in postgres 9.1

-- Table: smalltable

-- DROP TABLE smalltable;

CREATE TABLE smalltable
(
 id integer NOT NULL,
 name character varying(16),
 CONSTRAINT pkey PRIMARY KEY (id)
)
WITH (
 OIDS=FALSE
);
ALTER TABLE smalltable
 OWNER TO postgres;


Create a Catalyst app using these instructions


>>>>>Fixing the errors in the mk_garden script

#!/path/to/perl
use MyDB;
use Rose::DBx::Garden::Catalyst;
   
my $garden = Rose::DBx::Garden::Catalyst->new(
   garden_prefix               => 'RDBO',
   catalyst_prefix             => 'MyDatabase',
   controler_prefix            => 'MyController'
==========^^^ needs to be double L =============^^^ Need a comma

   db                          => MyDB->new,
   debug                       => 1,
   perltidy_opts               => '-pbp -nst -nse',                                      
   tt                          => 1,                 
   include_autoinc_form_fields => 0,
   column_to_label             =>
       sub {
           my ($garden_obj, $col_name) = @_;
          return join(' ', map { ucfirst } split(/_/, $col_name));
       },
   );
$garden->plant('lib');


Plant the Garden

This now works - OK

JUMP TO HERE IF YOU HAVE WORKING APP

now change the name of Smalldb::SmallTable::Form.pm to be UserForm.pm
Edit userform.pm - set package to reflect file name UserForm.pm

package RDBO::Smalldb::Smalltable::UserForm;
 ===================================^^^^^^


edit Controller TestCase::Controller::RDGC::Smalldb::Smalltable.pm

to reflect new form name

package TestCase::Controller::RDGC::Smalldb::Smalltable;
use strict;
use base qw( TestCase::Base::Controller::RHTMLO );
use MRO::Compat;
use mro 'c3';
use RDBO::Smalldb::Smalltable::UserForm;
=======================^^^^^^^
__PACKAGE__->config(
   form_class            => 'RDBO::Smalldb::Smalltable::UserForm',
                                                  ===============^^^^^^^^
   init_form             => 'init_with_smalltable',
   init_object           => 'smalltable_from_form',
   default_template      => 'rdgc/smalldb/smalltable/edit.tt',
   model_name            => 'RDGC::Smalldb::Smalltable',
   primary_key           => ['id'],
   view_on_single_result => 1,
   page_size             => 50,
);

1;

Now -try to view a record with this renamed form.

error is

Caught exception in TestCase::Controller::RDGC::Smalldb::Smalltable->auto "RDBO::Smalldb::Smalltable::UserForm is not an object at /usr/local/share/perl/5.14.2/Rose/HTMLx/Form/Related/RDBO/Metadata.pm line 39."



Peter Karman

unread,
May 13, 2014, 10:44:48 PM5/13/14
to cataly...@googlegroups.com
On 5/13/14 12:48 PM, Ian Bottomley wrote:

>
> My first foray is to simply try to add other forms and switch between them
>
> I have tried to evaluate this in a simple way: changing the name of a
> form and seeing what I must do to get this form to 'work'
> However despite my best efforts - I can make it sing. The CRUD ecosystem
> doesnt recognise the form as 'one of its own' and throws errors
>
> I have read other posts here which suggest I may be barking up the wrong
> tree in that there is a table=form=controller model here and what I am
> trying to do conflicts with this
>

You have read correctly. The convention is that the object class's form
class ends with ::Form.

E.g.

MySchema::Foo
MySchema::Foo::Form

You can change the convention by creating your own Form::Metadata class
and overriding the object_class but I think that's probably more
intrusive than you want.

If what you want to do is create new forms, in *addition* to the ones
created for you by the Garden, then why not just create new classes,
rather than renaming existing ones? That's what I do in my apps. I use
the generated classes as the basic building blocks and then create new,
custom classes to extend my app's functionality.

If you really must change the form name, you'll need to create your own
Form::Metadata class and set the object_class accordingly.

new file:

package RDBO::Smalldb::Smalltable::UserForm::Metadata;
use base 'Rose::DBx::Garden::Catalyst::Form::Metadata';

sub init_object_class {
return 'RDBO::Smalldb::Smalltable';
}

1;

and then edit your UserForm class, adding:

use RDBO::Smalldb::Smalltable::UserForm::Metadata;

sub init_metadata_class {
return 'RDBO::Smalldb::Smalltable::UserForm::Metadata';
}


Now you've told your app to use a custom Form::Metadata class, which
registers your object_class correctly. The idea is that the object_class
points at the form_class and vice-versa. That's the "Related" part of
the Rose::HTMLx::Form::Related module.



--
Peter Karman . http://peknet.com/ . pe...@peknet.com

Ian Bottomley

unread,
May 14, 2014, 9:55:28 AM5/14/14
to cataly...@googlegroups.com
Peter: Many thanks for that.

It answers both questions 
a) why isnt it working
b) am i barking up the wrong tree (Yes!) :)

Just a small follow up question

you say 

" ... then why not just create new classes, 
rather than renaming existing ones ..."

would that mean - classes which extend the "::Forms" planted in the garden 
or, a parallel set of classes with their own namespace and hierarchy re implementing the various actions that are provided by the garden?

cheers again
Ian B.

Peter Karman

unread,
May 14, 2014, 10:41:23 AM5/14/14
to cataly...@googlegroups.com
On 5/14/14 8:55 AM, Ian Bottomley wrote:

> Just a small follow up question
>
> you say
>
> " ... then why not just create new classes,
> rather than renaming existing ones ..."
>
> would that mean - classes which extend the "::Forms" planted in the garden
> or, a parallel set of classes with their own namespace and hierarchy re
> implementing the various actions that are provided by the garden?
>

I guess it depends on what problem you're trying to solve.

I tend to the latter (new classes in their own namespace). I use the
generated classes as the /admin section of my app, and then create a
/user space, and sometimes a /api space (with
CatalystX::CRUD::Controller::REST).

They can all re-use the same Model classes, but present that Model
through their own Controller+View.

Ian Bottomley

unread,
May 14, 2014, 1:14:00 PM5/14/14
to cataly...@googlegroups.com, pe...@peknet.com
Thanks again - should now have enough to start coding off into the distant sunset ... hopefully in the right direction

Ian B.
Reply all
Reply to author
Forward
0 new messages