* VRFS-2617 - allow only purchased jamtracks to be returned by list method. (reuse existing API)

This commit is contained in:
Seth Call 2015-01-07 16:16:57 -06:00
parent 3028e421fb
commit d58cf2a048
7 changed files with 62 additions and 6 deletions

View File

@ -54,7 +54,7 @@ module JamRuby
accepts_nested_attributes_for :jam_track_tap_ins, allow_destroy: true
class << self
def index(options = {}, force_all=false)
def index(options, user)
limit = options[:limit]
limit ||= 20
limit = limit.to_i
@ -65,12 +65,18 @@ module JamRuby
query = JamTrack.joins(:jam_track_tracks)
.offset(start)
.limit(limit)
if options[:show_purchased_only]
query = query.joins(:jam_track_rights)
query = query.where("jam_track_rights.user_id = ?", user.id)
end
query = query.where("jam_tracks.available = ?", true) unless force_all
query = query.where("jam_tracks.available = ?", true) unless user.admin
query = query.where("jam_tracks.genre_id = '#{options[:genre]}'") unless options[:genre].blank?
query = query.where("jam_track_tracks.instrument_id = '#{options[:instrument]}'") unless options[:instrument].blank?
query = query.where("jam_tracks.sales_region = '#{options[:availability]}'") unless options[:availability].blank?
query = query.group("jam_tracks.id")
query = query.order('jam_tracks.name')
if query.length == 0
[query, nil]

View File

@ -715,9 +715,16 @@ FactoryGirl.define do
reproduction_royalty_amount 0.999
licensor_royalty_amount 0.999
pro_royalty_amount 0.999
available true
genre JamRuby::Genre.first
association :licensor, factory: :jam_track_licensor
factory :jam_track_with_tracks do
after(:create) do |jam_track, evaluator|
FactoryGirl.create(:jam_track_track, jam_track: jam_track)
end
end
end
factory :jam_track_track, :class => JamRuby::JamTrackTrack do

View File

@ -2,6 +2,10 @@ require 'spec_helper'
describe EmailBatch do
before(:all) do
User.delete_all
end
after(:each) do
Timecop.return
end

View File

@ -6,6 +6,7 @@ describe JamTrack do
include CarrierWave::Test::Matchers
include UsesTempFiles
let(:user) {FactoryGirl.create(:user)}
it "created" do
jam_track = FactoryGirl.create(:jam_track)
@ -13,6 +14,46 @@ describe JamTrack do
jam_track.licensor.jam_tracks.should == [jam_track]
end
describe "index" do
it "empty query" do
query, pager = JamTrack.index({}, user)
query.size.should == 0
end
it "sorts by name" do
jam_track1 = FactoryGirl.create(:jam_track_with_tracks, name: 'a')
jam_track2 = FactoryGirl.create(:jam_track_with_tracks, name: 'b')
query, pager = JamTrack.index({}, user)
query.size.should == 2
query[0].should eq(jam_track1)
query[1].should eq(jam_track2)
# flip order by mucking with name
jam_track1.name = 'c'
jam_track1.save!
query, pager = JamTrack.index({}, user)
query[0].should eq(jam_track2)
query[1].should eq(jam_track1)
end
it "supports showing purchased only" do
jam_track1 = FactoryGirl.create(:jam_track_with_tracks, name: 'a')
# no results yet
query, pager = JamTrack.index({show_purchased_only:true}, user)
query.size.should == 0
# but after the user buys it, it is returned
FactoryGirl.create(:jam_track_right, jam_track: jam_track1, user: user)
query, pager = JamTrack.index({show_purchased_only:true}, user)
query.size.should == 1
query[0].should eq(jam_track1)
end
end
describe "validations" do
describe "bpm" do
it "1" do

View File

@ -7,7 +7,7 @@ class ApiJamTracksController < ApiController
respond_to :json
def index
data = JamTrack.index(params, current_user.admin)
data = JamTrack.index(params, current_user)
@jam_tracks, @next = data[0], data[1]
render "api_jam_tracks/index", :layout => nil

View File

@ -149,10 +149,9 @@ describe ApiJamTracksController do
response.status.should == 200
json = JSON.parse(response.body)
json.length.should == 1
puts json.inspect
json[0]['id'].should == @jam_track.id
json[0]['private'].should be_nil
json[0]['error'].should == 'not_purchased'
json[0]['error'].should == 'not_purchased'
end
it "track with no key" do

View File

@ -721,7 +721,6 @@ FactoryGirl.define do
after(:create) do |jam_track, evaluator|
FactoryGirl.create(:jam_track_track, jam_track: jam_track) if evaluator.make_track
end
end
factory :jam_track_track, :class => JamRuby::JamTrackTrack do