From 75e130a6bd9e603bec93e51efb7e6a6473a69902 Mon Sep 17 00:00:00 2001 From: Judd Lillestrand Date: Mon, 22 Jul 2013 10:46:55 -0700 Subject: [PATCH] added tags report --- app/controllers/application_controller.rb | 7 ++++++- app/controllers/tags_controller.rb | 13 +++++++++++++ app/views/layouts/application.html.slim | 5 +++++ app/views/tags/index.html.slim | 12 ++++++++++++ config/routes.rb | 2 +- 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 app/views/tags/index.html.slim diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4b289a0..edebd30 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,7 +1,7 @@ class ApplicationController < ActionController::Base protect_from_forgery - helper_method :current_user, :logged_in? + helper_method :current_user, :logged_in?, :current_user_is_admin? private @@ -12,4 +12,9 @@ def current_user def logged_in? @current_user end + + def current_user_is_admin? + return false if current_user.nil? || !current_user.admin? + true + end end diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 6d80532..94d466e 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -1,4 +1,9 @@ class TagsController < ApplicationController + before_filter :ensure_admin, only: [:index] + + def index + @tags = Tag.all + end def show @tag = Tag.find(params[:id]) @@ -10,4 +15,12 @@ def show end end +private + + def ensure_admin + if current_user.nil? || !current_user.admin? + redirect_to root_url + end + end + end \ No newline at end of file diff --git a/app/views/layouts/application.html.slim b/app/views/layouts/application.html.slim index 56a9dc9..ea60fc8 100644 --- a/app/views/layouts/application.html.slim +++ b/app/views/layouts/application.html.slim @@ -92,6 +92,11 @@ html ul.dropdown-menu li= link_to(current_user.twitter_handle, '#') + - if current_user_is_admin? + li.divider + li= link_to "Tags Report", tags_path + + li.divider li= link_to "Sign out", "/sessions/destroy", method: 'delete' - else diff --git a/app/views/tags/index.html.slim b/app/views/tags/index.html.slim new file mode 100644 index 0000000..f339e05 --- /dev/null +++ b/app/views/tags/index.html.slim @@ -0,0 +1,12 @@ +h1 Tags Overview + +table.table.table-striped + thead + tr + td Name + td Number of Tweets + tbody + - @tags.each do |tag| + tr + td= tag.name + td= tag.stories.count diff --git a/config/routes.rb b/config/routes.rb index b5e753a..dd5308a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,7 +13,7 @@ get 'sessions/create' - resources :tags, only: :show + resources :tags, only: [:show, :index] resources :events, only: :index resources :posts do