Skip to content

Commit dc3e3a8

Browse files
authored
[#137627] add links to 10 most recently purchased products to home page (#1429)
Add a list of the user's 10 most recently purchased products to the user's home page.
1 parent 6195f3c commit dc3e3a8

File tree

13 files changed

+128
-90
lines changed

13 files changed

+128
-90
lines changed

app/controllers/application_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def manageable_facilities
8888
when current_facility.blank?
8989
session_user.manageable_facilities
9090
when current_facility.cross_facility?
91-
Facility.sorted
91+
Facility.alphabetized
9292
else
9393
Facility.where(id: current_facility.id)
9494
end

app/controllers/facilities_controller.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class FacilitiesController < ApplicationController
88
before_action :check_acting_as, except: [:index, :show]
99
before_action :load_order_details, only: [:confirm_transactions, :move_transactions, :reassign_chart_strings]
1010
before_action :set_admin_billing_tab, only: [:confirm_transactions, :disputed_orders, :movable_transactions, :transactions]
11-
before_action :set_recently_used_facilities, only: [:index]
1211
before_action :store_fullpath_in_session, only: [:index, :show]
1312

1413
load_and_authorize_resource find_by: :url_name
@@ -23,11 +22,6 @@ class FacilitiesController < ApplicationController
2322
action_name.in?(%w(disputed_orders movable_transactions transactions)) ? "two_column_head" : "two_column"
2423
}
2524

26-
def set_recently_used_facilities
27-
@recently_used_facilities =
28-
acting_user.present? && acting_user.recently_used_facilities.sorted
29-
end
30-
3125
cattr_accessor(:facility_homepage_redirector) { DefaultFacilityHomepageRedirector }
3226

3327
# GET /facilities/:facility_url/dashboard
@@ -37,8 +31,10 @@ def dashboard
3731

3832
# GET /facilities
3933
def index
40-
@facilities = Facility.active.sorted
34+
@facilities = Facility.active.alphabetized
35+
@recently_used_facilities = MostRecentlyUsedSearcher.new(acting_user).recently_used_facilities.alphabetized
4136
@active_tab = "home"
37+
@recent_products = MostRecentlyUsedSearcher.new(acting_user).recently_used_products.includes(:facility).alphabetized
4238
render layout: "application"
4339
end
4440

@@ -57,7 +53,7 @@ def list
5753
# show list of operable facilities for current user, and admins manage all facilities
5854
@active_tab = "manage_facilites"
5955
if session_user.administrator?
60-
@facilities = Facility.sorted
56+
@facilities = Facility.alphabetized
6157
flash.now[:notice] = "No facilities have been added" if @facilities.empty?
6258
else
6359
@facilities = operable_facilities

app/helpers/products_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def options_for_relay
2828
def public_calendar_link(product)
2929
if product.respond_to? :reservations
3030
opts = public_calendar_options(product)
31-
link_to "", facility_instrument_public_schedule_path(current_facility, product), opts
31+
link_to "", facility_instrument_public_schedule_path(product.facility, product), opts
3232
end
3333
end
3434

@@ -43,7 +43,7 @@ def product_url_hint(product)
4343
private
4444

