fix formula and test it

This commit is contained in:
Ilion Beyst 2016-02-16 15:38:44 +01:00
parent b5970738b9
commit c6dc220d3d
2 changed files with 23 additions and 4 deletions

View file

@ -43,10 +43,10 @@ class User < ActiveRecord::Base
def calculate_frecency
num_orders = Rails.application.config.frecency_num_orders
last_timestamps = self.orders.order(created_at: :desc)
.limit(num_orders)
.pluck(:created_at)
self.frecency = last_timestamps.map(&:to_i).sum / num_orders
last_datetimes = self.orders.order(created_at: :desc)
.limit(num_orders)
.pluck(:created_at)
self.frecency = last_datetimes.map(&:to_time).map(&:to_i).sum / num_orders
self.save
end

View file

@ -133,4 +133,23 @@ describe User do
expect(User.publik).to eq([@user, user])
end
end
describe 'frecency' do
it 'should be recalculated on creating an order' do
expect(@user.frecency).to eq 0
create :order, user: @user
expect(@user.frecency).to_not eq 0
end
it 'should be valid' do
dates = [Date.today.to_time, Date.yesterday.to_time]
dates.each do |date|
create :order, user: @user, created_at: date
end
@user.reload
num_orders = Rails.application.config.frecency_num_orders
frecency = dates.last(num_orders).map(&:to_i).sum/num_orders
expect(@user.frecency).to eq(frecency)
end
end
end