-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
176 additions
and
28 deletions.
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
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,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 |
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,2 @@ | ||
module MusicRequestsHelper | ||
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
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 |
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,2 @@ | ||
<h1>MusicRequests#index</h1> | ||
<p>Find me in app/views/music_requests/index.html.erb</p> |
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,2 @@ | ||
<h1>MusicRequests#update</h1> | ||
<p>Find me in app/views/music_requests/update.html.erb</p> |
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,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 |
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,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 |
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,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 |
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,7 @@ | ||
require "test_helper" | ||
|
||
class MusicRequestTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |