File tree Expand file tree Collapse file tree 4 files changed +38
-20
lines changed Expand file tree Collapse file tree 4 files changed +38
-20
lines changed Original file line number Diff line number Diff line change @@ -23,8 +23,6 @@ use Mojo::Base 'Mojolicious';
2323use Config::ZOMG ();
2424use File::Temp ();
2525use List::Util qw( any ) ;
26- use MetaCPAN::Model::Search ();
27- use MetaCPAN::Model::User ();
2826use MetaCPAN::Script::Runner ();
2927use Search::Elasticsearch ();
3028use Try::Tiny qw( catch try ) ;
@@ -36,19 +34,6 @@ has es => sub {
3634 );
3735};
3836
39- has model_search => sub {
40- my $self = shift ;
41- return MetaCPAN::Model::Search-> new(
42- es => $self -> es,
43- index => ' cpan' ,
44- );
45- };
46-
47- has model_user => sub {
48- my $self = shift ;
49- return MetaCPAN::Model::User-> new( es => $self -> es, );
50- };
51-
5237sub startup {
5338 my $self = shift ;
5439
@@ -84,6 +69,7 @@ sub startup {
8469 $self -> minion-> add_task(
8570 index_favorite => $self -> _gen_index_task_sub(' favorite' ) );
8671
72+ $self -> plugin(' MetaCPAN::API::Plugin::Model' );
8773 $self -> _set_up_routes;
8874}
8975
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ sub identity_search_form { }
66
77sub search_identities {
88 my $self = shift ;
9- my $data = $self -> app -> model_user -> lookup( $self -> param(' name' ),
9+ my $data = $self -> model -> user -> lookup( $self -> param(' name' ),
1010 $self -> param(' key' ) );
1111 $self -> stash( user_data => $data );
1212 $self -> render(' admin/search_identities' );
Original file line number Diff line number Diff line change @@ -2,14 +2,12 @@ package MetaCPAN::API::Controller::Search;
22
33use Mojo::Base ' Mojolicious::Controller' ;
44
5- has model => sub { shift -> app-> model_search };
6-
75sub first {
86 my $c = shift ;
97 return unless $c -> openapi-> valid_input;
108 my $args = $c -> validation-> output;
119
12- my $results = $c -> model-> search_for_first_result( $args -> {q } );
10+ my $results = $c -> model-> search -> search_for_first_result( $args -> {q } );
1311 return $c -> render( openapi => $results ) if $results ;
1412 $c -> rendered(404);
1513}
@@ -21,7 +19,7 @@ sub web {
2119
2220 my @search = ( @{$args }{qw/ q from size/ } );
2321 push @search , $args -> {collapsed } if exists $args -> {collapsed };
24- my $results = $c -> model-> search_web(@search );
22+ my $results = $c -> model-> search -> search_web(@search );
2523
2624 return $c -> render( json => $results );
2725}
Original file line number Diff line number Diff line change 1+ package MetaCPAN::API::Plugin::Model ;
2+
3+ use Mojo::Base ' Mojolicious::Plugin' ;
4+
5+ use Carp ();
6+ use MetaCPAN::Model::Search ();
7+ use MetaCPAN::Model::User ();
8+
9+ has app => sub { Carp::croak ' app is required' }, weak => 1;
10+
11+ has search => sub {
12+ my $self = shift ;
13+ return MetaCPAN::Model::Search-> new(
14+ es => $self -> app-> es,
15+ index => ' cpan' ,
16+ );
17+ };
18+
19+ has user => sub {
20+ my $self = shift ;
21+ return MetaCPAN::Model::User-> new( es => $self -> app-> es );
22+ };
23+
24+ sub register {
25+ my ( $plugin , $app , $conf ) = @_ ;
26+ $plugin -> app($app );
27+
28+ # cached models
29+ $app -> helper( ' model.search' => sub { $plugin -> search } );
30+ $app -> helper( ' model.user' => sub { $plugin -> user } );
31+ }
32+
33+ 1;
34+
You can’t perform that action at this time.
0 commit comments