Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement Site Tips (Closes #32)
  • Loading branch information
zoffixznet committed Nov 21, 2015
1 parent 70c9ef5 commit e2f4cbc
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 10 deletions.
1 change: 1 addition & 0 deletions Build.PL
Expand Up @@ -46,6 +46,7 @@ my $build = Module::Build->new(
'Mojolicious::Plugin::AssetPack' => '0.68',
'Mojolicious::Plugin::Bootstrap3' => '3.3505',
'Moo' => '2.000002',
'namespace::clean' => '0.26',
'POSIX' => 0,
'Test::Mojo::Role::Debug' => '1.003003',
'Test::Mojo::Role::ElementCounter' => '1.001005',
Expand Down
6 changes: 5 additions & 1 deletion web/lib/ModulesPerl6.pm
Expand Up @@ -7,8 +7,9 @@ use Mojo::Base 'Mojolicious';
use FindBin; FindBin->again;
use File::Spec::Functions qw/catfile/;
use Mojo::Util qw/slurp/;
use ModulesPerl6::Model::Dists;
use ModulesPerl6::Model::BuildStats;
use ModulesPerl6::Model::Dists;
use ModulesPerl6::Model::SiteTips;

sub startup {
my $self = shift;
Expand Down Expand Up @@ -48,6 +49,9 @@ sub startup {
$self->helper( build_stats => sub {
state $stats = ModulesPerl6::Model::BuildStats->new;
});
$self->helper( site_tips => sub {
state $tips = ModulesPerl6::Model::SiteTips->new;
});
$self->helper( items_in => sub {
my ( $c, $what ) = @_;
return unless defined $what;
Expand Down
19 changes: 11 additions & 8 deletions web/lib/ModulesPerl6/Model/SiteTips.pm
Expand Up @@ -9,23 +9,26 @@ use Mojo::Util qw/trim/;
use Moo;
use namespace::clean;

has _tips => ( is => 'rw' );
has _tips => (
is => 'lazy',
default => sub { shift->_load_tip_file },
);

has _tip_file => (
is => 'ro',
is => 'lazy',
init_arg => 'tip_file',
default => sub {
$ENV{MODULESPERL6_TIP_FILE}
// catfile $FindBin::Bin, qw/.. site-tips.txt/;
},
trigger => 1,
);

sub _trigger__tip_file {
my ( $self, $file ) = @_;
sub _load_tip_file {
my $self = shift;
my $file = $self->_tip_file;

open my $fh, '<', $file
or croak "Could not open site tips file for reading: $!";
or croak "Could not open site tips file [$file] for reading: $!";

my @tips;
while ( <$fh> ) {
Expand All @@ -34,7 +37,7 @@ sub _trigger__tip_file {
push @tips, $_;
}

$self->_tips( \@tips );
return \@tips;
}

sub tip {
Expand Down Expand Up @@ -65,7 +68,7 @@ This module is used to access site usage tips that are shown to users.
=head1 TIP FILE FORMAT
# This is a comment and should be ignored, as are blank lines
# This is a comment and will be ignored, as are blank lines
Tip 1
Tip 2
Expand Down
10 changes: 10 additions & 0 deletions web/site-tips.txt
@@ -0,0 +1,10 @@
# This is a comment and wil be ignored, as are blank lines
# Each line represents a single tip
# ********************************
# ********************************
# IMPORTANT: You MUST escape HTML...
# ...that also means you can use HTML code in your tips
# ********************************
# ********************************
You can link to a search query, like so: <a href="http://modules.perl6.org/#q=Test">http://modules.perl6.org/#q=Test</a>
Go directly to a module's repository using <kbd>/repo/</kbd>: <a href="http://modules.perl6.org/repo/Pastebin::Gist">http://modules.perl6.org/repo/Pastebin::Gist</a>
6 changes: 5 additions & 1 deletion web/t/02-app/01-home-page.t
Expand Up @@ -4,7 +4,7 @@ use strict;
use warnings FATAL => 'all';
use Test::Most;
use Mojo::URL;
use Test::Mojo::WithRoles qw/SubmitForm ElementCounter/;
use Test::Mojo::WithRoles qw/SubmitForm ElementCounter/;
use t::Helper;

my $db_file = t::Helper::setup_db_file;
Expand Down Expand Up @@ -76,6 +76,10 @@ $_->{travis_url} = Mojo::URL->new($_->{url})->host('travis-ci.org')
->status_is(302)
->header_is(Location => 'https://github.com/perl6/modules.perl6.org/')
;

$t->dive_reset->get_ok('/')->status_is(200)
->text_like('#site_tip' => qr/^Tip \d\z/, 'Site tip has correct text');
;
}

done_testing;
2 changes: 2 additions & 0 deletions web/t/Helper.pm
Expand Up @@ -7,6 +7,8 @@ use ModulesPerl6::Model::BuildStats;
use File::Temp;

sub setup_db_file {
$ENV{MODULESPERL6_TIP_FILE} = 't/01-models/03-site-tips-TEST-TIPS.txt';

my $db_file = File::Temp->new( UNLINK => 0, SUFFIX => '.db' );
$ENV{MODULESPERL6_DB_FILE} = $db_file;

Expand Down
4 changes: 4 additions & 0 deletions web/templates/root/index.html.ep
Expand Up @@ -25,6 +25,10 @@
</div>
</header>

<p id="site_tip" class="text-center">
<i class="glyphicon glyphicon-info-sign"></i> <%== site_tips->tip %>
</p>

<div class="panel panel-default">
<div class="panel-heading">
<h2 class="panel-title">Project List</h2>
Expand Down

0 comments on commit e2f4cbc

Please sign in to comment.