Scaffold exercise collection statistics

This commit is contained in:
Maximilian Grundke
2018-03-14 11:44:18 +01:00
parent be2a4d84fd
commit 119cc9ee71
5 changed files with 27 additions and 3 deletions

View File

@ -1,7 +1,8 @@
class ExerciseCollectionsController < ApplicationController
include CommonBehavior
include TimeHelper
before_action :set_exercise_collection, only: [:show, :edit, :update, :destroy]
before_action :set_exercise_collection, only: [:show, :edit, :update, :destroy, :statistics]
def index
@exercise_collections = ExerciseCollection.all.paginate(:page => params[:page])
@ -34,6 +35,14 @@ class ExerciseCollectionsController < ApplicationController
update_and_respond(object: @exercise_collection, params: exercise_collection_params)
end
def statistics
@working_times = {}
@exercise_collection.exercises.each do |exercise|
@working_times[exercise.id] = time_to_f exercise.average_working_time
end
@average = @working_times.values.reduce(:+) / @working_times.size
end
private
def set_exercise_collection

View File

@ -1,3 +1,7 @@
class ExerciseCollectionPolicy < AdminOnlyPolicy
def statistics?
admin?
end
end

View File

@ -8,7 +8,7 @@ h1 = ExerciseCollection.model_name.human(count: 2)
th = t('activerecord.attributes.exercise_collections.name')
th = t('activerecord.attributes.exercise_collections.updated_at')
th = t('activerecord.attributes.exercise_collections.exercises')
th colspan=3 = t('shared.actions')
th colspan=4 = t('shared.actions')
tbody
- @exercise_collections.each do |collection|
tr
@ -18,6 +18,7 @@ h1 = ExerciseCollection.model_name.human(count: 2)
td = collection.exercises.size
td = link_to(t('shared.show'), collection)
td = link_to(t('shared.edit'), edit_exercise_collection_path(collection))
td = link_to(t('shared.statistics'), statistics_exercise_collection_path(collection))
td = link_to(t('shared.destroy'), collection, data: {confirm: t('shared.confirm_destroy')}, method: :delete)
= render('shared/pagination', collection: @exercise_collections)

View File

@ -0,0 +1,6 @@
h1 = @exercise_collection
= row(label: 'exercise_collections.name', value: @exercise_collection.name)
= row(label: 'exercise_collections.updated_at', value: @exercise_collection.updated_at)
= row(label: 'exercise_collections.exercises', value: @exercise_collection.exercises.count)
= row(label: 'exercises.statistics.average_worktime', value: @average.round(3).to_s + 's')

View File

@ -84,7 +84,11 @@ Rails.application.routes.draw do
end
end
resources :exercise_collections
resources :exercise_collections do
member do
get :statistics
end
end
resources :proxy_exercises do
member do