Dynamic calculations in Rails

4

I hope you can help me.

I have a Rated that has reviews , I have several calculations to determine whether or not it will go through a trial step. For example one of these calculations is calcula_pontuacao_minima()

#Cálculo definido pela resolução 1
def calcula_pontuacao_minima(avaliado)
  calcula_porcentagem(avaliado.avaliacoes)>60 #onde esta soma todas as notas do avaliado e retorna a porcentagem de aproveitamento
end

My problem here is the appearance of resolutions that change the calculation , so all evaluated before this resolution must be evaluated by the old calculations and the new ones must be sorted using the calculations changed. For example a resolution changes the minimum value to 50%

#Cálculo definido pela resolução 2
def calcula_pontuacao_minima(avaliado)
  calcula_porcentagem(avaliado.avaliacoes)>50 #onde esta soma todas as notas do avaliado e retorna a porcentagem de aproveitamento
end

And if in the future they still change to instead of taking the percentage, the minimum value is compared to the total value of the evaluations

#Cálculo definido pela resolução 3
def calcula_pontuacao_minima(avaliado)
  calcula_total(avaliado.avaliacoes)>70 #onde esta soma todas as notas do avaliado onde o máximo é 120
end

For example Evaluated that had their evaluations done before the X date that defined resolution 2 , they will be evaluated by the first calculation, after that date X will be evaluated by the second calculation , and after the date Y , will be evaluated by the third calculation .

Any ideas how this can be solved? Would it be good to store database-level calculations?

    
asked by anonymous 30.05.2014 / 01:03

1 answer

2

The resolution I would make would be to use a created field (which according to your comment will be created later) or the created_at field and based on the date the resolution was created (assuming you have a resolution table or the dates in that they were), I would only use the resolution creation dates , considering, for example, a date X for resolution 1, a date Y for resolution 2, and Z for resolution 3. If you want to run the method for each evaluated, it would look like this:

def calcula_pontuacao_minima(avaliado)
  if(avaliado.created_at < Y)
    return calcula_total(avaliado.avaliacoes) > 60
  elsif (avaliado.created_at >= Y && avaliado.created_at < Z)
    return calcula_total(avaliado.avaliacoes) > 50
  else
    return calcula_total(avaliado.avaliacoes) > 70
  end
end

If you want to do it all at once (which I advise), the ideal would be to do it as follows:

def calcula_pontuacao_minima
  resultado = []
  Avaliado.where('created_at < ?', Y).all.each do |avaliado|
    a.push(calcula_total(avaliado.avaliacoes) > 60)
  end
  Avaliado.where('created_at >= ? AND created_at < ?', Y, Z).all.each do |avaliado|
    a.push(calcula_total(avaliado.avaliacoes) > 60)
  end
  Avaliado.where('created_at >= ?', Z).all.each do |avaliado|
    a.push(calcula_total(avaliado.avaliacoes) > 60)
  end
  return a
end

I do not know if this is what you want, but that's what I got to infer from your question.

    
30.05.2014 / 18:54