Skip to content

Commit

Permalink
Fix API v1 sbd/remastered values, touch parent records on update
Browse files Browse the repository at this point in the history
  • Loading branch information
jcraigk committed Nov 18, 2024
1 parent 35fdc60 commit a73f6c2
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 7 deletions.
18 changes: 14 additions & 4 deletions app/models/concerns/show_api_v1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def as_json # rubocop:disable Metrics/MethodLength
date: date.iso8601,
duration:,
incomplete:,
sbd: false,
remastered: false,
sbd:,
remastered:,
tour_id:,
venue_id:,
likes_count:,
Expand All @@ -26,8 +26,8 @@ def as_json_api # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
date: date.iso8601,
duration:,
incomplete:,
sbd: false,
remastered: false,
sbd:,
remastered:,
tags: show_tags_for_api,
tour_id:,
venue: venue.as_json,
Expand All @@ -41,6 +41,16 @@ def as_json_api # rubocop:disable Metrics/AbcSize, Metrics/MethodLength

private

def remastered
tag_id = Tag.find_by(name: "Remastered")&.id
tag_id ? ShowTag.exists?(show_id: id, tag_id:) : false
end

def sbd
tag_id = Tag.find_by(name: "SBD")&.id
tag_id ? ShowTag.exists?(show_id: id, tag_id:) : false
end

def show_tags_for_api
show_tags.map { |show_tag| show_tag_json(show_tag) }.sort_by { |t| t[:priority] }
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/show_tag.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class ShowTag < ApplicationRecord
belongs_to :show, counter_cache: :tags_count
belongs_to :show, counter_cache: :tags_count, touch: true
belongs_to :tag, counter_cache: :shows_count

validates :show, uniqueness: { scope: :tag_id }
Expand Down
2 changes: 1 addition & 1 deletion app/models/track.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Track < ApplicationRecord
include TrackApiV1

belongs_to :show
belongs_to :show, touch: true
has_many :songs_tracks, dependent: :destroy
has_many :songs, through: :songs_tracks
has_many :likes, as: :likable, dependent: :destroy
Expand Down
2 changes: 1 addition & 1 deletion app/models/track_tag.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class TrackTag < ApplicationRecord
belongs_to :track, counter_cache: :tags_count
belongs_to :track, counter_cache: :tags_count, touch: true
belongs_to :tag, counter_cache: :tracks_count
end
21 changes: 21 additions & 0 deletions lib/tasks/db.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace :db do
desc "Backfill updated_at timestamps for touch: true associations"
task touch_backfill: :environment do
# Update shows via show_tags
ShowTag.includes(:show).find_each do |show_tag|
show_tag.show&.touch
end

# Update tracks via track_tags
TrackTag.includes(:track).find_each do |track_tag|
track_tag.track&.touch
end

# Update shows via tracks
Track.includes(:show).find_each do |track|
track.show&.touch
end

puts "Backfill complete! Timestamps have been updated."
end
end
6 changes: 6 additions & 0 deletions spec/requests/api/v1/shows_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@

let(:show) { create(:show) }

before do
create(:show_tag, show:, tag: create(:tag, name: 'SBD'))
end

it 'responds with expected data' do
expect(json_data).to eq(show.as_json_api)
expect(json_data[:remastered]).to eq(false)
expect(json_data[:sbd]).to eq(true)
end
end

Expand Down

0 comments on commit a73f6c2

Please sign in to comment.