forked from chicago-tool-library/circulate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitems_controller.rb
59 lines (46 loc) · 1.56 KB
/
items_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class ItemsController < ApplicationController
include Pagy::Backend
def index
item_scope = Item.listed_publicly.includes(:checked_out_exclusive_loan)
if params[:category]
@category = CategoryNode.where(id: params[:category]).first
redirect_to(items_path, error: "Category not found") && return unless @category
item_scope = @category.items
end
item_scope = item_scope.includes(:categories, :borrow_policy).with_attached_image.order(index_order)
@categories = CategoryNode.all
@pagy, @items = pagy(item_scope)
end
def show
end
private
delegate :holds, to: :current_member, prefix: true, allow_nil: true, private: true
def index_order
options = {
"name" => "items.name ASC",
"number" => "items.number ASC",
"added" => "items.created_at DESC"
}
options.fetch(params[:sort]) { options["name"] }
end
def on_hold_by_current_member?
current_item_hold_count.positive?
end
helper_method def current_item_hold_count
@current_item_hold_count ||= current_member_holds.active_hold_count_for_item(item).to_i
end
helper_method def item
@item ||= Item.listed_publicly.find(params[:id])
end
helper_method def place_hold_partial
if user_signed_in? && item.allow_multiple_holds_per_member?
"items/place_hold_on/item_that_allows_multiple_holds"
elsif user_signed_in? && on_hold_by_current_member?
"items/place_hold_on/item_already_with_a_hold"
elsif user_signed_in?
"items/place_hold_on/item"
else
"items/place_hold_on/not_logged_in"
end
end
end