[Jifty-commit] jifty branch, setupwizard-refactor, updated. 0c2c0b5b4c588435d046a1db333abd3d07f670f8

Jifty commits jifty-commit at lists.jifty.org
Thu Jun 17 15:41:25 EDT 2010


The branch, setupwizard-refactor has been updated
       via  0c2c0b5b4c588435d046a1db333abd3d07f670f8 (commit)
      from  96908537b3b568492dd60a8bd4095dc873048da3 (commit)

Summary of changes:
 lib/Jifty/Plugin/SetupWizard.pm                    |   52 +++-
 lib/Jifty/Plugin/SetupWizard/View.pm               |  407 +-------------------
 .../SetupWizard/{View.pm => View/Generic.pm}       |  130 ++-----
 lib/Jifty/Plugin/SetupWizard/View/Helpers.pm       |   73 ++++
 4 files changed, 161 insertions(+), 501 deletions(-)
 copy lib/Jifty/Plugin/SetupWizard/{View.pm => View/Generic.pm} (78%)
 create mode 100644 lib/Jifty/Plugin/SetupWizard/View/Helpers.pm

- Log -----------------------------------------------------------------
commit 0c2c0b5b4c588435d046a1db333abd3d07f670f8
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Thu Jun 17 15:42:06 2010 -0400

    First step in reorganizing the SetupWizard plugin

diff --git a/lib/Jifty/Plugin/SetupWizard.pm b/lib/Jifty/Plugin/SetupWizard.pm
index e895dc6..9b49d9d 100644
--- a/lib/Jifty/Plugin/SetupWizard.pm
+++ b/lib/Jifty/Plugin/SetupWizard.pm
@@ -3,7 +3,7 @@ use strict;
 use warnings;
 use base 'Jifty::Plugin';
 
-__PACKAGE__->mk_accessors(qw(steps));
+__PACKAGE__->mk_accessors(qw(opts steps));
 
 sub prereq_plugins { 'Config' }
 
