Class: RegisteredBookmarkable

Inherits:
Object
  • Object
show all
Defined in:
app/services/registered_bookmarkable.rb

Overview

Should only be created via the Plugin::Instance#register_bookmarkable method; this is used to let the BookmarkQuery class query and search additional bookmarks for the user bookmark list, and also to enumerate on the registered RegisteredBookmarkable types.

Post and Topic bookmarkables are registered by default.

Anything other than types registered in this way will throw an error when trying to save the Bookmark record. All things that are bookmarkable must be registered in this way.

See Plugin::Instance#register_bookmarkable for some examples on how registering bookmarkables works.

See BaseBookmarkable for documentation on what return types should be and what the arguments to the methods are.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bookmarkable_klass) ⇒ RegisteredBookmarkable

Returns a new instance of RegisteredBookmarkable.



25
26
27
# File 'app/services/registered_bookmarkable.rb', line 25

def initialize(bookmarkable_klass)
  @bookmarkable_klass = bookmarkable_klass
end

Instance Attribute Details

#bookmarkable_klassObject (readonly)

Returns the value of attribute bookmarkable_klass.



20
21
22
# File 'app/services/registered_bookmarkable.rb', line 20

def bookmarkable_klass
  @bookmarkable_klass
end

Instance Method Details

#after_create(guardian, bookmark, opts = {}) ⇒ Object



97
98
99
# File 'app/services/registered_bookmarkable.rb', line 97

def after_create(guardian, bookmark, opts = {})
  bookmarkable_klass.after_create(guardian, bookmark, opts)
end

#after_destroy(guardian, bookmark, opts = {}) ⇒ Object



101
102
103
# File 'app/services/registered_bookmarkable.rb', line 101

def after_destroy(guardian, bookmark, opts = {})
  bookmarkable_klass.after_destroy(guardian, bookmark, opts)
end

#bookmark_metadata(bookmark, user) ⇒ Object



89
90
91
# File 'app/services/registered_bookmarkable.rb', line 89

def (bookmark, user)
  bookmarkable_klass.(bookmark, user)
end

#can_see?(guardian, bookmark) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'app/services/registered_bookmarkable.rb', line 85

def can_see?(guardian, bookmark)
  bookmarkable_klass.can_see?(guardian, bookmark)
end

#can_send_reminder?(bookmark) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'app/services/registered_bookmarkable.rb', line 77

def can_send_reminder?(bookmark)
  bookmarkable_klass.reminder_conditions(bookmark)
end

#cleanup_deletedObject



105
106
107
# File 'app/services/registered_bookmarkable.rb', line 105

def cleanup_deleted
  bookmarkable_klass.cleanup_deleted
end

#perform_list_query(user, guardian) ⇒ Object



29
30
31
# File 'app/services/registered_bookmarkable.rb', line 29

def perform_list_query(user, guardian)
  bookmarkable_klass.list_query(user, guardian)
end

#perform_preload(bookmarks, guardian) ⇒ void

This method returns an undefined value.

When displaying the bookmarks in a list for a user there is often additional information drawn from other tables joined to the bookmarkable that must be displayed. We preload these additional associations here on top of the array of bookmarks which has already been filtered, offset by page, ordered, and limited. The preload_associations array should be in the same format as used for .includes() e.g.

{ topic: [:topic_users, :tags

}, :user]

For more advanced preloading, bookmarkable classes can implement ‘perform_custom_preload!`

Parameters:

  • bookmarks (Array)

    The array of bookmarks after initial listing and filtering, note this is array not an ActiveRecord::Relation.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/services/registered_bookmarkable.rb', line 63

def perform_preload(bookmarks, guardian)
  bookmarks_of_type = Bookmark.select_type(bookmarks, bookmarkable_klass.model.to_s)
  return if bookmarks_of_type.empty?

  if bookmarkable_klass.has_preloads?
    ActiveRecord::Associations::Preloader.new(
      records: bookmarks_of_type,
      associations: [bookmarkable: bookmarkable_klass.preload_associations],
    ).call
  end

  bookmarkable_klass.perform_custom_preload!(bookmarks_of_type, guardian)
end

#perform_search_query(bookmarks, query, ts_query) ⇒ Object

The block here warrants explanation – when the search_query is called, we call the provided block with the bookmark relation with additional joins as well as the where_sql string, and then also add the additional OR bookmarks.name filter. This is so every bookmarkable is filtered by its own customized columns _as well as_ the bookmark name, because the bookmark name must always be used in the search.

See BaseBookmarkable#search_query for argument docs.



42
43
44
45
46
# File 'app/services/registered_bookmarkable.rb', line 42

def perform_search_query(bookmarks, query, ts_query)
  bookmarkable_klass.search_query(bookmarks, query, ts_query) do |bookmarks_joined, where_sql|
    bookmarks_joined.where("#{where_sql} OR bookmarks.name ILIKE :q", q: query)
  end
end

#send_reminder_notification(bookmark) ⇒ Object



81
82
83
# File 'app/services/registered_bookmarkable.rb', line 81

def send_reminder_notification(bookmark)
  bookmarkable_klass.reminder_handler(bookmark)
end

#validate_before_create(guardian, bookmarkable) ⇒ Object



93
94
95
# File 'app/services/registered_bookmarkable.rb', line 93

def validate_before_create(guardian, bookmarkable)
  bookmarkable_klass.validate_before_create(guardian, bookmarkable)
end