Skip to content

Commit

Permalink
get tracks queries
Browse files Browse the repository at this point in the history
  • Loading branch information
michelson committed May 5, 2024
1 parent a5a3236 commit 43584ac
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
11 changes: 11 additions & 0 deletions app/controllers/likes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
class LikesController < ApplicationController

before_action :check_user

def create
@resource = find_resource
@button_class = current_user.toggle_like!(@resource) ? "button-active" : "button"
end

private

def find_resource
if params[:track_id]
@resource = Track.friendly.find(params[:track_id])
elsif params[:playlist_id]
@resource = Playlist.friendly.find(params[:playlist_id])
end
end

def check_user
redirect_to new_user_session_path and return if current_user.blank?
end


end
8 changes: 5 additions & 3 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def show
get_meta_tags
@as = :track
@section = "tracks/track_item"

# render @user.label ? "labels/show" : "show"
end

def tracks
Expand All @@ -28,7 +30,6 @@ def tracks
@as = :track
@title = "Tracks"
@section = "tracks/track_item"

paginated_render
end

Expand Down Expand Up @@ -152,13 +153,14 @@ def get_tracks
# @collection = @user.tracks.page(params[:page]).per(2)
@collection = if current_user && @user.id == current_user&.id
User.track_preloaded_by_user(current_user&.id)
.where(user_id: @user.id)
#.where(user_id: @user.id)
.with_attached_cover
.includes(user: {avatar_attachment: :blob})
.order("id desc")
.page(params[:page]).per(6)
else
@user.tracks.published
User.track_preloaded_by_user_n(@user&.id)
#@user.tracks.published
.with_attached_cover
.includes(user: {avatar_attachment: :blob})
.order("id desc").page(params[:page]).per(6)
Expand Down
43 changes: 38 additions & 5 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def self.track_preloaded_by_user(id)
# Track.left_outer_joins(:reposts, :likes)
# .where("reposts.user_id = :id OR likes.liker_id = :id OR reposts.user_id IS NULL OR likes.liker_id IS NULL", id: id)
# .includes(:audio_blob, :cover_blob, user: :avatar_attachment)

user = User.find(id)
tracks = Track.arel_table
users = User.arel_table
reposts_alias = Repost.arel_table.alias("r")
Expand All @@ -107,10 +107,43 @@ def self.track_preloaded_by_user(id)
.and(likes_alias[:liker_type].eq("User")))
.join_sources

result = Track.includes(:audio_blob, :cover_blob, user: :avatar_attachment)
.joins(reposts_join, likes_join)
.select("tracks.*, r.id as repost_id, l.id as like_id")
.references(:r, :l)
if !user.label
result = Track.includes(:audio_blob, :cover_blob, user: :avatar_attachment)
.joins(reposts_join, likes_join)
.select("tracks.*, r.id as repost_id, l.id as like_id")
.references(:r, :l)
return result
else

# Gather child account IDs
child_ids = User.find(id).child_accounts.pluck(:id)

# Adjust where clause to include tracks from child accounts
result = Track.includes(:audio_blob, :cover_blob, user: :avatar_attachment)
.joins(reposts_join, likes_join)
.where(tracks[:user_id].eq(id).or(tracks[:user_id].in(child_ids)))
.select("tracks.*, r.id as repost_id, l.id as like_id")
.references(:r, :l)
end

end

def self.track_preloaded_by_user_n(id)
user = User.find(id)
tracks = Track.arel_table
users = User.arel_table

if !user.label
result = Track.includes(:audio_blob, :cover_blob, user: :avatar_attachment)
return result
else
# Gather child account IDs
child_ids = User.find(id).child_accounts.pluck(:id)
# Adjust where clause to include tracks from child accounts
result = Track.includes(:audio_blob, :cover_blob, user: :avatar_attachment)
.where(tracks[:user_id].eq(id).or(tracks[:user_id].in(child_ids)))
end

end

def reposts_preloaded
Expand Down
2 changes: 1 addition & 1 deletion app/views/tracks/_track_item.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<%= track.title %>
<% end %>
</h4>
<h5 class="text-sm font-"><%= track.user.username %> </h5>
<h5 class="text-sm font-"><%= track.user.username %> <%= track.id %> </h5>
</div>

<% if track.private %>
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@
end
end

resources :labels

constraints(Constraints::UsernameRouteConstrainer.new) do
# Same route as before, only within the constraints block
resources :users, path: "" do
Expand Down

0 comments on commit 43584ac

Please sign in to comment.