Merge branch 'feature/video_mvp' into develop

This commit is contained in:
Steven Miers 2014-10-07 17:37:06 -05:00
commit 9473ffa352
2 changed files with 122 additions and 19 deletions

View File

@ -303,22 +303,82 @@ module JamRuby
since = 0 unless since || since == '' # guard against nil
uploads = []
RecordedTrack
.joins(:recording)
.where(:user_id => user.id)
.where(:fully_uploaded => false)
.where('recorded_tracks.id > ?', since)
.where("upload_failures <= #{APP_CONFIG.max_track_upload_failures}")
.where("duration IS NOT NULL")
.where('all_discarded = false')
.order('recorded_tracks.id')
.limit(limit).each do |recorded_track|
uploads.push({
:type => "recorded_track",
:client_track_id => recorded_track.client_track_id,
:recording_id => recorded_track.recording_id,
:next => recorded_track.id
})
# Uploads now include videos in addition to the tracks.
# This is accomplished using a SQL union via arel, as follows:
# Select fields from track. Note the reorder, which removes
# the default scope sort as it b0rks the union. Also note the
# alias so that we can differentiate tracks and videos when
# processing the results:
track_arel = RecordedTrack.select([
:id,
:recording_id,
:url,
:fully_uploaded,
:upload_failures,
:client_track_id,
Arel::Nodes::As.new('track', Arel.sql('item_type'))
]).reorder("")
# Select fields for video. Note that it must include
# the same number of fields as the track in order for
# the union to work:
vid_arel = RecordedVideo.select([
:id,
:recording_id,
:url,
:fully_uploaded,
:upload_failures,
:client_video_source_id,
Arel::Nodes::As.new('video', Arel.sql('item_type'))
]).reorder("")
# Glue them together:
union = track_arel.union(vid_arel)
# Create a table alias from the union so we can get back to arel:
utable = RecordedTrack.arel_table.create_table_alias(union, :recorded_items)
arel = track_arel.from(utable)
arel = arel.select([
:id,
:recording_id,
:url,
:fully_uploaded,
:upload_failures,
:client_track_id,
:item_type
])
# Further joining and criteria for the unioned object:
arel = arel.joins("INNER JOIN (SELECT id as rec_id, all_discarded, duration FROM recordings) recs ON rec_id=recorded_items.recording_id") \
.where('recorded_items.fully_uploaded =?', false) \
.where('recorded_items.id > ?', since) \
.where("upload_failures <= #{APP_CONFIG.max_track_upload_failures}") \
.where("duration IS NOT NULL") \
.where('all_discarded = false') \
.order('recorded_items.id') \
.limit(limit)
# Load into array:
arel.each do |recorded_item|
if(recorded_item.item_type=='video')
# A video:
uploads << ({
:type => "recorded_video",
:client_video_source_id => recorded_item.client_track_id,
:recording_id => recorded_item.recording_id,
:next => recorded_item.id
})
else
# A track:
uploads << ({
:type => "recorded_track",
:client_track_id => recorded_item.client_track_id,
:recording_id => recorded_item.recording_id,
:next => recorded_item.id
})
end
end
next_value = uploads.length > 0 ? uploads[-1][:next].to_s : nil
@ -326,7 +386,6 @@ module JamRuby
next_value = since # echo back to the client the same value they passed in, if there are no results
end
{
"uploads" => uploads,
"next" => next_value.to_s

View File

@ -7,8 +7,7 @@ describe Recording do
@instrument = FactoryGirl.create(:instrument, :description => 'a great instrument')
@music_session = FactoryGirl.create(:active_music_session, :creator => @user, :musician_access => true)
@connection = FactoryGirl.create(:connection, :user => @user, :music_session => @music_session)
@track = FactoryGirl.create(:track, :connection => @connection, :instrument => @instrument)
@video_source = FactoryGirl.create(:video_source, :connection => @connection)
@track = FactoryGirl.create(:track, :connection => @connection, :instrument => @instrument)
end
it "should allow finding of recorded tracks" do
@ -289,6 +288,8 @@ describe Recording do
it "set video from source" do
@video_source = FactoryGirl.create(:video_source, :connection => @connection)
@music_session.is_recording?.should be_false
@recording = Recording.start(@music_session, @user)
@music_session.reload
@ -299,6 +300,49 @@ describe Recording do
@recorded_videos.should have(1).items
@recorded_videos[0].client_video_source_id.should eq(@video_source.id)
end
it "should include video when listing uploads" do
@video_source = FactoryGirl.create(:video_source, :connection => @connection)
@recording = Recording.start(@music_session, @user)
@recording.stop
@recording.reload
@genre = FactoryGirl.create(:genre)
@recording.claim(@user, "Recording", "Recording Description", @genre, true)
# We should have 2 items; a track and a video:
uploads = Recording.list_uploads(@user)
uploads["uploads"].should have(2).items
uploads["uploads"][0][:type].should eq("recorded_track")
uploads["uploads"][1][:type].should eq("recorded_video")
uploads["uploads"][0].should include(:client_track_id)
uploads["uploads"][1].should include(:client_video_source_id)
# Next page should have nothing:
uploads = Recording.list_uploads(@user, 10, uploads["next"])
uploads["uploads"].should have(0).items
end
it "should paginate with video" do
@video_source = FactoryGirl.create(:video_source, :connection => @connection)
@recording = Recording.start(@music_session, @user)
@recording.stop
@recording.reload
@genre = FactoryGirl.create(:genre)
@recording.claim(@user, "Recording", "Recording Description", @genre, true)
# Limit to 1, so we can test pagination:
uploads = Recording.list_uploads(@user, 1)
uploads["uploads"].should have(1).items # First page
# Second page:
uploads = Recording.list_uploads(@user, 1, uploads["next"])
uploads["uploads"].should have(1).items
# Last page (should be empty):
uploads = Recording.list_uploads(@user, 10, uploads["next"])
uploads["uploads"].should have(0).items
end
end