-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Backend: Basic plumbing for bookmark feature #4308
Draft
ChargrilledChook
wants to merge
2
commits into
main
Choose a base branch
from
feature/lesson-bookmarks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<div class="flex items-center justify-center" id="bookmark-button"> | ||
<%= button_to create_or_destroy_path, | ||
form_class: 'w-full h-full', | ||
method: bookmarked? ? :delete : :post, | ||
data: { test_id: 'bookmark-button' }, | ||
params: { lesson_id: lesson.id }, | ||
class: 'button button--secondary h-[54px] sm:h-full w-full hover:bg-teal-700' do %> | ||
<span class="flex items-center"><i class='<%= icon %>'></i></span> | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Bookmarks::ButtonComponent < ApplicationComponent | ||
def initialize(lesson:, bookmark:, current_user: nil) | ||
@lesson = lesson | ||
@current_user = current_user | ||
@bookmark = bookmark | ||
end | ||
|
||
private | ||
|
||
attr_reader :lesson, :current_user, :bookmark | ||
|
||
def render? | ||
return false unless current_user | ||
|
||
Feature.enabled?(:bookmarks, current_user) | ||
end | ||
|
||
def bookmarked? | ||
bookmark.present? | ||
end | ||
|
||
def icon | ||
bookmarked? ? 'fa-solid fa-bookmark' : 'fa-regular fa-bookmark' | ||
end | ||
|
||
def create_or_destroy_path | ||
bookmarked? ? users_bookmark_path(bookmark) : users_bookmarks_path | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,24 @@ | ||
class LessonsController < ApplicationController | ||
before_action :set_cache_control_header_to_no_store | ||
before_action :set_lesson | ||
before_action :set_bookmark | ||
|
||
def show | ||
@lesson = Lesson.find(params[:id]) | ||
|
||
if user_signed_in? | ||
Courses::MarkCompletedLessons.call(user: current_user, lessons: Array(@lesson)) | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_lesson | ||
@lesson = Lesson.find(params[:id]) | ||
end | ||
|
||
def set_bookmark | ||
return unless current_user | ||
return unless Feature.enabled?(:bookmarks, current_user) | ||
|
||
@bookmark = current_user.bookmarks.find_by(lesson_id: @lesson.id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
class Users::BookmarksController < ApplicationController | ||
before_action :authenticate_user! | ||
before_action :set_lesson, only: %i[create destroy] | ||
|
||
def index | ||
@bookmarks = current_user.bookmarks | ||
@lessons = current_user.bookmarked_lessons | ||
end | ||
|
||
def new | ||
@bookmark = Bookmark.new | ||
end | ||
|
||
def create | ||
respond_to do |format| | ||
@bookmark = current_user.bookmarks.build(lesson: @lesson) | ||
|
||
if @bookmark.save | ||
format.turbo_stream { flash.now[:notice] = create_or_destroy_bookmark_helper } | ||
format.html { redirect_to({ action: 'index' }, notice: create_or_destroy_bookmark_helper) } | ||
else | ||
format.html { redirect_back status: :unprocessable_entity, alert: 'Unable to create bookmark' } | ||
end | ||
end | ||
end | ||
|
||
def destroy | ||
respond_to do |format| | ||
@bookmark = Bookmark.find(params[:id]) | ||
@bookmark.destroy | ||
|
||
format.turbo_stream { flash.now[:notice] = create_or_destroy_bookmark_helper(destroy: true) } | ||
format.html { redirect_to({ action: 'index' }, notice: create_or_destroy_bookmark_helper(destroy: true)) } | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_lesson | ||
@lesson = Lesson.find(params[:lesson_id]) | ||
end | ||
|
||
# HACK: Temp method for easier prototyping | ||
def create_or_destroy_bookmark_helper(destroy: false) | ||
<<~FLASH.html_safe # rubocop:disable Rails/OutputSafety | ||
Bookmark #{destroy ? 'removed' : 'created'}! | ||
#{helpers.link_to 'Click to see saved bookmarks', users_bookmarks_path} | ||
FLASH | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Bookmark < ApplicationRecord | ||
belongs_to :user | ||
belongs_to :lesson | ||
|
||
validates :lesson, uniqueness: { scope: :user, message: 'user has already bookmarked this lesson' } | ||
|
||
delegate :title, to: :lesson | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<%= turbo_stream.replace 'bookmark-button' do %> | ||
<%= render Bookmarks::ButtonComponent.new(lesson: @lesson, current_user:, bookmark: @bookmark) %> | ||
<% end %> | ||
|
||
<%= turbo_stream.update 'flash-messages', partial: 'shared/flash' %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<%= turbo_stream.replace 'bookmark-button' do %> | ||
<%= render Bookmarks::ButtonComponent.new(lesson: @lesson, current_user:, bookmark: nil) %> | ||
<% end %> | ||
|
||
<%= turbo_stream.update 'flash-messages', partial: 'shared/flash' %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
<div class="page-container"> | ||
<div class="max-w-4xl mx-auto"> | ||
<h1 class="page-heading-title">My Bookmarked Lessons</h1> | ||
<section class="mb-24" data-test-id="bookmarks"> | ||
<div class="text-center mt-0 mb-6 mx-auto"> | ||
<% @lessons.each do|lesson| %> | ||
<div> | ||
<icon class="fa-solid fa-bookmark"></icon> | ||
<%= link_to lesson.title, lesson %> | ||
</div> | ||
<% end %> | ||
</div> | ||
</section> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateBookmarks < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :bookmarks do |t| | ||
t.belongs_to :lesson, null: false, foreign_key: true | ||
t.belongs_to :user, null: false, foreign_key: true | ||
|
||
t.timestamps | ||
|
||
t.index %i[user_id lesson_id], unique: true | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FactoryBot.define do | ||
factory :bookmark do | ||
user | ||
lesson | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Bookmark do | ||
subject(:bookmark) { create(:bookmark, lesson:) } | ||
|
||
let!(:lesson) { create(:lesson, section: create(:section, course:), course:) } | ||
let!(:course) { create(:course) } | ||
|
||
describe 'associations' do | ||
it { is_expected.to belong_to(:user) } | ||
it { is_expected.to belong_to(:lesson) } | ||
end | ||
|
||
describe 'validations' do | ||
it 'validates uniqueness of lesson scoped to user' do | ||
pending 'type error, course not getting attached to lesson' | ||
expect(bookmark) | ||
.to validate_uniqueness_of(:lesson) | ||
.scoped_to(:user) | ||
.with_message('user has already bookmarked this lesson') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would have preferred to keep the logic in the VC class with the rest of it, but putting it in a method didn't work for some reason 🤷