Merge
This commit is contained in:
commit
95c209001d
|
@ -26,11 +26,13 @@
|
||||||
}
|
}
|
||||||
button.product {
|
button.product {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
|
height: 100px;
|
||||||
p {
|
p {
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
}
|
}
|
||||||
img {
|
img {
|
||||||
|
max-height: 70%;
|
||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
margin-left: auto;
|
margin-left: auto;
|
||||||
margin-right: auto;
|
margin-right: auto;
|
||||||
|
|
|
@ -26,7 +26,7 @@ class OrdersController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def overview
|
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)
|
@last = Order.order(:created_at).reverse_order.includes(:user).limit(10).map(&:user)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,9 @@ class Order < ActiveRecord::Base
|
||||||
before_save { |o| o.order_items = o.order_items.reject{ |oi| oi.count == 0 } }
|
before_save { |o| o.order_items = o.order_items.reject{ |oi| oi.count == 0 } }
|
||||||
after_create :create_api_job, unless: -> { user.guest? }
|
after_create :create_api_job, unless: -> { user.guest? }
|
||||||
|
|
||||||
|
after_create :update_user_frecency
|
||||||
|
after_destroy :update_user_frecency
|
||||||
|
|
||||||
validates :user, presence: true
|
validates :user, presence: true
|
||||||
validates_associated :order_items
|
validates_associated :order_items
|
||||||
validate :product_presence
|
validate :product_presence
|
||||||
|
@ -66,4 +69,8 @@ class Order < ActiveRecord::Base
|
||||||
def product_presence
|
def product_presence
|
||||||
errors.add(:base, "You have to order at least one product.") if order_items.map(&:count).sum.zero?
|
errors.add(:base, "You have to order at least one product.") if order_items.map(&:count).sum.zero?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update_user_frecency
|
||||||
|
self.user.calculate_frecency
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -41,6 +41,15 @@ class User < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def calculate_frecency
|
||||||
|
num_orders = Rails.application.config.frecency_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
|
||||||
|
|
||||||
def balance
|
def balance
|
||||||
@balance || begin
|
@balance || begin
|
||||||
headers = {
|
headers = {
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
.col-md-2{ data: { name: product.name } }
|
.col-md-2{ data: { name: product.name } }
|
||||||
%button.btn.btn-default.product{ data: { product: product.id, dismiss: :modal } }
|
%button.btn.btn-default.product{ data: { product: product.id, dismiss: :modal } }
|
||||||
%p= product.name
|
%p= product.name
|
||||||
= image_tag product.avatar(:dagschotel), class: "center"
|
= image_tag product.avatar(:dagschotel), class: "center img-responsive"
|
||||||
.modal-footer
|
.modal-footer
|
||||||
%button.btn.btn-default{ data: { dismiss: :modal } }
|
%button.btn.btn-default{ data: { dismiss: :modal } }
|
||||||
Close
|
Close
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
= form_tag nil, id: "from_barcode_form", data: { url: URI.join(root_url, "barcodes").to_s } do
|
= form_tag nil, id: "from_barcode_form", data: { url: URI.join(root_url, "barcodes").to_s } do
|
||||||
%input.center-block{ type: :number, name: :id, autofocus: true }
|
%input.center-block{ type: :number, name: :id, autofocus: true }
|
||||||
= "- OR -"
|
= "- OR -"
|
||||||
%button.btn.btn-default.center-block{ data: { toggle: :modal, target: "#products_modal" } }
|
%p
|
||||||
Select Product Without Barcode
|
%button.btn.btn-default.center-block{ data: { toggle: :modal, target: "#products_modal" } }
|
||||||
%br
|
Select Product Without Barcode
|
||||||
.col-md-4.col-md-offset-1
|
.col-md-4.col-md-offset-1
|
||||||
= form_for [@user, @order] do |f|
|
= form_for [@user, @order] do |f|
|
||||||
= render 'errors', object: @order
|
= render 'errors', object: @order
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
.row
|
.row
|
||||||
.col-md-7
|
.col-md-7
|
||||||
%h4.pull-right Select a product to link the barcode to an existing product ...
|
%h4.center Select a product to link the barcode to an existing product ...
|
||||||
#product_buttons.row
|
%p
|
||||||
- Product.all.each do |product|
|
#product_buttons.row
|
||||||
.col-md-3
|
- Product.all.each do |product|
|
||||||
= button_to product_barcodes_path(product), class: "btn btn-default product", data: { product: product.id, dismiss: :modal }, params: { "barcode[code]" => params[:barcode] } do
|
%tr
|
||||||
%p= product.name
|
.col-md-3
|
||||||
= image_tag product.avatar(:dagschotel), class: "center"
|
= button_to product_barcodes_path(product), class: "btn btn-default product", data: { product: product.id, dismiss: :modal }, params: { "barcode[code]" => params[:barcode] } do
|
||||||
|
%p= product.name
|
||||||
|
= image_tag product.avatar(:dagschotel), class: "center img-responsive"
|
||||||
.col-md-5
|
.col-md-5
|
||||||
%h4 or create a new one
|
%h4 or create a new one
|
||||||
= render 'products/form'
|
= render 'products/form'
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
= content_for :title, "Login"
|
= content_for :title, "Login"
|
||||||
If this is the first time you log in, an account will be created for you.
|
%p
|
||||||
|
If this is the first time you log in, an account will be created for you.
|
||||||
%div
|
%div
|
||||||
= link_to "Sign in with Zeus WPI account.", omniauth_authorize_path("user", "zeuswpi"), class: "btn btn-large btn-primary"
|
= link_to "Sign in with Zeus WPI account.", omniauth_authorize_path("user", "zeuswpi"), class: "btn btn-large btn-primary"
|
||||||
|
|
|
@ -22,5 +22,6 @@ module Tab002
|
||||||
config.active_record.raise_in_transactional_callbacks = true
|
config.active_record.raise_in_transactional_callbacks = true
|
||||||
config.active_job.queue_adapter = :delayed_job
|
config.active_job.queue_adapter = :delayed_job
|
||||||
config.call_api_after = 5.minutes
|
config.call_api_after = 5.minutes
|
||||||
|
config.frecency_num_orders = 10
|
||||||
end
|
end
|
||||||
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.
|
# 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|
|
create_table "barcodes", force: :cascade do |t|
|
||||||
t.integer "product_id"
|
t.integer "product_id"
|
||||||
|
@ -85,6 +85,7 @@ ActiveRecord::Schema.define(version: 20160202133903) do
|
||||||
t.boolean "koelkast", default: false
|
t.boolean "koelkast", default: false
|
||||||
t.string "name"
|
t.string "name"
|
||||||
t.boolean "private", default: false
|
t.boolean "private", default: false
|
||||||
|
t.integer "frecency", default: 0, null: false
|
||||||
end
|
end
|
||||||
|
|
||||||
add_index "users", ["koelkast"], name: "index_users_on_koelkast"
|
add_index "users", ["koelkast"], name: "index_users_on_koelkast"
|
||||||
|
|
|
@ -133,4 +133,23 @@ describe User do
|
||||||
expect(User.publik).to eq([@user, user])
|
expect(User.publik).to eq([@user, user])
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue