From 95b84f003c6df39965ce16a5df1761fd6e08d86b Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Wed, 9 Sep 2015 17:40:15 +0200 Subject: [PATCH] Add problem count changing to the admin dashboard --- app/controllers/admin/dashboard_controller.rb | 12 ++++++++++++ app/views/admin/dashboard/index.html.erb | 8 +++++++- config/locales/views/admin/de.yml | 8 +++++++- config/routes.rb | 1 + test/controllers/admin/dashboard_controller_test.rb | 6 ++++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/dashboard_controller.rb b/app/controllers/admin/dashboard_controller.rb index 11fcc7b..c049480 100644 --- a/app/controllers/admin/dashboard_controller.rb +++ b/app/controllers/admin/dashboard_controller.rb @@ -1,4 +1,16 @@ class Admin::DashboardController < AdminController def index + @current_problem_count = Problem.count + end + + def update_problem_count + begin + new_problem_count = params[:problem_count].to_i + raise t('no_problem_count') unless new_problem_count + Problem.update_count(new_problem_count) + redirect_to({:controller => 'admin/dashboard', :action => :index}, notice: t('.success_message')) + rescue => e + redirect_to({:controller => 'admin/dashboard', :action => :index}, alert: t('.failure_message', error: e.message)) + end end end diff --git a/app/views/admin/dashboard/index.html.erb b/app/views/admin/dashboard/index.html.erb index b1ae911..22e7719 100644 --- a/app/views/admin/dashboard/index.html.erb +++ b/app/views/admin/dashboard/index.html.erb @@ -1,2 +1,8 @@

<%= t('.administration') %>

-<%= link_to t('.view_translations'), admin_translations_path %> \ No newline at end of file +<%= link_to t('.view_translations'), admin_translations_path, class: 'btn btn-default' %> + +

<%= t('.update_problem_count') %>

+<%= form_tag '/admin/update_problem_count', method: :post, class: 'form-inline' do %> + <%= number_field_tag 'problem_count', @current_problem_count, min: @current_problem_count, class: 'form-control' %> + <%= submit_tag t('.update'), class: 'btn btn-warning' %> +<% end %> \ No newline at end of file diff --git a/config/locales/views/admin/de.yml b/config/locales/views/admin/de.yml index 579521d..4a41b80 100644 --- a/config/locales/views/admin/de.yml +++ b/config/locales/views/admin/de.yml @@ -3,4 +3,10 @@ de: dashboard: index: administration: "Administration" - view_translations: "Übersetzungen anschauen" \ No newline at end of file + view_translations: "Übersetzungen anschauen" + update_problem_count: "Problem-Anzahl aktualisieren" + update: "Aktualisieren" + update_problem_count: + success_message: "Problem-Anzahl wurde erfolgreich aktualisiert!" + failure_message: "Problem-Anzahl konnte nicht aktualisiert werden! Grund: %{error}" + no_problem_count: "Keine Problem-Anzahl gegeben!" \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 357d745..316d829 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,6 +6,7 @@ Rails.application.routes.draw do namespace :admin do get '', to: 'dashboard#index', as: '/' + post '/update_problem_count', to: 'dashboard#update_problem_count' resources :translations, only: [:index, :show] end diff --git a/test/controllers/admin/dashboard_controller_test.rb b/test/controllers/admin/dashboard_controller_test.rb index 30f29e0..99ea7bb 100644 --- a/test/controllers/admin/dashboard_controller_test.rb +++ b/test/controllers/admin/dashboard_controller_test.rb @@ -12,4 +12,10 @@ class Admin::DashboardControllerTest < ActionController::TestCase assert_response :success end + test "should post new problem count" do + post :update_problem_count, problem_count: 15 + assert_redirected_to controller: 'admin/dashboard', action: 'index' + assert_equal 15, Problem.count + end + end