[Jifty-commit] jifty branch, setupwizard-refactor, updated. 37efb99a2827e81c5f883e528bd390be2c84ecb3
Jifty commits
jifty-commit at lists.jifty.org
Mon Jun 21 19:12:12 EDT 2010
The branch, setupwizard-refactor has been updated
via 37efb99a2827e81c5f883e528bd390be2c84ecb3 (commit)
via da9e2d9ccf825d77024fcfa966c8da495f5231a2 (commit)
from 0d3f6a68b0d2422558075e4d2a66a881d3b10c50 (commit)
Summary of changes:
.../SetupWizard/Action/TestDatabaseConnectivity.pm | 9 +-
lib/Jifty/Plugin/SetupWizard/Dispatcher.pm | 32 +++
lib/Jifty/Plugin/SetupWizard/View/Generic.pm | 188 +--------------
lib/Jifty/Plugin/SetupWizard/View/Helpers.pm | 266 ++++++++++++++++++--
lib/Jifty/Web/Form.pm | 1 +
5 files changed, 283 insertions(+), 213 deletions(-)
create mode 100644 lib/Jifty/Plugin/SetupWizard/Dispatcher.pm
- Log -----------------------------------------------------------------
commit da9e2d9ccf825d77024fcfa966c8da495f5231a2
Author: Thomas Sibley <trs at bestpractical.com>
Date: Mon Jun 21 19:11:29 2010 -0400
Log action registration when debugging
diff --git a/lib/Jifty/Web/Form.pm b/lib/Jifty/Web/Form.pm
index 5095a56..15d4285 100644
--- a/lib/Jifty/Web/Form.pm
+++ b/lib/Jifty/Web/Form.pm
@@ -150,6 +150,7 @@ used in.
sub register_action {
my $self = shift;
my $action = shift;
+ $self->log->debug("Registering action ". $action->moniker);
$self->actions->{ $action->moniker } = $action;
weaken $self->actions->{ $action->moniker};
return $action;
commit 37efb99a2827e81c5f883e528bd390be2c84ecb3
Author: Thomas Sibley <trs at bestpractical.com>
Date: Mon Jun 21 19:12:34 2010 -0400
Checkpoint of refactoring into a generic, modifiable database widget
diff --git a/lib/Jifty/Plugin/SetupWizard/Action/TestDatabaseConnectivity.pm b/lib/Jifty/Plugin/SetupWizard/Action/TestDatabaseConnectivity.pm
index 2a54ddc..d892221 100644
--- a/lib/Jifty/Plugin/SetupWizard/Action/TestDatabaseConnectivity.pm
+++ b/lib/Jifty/Plugin/SetupWizard/Action/TestDatabaseConnectivity.pm
@@ -41,7 +41,12 @@ sub take_action {
# Remove empty arguments (empty port confuses DBI)
# Maybe should go in Jifty::DBI. it does handle undef..
- my %args = %{ $self->argument_values };
+ my $params = $self->arguments;
+ my %defaults = map { $_ => $params->{$_}{default_value} }
+ keys %$params;
+
+ my %args = (%defaults, %{ $self->argument_values });
+
for my $key (keys %args) {
delete $args{$key} if !defined($args{$key}) || !length($args{$key});
}
@@ -69,7 +74,7 @@ sub take_action {
return $self->result->error($error);
}
- $self->result->message(_('Connection successful'));
+ $self->result->message(_('Connection to %1 database "%2" successful', $args{'driver'}, $args{'database'}));
}
1;
diff --git a/lib/Jifty/Plugin/SetupWizard/Dispatcher.pm b/lib/Jifty/Plugin/SetupWizard/Dispatcher.pm
new file mode 100644
index 0000000..9722311
--- /dev/null
+++ b/lib/Jifty/Plugin/SetupWizard/Dispatcher.pm
@@ -0,0 +1,32 @@
+use warnings;
+use strict;
+
+package Jifty::Plugin::SetupWizard::Dispatcher;
+
+=head1 NAME
+
+Jifty::Plugin::SetupWizard::Dispatcher - dispatcher of the SetupWizard plugin
+
+=head1 DESCRIPTION
+
+Adds dispatching rules required for the SetupWizard plugin.
+
+=cut
+
+use Jifty::Dispatcher -base;
+
+=head1 RULES
+
+=head2 before '*'
+
+Allows running L<Jifty::Plugin::SetupWizard::Action::TestDatabaseConnectivity>
+if C<SetupMode> is turned on.
+
+=cut
+
+before '*' => run {
+ return if not Jifty->setup_mode;
+ Jifty->api->allow('Jifty::Plugin::SetupWizard::Action::TestDatabaseConnectivity');
+};
+
+1;
diff --git a/lib/Jifty/Plugin/SetupWizard/View/Generic.pm b/lib/Jifty/Plugin/SetupWizard/View/Generic.pm
index c529dea..28cf883 100644
--- a/lib/Jifty/Plugin/SetupWizard/View/Generic.pm
+++ b/lib/Jifty/Plugin/SetupWizard/View/Generic.pm
@@ -147,43 +147,6 @@ template 'language' => sub {
template 'database' => sub {
my $self = shift;
- # XXX: We've got to add a sane way to unquote stuff in onfoo handlers...
- my $onchange = 'Jifty.update('
- . Jifty::JSON::encode_json({
- actions => {},
- action_arguments => {},
- fragments => [
- {
- mode => 'Replace',
- path => '/__jifty/admin/setupwizard/database/PLACEHOLDER',
- region => Jifty->web->qualified_region('database_details'),
- },
- ],
- continuation => undef,
-
- })
- . ', this)';
-
- $onchange =~ s/PLACEHOLDER/"+this.value+"/;
-
- # Only show them drivers they have available
- my (@available_drivers, @unavailable_drivers);
- my @all_drivers = (
- { display => 'SQLite', value => 'SQLite' },
- { display => 'MySQL', value => 'mysql' },
- { display => 'PostgreSQL', value => 'Pg' },
- #{ display => 'Oracle', value => 'Oracle' },
- );
- for (@all_drivers) {
- if (Jifty->handle->is_available_driver($_->{value})) {
- push @available_drivers, $_;
- }
- else {
- push @unavailable_drivers, $_;
- }
- }
-
- my $current_driver = Jifty->config->framework('Database')->{Driver};
my $appname = Jifty->config->framework('ApplicationName');
p { _("You may choose a database engine.") };
@@ -194,156 +157,7 @@ template 'database' => sub {
p { _("MySQL and PostgreSQL are well-supported production-quality database engines. ") };
- if (@unavailable_drivers) {
- my @drivers = map { "DBD::$_->{value}" } @unavailable_drivers;
- my $drivers = join ', ', @drivers;
-
- $drivers =~ s/, (?!.*,)/ or /;
-
- p { _("If your preferred database is not listed in the dropdown below, that means we could not find a database driver for it. You may be able to remedy this by using CPAN to download and install $drivers.") };
- }
-
- $self->config_field(
- field => 'Driver',
- context => '/framework/Database',
- value_args => {
- label => _('Database Engine'),
- render_as => 'select',
- available_values => \@available_drivers,
- onchange => [$onchange],
- },
- );
-
- $self->config_field(
- field => 'Database',
- context => '/framework/Database',
- value_args => {
- label => _('Database Name'),
- },
- );
-
- render_region(
- name => 'database_details',
- path => "/__jifty/admin/setupwizard/database/$current_driver",
- );
-
- render_region(
- name => 'test_connectivity',
- path => '/__jifty/admin/setupwizard/database/test_connectivity_button',
- );
-};
-
-template 'database/SQLite' => sub {
- # Nothing more needed!
-};
-
-private template 'database/configure_connectivity' => sub {
- my $self = shift;
- my $driver = shift;
-
- $self->config_field(
- field => 'Host',
- context => '/framework/Database',
- value_args => {
- hints => _('The domain name of your database server (for example, db.example.com)'),
- },
- empty_is_undef => 1,
- );
-
- $self->config_field(
- field => 'Port',
- context => '/framework/Database',
- value_args => {
- hints => _('Leave blank to use the default value for your database'),
- },
- empty_is_undef => 1,
- );
-
- # Better default for postgres ("root" is Jifty's current default)
- my %user_value_args;
- $user_value_args{default_value} = 'postgres'
- if $driver eq 'Pg'
- && Jifty->config->framework('Database')->{User} eq 'root';
-
- $self->config_field(
- field => 'User',
- context => '/framework/Database',
- empty_is_undef => 1,
- value_args => \%user_value_args,
- );
-
- $self->config_field(
- field => 'Password',
- context => '/framework/Database',
- value_args => {
- render_as => 'password',
- },
- empty_is_undef => 1,
- );
-};
-
-template 'database/mysql' => sub {
- show configure_connectivity => 'mysql';
-};
-
-template 'database/Pg' => sub {
- my $self = shift;
- show configure_connectivity => 'Pg';
-
- $self->config_field(
- field => 'RequireSSL',
- context => '/framework/Database',
- value_args => {
- label => _('Use SSL?'),
- render_as => 'checkbox',
- },
- );
-};
-
-template 'database/test_connectivity_button' => sub {
- hyperlink(
- label => _("Test connectivity"),
- as_button => 1,
- onclick => {
- # Submit all actions
- submit => undef,
-
- # Actually test connectivity
- replace_with => '/__jifty/admin/setupwizard/database/test_connectivity',
- },
- );
-};
-
-template 'database/test_connectivity' => sub {
- my $db_args = Jifty->config->framework('Database');
- my $action = Jifty::Plugin::SetupWizard::Action::TestDatabaseConnectivity->new(
- arguments => {
- driver => $db_args->{Driver},
- database => $db_args->{Database},
- host => $db_args->{Host},
- port => $db_args->{Port},
- user => $db_args->{User},
- password => $db_args->{Password},
- requiressl => $db_args->{RequireSSL},
- },
- );
- $action->validate;
- $action->run;
-
- if ($action->result->success) {
- p {
- attr { class => 'popup_message' };
- outs $action->result->message;
- }
- }
- else {
- p {
- attr { class => 'popup_error' };
- outs $action->result->error;
- }
- }
-
- show 'test_connectivity_button';
+ show 'database_widget';
};
template 'web' => sub {
diff --git a/lib/Jifty/Plugin/SetupWizard/View/Helpers.pm b/lib/Jifty/Plugin/SetupWizard/View/Helpers.pm
index 1b77d3f..3851e29 100644
--- a/lib/Jifty/Plugin/SetupWizard/View/Helpers.pm
+++ b/lib/Jifty/Plugin/SetupWizard/View/Helpers.pm
@@ -3,44 +3,209 @@ use strict;
use warnings;
use Jifty::View::Declare -base;
-sub config_field {
+=head1 NAME
+
+Jifty::Plugin::SetupWizard::View::Helpers - Helper templates and functions for SetupWizard
+
+=head1 TEMPLATES
+
+=head2 database_widget
+
+Provides a simple database configuration widget
+
+=cut
+
+private template 'database_widget' => sub {
my $self = shift;
- my %args = @_;
+ # XXX: We've got to add a sane way to unquote stuff in onfoo handlers...
+ my $onchange = 'Jifty.update('
+ . Jifty::JSON::encode_json({
+ actions => {},
+ action_arguments => {},
+ fragments => [
+ {
+ mode => 'Replace',
+ path => $self->fragment_for('database_widget/PLACEHOLDER'),
+ region => Jifty->web->qualified_region('database_details'),
+ },
+ ],
+ continuation => undef,
- my $action = new_action('AddConfig');
+ })
+ . ', this)';
- my %value_args = %{ $args{value_args} || {} };
+ $onchange =~ s/PLACEHOLDER/"+this.value+"/;
- # Grab a sensible default, the current value of config
- if (!exists($value_args{default_value})) {
- $value_args{default_value} = Jifty->config->contextual_get($args{context}, $args{field});
+ # Only show them drivers they have available
+ my (@available_drivers, @unavailable_drivers);
+ my @all_drivers = (
+ { display => 'SQLite', value => 'SQLite' },
+ { display => 'MySQL', value => 'mysql' },
+ { display => 'PostgreSQL', value => 'Pg' },
+ #{ display => 'Oracle', value => 'Oracle' },
+ );
+ for (@all_drivers) {
+ if (Jifty->handle->is_available_driver($_->{value})) {
+ push @available_drivers, $_;
+ }
+ else {
+ push @unavailable_drivers, $_;
+ }
}
- # Grab sensible label, the value of field
- if (!exists($value_args{label})) {
- $value_args{label} = $args{field};
+ if (@unavailable_drivers) {
+ show 'database_widget/unavailable_drivers', @unavailable_drivers;
}
- outs_raw($action->form_field('value' => %value_args));
+ my $current_driver = Jifty->config->framework('Database')->{Driver};
- for my $field (qw/field context target_file message empty_is_undef/) {
- outs_raw($action->form_field(
- $field,
- render_as => 'hidden',
- (exists($args{$field}) ? (default_value => $args{$field}) : ()),
- ));
- }
+ $self->config_field(
+ field => 'Driver',
+ context => '/framework/Database',
+ value_args => {
+ label => _('Database Engine'),
+ render_as => 'select',
+ available_values => \@available_drivers,
+ onchange => [$onchange],
+ },
+ );
- return $action;
-}
+ $self->config_field(
+ field => 'Database',
+ context => '/framework/Database',
+ value_args => {
+ label => _('Database Name'),
+ },
+ );
-1;
+ render_region(
+ name => 'database_details',
+ path => $self->fragment_for("database_widget/$current_driver"),
+ );
-__END__
+ render_region(
+ name => 'test_connectivity',
+ path => $self->fragment_for("database_widget/test_connectivity"),
+ );
+};
-=head1 NAME
+private template 'database_widget/unavailable_drivers' => sub {
+ my $self = shift;
+ my $databases = join ', ', map { $_->{display} } @_;
+ my $drivers = join ', ', map { "DBD::$_->{value}" } @_;
+
+ $databases =~ s/, (?!.*,)/ and /;
+ $drivers =~ s/, (?!.*,)/ or /;
+
+ p { _("%quant(%1,%2 is,%2 are) also supported, but we couldn't find the database %quant(%1,driver,drivers). You may be able to remedy this by installing %3 from CPAN.", scalar @_, $databases, $drivers) };
+};
+
+private template 'database_widget/configure_connectivity' => sub {
+ my $self = shift;
+ my $driver = shift;
+
+ $self->config_field(
+ field => 'Host',
+ context => '/framework/Database',
+ value_args => {
+ hints => _('The host name of your database server (for example, localhost or db.example.com)'),
+ },
+ empty_is_undef => 1,
+ );
+
+ $self->config_field(
+ field => 'Port',
+ context => '/framework/Database',
+ value_args => {
+ hints => _('Leave blank to use the default value for your database'),
+ },
+ empty_is_undef => 1,
+ );
+
+ # Better default for postgres ("root" is Jifty's current default)
+ my %user_value_args;
+ $user_value_args{default_value} = 'postgres'
+ if $driver eq 'Pg'
+ && Jifty->config->framework('Database')->{User} eq 'root';
+
+ $self->config_field(
+ field => 'User',
+ context => '/framework/Database',
+ empty_is_undef => 1,
+ value_args => \%user_value_args,
+ );
+
+ $self->config_field(
+ field => 'Password',
+ context => '/framework/Database',
+ value_args => {
+ render_as => 'password',
+ },
+ empty_is_undef => 1,
+ );
+};
+
+template 'database_widget/SQLite' => sub {
+ # Nothing more needed!
+};
+
+template 'database_widget/mysql' => sub {
+ show configure_connectivity => 'mysql';
+};
+
+template 'database_widget/Pg' => sub {
+ my $self = shift;
+ show configure_connectivity => 'Pg';
+
+ $self->config_field(
+ field => 'RequireSSL',
+ context => '/framework/Database',
+ value_args => {
+ label => _('Require SSL?'),
+ render_as => 'checkbox',
+ },
+ );
+};
+
+template 'database_widget/test_connectivity' => sub {
+ my $self = shift;
+ use Data::Dumper;
+ warn Dumper([ keys %{ Jifty->web->form->actions } ]);
+
+ my @monikers = grep { /^addconfig-framework-Database-/i }
+ keys %{ Jifty->web->form->actions || {} };
+
+ Jifty->log->debug("Submitting actions: " . join ", ", @monikers );
+
+ my $action = new_action(
+ class => 'Jifty::Plugin::SetupWizard::Action::TestDatabaseConnectivity',
+ moniker => 'test-db-connectivity',
+ order => 100, # after everything else
+ );
+
+ if ( my $result = Jifty->web->response->result('test-db-connectivity') ) {
+ p {{ class is 'test-connectivity-result' };
+ if ($result->success) {
+ outs $result->message;
+ }
+ else {
+ outs $result->error;
+ }
+ };
+ }
+
+ hyperlink(
+ label => _("Test connectivity"),
+ as_button => 1,
+ onclick => {
+ # Submit the database config actions and then the connectivity test
+ submit => [ @monikers, $action ],
+ refresh_self => 1,
+ },
+ );
+
+};
-Jifty::Plugin::SetupWizard::View::Helpers - Helper templates and functions for SetupWizard
=head1 FUNCTIONS
@@ -71,3 +236,56 @@ These parameters are for specifying defaults for each action argument.
=cut
+sub config_field {
+ my $self = shift;
+ my %args = @_;
+
+ my $moniker = $args{context} || Jifty->web->serial;
+ $moniker =~ s{^/}{addconfig-}g;
+ $moniker =~ s{/}{-}g;
+ $moniker .= "-$args{field}";
+
+ my $action = new_action(
+ class => 'AddConfig',
+ moniker => $moniker,
+ %{ $args{action_args} || {} }
+ );
+
+ my %value_args = %{ $args{value_args} || {} };
+
+ # Grab a sensible default, the current value of config
+ if (!exists($value_args{default_value})) {
+ $value_args{default_value} = Jifty->config->contextual_get($args{context}, $args{field});
+ }
+
+ # Grab sensible label, the value of field
+ if (!exists($value_args{label})) {
+ $value_args{label} = $args{field};
+ }
+
+ outs_raw($action->form_field('value' => %value_args));
+
+ for my $field (qw/field context target_file message empty_is_undef/) {
+ outs_raw($action->form_field(
+ $field,
+ render_as => 'hidden',
+ (exists($args{$field}) ? (default_value => $args{$field}) : ()),
+ ));
+ }
+
+ return $action;
+}
+
+=head2 fragment_for PATH
+
+Returns an absolute version of the relative path provided for use with regions.
+
+=cut
+
+sub fragment_for {
+ my $self = shift;
+ return current_base_path() . '/' . shift;
+}
+
+1;
+
-----------------------------------------------------------------------
More information about the Jifty-commit
mailing list