mirror of
https://github.com/projekteuler/projekteuler.git
synced 2026-01-27 10:38:50 +01:00
Merge pull request #39 from PFischbeck/problem-count
Add Problem count updating
This commit is contained in:
@@ -1,4 +1,16 @@
|
|||||||
class Admin::DashboardController < AdminController
|
class Admin::DashboardController < AdminController
|
||||||
def index
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -15,4 +15,13 @@ class Problem < ActiveRecord::Base
|
|||||||
def original_url
|
def original_url
|
||||||
"https://projecteuler.net/problem=#{self.id}"
|
"https://projecteuler.net/problem=#{self.id}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.update_count(new_count)
|
||||||
|
old_count = Problem.count
|
||||||
|
raise ArgumentError, "new count has to be larger than old count" if new_count < old_count
|
||||||
|
|
||||||
|
new_count.times do |i|
|
||||||
|
Problem.where(id: i + 1).first_or_create!
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,2 +1,8 @@
|
|||||||
<h1><%= t('.administration') %></h1>
|
<h1><%= t('.administration') %></h1>
|
||||||
<%= link_to t('.view_translations'), admin_translations_path %>
|
<%= link_to t('.view_translations'), admin_translations_path, class: 'btn btn-default' %>
|
||||||
|
|
||||||
|
<h1><%= t('.update_problem_count') %></h1>
|
||||||
|
<%= 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 %>
|
||||||
@@ -4,3 +4,9 @@ de:
|
|||||||
index:
|
index:
|
||||||
administration: "Administration"
|
administration: "Administration"
|
||||||
view_translations: "Übersetzungen anschauen"
|
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!"
|
||||||
@@ -6,6 +6,7 @@ Rails.application.routes.draw do
|
|||||||
|
|
||||||
namespace :admin do
|
namespace :admin do
|
||||||
get '', to: 'dashboard#index', as: '/'
|
get '', to: 'dashboard#index', as: '/'
|
||||||
|
post '/update_problem_count', to: 'dashboard#update_problem_count'
|
||||||
resources :translations, only: [:index, :show]
|
resources :translations, only: [:index, :show]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -12,4 +12,16 @@ class Admin::DashboardControllerTest < ActionController::TestCase
|
|||||||
assert_response :success
|
assert_response :success
|
||||||
end
|
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
|
||||||
|
|
||||||
|
test "should fail incorrect problem count" do
|
||||||
|
post :update_problem_count, problem_count: 2
|
||||||
|
assert_redirected_to controller: 'admin/dashboard', action: 'index'
|
||||||
|
assert_equal 3, Problem.count
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -17,4 +17,21 @@ class ProblemTest < ActiveSupport::TestCase
|
|||||||
test "should have correct original url" do
|
test "should have correct original url" do
|
||||||
assert_equal "https://projecteuler.net/problem=1", problems(:one).original_url
|
assert_equal "https://projecteuler.net/problem=1", problems(:one).original_url
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "should allow for problem count updating" do
|
||||||
|
Problem.update_count(10)
|
||||||
|
assert_not Problem.where(id: 0).exists?
|
||||||
|
(1..10).each do |i|
|
||||||
|
assert Problem.where(id: i).exists?
|
||||||
|
end
|
||||||
|
assert_equal "First title", Problem.find(1).title
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should not allow decreasing problem count update" do
|
||||||
|
|
||||||
|
assert_raises ArgumentError do
|
||||||
|
Problem.update_count(1)
|
||||||
|
end
|
||||||
|
assert_equal 3, Problem.count()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user