Skip to content

Commit

Permalink
Add prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
gesteves committed May 26, 2024
1 parent d86df02 commit 21903bf
Show file tree
Hide file tree
Showing 17 changed files with 176 additions and 28 deletions.
3 changes: 2 additions & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ class HomeController < ApplicationController
before_action :authenticate_user!

def index
@preference = current_user&.preference
@preference = current_user&.preference || current_user.build_preference
@todays_playlists = current_user&.todays_playlists
@music_request = current_user&.current_music_request || current_user.music_requests.build
@page_title = "Today’s Playlists"
end
end
45 changes: 45 additions & 0 deletions app/controllers/music_requests_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class MusicRequestsController < ApplicationController
before_action :authenticate_user!
before_action :set_request, only: [:activate, :destroy]

def index
@music_requests = current_user.music_requests
end

def activate
current_user.music_requests.update_all(active: false)
@music_request.update(active: true)
redirect_to music_requests_path, notice: 'Your music preferences have been restored!'
end

def create
@music_request = current_user.music_requests.build(music_request_params)
@music_request.active = true

if @music_request.save
GenerateUserPlaylistsJob.perform_inline(current_user.id)
redirect_to root_path, notice: 'Your music preferences have been saved!'
else
redirect_to root_path
end
end

def destroy
@music_request.destroy
if @music_request.active?
most_recent_request = current_user.music_requests.order(created_at: :desc).first
most_recent_request.update(active: true) if most_recent_request.present?
end
redirect_to music_requests_path, notice: 'Your music preferences have been deleted!'
end

private

def music_request_params
params.require(:music_request).permit(:prompt)
end

def set_request
@music_request = current_user.music_requests.find(params[:id])
end
end
1 change: 0 additions & 1 deletion app/controllers/preferences_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def edit
def update
@preference = current_user.preference || current_user.build_preference
if @preference.update(preference_params)
GenerateUserPlaylistsJob.perform_inline(current_user.id) unless current_user.todays_playlists.any?
redirect_to root_path, notice: 'Your changes have been saved!'
else
render :edit
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/music_requests_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module MusicRequestsHelper
end
2 changes: 1 addition & 1 deletion app/jobs/generate_playlist_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def chatgpt_user_prompt(user, playlist)
#{playlist.workout_name}
#{playlist.workout_description}
#{user.preference.musical_tastes}
#{user.current_music_request.prompt}
#{user.excluded_tracks_string}
PROMPT
Expand Down
26 changes: 26 additions & 0 deletions app/models/music_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class MusicRequest < ApplicationRecord
belongs_to :user

validates :prompt, presence: true
validate :no_duplicate_active_prompt, if: :active?

default_scope { order(active: :desc, created_at: :desc) }

before_save :ensure_only_one_active

scope :active, -> { where(active: true) }

private

def no_duplicate_active_prompt
return unless user.music_requests.active.where(prompt: prompt).exists?

errors.add(:prompt, 'already exists.')
end

def ensure_only_one_active
if active
user.music_requests.update_all(active: false)
end
end
end
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class User < ApplicationRecord
has_many :authentications, dependent: :destroy
has_one :preference, dependent: :destroy
has_many :playlists, dependent: :destroy
has_many :music_requests, dependent: :destroy

def self.from_omniauth(auth)
authentication = Authentication.where(provider: auth.provider, uid: auth.uid).first_or_initialize
Expand Down Expand Up @@ -96,4 +97,11 @@ def has_valid_spotify_token?
false
end
end

# Returns the active music request for the user
#
# @return [MusicRequest, nil] The active music request, or nil if not found.
def current_music_request
music_requests.find_by(active: true)
end
end
52 changes: 29 additions & 23 deletions app/views/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,37 @@
</section>

<section class="container is-max-desktop">
<% if @todays_playlists.any? %>
<% @todays_playlists.each do |playlist| %>
<% if playlist.processing? %>
<%= render partial: 'playlists/processing' %>
<% else %>
<%= render partial: 'playlists/card', locals: { playlist: playlist } %>
<% end %>
<%= form_with model: @music_request, url: music_requests_path, method: :post, local: true do |form| %>
<div class="field">
<%= form.label :prompt, "What do you want to listen to today?", class: 'label' %>
<div class="control">
<%= form.text_area :prompt, class: 'textarea' %>
</div>
</div>
<div class="field is-grouped is-grouped-right">
<div class="control">
<%= form.submit "Save", class: "button is-primary", disabled: regenerate_playlists_disabled? %>
</div>
</div>
<% end %>

<% if @todays_playlists.any? %>
<hr>
<div class="field is-grouped is-grouped-right">
<%= button_to regenerate_all_playlists_path, method: :post, data: { turbo_confirm: 'Are you sure you want to regenerate all unlocked playlists?' }, disabled: regenerate_playlists_disabled?, class: "button" do %>
<span class="icon">
<i class="fa-solid fa-sparkles"></i>
</span>
<span>Regenerate all</span>
<% @todays_playlists.each do |playlist| %>
<% if playlist.processing? %>
<%= render partial: 'playlists/processing' %>
<% else %>
<%= render partial: 'playlists/card', locals: { playlist: playlist } %>
<% end %>
</div>
<% else %>
<div class="field is-grouped is-grouped-centered">
<%= button_to regenerate_all_playlists_path, method: :post, class: "button" do %>
<span class="icon">
<i class="fa-solid fa-arrows-rotate"></i>
</span>
<span>Check for new workouts</span>
<% end %>
</div>
<% end %>
<hr>
<div class="field is-grouped is-grouped-right">
<%= button_to regenerate_all_playlists_path, method: :post, data: { turbo_confirm: 'Are you sure you want to regenerate all unlocked playlists?' }, disabled: regenerate_playlists_disabled?, class: "button" do %>
<span class="icon">
<i class="fa-solid fa-sparkles"></i>
</span>
<span>Regenerate all</span>
<% end %>
</div>
<% end %>
</section>
2 changes: 2 additions & 0 deletions app/views/music_requests/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>MusicRequests#index</h1>
<p>Find me in app/views/music_requests/index.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/music_requests/update.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>MusicRequests#update</h1>
<p>Find me in app/views/music_requests/update.html.erb</p>
2 changes: 1 addition & 1 deletion app/views/preferences/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@

<hr>

<div class="field is-grouped">
<div class="field is-grouped is-grouped-right">
<div class="control">
<%= form.submit "Save changes", class: "button is-primary" %>
</div>
Expand Down
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
post :regenerate_all, to: 'playlists#regenerate_all'
end
end
resources :music_requests, only: [:index, :create, :destroy] do
member do
patch :activate
end
end

get "/settings", to: "preferences#edit", as: :settings
# Defines the root path route ("/")
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20240526205120_create_music_requests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateMusicRequests < ActiveRecord::Migration[7.1]
def change
create_table :music_requests do |t|
t.text :prompt
t.boolean :active, default: true
t.references :user, null: false, foreign_key: true

t.timestamps
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions test/controllers/music_requests_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "test_helper"

class MusicRequestsControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
get music_requests_index_url
assert_response :success
end

test "should get update" do
get music_requests_update_url
assert_response :success
end
end
11 changes: 11 additions & 0 deletions test/fixtures/music_requests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
prompt: MyText
active: false
user: one

two:
prompt: MyText
active: false
user: two
7 changes: 7 additions & 0 deletions test/models/music_request_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class MusicRequestTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 21903bf

Please sign in to comment.