@@ -16,7 +16,7 @@ sub init {
     if ($opt{steps}) {
         $self->steps($opt{steps});
     }
-    else {
+    elsif ( not $opt{'nodefault'} ) {
         $self->steps([
             {
                 template    => 'welcome',
@@ -43,10 +43,16 @@ sub init {
             },
         ]);
     }
+    else {
+        $self->steps([]);
+    }
 
     for my $step (@{ $opt{add_steps} || [] }) {
         $self->add_step(%$step);
     }
+    
+    # Save our config options for later
+    $self->opts( \%opt );
 }
 
 sub add_step {
@@ -76,11 +82,43 @@ Jifty::Plugin::SetupWizard - make it easy for end-users to set up your app
 
 =head1 USAGE
 
-Add the following to your site_config.yml
-
- framework:
-   Plugins:
-     - SetupWizard: {}
+Add the following to your site_config.yml to use the default generic setup
+wizard:
+
+    framework:
+      Plugins:
+        - SetupWizard: {}
+
+You may write your own steps to add to the generic steps:
+
+    framework:
+      Plugins:
+        - SetupWizard:
+            add_steps:
+              - template: /setup/bar
+                header: MyApp's Bar
+              - template: /setup/baz
+                header: Baz
+
+Or you may replace the generic steps altogether, but still use the generic
+wizard flow:
+
+    framework:
+      Plugins:
+        - SetupWizard:
+            steps:
+              - template: /setup/bar
+                header: MyApp's Bar
+              - template: /setup/baz
+                header: Baz
+
+Finally, you can disable the generic wizard and default setup steps entirely
+and just use the helpers provided by this plugin.
+
+    framework:
+      Plugins:
+        - SetupWizard:
+            nodefault: 1
 
 =head1 METHODS
 
diff --git a/lib/Jifty/Plugin/SetupWizard/View.pm b/lib/Jifty/Plugin/SetupWizard/View.pm
index 08105f6..6ff4980 100644
--- a/lib/Jifty/Plugin/SetupWizard/View.pm
+++ b/lib/Jifty/Plugin/SetupWizard/View.pm
@@ -3,411 +3,14 @@ use strict;
 use warnings;
 use Jifty::View::Declare -base;
 
-template '/__jifty/admin/setupwizard' => page {
-    my $appname = Jifty->config->framework('ApplicationName');
-    h1 { _("Welcome to %1!", $appname) };
+my $plugin = Jifty->find_plugin('Jifty::Plugin::SetupWizard');
 
-    render_region(
-        name      => 'WizardStep',
-        path      => "/__jifty/admin/setupwizard/step",
-        arguments => {
-            step => 0,
-        },
-    );
-};
-
-template '/__jifty/admin/setupwizard/step' => sub {
-    my $step = get('step');
-    my $steps = Jifty->find_plugin('Jifty::Plugin::SetupWizard')->steps;
-    my $step_info = $steps->[$step] or die "Invalid step index";
-
-    div {
-        class is 'setupwizard-step';
-
-        form {
-            h3 { _('%1. %2', $step+1, $step_info->{header}) } if $step_info->{header};
-
-            show "/__jifty/admin/setupwizard/$step_info->{template}";
-
-            unless ($step_info->{hide_button}) {
-                form_submit(
-                    label => _('Next'),
-                    onclick => {
-                        # Submit all actions
-                        submit => undef,
-
-                        # Advance to the next step
-                        refresh_self => 1,
-                        arguments => {
-                            step => $step + 1,
-                        },
-                    },
-                );
-            }
-        };
-    };
-
-    div {
-        class is 'setupwizard-links';
-
-        span { 'Skip to: ' };
-
-        for my $i (0 .. @$steps - 1) {
-            # Separator
-            if ($i > 0) {
-                span { ' | ' }
-            }
-
-            step_link(
-                index   => $i,
-                label   => "%1",
-                current => $i == $step,
-            );
-        }
-    };
-};
-
-sub step_link {
-    my %args = (
-        index   => 0,
-        label   => "%1",
-        current => 0,
-        @_,
-    );
-
-    my $index = $args{index};
-
-    my $steps = Jifty->find_plugin('Jifty::Plugin::SetupWizard')->steps;
-    return unless $index >= 0 && $index < @$steps;
-
-    my $info = $steps->[$index];
-    my $name = $info->{link} || $info->{header} || $info->{template};
-
-    if ($args{current}) {
-        b { _($args{label}, $name) },
-    }
-    else {
-        hyperlink(
-            label => _($args{label}, $name),
-            onclick => {
-                replace_self => 1,
-                arguments => {
-                    step => $index,
-                },
-            },
-        );
-    }
-}
-
-sub config_field {
-    my %args = @_;
-
-    my $action = new_action('AddConfig');
-
-    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;
-}
-
-template '/__jifty/admin/setupwizard/welcome' => sub {
-    my $appname = Jifty->config->framework('ApplicationName');
-
-    p {
-        _("This installer will help you configure %1 by walking you through the following steps.", $appname)
-    }
-
-    ol {
-        for my $i (0 .. @{ Jifty->find_plugin('Jifty::Plugin::SetupWizard')->steps } - 1) {
-            li {
-                step_link(
-                    index   => $i,
-                    label   => "%1",
-                    current => $i == get('step'),
-                );
-            }
-        }
-    }
-
-    p {
-        _("At any time you may close the wizard; your progress will be saved for next time. You may also skip around, doing these steps in whatever order suits you.");
-    }
-
-    p {
-        outs_raw _("This setup wizard was activated by the presence of <tt>SetupMode: 1</tt> in one of your configuration files. If you are seeing this erroneously, you may restore normal operation by adjusting the <tt>etc/site_config.yml</tt> file to have <tt>SetupMode: 0</tt> set under <tt>framework</tt>.");
-    };
-
-    form_submit(
-        label => _('Begin'),
-        onclick => {
-            # Advance to the next step
-            refresh_self => 1,
-            arguments => {
-                step => get('step') + 1,
-            },
-        },
-    );
-};
-
-template '/__jifty/admin/setupwizard/language' => sub {
-    p { _("You may select a different language.") };
-};
-
-template '/__jifty/admin/setupwizard/database' => sub {
-    # 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.") };
-
-    p { _("%1 works with a number of different databases. MySQL, PostgreSQL, and SQLite are all supported. You should choose the database that you or your local database administrator knows best.", $appname) };
-
-    p { _("SQLite is a small database engine that does not need a server or any configuration. We recommend it for testing, demonstration, and development, but it is not quite right for a high-volume production server.") };
-
-    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.") };
-    }
-
-    config_field(
-        field      => 'Driver',
-        context    => '/framework/Database',
-        value_args => {
-            label            => _('Database Engine'),
-            render_as        => 'select',
-            available_values => \@available_drivers,
-            onchange         => [$onchange],
-        },
-    );
-
-    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 '/__jifty/admin/setupwizard/database/SQLite' => sub {
-    # Nothing more needed!
-};
-
-sub _configure_database_connectivity {
-    my $driver = shift;
-
-    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,
-    );
-
-    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';
-
-    config_field(
-        field   => 'User',
-        context => '/framework/Database',
-        empty_is_undef => 1,
-        value_args => \%user_value_args,
-    );
-
-    config_field(
-        field   => 'Password',
-        context => '/framework/Database',
-        value_args => {
-            render_as => 'password',
-        },
-        empty_is_undef => 1,
-    );
+unless ( $plugin->opts->{'nodefault'} ) {
+    use Jifty::Plugin::SetupWizard::View::Generic;
+    alias Jifty::Plugin::SetupWizard::View::Generic
+          under '/__jifty/admin/setupwizard';
 }
 
-template '/__jifty/admin/setupwizard/database/mysql' => sub {
-    _configure_database_connectivity('mysql');
-};
-
-template '/__jifty/admin/setupwizard/database/Pg' => sub {
-    _configure_database_connectivity('Pg');
-
-    config_field(
-        field   => 'RequireSSL',
-        context => '/framework/Database',
-        value_args => {
-            label     => _('Use SSL?'),
-            render_as => 'checkbox',
-        },
-    );
-};
-
-template '/__jifty/admin/setupwizard/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 '/__jifty/admin/setupwizard/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 '/__jifty/admin/setupwizard/database/test_connectivity_button';
-};
-
-template '/__jifty/admin/setupwizard/web' => sub {
-    p { _("You may change web server settings.") };
-
-    my $appname = lc Jifty->config->framework('ApplicationName');
-    $appname =~ s/-//g;
-
-    config_field(
-        field      => 'BaseURL',
-        context    => '/framework/Web',
-        value_args => {
-            hints => _('The root URL for the web server (examples: http://%1.yourcompany.com, http://business.com/%1)', $appname),
-        },
-    );
-
-    config_field(
-        field   => 'Port',
-        context => '/framework/Web',
-    );
-};
-
-template '/__jifty/admin/setupwizard/finalize' => sub {
-    p { _("You may finalize your configuration.") };
-
-    my $appname = Jifty->config->framework('ApplicationName');
-    my $action = config_field(
-        field      => 'SetupMode',
-        context    => '/framework',
-        message    => _('Setup finished. Welcome to %1!', $appname),
-        value_args => {
-            render_as     => 'hidden',
-            default_value => 0,
-        },
-    );
-
-    form_next_page url => '/';
-    form_submit( label => _('Done!') );
-};
-
 1;
 
 __END__
diff --git a/lib/Jifty/Plugin/SetupWizard/View.pm b/lib/Jifty/Plugin/SetupWizard/View/Generic.pm
similarity index 78%
copy from lib/Jifty/Plugin/SetupWizard/View.pm
copy to lib/Jifty/Plugin/SetupWizard/View/Generic.pm
index 08105f6..c529dea 100644
--- a/lib/Jifty/Plugin/SetupWizard/View.pm
+++ b/lib/Jifty/Plugin/SetupWizard/View/Generic.pm
@@ -1,9 +1,10 @@
-package Jifty::Plugin::SetupWizard::View;
+package Jifty::Plugin::SetupWizard::View::Generic;
 use strict;
 use warnings;
 use Jifty::View::Declare -base;
+use base qw/ Jifty::Plugin::SetupWizard::View::Helpers /;
 
-template '/__jifty/admin/setupwizard' => page {
+template 'index.html' => page {
     my $appname = Jifty->config->framework('ApplicationName');
     h1 { _("Welcome to %1!", $appname) };
 
@@ -16,7 +17,8 @@ template '/__jifty/admin/setupwizard' => page {
     );
 };
 
-template '/__jifty/admin/setupwizard/step' => sub {
+template 'step' => sub {
+    my $self = shift;
     my $step = get('step');
     my $steps = Jifty->find_plugin('Jifty::Plugin::SetupWizard')->steps;
     my $step_info = $steps->[$step] or die "Invalid step index";
@@ -27,7 +29,7 @@ template '/__jifty/admin/setupwizard/step' => sub {
         form {
             h3 { _('%1. %2', $step+1, $step_info->{header}) } if $step_info->{header};
 
-            show "/__jifty/admin/setupwizard/$step_info->{template}";
+            show $step_info->{template};
 
             unless ($step_info->{hide_button}) {
                 form_submit(
@@ -99,37 +101,8 @@ sub step_link {
     }
 }
 
-sub config_field {
-    my %args = @_;
-
-    my $action = new_action('AddConfig');
-
-    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;
-}
-
-template '/__jifty/admin/setupwizard/welcome' => sub {
+template 'welcome' => sub {
+    my $self = shift;
     my $appname = Jifty->config->framework('ApplicationName');
 
     p {
@@ -168,11 +141,12 @@ template '/__jifty/admin/setupwizard/welcome' => sub {
     );
 };
 
-template '/__jifty/admin/setupwizard/language' => sub {
+template 'language' => sub {
     p { _("You may select a different language.") };
 };
 
-template '/__jifty/admin/setupwizard/database' => 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({
@@ -229,7 +203,7 @@ template '/__jifty/admin/setupwizard/database' => sub {
         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.") };
     }
 
-    config_field(
+    $self->config_field(
         field      => 'Driver',
         context    => '/framework/Database',
         value_args => {
@@ -240,7 +214,7 @@ template '/__jifty/admin/setupwizard/database' => sub {
         },
     );
 
-    config_field(
+    $self->config_field(
         field      => 'Database',
         context    => '/framework/Database',
         value_args => {
@@ -259,14 +233,15 @@ template '/__jifty/admin/setupwizard/database' => sub {
     );
 };
 
-template '/__jifty/admin/setupwizard/database/SQLite' => sub {
+template 'database/SQLite' => sub {
     # Nothing more needed!
 };
 
-sub _configure_database_connectivity {
+private template 'database/configure_connectivity' => sub {
+    my $self   = shift;
     my $driver = shift;
 
-    config_field(
+    $self->config_field(
         field   => 'Host',
         context => '/framework/Database',
         value_args => {
@@ -275,7 +250,7 @@ sub _configure_database_connectivity {
         empty_is_undef => 1,
     );
 
-    config_field(
+    $self->config_field(
         field   => 'Port',
         context => '/framework/Database',
         value_args => {
@@ -290,14 +265,14 @@ sub _configure_database_connectivity {
         if $driver eq 'Pg'
         && Jifty->config->framework('Database')->{User} eq 'root';
 
-    config_field(
+    $self->config_field(
         field   => 'User',
         context => '/framework/Database',
         empty_is_undef => 1,
         value_args => \%user_value_args,
     );
 
-    config_field(
+    $self->config_field(
         field   => 'Password',
         context => '/framework/Database',
         value_args => {
@@ -305,16 +280,17 @@ sub _configure_database_connectivity {
         },
         empty_is_undef => 1,
     );
-}
+};
 
-template '/__jifty/admin/setupwizard/database/mysql' => sub {
-    _configure_database_connectivity('mysql');
+template 'database/mysql' => sub {
+    show configure_connectivity => 'mysql';
 };
 
-template '/__jifty/admin/setupwizard/database/Pg' => sub {
-    _configure_database_connectivity('Pg');
+template 'database/Pg' => sub {
+    my $self = shift;
+    show configure_connectivity => 'Pg';
 
-    config_field(
+    $self->config_field(
         field   => 'RequireSSL',
         context => '/framework/Database',
         value_args => {
@@ -324,7 +300,7 @@ template '/__jifty/admin/setupwizard/database/Pg' => sub {
     );
 };
 
-template '/__jifty/admin/setupwizard/database/test_connectivity_button' => sub {
+template 'database/test_connectivity_button' => sub {
     hyperlink(
         label     => _("Test connectivity"),
         as_button => 1,
@@ -338,7 +314,7 @@ template '/__jifty/admin/setupwizard/database/test_connectivity_button' => sub {
     );
 };
 
-template '/__jifty/admin/setupwizard/database/test_connectivity' => sub {
+template 'database/test_connectivity' => sub {
     my $db_args = Jifty->config->framework('Database');
     my $action = Jifty::Plugin::SetupWizard::Action::TestDatabaseConnectivity->new(
         arguments => {
@@ -367,16 +343,17 @@ template '/__jifty/admin/setupwizard/database/test_connectivity' => sub {
         }
     }
 
-    show '/__jifty/admin/setupwizard/database/test_connectivity_button';
+    show 'test_connectivity_button';
 };
 
-template '/__jifty/admin/setupwizard/web' => sub {
+template 'web' => sub {
+    my $self = shift;
     p { _("You may change web server settings.") };
 
     my $appname = lc Jifty->config->framework('ApplicationName');
     $appname =~ s/-//g;
 
-    config_field(
+    $self->config_field(
         field      => 'BaseURL',
         context    => '/framework/Web',
         value_args => {
@@ -384,17 +361,18 @@ template '/__jifty/admin/setupwizard/web' => sub {
         },
     );
 
-    config_field(
+    $self->config_field(
         field   => 'Port',
         context => '/framework/Web',
     );
 };
 
-template '/__jifty/admin/setupwizard/finalize' => sub {
+template 'finalize' => sub {
+    my $self = shift;
     p { _("You may finalize your configuration.") };
 
     my $appname = Jifty->config->framework('ApplicationName');
-    my $action = config_field(
+    my $action = $self->config_field(
         field      => 'SetupMode',
         context    => '/framework',
         message    => _('Setup finished. Welcome to %1!', $appname),
@@ -414,39 +392,7 @@ __END__
 
 =head1 NAME
 
-Jifty::Plugin::SetupWizard::View - templates for SetupWizard
-
-=head1 FUNCTIONS
-
-=head2 step_link
-
-A helper function for constructing a link to a different step. Expected
-arguments: the C<index> of the step and the C<label> for the link.
-
-=head2 config_field
-
-A helper function for constructing a mini-form for a config field. It returns
-the action that was created. Expected arguments are:
-
-=over 4
-
-=item value_args
-
-The arguments for the C<form_field> call for value. If there's no C<default_value>, one will be constructed using the C<context> parameter.
-
-=item field
-
-=item context
-
-=item target_file
-
-=item message
-
-=item empty_is_undef
-
-These parameters are for specifying defaults for each action argument.
-
-=back
+Jifty::Plugin::SetupWizard::View::Generic - generic templates for SetupWizard
 
 =cut
 
diff --git a/lib/Jifty/Plugin/SetupWizard/View/Helpers.pm b/lib/Jifty/Plugin/SetupWizard/View/Helpers.pm
new file mode 100644
index 0000000..1b77d3f
--- /dev/null
+++ b/lib/Jifty/Plugin/SetupWizard/View/Helpers.pm
@@ -0,0 +1,73 @@
+package Jifty::Plugin::SetupWizard::View::Helpers;
+use strict;
+use warnings;
+use Jifty::View::Declare -base;
+
+sub config_field {
+    my $self = shift;
+    my %args = @_;
+
+    my $action = new_action('AddConfig');
+
+    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;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+Jifty::Plugin::SetupWizard::View::Helpers - Helper templates and functions for SetupWizard
+
+=head1 FUNCTIONS
+
+=head2 config_field
+
+A helper function for constructing a mini-form for a config field. It returns
+the action that was created. Expected arguments are:
+
+=over 4
+
+=item value_args
+
+The arguments for the C<form_field> call for value. If there's no C<default_value>, one will be constructed using the C<context> parameter.
+
+=item field
+
+=item context
+
+=item target_file
+
+=item message
+
+=item empty_is_undef
+
+These parameters are for specifying defaults for each action argument.
+
+=back
+
+=cut
+

-----------------------------------------------------------------------


More information about the Jifty-commit mailing list