Revision ec3a4636
Von Sven Schöling vor fast 11 Jahren hinzugefügt
SL/Controller/Helper/GetModels.pm | ||
---|---|---|
2 | 2 |
|
3 | 3 |
use strict; |
4 | 4 |
|
5 |
use Exporter qw(import); |
|
6 |
our @EXPORT = qw(get_models_url_params get_callback get_models); |
|
5 |
use parent 'Rose::Object'; |
|
6 |
use SL::Controller::Helper::GetModels::Filtered; |
|
7 |
use SL::Controller::Helper::GetModels::Sorted; |
|
8 |
use SL::Controller::Helper::GetModels::Paginated; |
|
9 |
|
|
10 |
use Rose::Object::MakeMethods::Generic ( |
|
11 |
scalar => [ qw(controller model query with_objects filtered sorted paginated) ], |
|
12 |
'scalar --get_set_init' => [ qw(handlers) ], |
|
13 |
); |
|
7 | 14 |
|
8 | 15 |
use constant PRIV => '__getmodelshelperpriv'; |
9 | 16 |
|
10 |
my $registered_handlers = {}; |
|
17 |
#my $registered_handlers = {}; |
|
18 |
|
|
19 |
sub init { |
|
20 |
my ($self, %params) = @_; |
|
21 |
|
|
22 |
# for my $plugin (qw(filtered sorted paginated)) { |
|
23 |
# next unless $params{$plugin}; |
|
24 |
# $self->${ \"make_$plugin" }(%{ delete $params{$plugin} || {} }); |
|
25 |
# } |
|
26 |
# |
|
27 |
# TODO: default model |
|
28 |
$self->model(delete $params{model}); |
|
29 |
|
|
30 |
for my $plugin (qw(filtered sorted paginated)) { |
|
31 |
next unless my $spec = delete $params{$plugin} // {}; |
|
32 |
my $plugin_class = "SL::Controller::Helper::GetModels::" . ucfirst $plugin; |
|
33 |
$self->$plugin($plugin_class->new(%$spec, get_models => $self)); |
|
34 |
} |
|
35 |
|
|
36 |
$self->SUPER::init(%params); |
|
37 |
} |
|
11 | 38 |
|
12 |
sub register_get_models_handlers {
|
|
13 |
my ($class, %additional_handlers) = @_;
|
|
39 |
sub register_handlers { |
|
40 |
my ($self, %additional_handlers) = @_;
|
|
14 | 41 |
|
15 |
my $only = delete($additional_handlers{ONLY}) || []; |
|
16 |
$only = [ $only ] if !ref $only; |
|
17 |
my %hook_params = @{ $only } ? ( only => $only ) : (); |
|
42 |
# my $only = delete($additional_handlers{ONLY}) || [];
|
|
43 |
# $only = [ $only ] if !ref $only;
|
|
44 |
# my %hook_params = @{ $only } ? ( only => $only ) : ();
|
|
18 | 45 |
|
19 |
my $handlers = _registered_handlers($class);
|
|
46 |
my $handlers = $self->handlers;
|
|
20 | 47 |
map { push @{ $handlers->{$_} }, $additional_handlers{$_} if $additional_handlers{$_} } keys %$handlers; |
21 | 48 |
} |
22 | 49 |
|
... | ... | |
39 | 66 |
sub get_callback { |
40 | 67 |
my ($self, %override_params) = @_; |
41 | 68 |
|
42 |
my %default_params = _run_handlers($self, 'callback', action => $self->action_name);
|
|
69 |
my %default_params = $self->_run_handlers('callback', action => $self->controller->action_name);
|
|
43 | 70 |
|
44 |
return $self->url_for(%default_params, %override_params); |
|
71 |
return $self->controller->url_for(%default_params, %override_params);
|
|
45 | 72 |
} |
46 | 73 |
|
47 |
sub get_models { |
|
48 |
my ($self, %override_params) = @_; |
|
74 |
sub get { |
|
75 |
my ($self, %params) = @_; |
|
76 |
|
|
77 |
push @{ $params{query} ||= [] }, @{ $self->query || [] }; |
|
78 |
push @{ $params{with_objects} ||= [] }, @{ $self->with_objects || [] }; |
|
79 |
|
|
80 |
%params = $self->_run_handlers('get_models', %params); |
|
81 |
|
|
82 |
return $self->manager->get_all(%params); |
|
83 |
} |
|
84 |
|
|
85 |
sub get_paginate_args { |
|
86 |
my ($self, %params) = @_; |
|
49 | 87 |
|
50 |
my %params = _run_handlers($self, 'get_models', %override_params); |
|
88 |
push @{ $params{query} ||= [] }, @{ $self->query || [] }; |
|
89 |
push @{ $params{with_objects} ||= [] }, @{ $self->with_objects || [] }; |
|
51 | 90 |
|
52 |
my $model = delete($params{model}) || die "No 'model' to work on"; |
|
91 |
$self->paginated->get_current_paginate_params(%params); |
|
92 |
} |
|
53 | 93 |
|
54 |
return "SL::DB::Manager::${model}"->get_all(%params); |
|
94 |
sub manager { |
|
95 |
die "No 'model' to work on" unless $_[0]->model; |
|
96 |
"SL::DB::Manager::" . $_[0]->model; |
|
55 | 97 |
} |
56 | 98 |
|
57 | 99 |
# |
... | ... | |
61 | 103 |
sub _run_handlers { |
62 | 104 |
my ($self, $handler_type, %params) = @_; |
63 | 105 |
|
64 |
foreach my $sub (@{ _registered_handlers(ref $self)->{$handler_type} }) {
|
|
106 |
foreach my $sub (@{ $self->handlers->{$handler_type} }) {
|
|
65 | 107 |
if (ref $sub eq 'CODE') { |
66 | 108 |
%params = $sub->($self, %params); |
67 | 109 |
} elsif ($self->can($sub)) { |
... | ... | |
74 | 116 |
return %params; |
75 | 117 |
} |
76 | 118 |
|
77 |
sub _registered_handlers { |
|
78 |
$registered_handlers->{$_[0]} //= { callback => [], get_models => [] } |
|
119 |
sub init_handlers { |
|
120 |
{ |
|
121 |
callback => [], |
|
122 |
get_models => [], |
|
123 |
} |
|
79 | 124 |
} |
80 | 125 |
|
81 | 126 |
1; |
Auch abrufbar als: Unified diff
Erste Version GetModels rewrite
known bugs:
disable pagination funktioniert nicht
compiletime optimizations werden noch nicht benutzt
doku fehlt