Skip to content

Commit

Permalink
add an entry_tags link between entries and tags
Browse files Browse the repository at this point in the history
  • Loading branch information
yanick committed Nov 23, 2010
1 parent 6b85b98 commit 82271c0
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 69 deletions.
24 changes: 4 additions & 20 deletions lib/Galuga/Controller/Root.pm
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,17 @@ 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
};

}

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 ];
Expand Down
50 changes: 29 additions & 21 deletions lib/Galuga/Schema/Result/Entries.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
27 changes: 27 additions & 0 deletions lib/Galuga/Schema/Result/EntryTags.pm
Original file line number Diff line number Diff line change
@@ -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;
19 changes: 13 additions & 6 deletions lib/Galuga/Schema/Result/Tags.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
22 changes: 8 additions & 14 deletions lib/Galuga/Widget/RecentTags.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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} );
};

Expand Down Expand Up @@ -48,32 +48,26 @@ 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;
}
}
};

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;
2 changes: 1 addition & 1 deletion root/entry/index.mason
Original file line number Diff line number Diff line change
Expand Up @@ -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>

2 changes: 1 addition & 1 deletion script/populate_blog.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Expand Down
9 changes: 3 additions & 6 deletions t/controller_Root.t
Original file line number Diff line number Diff line change
Expand Up @@ -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();

0 comments on commit 82271c0

Please sign in to comment.