4545
def public_calendar_options(product)
46-
if current_facility.show_instrument_availability?
46+
if current_facility&.show_instrument_availability? || !current_facility && product.facility.show_instrument_availability?
4747
public_calendar_availability_options(product)
4848
else
4949
{ class: ["fa fa-calendar fa-2x"],

app/models/concerns/users/roles.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ module Roles
2525
# Returns relation of facilities for which this user is staff, a director, or an admin
2626
def operable_facilities
2727
if administrator?
28-
Facility.sorted
28+
Facility.alphabetized
2929
else
30-
facilities.sorted.where(user_roles: { role: UserRole.facility_roles })
30+
facilities.alphabetized.where(user_roles: { role: UserRole.facility_roles })
3131
end
3232
end
3333

3434
# Returns relation of facilities for which this user is a director or admin
3535
def manageable_facilities
3636
if administrator? || billing_administrator?
37-
Facility.sorted
37+
Facility.alphabetized
3838
else
39-
facilities.sorted.where(user_roles: { role: UserRole.facility_management_roles })
39+
facilities.alphabetized.where(user_roles: { role: UserRole.facility_management_roles })
4040
end
4141
end
4242

app/models/facility.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def accounts
4343
delegate :in_dispute, to: :order_details, prefix: true
4444

4545
scope :active, -> { where(is_active: true) }
46-
scope :sorted, -> { order(:name) }
46+
scope :alphabetized, -> { order(:name) }
4747

4848
cattr_accessor :facility_account_validators { [] }
4949

app/models/product.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Product < ActiveRecord::Base
5959

6060
scope :active, -> { where(is_archived: false, is_hidden: false) }
6161
scope :active_plus_hidden, -> { where(is_archived: false) } # TODO: phase out in favor of the .not_archived scope
62-
scope :alphabetized, -> { order("lower(name)") }
62+
scope :alphabetized, -> { order("lower(products.name)") }
6363
scope :archived, -> { where(is_archived: true) }
6464
scope :not_archived, -> { where(is_archived: false) }
6565
scope :mergeable_into_order, -> { not_archived.where(type: mergeable_types) }

app/models/user.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,6 @@ def deactivated_string
175175
end
176176
end
177177

178-
def recently_used_facilities(limit = 5)
179-
@recently_used_facilities ||= Hash.new do |hash, key|
180-
facility_ids = orders.purchased.order("MAX(ordered_at) DESC").limit(limit).group(:facility_id).pluck(:facility_id)
181-
hash[key] = Facility.where(id: facility_ids).sorted
182-
end
183-
@recently_used_facilities[limit]
184-
end
185-
186178
# Devise uses this method for determining if a user is allowed to log in. It
187179
# also gets called on each request, so if a user gets deactivated, they'll be
188180
# kicked out of their session.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class MostRecentlyUsedSearcher
2+
3+
attr_reader :user
4+
5+
def initialize(user)
6+
@user = user
7+
end
8+
9+
def recently_used_facilities(limit = 5)
10+
return [] unless user
11+
12+
facility_ids = user.orders.purchased.order("MAX(ordered_at) DESC").limit(limit).group(:facility_id).pluck(:facility_id)
13+
Facility.where(id: facility_ids)
14+
end
15+
16+
def recently_used_products(limit = 10)
17+
return [] unless user
18+
19+
product_ids = user.orders.joins(order_details: :product).merge(Product.active.in_active_facility).purchased.order("MAX(orders.ordered_at) DESC").limit(limit).group(:product_id).pluck(:product_id)
20+
Product.where(id: product_ids)
21+
end
22+
23+
end

app/views/facilities/_product_list.html.haml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
- if products.any?
22
.product_list{ class: products.first.class.model_name.to_s.pluralize.downcase }
3-
%h3= product_list_title products, local_assigns[:title_extra]
4-
- if products.first.class.model_name == "TimedService"
5-
%p= t("facilities.show.timed_services_note")
3+
4+
- if current_facility.present?
5+
%h3= product_list_title products, local_assigns[:title_extra]
6+
7+
- if products.first.class.model_name == "TimedService"
8+
%p= t("facilities.show.timed_services_note")
69
%ul
710
- products.sort.each do |product|
811
%li{ class: product.class.model_name.to_s.downcase }
@@ -12,7 +15,7 @@
1215
- if session_user.can_override_restrictions?(product) || product.can_be_used_by?(acting_user)
1316
= builder.text_field :quantity, value: 0, class: "product_quantity", index: nil
1417
= builder.hidden_field :product_id, value: product.id, index: nil
15-
= link_to product.name + (product.is_hidden? ? ' (hidden)' : ''), facility_product_path(current_facility, product)
18+
= link_to product.name + (product.is_hidden? ? ' (hidden)' : ''), facility_product_path(current_facility || product.facility, product)
1619
- if acting_user.present? && !product.can_be_used_by?(acting_user)
1720
%i.fa.fa-lock
1821
= " (#{product.class.human_attribute_name(:requires_approval_show)})"

app/views/facilities/index.html.haml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@
1313
- @recently_used_facilities.each do |facility|
1414
%h4.header-tight= link_to facility, facility_path(facility)
1515
%hr
16-
%h4= text(".all")
1716

17+
- if @recent_products.any?
18+
19+
%h4= text(".recently_purchased")
20+
= render 'product_list', products: @recent_products
21+
%hr
22+
23+
%h4= text(".all")
1824
- @facilities.each do |facility|
1925
%h4.header-tight= link_to facility, facility_path(facility)
2026
%p= facility.short_description
2127

2228
- else
2329
.alert.alert-info= t(".empty", items: Facility.model_name.human(count: 2))
30+

0 commit comments

Comments
 (0)