Skip to content
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

Allow scope by id on main models #44

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/albums_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class AlbumsController < ApplicationController
before_action :set_album, only: %i[show update destroy]

has_scope :by_filter, as: 'filter'
has_scope :by_ids, as: 'ids', type: :array
has_scope :by_label, as: 'label'
has_scope :by_labels, as: 'labels', type: :array
has_scope :by_artist, as: 'artist_id'
Expand Down
1 change: 1 addition & 0 deletions app/controllers/artists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class ArtistsController < ApplicationController
before_action :set_artist, only: %i[show update destroy]

has_scope :by_filter, as: 'filter'
has_scope :by_ids, as: 'ids', type: :array

def index
authorize Artist
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/genres_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class GenresController < ApplicationController
before_action :set_genre, only: %i[show update destroy merge]

has_scope :by_ids, as: 'ids', type: :array

def index
authorize Genre
@genres = apply_scopes(policy_scope(Genre))
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/labels_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class LabelsController < ApplicationController
before_action :set_label, only: %i[show update destroy merge]

has_scope :by_ids, as: 'ids', type: :array

def index
authorize Label
@labels = apply_scopes(policy_scope(Label))
Expand Down
1 change: 1 addition & 0 deletions app/controllers/tracks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class TracksController < ApplicationController
before_action :set_track, only: %i[show update destroy audio merge]

has_scope :by_filter, as: 'filter'
has_scope :by_ids, as: 'ids', type: :array
has_scope :by_album, as: 'album_id'
has_scope :by_artist, as: 'artist_id'
has_scope :by_genre, as: 'genre_id'
Expand Down
1 change: 1 addition & 0 deletions app/models/album.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Album < ApplicationRecord
normalized_col_generator :title

scope :by_filter, ->(filter) { where('"albums"."normalized_title" LIKE ?', "%#{Album.normalize(filter)}%") }
scope :by_ids, ->(ids) { where(id: ids) }
scope :by_artist, ->(artist) { joins(:artists).where(artists: { id: artist }) }
scope :by_label, ->(label) { joins(:album_labels).where(album_labels: { label_id: label }) }
scope :by_labels, ->(labels) { joins(:album_labels).where(album_labels: { label_id: labels }) }
Expand Down
1 change: 1 addition & 0 deletions app/models/artist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Artist < ApplicationRecord
normalized_col_generator :name

scope :by_filter, ->(filter) { where('normalized_name LIKE ?', "%#{Artist.normalize(filter)}%") }
scope :by_ids, ->(ids) { where(id: ids) }
scope :sorted, lambda { |key, direction|
case key
when 'name'
Expand Down
2 changes: 2 additions & 0 deletions app/models/genre.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Genre < ApplicationRecord

normalized_col_generator :name

scope :by_ids, ->(ids) { where(id: ids) }

def merge(other)
other.tracks.find_each do |track|
tracks << track unless tracks.include?(track)
Expand Down
2 changes: 2 additions & 0 deletions app/models/label.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Label < ApplicationRecord

normalized_col_generator :name

scope :by_ids, ->(ids) { where(id: ids) }

def merge(other)
other.album_labels.find_each do |al|
al.update(label_id: id) unless albums.include?(al.album)
Expand Down
1 change: 1 addition & 0 deletions app/models/track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Track < ApplicationRecord
before_save :normalize_artist_order

scope :by_filter, ->(filter) { where('"tracks"."normalized_title" LIKE ?', "%#{Track.normalize(filter)}%") }
scope :by_ids, ->(ids) { where(id: ids) }
scope :by_artist, ->(artist) { joins(:artists).where(artists: { id: artist }) }
scope :by_album, ->(album) { where(album: album) }
scope :by_genre, ->(genre) { joins(:genres).where(genres: { id: genre }) }
Expand Down
10 changes: 10 additions & 0 deletions test/models/album_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,14 @@ class AlbumTest < ActiveSupport::TestCase
assert_not album.valid?
assert_not_empty album.errors[:album_artists]
end

test 'should be able to search by id' do
a1 = create(:album)
a2 = create(:album)
a3 = create(:album)

assert_equal [a1], Album.by_ids(a1.id).to_a
assert_equal [a1, a2], Album.by_ids([a1.id, a2.id]).to_a
assert_equal [a3], Album.by_ids(a3.id).to_a
end
end
10 changes: 10 additions & 0 deletions test/models/artist_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ class ArtistTest < ActiveSupport::TestCase
assert_not artist.normalized_name.nil?
assert_equal 'iouaaa', artist.normalized_name
end

test 'should be able to search by id' do
a1 = create(:artist)
a2 = create(:artist)
a3 = create(:artist)

assert_equal [a1], Artist.by_ids(a1.id).to_a
assert_equal [a1, a2], Artist.by_ids([a1.id, a2.id]).to_a
assert_equal [a3], Artist.by_ids(a3.id).to_a
end
end
10 changes: 10 additions & 0 deletions test/models/genre_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,14 @@ class GenreTest < ActiveSupport::TestCase
assert_not track.reload.genres.include?(genre1)
assert track.reload.genres.include?(genre2)
end

test 'should be able to search by id' do
g1 = create(:genre)
g2 = create(:genre)
g3 = create(:genre)

assert_equal [g1], Genre.by_ids(g1.id).to_a
assert_equal [g1, g2], Genre.by_ids([g1.id, g2.id]).to_a
assert_equal [g3], Genre.by_ids(g3.id).to_a
end
end
10 changes: 10 additions & 0 deletions test/models/label_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,14 @@ class LabelTest < ActiveSupport::TestCase
assert_not album.reload.labels.include?(label1)
assert album.reload.labels.include?(label2)
end

test 'should be able to search by id' do
l1 = create(:label)
l2 = create(:label)
l3 = create(:label)

assert_equal [l1], Label.by_ids(l1.id).to_a
assert_equal [l1, l2], Label.by_ids([l1.id, l2.id]).to_a
assert_equal [l3], Label.by_ids(l3.id).to_a
end
end
10 changes: 10 additions & 0 deletions test/models/track_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ class TrackTest < ActiveSupport::TestCase
assert_equal [t2], Track.by_genre(g3.id).to_a
end

test 'should be able to search by id' do
t1 = create(:track)
t2 = create(:track)
t3 = create(:track)

assert_equal [t1], Track.by_ids(t1.id).to_a
assert_equal [t1, t2], Track.by_ids([t1.id, t2.id]).to_a
assert_equal [t3], Track.by_ids(t3.id).to_a
end

test 'should automatically generate normalized_title' do
track = build(:track, title: 'ïóùåAÁ')
track.save
Expand Down