diff --git a/app/controllers/exercise_collections_controller.rb b/app/controllers/exercise_collections_controller.rb index 3a6112ef..de425dcd 100644 --- a/app/controllers/exercise_collections_controller.rb +++ b/app/controllers/exercise_collections_controller.rb @@ -1,6 +1,5 @@ class ExerciseCollectionsController < ApplicationController include CommonBehavior - include TimeHelper before_action :set_exercise_collection, only: [:show, :edit, :update, :destroy, :statistics] @@ -37,11 +36,6 @@ class ExerciseCollectionsController < ApplicationController 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 diff --git a/app/models/exercise_collection.rb b/app/models/exercise_collection.rb index f9f09269..249e7269 100644 --- a/app/models/exercise_collection.rb +++ b/app/models/exercise_collection.rb @@ -1,8 +1,17 @@ class ExerciseCollection < ActiveRecord::Base + include TimeHelper has_and_belongs_to_many :exercises belongs_to :user, polymorphic: true + def average_working_time + working_times = {} + exercises.each do |exercise| + working_times[exercise.id] = time_to_f exercise.average_working_time + end + working_times.values.reduce(:+) / working_times.size + end + def to_s "#{I18n.t('activerecord.models.exercise_collection.one')}: #{name} (#{id})" end diff --git a/app/views/exercise_collections/statistics.html.slim b/app/views/exercise_collections/statistics.html.slim index 9ca7cc78..686c79c1 100644 --- a/app/views/exercise_collections/statistics.html.slim +++ b/app/views/exercise_collections/statistics.html.slim @@ -3,4 +3,4 @@ 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') += row(label: 'exercises.statistics.average_worktime', value: @exercise_collection.average_working_time.round(3).to_s + 's')