Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the map_posts utility function #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 0 additions & 47 deletions lib/PearlBee/Helpers/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -72,51 +72,4 @@ Generate a valid slug kind name

=cut

sub map_posts {
my (@posts) = @_;

# map info (utf8 compliance)
my @mapped_posts = ();
foreach my $post (@posts) {
my $el;
map {
$el->{$_} = eval { $post->$_ }
} (
'title', 'content',
'id', 'slug',
'description', 'cover',
'created_date', 'status',
'user_id', 'nr_of_comments'
);

# extract a sample from the content (first words)
my $chunk = 600;
my $post_content = $el->{content};

=aaa
$el->{content} = substr($post_content, 0, $chunk);
$el->{content} =~ s/([,\s\.])*[^,\s\.]*$/ /is; # make sure we do not split inside of a word
$el->{content} =~ s/\W*$//is; # make sure we do not split inside of a word

$el->{content} .= ' ...';
=cut

# get post author
$el->{user}->{username} = $post->user->username;
$el->{user}->{id} = $post->user->id;

# add post categories
foreach my $category ( $post->post_categories ) {
my $details;
$details->{category}->{name} = $category->category->name;
$details->{category}->{slug} = $category->category->slug;
push( @{ $el->{post_categories} }, $details );
}

push( @mapped_posts, $el );
}

return @mapped_posts;
}

1;
6 changes: 1 addition & 5 deletions lib/PearlBee/Posts.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package PearlBee::Posts;
use Dancer2 appname => 'PearlBee';
use Dancer2::Plugin::DBIC;

use PearlBee::Helpers::Util qw<map_posts>;
use PearlBee::Helpers::Pagination qw<get_total_pages get_previous_next_link>;
use PearlBee::Helpers::Captcha;

Expand All @@ -31,16 +30,13 @@ prefix '/posts' => sub {
my @popular
= resultset('View::PopularPosts')->search( {}, { rows => 3 } );

# extract demo posts info
my @mapped_posts = map_posts(@posts);

# Calculate the next and previous page link
my $total_pages = get_total_pages( $nr_of_posts, $nr_of_rows );
my ( $previous_link, $next_link )
= get_previous_next_link( $page, $total_pages );

template 'index' => {
posts => \@mapped_posts,
posts => \@posts,
recent => \@recent,
popular => \@popular,
tags => \@tags,
Expand Down