Bookmarks

Users can bookmark resources for quick access. Bookmarks use a polymorphic association so any model can be bookmarked.

Model (app/models/bookmark.rb)

  • user_id – the bookmarking user
  • bookmarkable_type, bookmarkable_id – polymorphic reference to the bookmarked resource
  • related_bookmarkable_type, related_bookmarkable_id – optional related resource
  • target_language, fluent_language – auto-set from the bookmarked resource
  • Uniqueness constraint on (user_id, bookmarkable_type, bookmarkable_id)

Bookmarkable Concern

Models include the Bookmarkable concern to gain bookmark support:

  • Included in: GrammarNote, Document, Deck
  • Methods: bookmarked_by?(user), bookmark_for(user), toggle_bookmark_for(user)
  • Association: has_many :bookmarks and has_many :bookmarkers (users through bookmarks)

Controller (app/controllers/users/bookmarks_controller.rb)

  • Actions: create, destroy
  • Responds to HTML (redirect) and Turbo Stream (replaces bookmark icon partial inline)
  • Params: bookmarkable_type, bookmarkable_id

UI

The shared partial app/views/shared/_bookmark_button.html.erb renders a toggle button. It uses data-turbo-confirm for the unbookmark action and Turbo Streams for inline updates without page reload.


This site uses Just the Docs, a documentation theme for Jekyll.