OSDN Git Service

Plugins: add NewslashHelpers and move sidebar function
authorhylom <hylom@users.sourceforge.jp>
Fri, 22 Jun 2018 10:00:52 +0000 (19:00 +0900)
committerhylom <hylom@users.sourceforge.jp>
Fri, 22 Jun 2018 10:00:52 +0000 (19:00 +0900)
src/newslash_web/lib/Newslash/Plugin/NewslashHelpers.pm [new file with mode: 0644]
src/newslash_web/lib/Newslash/Plugin/TT2Renderer.pm
src/newslash_web/lib/Newslash/Web.pm
src/newslash_web/templates/common/sidebar.html.tt2

diff --git a/src/newslash_web/lib/Newslash/Plugin/NewslashHelpers.pm b/src/newslash_web/lib/Newslash/Plugin/NewslashHelpers.pm
new file mode 100644 (file)
index 0000000..690e42e
--- /dev/null
@@ -0,0 +1,116 @@
+package Newslash::Plugin::NewslashHelpers;
+use Mojo::Base 'Mojolicious::Plugin';
+use Mojo::JSON qw(to_json from_json);
+
+use Template;
+
+sub register {
+    my ($self, $app, $conf) = @_;
+    $self->{app} = $app;
+
+    for my $k (qw[boxes]) {
+        $app->helper($k => $self->can("_$k"))
+    }
+}
+
+sub _boxes {
+    my $c = shift;
+
+    my $users = $c->model('users');
+    my $user = $c->stash("user");
+    my $user_boxes = $users->sidebar_items(uid => $user->{uid});
+
+    my $items = [];
+    my $tt = Template->new();
+
+    for my $box (@$user_boxes) {
+        #$c->app->log->debug("load box $box->{name}");
+        my $q_params = {};
+        if ($box->{query_params}) {
+            $q_params = from_json($box->{query_params});
+        }
+        else {
+            if (!$box->{type} || $box->{type} ne "custom") {
+                push @$items, { type => $box->{type}, name => $box->{name}, contents => $box->{template} };
+                next;
+            }
+        }
+        if (!$q_params) {
+            $c->app->log->warn("Sidebar: invalid query_params in $box->{name}");
+            next;
+        }
+        $q_params->{limit} = $box->{limit} || 20;
+
+        # regularize query parameters
+        my @keys = sort { $a cmp $b } keys %$q_params;
+        my @params;
+        for my $k (@keys) {
+            push @params, $k;
+            push @params, $q_params->{$k};
+        }
+        push @params, "_user";
+        push @params, $user;
+
+        my $rs;
+        if ($box->{type} eq "custom") {
+            my $custom_boxes = $c->app->custom_boxes;
+            if ($custom_boxes) {
+                $rs = $custom_boxes->query($box->{model}, $c, @params);
+            }
+        }
+        else {
+            $rs = $c->model_cache->select($box->{model}, 60, @params);
+        }
+
+        my $result = '';
+        my $template = $box->{template};
+        next if !$rs || !$template;
+
+        if ($tt->process(\$template, {items => $rs}, \$result)) {
+            push @$items, { type => $box->{type}, name => $box->{name}, contents => $result };
+        }
+    }
+    return $items;
+}
+
+1;
+
+=encoding utf8
+
+=head1 NAME
+
+Newslash::Plugin::NewslashHelper - Newslash helpers plugin
+
+=head1 SYNOPSIS
+
+  # Mojolicious
+  $app->plugin('Newslash::Plugin::NewslashHelpers');
+
+=head1 DESCRIPTION
+
+L<Newslash::Plugin::NewslashHelpers> is collection of helpers for L<Newslash>.
+
+
+=head1 HELPERS
+
+L<Mojolicious::Plugin::NewslashHelpers> implements the following helpers.
+
+=head2 boxes
+
+  [% helpers.boxes() %]
+
+Fetch box contents for current user and returns them.
+
+=head1 METHODS
+
+=head2 register
+
+  $plugin->register(Mojolicious->new);
+
+Register helpers in L<Mojolicious> application.
+
+=head1 SEE ALSO
+
+L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
+
+=cut
index 6606736..92769a3 100644 (file)
@@ -161,6 +161,14 @@ sub register {
                                  $vars->{Newslash} = {};
                                  $vars->{Newslash}->{config} = $c->app->config;
 
+                                 $vars->{helpers} = {};
+
+                                 # register helpers
+                                 my $helpers = $renderer->helpers;
+                                 for my $helper (keys %$helpers) {
+                                     $vars->{helpers}->{$helper} = sub { $helpers->{$helper}->($c, @_); };
+                                 }
+
                                  if ($app->renderer_helper) {
                                      my $funcs = $app->renderer_helper->get_functions('Newslash');
                                      for my $name (keys %$funcs) {
@@ -182,7 +190,7 @@ sub register {
                                          $vars->{NS}->{$k} = $self->{NS_plugin};
                                      }
                                  }
-                                 $vars->{NS}->{sidebar_items} = sidebar_func($c);
+
                                  for my $key (keys %{$app->config}) {
                                      $vars->{$key} = $app->config->{$key};
                                  }
@@ -239,69 +247,5 @@ sub last_error {
     return $self->{tt}->error();
 }
 
-sub sidebar_func {
-    my $c = shift;
-
-    my $f = sub {
-        my $self = shift;
-        #my $boxes = $c->model('boxes');
-        my $users = $c->model('users');
-        my $user = $c->stash("user");
-        my $user_boxes = $users->sidebar_items(uid => $user->{uid});
-
-        my $items = [];
-        my $tt = Template->new();
-
-        for my $box (@$user_boxes) {
-            #$c->app->log->debug("load box $box->{name}");
-            my $q_params = {};
-            if ($box->{query_params}) {
-                $q_params = from_json($box->{query_params});
-            }
-            else {
-                if (!$box->{type} || $box->{type} ne "custom") {
-                    push @$items, { type => $box->{type}, name => $box->{name}, contents => $box->{template} };
-                    next;
-                }
-            }
-            if (!$q_params) {
-                $c->app->log->warn("Sidebar: invalid query_params in $box->{name}");
-                next;
-            }
-            $q_params->{limit} = $box->{limit} || 20;
-
-            # regularize query parameters
-            my @keys = sort { $a cmp $b } keys %$q_params;
-            my @params;
-            for my $k (@keys) {
-                push @params, $k;
-                push @params, $q_params->{$k};
-            }
-            push @params, "_user";
-            push @params, $user;
-
-            my $rs;
-            if ($box->{type} eq "custom") {
-                my $custom_boxes = $c->app->custom_boxes;
-                if ($custom_boxes) {
-                    $rs = $custom_boxes->query($box->{model}, $c, @params);
-                }
-            }
-            else {
-                $rs = $c->model_cache->select($box->{model}, 60, @params);
-            }
-
-            my $result = '';
-            my $template = $box->{template};
-            next if !$rs || !$template;
-
-            if ($tt->process(\$template, {items => $rs}, \$result)) {
-                push @$items, { type => $box->{type}, name => $box->{name}, contents => $result };
-            }
-        }
-        return $items;
-    };
-    return $f;
-}
 
 1;
index 06e2b60..723b767 100644 (file)
@@ -83,6 +83,9 @@ sub startup {
     # secret key for hasing
     $app->secrets([$app->config->{System}->{secret_key},]);
 
+    # Helpers for Newslash
+    $app->plugin('Newslash::Plugin::NewslashHelpers');
+
     # stash for plugins
     #$app->config->{_Plugins} = {};
 
index 990e775..80d05ea 100644 (file)
@@ -2,7 +2,8 @@
 
   [%- IF page.type == "timeline" || page.type == "archive" || page.type == "tag" -%]
   [%- ad_code("sidebar-top") -%]
-  [%- FOREACH box IN NS.sidebar_items() -%]
+  [%#- FOREACH box IN NS.sidebar_items() -%]
+  [%- FOREACH box IN helpers.boxes() -%]
   <div class="sidebar-item [% box.type %] [% box.name %]">
     [%- box.contents -%]
   </div>