This is Bavaria, Germany!
... written in a hurry. Why do days have 24 hours only?
To be short: don't (all kind of strange problems, tons of warnings and veeeery slow). Here is how to do it when you have to. The package seems to be available only at theoryx5. YAML is not installed automatically with Jifty.
ppm ppm> rep add theoryx5 http://theoryx5.uwinnipeg.ca/cgi-bin/ppmserver?urn:/PPMServer58 ppm> install YAMLTake care that HTML::Mason is at least 1.29 (you can force installation of a special package found in the search with ppm> install -force 2
ppm> search HTML::Mason 1. HTML-Mason [1.33] High-performance, dynamic web site authoring system
ppm> install HTML::Mason ppm> install Jifty
Some packages where not found immediately. PPM gave me an error-message to search for it Error: Package 'Lingua-EN-Inflect' not found on server. Please 'search' for it first.
ppm> search Lingua-EN-Inflect Searching in Active Repositories 1. Lingua-EN-Inflect [1.89] Lingua-EN-Inflect 2. Lingua-EN-Inflect-Number [1.1] Lingua-EN-Inflect-Number ppm> install 1 ppm> install Jifty
$ mysql -hlocalhost -uroot -p mysql> create database jifty_customers; mysql> use mysql; mysql> grant all on jifty_customers.* to jifty@localhost identified by 'jifty'; mysql> grant all on jifty_customers.* to jifty@'%' identified by 'jifty'; mysql> flush privileges; mysql> quit $ # is mysql ready for my new account? $ mysql -ujifty -pjifty -hlocalhost jifty_customers mysql>
Create schaffolding and create an textfield to input some data. See Jifty Tutorial for more hints about schaffolding. Ucfirst for your application's directory because this will be used for your class names. Not "customers", but "Customers".
Z:\jifty> jifty app --name Customers Can't guess application root from current path (Z:/jifty) or bin path (C:/Perl/bin) Creating new application Customers Creating directory lib Creating directory lib/Customers Creating directory bin Creating directory etc Creating directory doc Creating directory log Creating directory var Creating directory var/mason Creating directory share Creating directory share/po Creating directory share/web Creating directory share/web/templates Creating directory share/web/static Creating directory lib/Customers/Model Creating directory lib/Customers/Action Creating directory t Creating configuration file Customers/etc/config.yml
$ cd Customers
$ vim etc/config.yml
...
Database:
Database: jifty_customers
Driver: mysql
Host: localhost
User: jifty
Password: jifty
RecordBaseClass: Jifty::DBI::Record::Cachable
Version: 0.0.1
...
Datamodels are created in a perl-module. My first model is "Library" to create a table where all my customers live in.
$ bin/jifty model --name Library
Writing file lib/Customers/Model/Library.pm
Writing file t/00-model-Library.t
$ vim lib/Customers/Model/Library.pm
...
use Jifty::DBI::Schema;
# my identifier
column customer =>
type is 'text',
label is 'Customer shortname',
render_as 'Text',
since '0.0.1';
package Customers::Model::Library;
...
Database version is set in etc/config.yml to '0.0.1', so nothing is to do there.
Now update your database schema:
$ bin/jifty schema --setup INFO - Generating SQL for application Customers... INFO - Using Customers::Model::Library INFO - Using Jifty::Model::Session INFO - Using Jifty::Model::Metadata INFO - Set up version v0.0.1, jifty version 0.605070
Your database should show you the new table libraries (yes, it's lowercase and plural) like this:
mysql> show tables; +---------------------------+ | Tables_in_jifty_customers | +---------------------------+ | _jifty_metadata | | _jifty_sessions | | libraries | +---------------------------+ 3 rows in set (0.00 sec) mysql> describe libraries; +----------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------+------+-----+---------+----------------+ | id | int(11) | | PRI | NULL | auto_increment | | customer | text | YES | | NULL | | +----------+---------+------+-----+---------+----------------+ 2 rows in set (0.00 sec)Great, there is a new table libraries and there is a field customer in it.
After a short time you need more fields in the table libraries. You have to change your data definition in the Perlmodule lib/Customers/Model/Library.pm
icolumn customer =>
type is 'text',
label is 'Customer shortname',
render_as 'Text',
since '0.0.1';
column longname =>
type is 'text',
label is 'Customer longname',
render_as 'Text',
since '0.0.2';
column partner =>
type is 'text',
label is 'Who do we talk to',
render_as 'Text',
since '0.0.2';
You must add a higher version number of the database to the columns if you add or change them. After that you must change database-version number in etc/config.yml
$ vim etc/config.yml
...
Database:
...
Version: 0.0.2
...
Update your database schema. Jifty gives you the opportunity do some kind of conversion work, but you are not interested and get an error because of this. But the added fiels will be there. Don't think about the error-message.
$ bin/jifty schema --setup
Jifty version 0.605070 up to date.
INFO - Gerating SQL to upgrade Customers v0.0.1 database to v0.0.2
ERROR - Can't locate Customers/Upgrade.pm in @INC
at /Library/Perl/5.8.6/Jifty/Script/Schema.pm line 319
INFO - Upgrading through 0.0.2
INFO - Upgraded to version v0.0.2
Take a look into your database:
mysql> describe libraries; +----------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------+------+-----+---------+----------------+ | id | int(11) | | PRI | NULL | auto_increment | | customer | text | YES | | NULL | | | partner | text | YES | | NULL | | | longname | text | YES | | NULL | | +----------+---------+------+-----+---------+----------------+ 4 rows in set (0.01 sec)
$ bin/jifty server INFO - You can connect to your server at http://localhost:8888/
You should see a pony in your webbrowser. You can fill in records to your libraries-table by navigating to Administration - Library. It looks like this:
$ bin/jifty action --name InputCustomer Writing file lib/Customers/Action/InputCustomer.pm Writing file t/00-action-InputCustomer.t
Edit this file
$ vi lib/Customers/Action/InputCustomer.pm
Replace the subroutine arguments with this:
sub arguments {
my $self = shift;
return {
partner => { label => "Input some partner, I don't care", },
longname => { label => "Input some longname, I don't care", },
customer => {
label => 'Name of Customer (try: V...)',
# Give a hint of valid values for this field when
# the user keys in his stuff
autocompleter => sub {
my ( $self, $userinput_until_now ) = @_;
my $logger = Log::Log4perl::get_logger;
$logger->info( "autocompleter for customer was called"
. " with argument: $userinput_until_now" );
# Find possible values for this field.
# Usually you would search in a database.
my @customers = $self->get_all_customers;
my @found_vals = grep { m/$userinput_until_now/ } @customers;
$logger->info(
sprintf(
"I found these %d values: %s",
scalar(@found_vals), join ( ', ', @found_vals )
)
);
$logger->info( 'I give these hints: ',
join ( ', ', @found_vals ) );
# Return a list of hashes like this
# (
# { value => 'Miller', }
# { value => 'Mistake', }
# )
my @possible_vals = ();
foreach (@found_vals) {
push @possible_vals, { value => $_ };
}
return @possible_vals;
},
ajax_validates => 1,
validator => sub {
my ( $self, $userinput ) = @_;
my $logger = Log::Log4perl::get_logger;
# check bad possibilities
if ( length($userinput) < 3 ) {
my $msg = 'Customer must have 3 characters at least.';
$logger->info($msg);
return $self->validation_error( 'customer' => $msg );
}
if ( index($userinput,' ') > 0 ) {
my $msg = 'No spaces, please.';
$logger->info($msg);
return $self->validation_error( 'customer' => $msg );
}
# No errors found -> no errormsg to user
$logger->info('validator customer: ok');
return $self->validation_ok('customer');
},
},
};
}
sub get_all_customers {
my $self = shift;
# Usually you would search in a database.
my @customers = qw{
Berlin
Paris
London
Vienna
Vastopol
Villaverde
Wuppertal
};
return @customers;
}
Now you can restart your server and navigate to Administration - Library - Actions - Customers::Action::InputCustomer and you will see this inputmask which helps you in putting in new data.
The sense is to get some data of the user to do "something". This data can be achieved in the subroutine take_action of the action-module. To simply log the data the user gave us you could do:
sub take_action {
my $self = shift;
# Custom action code
my $logger = Log::Log4perl::get_logger;
# Write the fields the user gave us to log
# field partner = Mr. X
# field longname = Public library Nowhere
# field customer = nowhere
$logger->info('This is take_action');
foreach my $input_field (qw{partner longname customer}) {
my $msg = sprintf 'field %s = %s', $input_field,
$self->argument_value($input_field);
$logger->info($msg);
}
# Put these fields as a news record into the table
# or some search action or whatever
$self->report_success if not $self->result->failure;
return 1;
}
|
Last modified: 20.09.2006 13:54 Impressum |
Wozu Witze ausdenken?
Das Leben schreibt sie selbst: Das haben wir jetzt 40 Jahre so gemacht, warum sollten wir das ändern? |
Why inventing jokes?
Real life does a better job: We are doing it this way for 40 years, why should we change it? |