diff options
author | Edmund von der Burg <evdb@mysociety.org> | 2011-04-06 10:42:29 +0100 |
---|---|---|
committer | Edmund von der Burg <evdb@mysociety.org> | 2011-04-06 10:42:29 +0100 |
commit | 72b67f1af5c4afac2205fe4bc08b02813d66050d (patch) | |
tree | 48f323889ec4b27c06901081c5412e6581705d7d | |
parent | 4609689f44b4e0b1f95b7fdf096d3356241c271a (diff) |
Added overrides to staging servers to make testing/dev easier
-rw-r--r-- | perllib/FixMyStreet/App.pm | 95 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Root.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand.pm | 20 | ||||
-rw-r--r-- | templates/web/default/debug_footer.html | 29 |
4 files changed, 134 insertions, 12 deletions
diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm index 1f97daf9c..1adba8482 100644 --- a/perllib/FixMyStreet/App.pm +++ b/perllib/FixMyStreet/App.pm @@ -113,23 +113,33 @@ sub cobrand { } sub _get_cobrand { - my $c = shift; - my $host = $c->req->uri->host; - my $cobrand_class = FixMyStreet::Cobrand->get_class_for_host($host); + my $c = shift; + + my $host = $c->req->uri->host; + my $override_moniker = $c->get_override('cobrand_moniker'); + + my $cobrand_class = + $override_moniker + ? FixMyStreet::Cobrand->get_class_for_moniker($override_moniker) + : FixMyStreet::Cobrand->get_class_for_host($host); + return $cobrand_class->new( { request => $c->req } ); } -=head2 setup_cobrand +=head2 setup_request - $cobrand = $c->setup_cobrand(); + $cobrand = $c->setup_request(); Work out which cobrand we should be using. Set the environment correctly - eg -template paths +template paths, maps, languages etc, etc. =cut -sub setup_cobrand { - my $c = shift; +sub setup_request { + my $c = shift; + + $c->setup_dev_overrides(); + my $cobrand = $c->cobrand; # append the cobrand templates to the include path @@ -137,9 +147,12 @@ sub setup_cobrand { [ $cobrand->path_to_web_templates->stringify ] unless $cobrand->is_default; - my $host = $c->req->uri->host; + # work out which language to use + my $lang_override = $c->get_override('lang'); + my $host = $c->req->uri->host; my $lang = - $host =~ /^en\./ ? 'en-gb' + $lang_override ? $lang_override + : $host =~ /^en\./ ? 'en-gb' : $host =~ /cy/ ? 'cy' : undef; @@ -164,6 +177,68 @@ sub setup_cobrand { return $cobrand; } +=head2 setup_dev_overrides + + $c->setup_dev_overrides(); + +This is only run if STAGING_SITE is true. + +It is intended as an easy way to change the cobrand, language, map etc etc etc +without having to muck around with domain names and so on. The overrides are set +by passing _override_xxx parameters in the query. The values and stored in the +session and are used in preference to the defaults. + +All overrides can be easily cleared by setting the _override_clear_all parameter +to true. + +=cut + +sub setup_dev_overrides { + my $c = shift; + + # If not on STAGING_SITE bail out + return unless $c->config->{STAGING_SITE}; + + # Extract all the _override_xxx parameters + my $params = $c->req->parameters; + delete $params->{$_} for grep { !m{^_override_} } keys %$params; + + # stop if there is nothing to add + return 1 unless scalar keys %$params; + + # Check to see if we should clear all + if ( $params->{_override_clear_all} ) { + delete $c->session->{overrides}; + return; + } + + # check for all the other _override params and set their values + my $overrides = $c->session->{overrides} ||= {}; + foreach my $raw_key ( keys %$params ) { + my ($key) = $raw_key =~ m{^_override_(.*)$}; + $overrides->{$key} = $params->{$raw_key}; + } + + return $overrides; +} + +=head2 get_override + + $value = $c->get_override( 'cobrand_moniker' ); + +Checks the overrides for the value given and returns it if found, undef if not. + +Always returns undef unless on a staging site (avoids autovivifying overrides +hash in session and so creating a session for all users). + +=cut + +sub get_override { + my ( $c, $key ) = @_; + return unless $c->config->{STAGING_SITE}; + return $c->session->{overrides}->{$key}; +} + =head2 send_email $email_sent = $c->send_email( 'email_template.txt', $extra_stash_values ); diff --git a/perllib/FixMyStreet/App/Controller/Root.pm b/perllib/FixMyStreet/App/Controller/Root.pm index 42ac856c6..2a25d4040 100644 --- a/perllib/FixMyStreet/App/Controller/Root.pm +++ b/perllib/FixMyStreet/App/Controller/Root.pm @@ -26,7 +26,7 @@ sub auto : Private { my ( $self, $c ) = @_; # decide which cobrand this request should use - $c->setup_cobrand(); + $c->setup_request(); return 1; } diff --git a/perllib/FixMyStreet/Cobrand.pm b/perllib/FixMyStreet/Cobrand.pm index 91155db6e..6fe2a2bc8 100644 --- a/perllib/FixMyStreet/Cobrand.pm +++ b/perllib/FixMyStreet/Cobrand.pm @@ -68,4 +68,24 @@ sub get_class_for_host { return 'FixMyStreet::Cobrand::Default'; } +=head2 get_class_for_moniker + + $cobrand_class = FixMyStreet::Cobrand->get_class_for_moniker( $moniker ); + +Given a moniker determine which cobrand we should be using. + +=cut + +sub get_class_for_moniker { + my $class = shift; + my $moniker = shift; + + foreach my $avail ( $class->available_cobrand_classes ) { + return $avail if $moniker eq $avail->moniker; + } + + # if none match then use the default + return 'FixMyStreet::Cobrand::Default'; +} + 1; diff --git a/templates/web/default/debug_footer.html b/templates/web/default/debug_footer.html index cf504e5a2..2fd3de591 100644 --- a/templates/web/default/debug_footer.html +++ b/templates/web/default/debug_footer.html @@ -5,7 +5,34 @@ <li>additional_template_paths: [% additional_template_paths.join(', ') || '--empty--' %]</li> <li>lang_code: [% lang_code %]</li> <li>user.id: [% c.user.id || '--not logged in--' %]</li> - </ul> + +<style> + #overrides_form { + font-size: 80%; + } + #overrides_form label { + float: left; + text-align: right; + padding-right: 0.5em; + width: 12em; + } + +</style> + +<!-- Use a post so that we don't clutter up the url --> +<form action="" method="post" id="overrides_form"> + + <label for="_override_clear_all">Clear all overrides:</label> + <input type="checkbox" name="_override_clear_all" id="_override_clear_all" value="1"><br> + + [% FOREACH k IN ['cobrand_moniker', 'lang'] %] + <label for="override_[% k %]">[% k %]:</label> + <input type="text" name="_override_[% k %]" id="override_[% k %]" value="[% c.get_override(k)%]"><br> + [% END %] + + <label> </label><input type="submit" value="Change overrides"> +</form> + [% END %] |