Module: Card::Bookmark

Defined in:
lib/card/bookmark.rb

Constant Summary collapse

CURRENT_IDS_KEY =
"BM-current_ids".freeze
CURRENT_BOOKMARKS_KEY =
"BM-current_bookmarks".freeze

Class Method Summary collapse

Class Method Details

.add_sort_join(rel, join_field) ⇒ Object



58
59
60
61
62
63
# File 'lib/card/bookmark.rb', line 58

def add_sort_join rel, join_field
  rel.joins(
    "LEFT JOIN counts cts " \
    "ON #{join_field} = cts.left_id AND cts.right_id = #{Card::BookmarkersID}"
  )
end

.bookmark_hashObject



37
38
39
# File 'lib/card/bookmark.rb', line 37

def bookmark_hash
  ok? ? yield : {}
end

.bookmark_hash_for_list_card(list_card) ⇒ Hash

Returns . keys are type_ids, values are list of bookmarked card ids.

Parameters:

  • list_card (Card)

    +bookmarks card for (eg) user

Returns:

  • (Hash)

    . keys are type_ids, values are list of bookmarked card ids



20
21
22
23
24
25
# File 'lib/card/bookmark.rb', line 20

def bookmark_hash_for_list_card list_card
  list_card.item_cards.each_with_object({}) do |item, hash|
    hash[item.type_id] ||= []
    hash[item.type_id] << item.id
  end
end

.cacheObject



49
50
51
# File 'lib/card/bookmark.rb', line 49

def cache
  Card.cache.soft
end

.clearObject



53
54
55
56
# File 'lib/card/bookmark.rb', line 53

def clear
  cache.delete CURRENT_IDS_KEY
  cache.delete CURRENT_BOOKMARKS_KEY
end

.current_bookmarksObject

Returns Hash key is type_id, value is list of ids.

Returns:

  • Hash key is type_id, value is list of ids



12
13
14
15
16
# File 'lib/card/bookmark.rb', line 12

def current_bookmarks
  cache.fetch CURRENT_BOOKMARKS_KEY do
    ok? ? bookmark_hash_for_list_card(current_list_card) : {}
  end
end

.current_idsObject



31
32
33
34
35
# File 'lib/card/bookmark.rb', line 31

def current_ids
  cache.fetch CURRENT_IDS_KEY do # MOVE to session?
    ok? ? current_list_card.item_ids : []
  end
end

.current_list_cardObject



27
28
29
# File 'lib/card/bookmark.rb', line 27

def current_list_card
  Auth.current.bookmarks_card if ok?
end

.id_restriction(bookmarked = true) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/card/bookmark.rb', line 41

def id_restriction bookmarked=true
  if current_ids.empty?
    yield [] if bookmarked
  else
    yield([(bookmarked ? "in" : "not in")] + current_ids)
  end
end

.ok?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/card/bookmark.rb', line 7

def ok?
  Auth.current.respond_to?(:bookmarks_card)
end