Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Blogs RSS feeds (refactoring needed!)

  • Loading branch information...
commit aaae411f63453fcbb4396f296389a79babad82e1 1 parent 0f7e5ae
Zbigniew Lukasiak authored
View
14 lib/Nblog/Controller/Blog.pm
@@ -4,7 +4,7 @@ use warnings;
package Nblog::Controller::Blog;
use Moose;
use MooseX::NonMoose;
-
+use Nblog::Controller::Blog::Feed;
extends 'WebNano::Controller';
@@ -179,6 +179,18 @@ sub page_action {
)
}
+sub feed_action {
+ my ( $self, $action ) = @_;
+ Nblog::Controller::Blog::Feed->handle(
+ path => [ $action ],
+ self_url => $self->self_url . 'feed/',
+ app => $self->app,
+ blog => $self->blog,
+ );
+}
+
+
+
1;
View
27 lib/Nblog/Controller/Blog/Feed.pm
@@ -0,0 +1,27 @@
+use strict;
+use warnings;
+
+package Nblog::Controller::Blog::Feed;
+use Moose;
+extends 'Nblog::Controller::Feed';
+
+has blog => ( is => 'ro', required => 1 );
+
+sub article_rs {
+ my $self = shift;
+ return $self->blog->articles;
+}
+
+sub comment_rs {
+ my $self = shift;
+ return $self->blog->comments;
+}
+
+sub tag_rs {
+ my $self = shift;
+ return $self->blog->tags;
+}
+
+
+1;
+
View
15 lib/Nblog/Controller/Feed.pm
@@ -11,6 +11,13 @@ extends 'WebNano::Controller';
use XML::Feed;
use Text::Textile qw(textile);
+sub artile_rs { $_[0]->app->schema->resultset( 'Article' ) }
+
+sub comment_rs { $_[0]->app->schema->resultset( 'Comment' ) }
+
+sub tag_rs { $_[0]->app->schema->resultset( 'Tag' ) }
+
+
sub comments_action {
my ( $self, $subject ) = @_;
@@ -22,12 +29,12 @@ sub comments_action {
my @comments;
if ( !defined $subject )
{
- @comments = $self->app->schema->resultset( 'Comment' )->all();
+ @comments = $self->comment_rs->all();
}
else
{
@comments =
- $self->app->schema->resultset( 'Article' )
+ $self->article_rs
->search( { subject => { like => $self->app->ravlog_url_to_query($subject) } } )->first()
->comments();
}
@@ -60,12 +67,12 @@ sub articles_action {
my @articles;
if ( !defined $tag )
{
- @articles = $self->app->schema->resultset('Article')->get_latest_articles();
+ @articles = $self->article_rs->get_latest_articles();
}
else
{
@articles =
- $self->app->schema->resultset('Tag')
+ $self->tag_rs
->search( { name => { like => $self->app->ravlog_url_to_query($tag) } } )->first()->articles();
}
View
30 lib/Nblog/Schema/Result/Blog.pm
@@ -65,6 +65,36 @@ __PACKAGE__->has_many(
'blog_id'
);
+sub comments {
+ my ( $self, $args ) = @_;
+ my $schema = $self->result_source->schema;
+ my %search = ( 'blog.blog_id' => $self->id );
+ if( $args ){
+ %search = ( %search, %$args );
+ }
+ return $schema->resultset('Comment')->search(
+ \%search,
+ {
+ join => { article => 'blog' },
+ }
+ );
+}
+
+sub tags {
+ my ( $self, $args ) = @_;
+ my $schema = $self->result_source->schema;
+ my %search = ( 'blog.blog_id' => $self->id );
+ if( $args ){
+ %search = ( %search, %$args );
+ }
+ return $schema->resultset('Tag')->search(
+ \%search,
+ {
+ join => { tags_articles => { article => 'blog' } },
+ }
+ );
+}
+
__PACKAGE__->has_many(
'blogs_users' => 'Nblog::Schema::Result::BlogUser',
'blog_id'
View
35 share/templates/globals/elements/articles_list.tt
@@ -0,0 +1,35 @@
+[% IF articles.count == 0 %]
+<p>No matching articles found</p>
+[% END %]
+[% WHILE ( article = articles.next ) %]
+ <div class="post">
+ [% url = '/Article/' _ c.app.ravlog_txt_to_url(article.subject, article.id) %]
+ <div class="post-info">
+ <h2 class="post-title">
+ <a title="[% url %]" href="[% url %]">[% article.subject %]</a>
+ </h2>
+ <a href="[% c.base_uri %]/Feed/comments/[% c.app.ravlog_txt_to_url(article.subject) %]"><img src="/static/images/rss.png" alt="RSS" /></a> &nbsp;
+ <br/>
+ <span>Posted in <a href="/Blog/[% article.blog.seo_url %]">[% article.blog.title %]</a>&nbsp;&nbsp;&nbsp;</span>
+ <p class="post-date">
+ [% c.app.render_ravlog_date(article.created_at) %]
+ </p>
+ </div>
+
+ <div class="post-content">
+ [% article.formatted_body %]
+ </div>
+ <div class="post-footer">&nbsp;
+ [% IF article.tags %]
+ <span>Tags:</span>
+ [% FOREACH tag = article.tags %]
+ <a href="/tag/[% c.app.ravlog_txt_to_url(tag.name) %]">[% tag.name %]</a>
+ [% END %]
+ [% END %]
+ &nbsp;&nbsp;<a href="[% url %]">[[% article.comments_rs.count %]] comments</a>
+ </div>
+ </div>
+ <br />
+ <br />
+[% END %]
+
View
4 share/templates/globals/elements/feed_list.tt
@@ -1,7 +1,7 @@
<h2>Feeds</h2>
<ul>
-<li><a href="/Feed/articles"><img src="/static/images/rss.png" alt="RSS" /> Articles</a></li>
-<li><a href="/Feed/comments"><img src="/static/images/rss.png" alt="RSS" /> Comments</a></li>
+<li><a href="[% IF c.myroot; c.myroot _ 'feed'; ELSE; '/Feed'; END %]/articles"><img src="/static/images/rss.png" alt="RSS" /> Articles</a></li>
+<li><a href="[% IF c.myroot; c.myroot _ 'feed'; ELSE; '/Feed'; END %]/comments"><img src="/static/images/rss.png" alt="RSS" /> Comments</a></li>
[% IF rss %]
<li><a href="/Feed/articles/[% c.app.ravlog_txt_to_url(rss) %]"><img src="/static/images/rss.png" alt="RSS" /> [% rss %]</a></li>
View
9 t/db.t
@@ -42,6 +42,15 @@ my @archived = $schema->resultset('Article')->archived(
);
is( ref $archived[0], 'Nblog::Schema::Result::Article', 'archived for a blog' );
is( scalar @archived, 1, 'archived for a blog' );
+$archived[0]->add_to_comments( { body => 'aaa' } );
+my @comments = $blog->comments;
+is( ref $comments[0], 'Nblog::Schema::Result::Comment', '$blog->comments' );
+is( scalar @comments, 1, '$blog->comments' );
+$archived[0]->add_to_tags( { name => 'bbb' } );
+my @tags = $blog->tags;
+is( ref $tags[0], 'Nblog::Schema::Result::Tag', '$blog->tags' );
+is( scalar @tags, 1, '$blog->tags' );
+
my @articles = $schema->resultset('Article')->get_latest_articles( 10, { blog_id => $blog->blog_id } );
is( ref $articles[0], 'Nblog::Schema::Result::Article', 'get_latest_articles' );
Please sign in to comment.
Something went wrong with that request. Please try again.