Move method to model

This commit is contained in:
Maximilian Grundke
2018-03-19 15:01:50 +01:00
parent 667d1cb38b
commit f1f1594e5b
3 changed files with 10 additions and 7 deletions

View File

@ -1,6 +1,5 @@
class ExerciseCollectionsController < ApplicationController class ExerciseCollectionsController < ApplicationController
include CommonBehavior include CommonBehavior
include TimeHelper
before_action :set_exercise_collection, only: [:show, :edit, :update, :destroy, :statistics] before_action :set_exercise_collection, only: [:show, :edit, :update, :destroy, :statistics]
@ -37,11 +36,6 @@ class ExerciseCollectionsController < ApplicationController
end end
def statistics 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 end
private private

View File

@ -1,8 +1,17 @@
class ExerciseCollection < ActiveRecord::Base class ExerciseCollection < ActiveRecord::Base
include TimeHelper
has_and_belongs_to_many :exercises has_and_belongs_to_many :exercises
belongs_to :user, polymorphic: true 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 def to_s
"#{I18n.t('activerecord.models.exercise_collection.one')}: #{name} (#{id})" "#{I18n.t('activerecord.models.exercise_collection.one')}: #{name} (#{id})"
end end

View File

@ -3,4 +3,4 @@ h1 = @exercise_collection
= row(label: 'exercise_collections.name', value: @exercise_collection.name) = row(label: 'exercise_collections.name', value: @exercise_collection.name)
= row(label: 'exercise_collections.updated_at', value: @exercise_collection.updated_at) = row(label: 'exercise_collections.updated_at', value: @exercise_collection.updated_at)
= row(label: 'exercise_collections.exercises', value: @exercise_collection.exercises.count) = 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')