implement frecency
This commit is contained in:
parent
0b63edd40e
commit
727372c7ef
6 changed files with 24 additions and 2 deletions
|
@ -26,7 +26,7 @@ class OrdersController < ApplicationController
|
|||
end
|
||||
|
||||
def overview
|
||||
@users = User.members.publik.order(:name)
|
||||
@users = User.members.publik.order(:frecency, :desc)
|
||||
@last = Order.order(:created_at).reverse_order.includes(:user).limit(10).map(&:user)
|
||||
end
|
||||
|
||||
|
|
|
@ -21,6 +21,9 @@ class Order < ActiveRecord::Base
|
|||
before_save { |o| o.order_items = o.order_items.reject{ |oi| oi.count == 0 } }
|
||||
after_create :create_api_job, unless: -> { user.guest? }
|
||||
|
||||
after_create :update_user_frecency
|
||||
after_destroy :update_user_frecency
|
||||
|
||||
validates :user, presence: true
|
||||
validates_associated :order_items
|
||||
validate :product_presence
|
||||
|
@ -66,4 +69,8 @@ class Order < ActiveRecord::Base
|
|||
def product_presence
|
||||
errors.add(:base, "You have to order at least one product.") if order_items.map(&:count).sum.zero?
|
||||
end
|
||||
|
||||
def update_user_frecency
|
||||
self.user.calculate_frecency
|
||||
end
|
||||
end
|
||||
|
|
|
@ -41,6 +41,14 @@ class User < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
def balance
|
||||
@balance || begin
|
||||
headers = {
|
||||
|
|
|
@ -22,5 +22,6 @@ module Tab002
|
|||
config.active_record.raise_in_transactional_callbacks = true
|
||||
config.active_job.queue_adapter = :delayed_job
|
||||
config.call_api_after = 5.minutes
|
||||
config.frecency_num_orders = 10
|
||||
end
|
||||
end
|
||||
|
|
5
db/migrate/20160216133104_add_frecency_to_users.rb
Normal file
5
db/migrate/20160216133104_add_frecency_to_users.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class AddFrecencyToUsers < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :users, :frecency, :integer, default: 0, null: false
|
||||
end
|
||||
end
|
|
@ -11,7 +11,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20160202133903) do
|
||||
ActiveRecord::Schema.define(version: 20160216133104) do
|
||||
|
||||
create_table "barcodes", force: :cascade do |t|
|
||||
t.integer "product_id"
|
||||
|
@ -85,6 +85,7 @@ ActiveRecord::Schema.define(version: 20160202133903) do
|
|||
t.boolean "koelkast", default: false
|
||||
t.string "name"
|
||||
t.boolean "private", default: false
|
||||
t.integer "frecency", default: 0, null: false
|
||||
end
|
||||
|
||||
add_index "users", ["koelkast"], name: "index_users_on_koelkast"
|
||||
|
|
Loading…
Reference in a new issue