Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add an entry_tags link between entries and tags

  • Loading branch information...
commit 82271c0616f9bdc9929aa1a8bc28addb05bcbd3a 1 parent 6b85b98
@yanick authored
View
24 lib/Galuga/Controller/Root.pm
@@ -35,14 +35,7 @@ sub tags :Path('tags') :Args(0) {
# get all tags and their tally
$c->stash->{tags} = {
- map { $_->tag => $_->get_column( 'nbr_entries' ) }
- $c->model('DB::Tags')->search(
- { },
- { group_by => 'tag',
- order_by => 'tag',
- select => [ 'tag', { count => 'entry_path' } ],
- as => [qw/ tag nbr_entries /],
- } )->all
+ map { $_->label => $_->count_related( 'entry_tags' ) } $c->model('DB::Tags')->search->all
};
}
@@ -50,18 +43,9 @@ sub tags :Path('tags') :Args(0) {
sub entries :Path( 'entries' ) :Args(0) {
my ( $self, $c ) = @_;
-
- my $tags = $c->model('DB::Tags')->search({}, {
- group_by => 'tag',
- select => [
- 'tag',
- { count => 'entry_path' }
- ],
- as => [ qw/ tag nbr_entries / ],
- } );
-
- $c->stash->{tags} = [ $tags->all ];
- # the whole she-bang
+ $c->stash->{tags} = {
+ map { $_->label => $_->count_related( 'entry_tags' ) } $c->model('DB::Tags')->search->all
+ };
$c->stash->{entries} = [ $c->model('DB::Entries')->search({},{order_by=>{
'-desc' => 'created' } } )->all ];
View
50 lib/Galuga/Schema/Result/Entries.pm
@@ -7,59 +7,67 @@ use base qw/DBIx::Class::Core/;
__PACKAGE__->load_components(qw/ Core InflateColumn::DateTime/);
-__PACKAGE__->table( 'Entries' );
+__PACKAGE__->table('Entries');
__PACKAGE__->add_columns(
+ id => {
+ data_type => 'INTEGER',
+ auto_increment => 1,
+ },
path => {
- data_type => 'VARCHAR',
- size => 50,
+ data_type => 'VARCHAR',
+ size => 50,
is_nullable => 0,
},
filename => {
- data_type => 'VARCHAR',
- size => 50,
+ data_type => 'VARCHAR',
+ size => 50,
is_nullable => 0,
},
md5 => {
- data_type => 'VARCHAR',
- size => 32,
+ data_type => 'VARCHAR',
+ size => 32,
is_nullable => 1,
},
url => {
- data_type => 'VARCHAR',
- size => 32,
+ data_type => 'VARCHAR',
+ size => 32,
is_nullable => 0,
},
title => {
- data_type => 'VARCHAR',
- size => 50,
+ data_type => 'VARCHAR',
+ size => 50,
is_nullable => 0,
- },
+ },
body => {
- data_type => 'TEXT',
+ data_type => 'TEXT',
is_nullable => 0,
},
created => {
- data_type => 'DATETIME',
+ data_type => 'DATETIME',
is_nullable => 1,
},
last_updated => {
- data_type => 'DATETIME',
+ data_type => 'DATETIME',
is_nullable => 1,
},
original => {
- data_type => 'VARCHAR',
- size => 100,
+ data_type => 'VARCHAR',
+ size => 100,
is_nullable => 1,
},
);
-__PACKAGE__->set_primary_key( 'path' );
+__PACKAGE__->set_primary_key('id');
-__PACKAGE__->add_unique_constraint([ 'url' ]);
+__PACKAGE__->add_unique_constraint( ['path'] );
+__PACKAGE__->add_unique_constraint( ['url'] );
-__PACKAGE__->has_many( tags => 'Galuga::Schema::Result::Tags',
- { 'foreign.entry_path' => 'self.path' },
+__PACKAGE__->has_many(
+ entry_tags => 'Galuga::Schema::Result::EntryTags',
+ 'entry_id'
);
+__PACKAGE__->many_to_many( tags => 'entry_tags', 'tag' );
+
1;
View
27 lib/Galuga/Schema/Result/EntryTags.pm
@@ -0,0 +1,27 @@
+package Galuga::Schema::Result::EntryTags;
+
+use strict;
+use warnings;
+
+use base qw/DBIx::Class::Core/;
+
+__PACKAGE__->table( 'EntryTags' );
+
+__PACKAGE__->add_columns(
+ entry_id => {
+ data_type => 'INTEGER',
+ },
+ tag_id => {
+ data_type => 'INTEGER',
+ }
+);
+
+__PACKAGE__->set_primary_key(qw/ entry_id tag_id /);
+
+
+__PACKAGE__->belongs_to(entry => 'Galuga::Schema::Result::Entries',
+'entry_id' );
+__PACKAGE__->belongs_to(tag => 'Galuga::Schema::Result::Tags',
+'tag_id' );
+
+1;
View
19 lib/Galuga/Schema/Result/Tags.pm
@@ -8,18 +8,25 @@ use base qw/DBIx::Class::Core/;
__PACKAGE__->table( 'Tags' );
__PACKAGE__->add_columns(
- entry_path => {
- data_type => 'VARCHAR',
- size => 50,
- is_nullable => 0,
+ id => {
+ data_type => 'INTEGER',
+ auto_increment => 1,
},
- tag => {
+ label => {
data_type => 'VARCHAR',
size => 20,
is_nullable => 0,
},
);
-__PACKAGE__->set_primary_key(qw/ entry_path tag / );
+__PACKAGE__->set_primary_key(qw/ id / );
+
+
+__PACKAGE__->has_many(
+ entry_tags => 'Galuga::Schema::Result::EntryTags',
+ 'tag_id'
+);
+
+__PACKAGE__->many_to_many( entries => 'entry_tags', 'entry' );
1;
View
22 lib/Galuga/Widget/RecentTags.pm
@@ -17,7 +17,7 @@ template widget => sub {
ul {
attr { id => 'recent_tags' };
map { show( 'tag', %arg, tag => $_ ) }
- sort { lc( $a->tag ) cmp lc( $b->tag ) }
+ sort { lc( $a->label ) cmp lc( $b->label ) }
$self->get_tags( $arg{c} );
};
@@ -48,12 +48,12 @@ template 'tag' => sub {
li {
attr {
- value => $arg{tag}->get_column('nbr_entries'),
- title => $arg{tag}->tag
+ value => $arg{tag}->count_related( 'entry_tags' ),
+ title => $arg{tag}->label
};
a {
- attr { href => $arg{c}->uri_for( '/tag', $arg{tag}->tag ) };
- $arg{tag}->tag;
+ attr { href => $arg{c}->uri_for( '/tag', $arg{tag}->label ) };
+ $arg{tag}->label;
}
}
};
@@ -61,19 +61,13 @@ template 'tag' => sub {
sub get_tags {
my ( $self, $c ) = @_;
- my @tags = $c->model('DB::Entries')->search(
+ my %tags = map { $_->label => $_ } $c->model('DB::Entries')->search(
{},
{ order_by => { '-desc' => 'created' },
rows => 5
- } )->search_related( 'tags', {} )->all;
+ } )->search_related( 'entry_tags' )->search_related( 'tag' )->all;
- return $c->model('DB::Tags')->search(
- { tag => { 'IN' => [ map { $_->tag } @tags ] }, },
- { group_by => 'tag',
- order_by => 'tag',
- select => [ 'tag', { count => 'entry_path' } ],
- as => [qw/ tag nbr_entries /],
- } )->all;
+ return values %tags;
}
1;
View
2  root/entry/index.mason
@@ -48,7 +48,7 @@ $entry
<b>tags: </b>
% $m->print( join ', ',
% map { "<a href='" . $c->uri_for( "/tag/$_" ) . "'>$_</a>" }
-% sort $entry->tags->get_column( 'tag' )->all );
+% sort $entry->tags->get_column( 'label' )->all );
</div>
</%def>
View
2  script/populate_blog.pl
@@ -116,7 +116,7 @@ sub import_entry {
$entry->insert;
for ( @{ $header->{tags} } ) {
- $entry->create_related( 'tags', { tag => $_ } );
+ $entry->add_to_tags( { label => $_ } ) unless $entry->tags({label => $_ })->find;
}
}
View
9 t/controller_Root.t
@@ -2,13 +2,10 @@ use strict;
use warnings;
use Test::More;
- # We're in a t/*.t test script...
- use Test::WWW::Mechanize::Catalyst 'Galuga';
- my $mech =
- Test::WWW::Mechanize::Catalyst->new;
+use Test::WWW::Mechanize::Catalyst 'Galuga';
-$mech->get_ok('/');
+my $mech = Test::WWW::Mechanize::Catalyst->new;
-print $mech->content;
+$mech->get_ok('/entries');
done_testing();
Please sign in to comment.
Something went wrong with that request. Please try again.