* merged
This commit is contained in:
parent
0218ca5ad6
commit
5654f27b55
|
|
@ -111,3 +111,4 @@ feed.sql
|
|||
like_follower_poly_assoc.sql
|
||||
feed_use_recording.sql
|
||||
feed_autoincrement_primary_key.sql
|
||||
music_session_plays.sql
|
||||
|
|
|
|||
|
|
@ -118,6 +118,7 @@ require "jam_ruby/models/icecast_template_socket"
|
|||
require "jam_ruby/models/icecast_server_group"
|
||||
require "jam_ruby/models/icecast_mount_template"
|
||||
require "jam_ruby/models/facebook_signup"
|
||||
require "jam_ruby/models/recordings_plays"
|
||||
require "jam_ruby/models/feed"
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -5,8 +5,27 @@ module JamRuby
|
|||
belongs_to :recording, class_name: "JamRuby::Recording", inverse_of: :feed, foreign_key: 'recording_id'
|
||||
belongs_to :music_session_history, class_name: "JamRuby::MusicSessionHistory", inverse_of: :feed, foreign_key: 'music_session_id'
|
||||
|
||||
def self.index(params = {start:0, limit:20})
|
||||
Feed.includes(:recording).includes(:music_session_history).order('created_at DESC').offset(params[:start]).limit(params[:limit])
|
||||
def self.index(params = {start:0, limit:20, sort: 'date'})
|
||||
|
||||
start = params[:start]
|
||||
start ||= 0
|
||||
|
||||
limit = params[:limit]
|
||||
limit ||= 20
|
||||
|
||||
# validate sort
|
||||
sort = params[:sort]
|
||||
sort ||= 'date'
|
||||
raise "not valid sort #{sort}" unless ['date', 'plays', 'likes'].include?(sort)
|
||||
|
||||
query = Feed.includes(:recording).includes(:music_session_history).offset(start).limit(limit)
|
||||
if sort == 'date'
|
||||
query = query.order('id DESC')
|
||||
else
|
||||
throw "not implemented"
|
||||
end
|
||||
|
||||
query
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -368,4 +368,8 @@ FactoryGirl.define do
|
|||
sequence(:token) { |n| "token-#{n}"}
|
||||
token_expires_at Time.now
|
||||
end
|
||||
|
||||
factory :recordings_plays, :class => JamRuby::RecordingsPlays do
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -35,11 +35,35 @@ describe Feed do
|
|||
feeds[0].music_session_history == music_session.music_session_history
|
||||
end
|
||||
|
||||
describe "sorting" do
|
||||
it "sorts by index (date) DESC" do
|
||||
claimed_recording = FactoryGirl.create(:claimed_recording)
|
||||
|
||||
feeds = Feed.index()
|
||||
feeds.length.should == 2
|
||||
feeds[0].recording.should == claimed_recording.recording
|
||||
feeds[1].music_session_history.should == claimed_recording.recording.music_session.music_session_history
|
||||
end
|
||||
|
||||
it "sort by plays DESC" do
|
||||
claimed_recording1 = FactoryGirl.create(:claimed_recording)
|
||||
claimed_recording2 = FactoryGirl.create(:claimed_recording)
|
||||
MusicSessionHistory.delete_all
|
||||
|
||||
FactoryGirl.create(:recordings_plays, recording: claimed_recording1.recording, player:claimed_recording1.user)
|
||||
|
||||
feeds = Feed.index(:sort => 'plays')
|
||||
|
||||
feeds.length.should == 2
|
||||
feeds[0].recording.should == claimed_recording1.recording
|
||||
feeds[1].recording.should == claimed_recording2.recording
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
describe "pagination" do
|
||||
it "supports pagination" do
|
||||
claimed_recording = FactoryGirl.create(:claimed_recording)
|
||||
claimed_recording.recording.created_at = 3.days.ago
|
||||
claimed_recording.recording.save!
|
||||
|
||||
options = {limit: 1}
|
||||
feeds = Feed.index(options)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
module JamRuby
|
||||
class RecordingsPlays < ActiveRecord::Base
|
||||
|
||||
|
||||
self.table_name = "recordings_plays"
|
||||
|
||||
self.primary_key = 'id'
|
||||
|
||||
belongs_to :recording,
|
||||
class_name: "JamRuby::Recording",
|
||||
foreign_key: "recording_id"
|
||||
|
||||
belongs_to :player,
|
||||
class_name: "JamRuby::User",
|
||||
foreign_key: "player_id"
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue