Move method to model
This commit is contained in:
@ -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
|
||||
|
@ -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
|
||||
|
@ -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')
|
||||
|
Reference in New Issue
Block a user