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



45
46
47
48
49
50
# File 'lib/card/bookmark.rb', line 45

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



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

def bookmark_hash
  ok? ? yield : {}
end

.clearObject



40
41
42
43
# File 'lib/card/bookmark.rb', line 40

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



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

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

.current_list_cardObject



18
19
20
# File 'lib/card/bookmark.rb', line 18

def current_list_card
  Auth.current.bookmarks_card if ok?
end

.id_restriction(bookmarked = true, &block) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/card/bookmark.rb', line 32

def id_restriction bookmarked=true, &block
  if current_ids.empty?
    yield [] if bookmarked
  else
    restrict_to_current_ids bookmarked, &block
  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