Merge branch 'develop' of bitbucket.org:jamkazam/jam-cloud into develop
This commit is contained in:
commit
85f8bf6cad
|
|
@ -7,10 +7,10 @@ module JamRuby
|
|||
|
||||
FIXNUM_MAX = (2**(0.size * 8 -2) -1)
|
||||
SORT_TYPES = ['date', 'plays', 'likes']
|
||||
TIME_RANGES = ['today', 'week', 'month', 'all']
|
||||
TYPE_FILTERS = ['session', 'recording', 'all']
|
||||
TIME_RANGES = { "today" => 1 , "week" => 7, "month" => 30, "all" => 0}
|
||||
TYPE_FILTERS = ['music_session_history', 'recording', 'all']
|
||||
|
||||
def self.index(params = {})
|
||||
def self.index(user, params = {})
|
||||
limit = params[:limit]
|
||||
limit ||= 20
|
||||
limit = limit.to_i
|
||||
|
|
@ -30,12 +30,17 @@ module JamRuby
|
|||
|
||||
time_range = params[:time_range]
|
||||
time_range ||= 'month'
|
||||
raise "not valid time_range #{time_range}" unless TIME_RANGES.include?(time_range)
|
||||
raise "not valid time_range #{time_range}" unless TIME_RANGES.has_key?(time_range)
|
||||
|
||||
type_filter = params[:type]
|
||||
type_filter ||= 'all'
|
||||
raise "not valid type #{type_filter}" unless TYPE_FILTERS.include?(type_filter)
|
||||
|
||||
include_private = params[:include_private]
|
||||
include_private ||= false # default to false
|
||||
include_private = false if user.nil? # and force to false if the current user is nil
|
||||
|
||||
|
||||
query = Feed.includes([:recording]).includes([:music_session_history]).limit(limit)
|
||||
|
||||
# handle sort
|
||||
|
|
@ -53,14 +58,25 @@ module JamRuby
|
|||
end
|
||||
|
||||
# handle time range
|
||||
days = TIME_RANGES[time_range]
|
||||
if days > 0
|
||||
query = query.where("feeds.created_at > NOW() - '#{days} day'::INTERVAL")
|
||||
end
|
||||
|
||||
# handle type filters
|
||||
if type_filter == 'session'
|
||||
if type_filter == 'music_session_history'
|
||||
query = query.where('music_session_id is not NULL')
|
||||
elsif type_filter == 'recording'
|
||||
query = query.where('recording_id is not NULL')
|
||||
end
|
||||
|
||||
# handle private times
|
||||
if include_private
|
||||
|
||||
else
|
||||
|
||||
end
|
||||
|
||||
if query.length == 0
|
||||
[query, nil]
|
||||
elsif query.length < limit
|
||||
|
|
|
|||
|
|
@ -8,14 +8,14 @@ describe Feed do
|
|||
let (:user4) { FactoryGirl.create(:user) }
|
||||
|
||||
it "no result" do
|
||||
feeds, start = Feed.index()
|
||||
feeds, start = Feed.index(user1)
|
||||
feeds.length.should == 0
|
||||
end
|
||||
|
||||
it "one claimed recording" do
|
||||
claimed_recording = FactoryGirl.create(:claimed_recording)
|
||||
MusicSessionHistory.delete_all # the factory makes a music_session while making the recording/claimed_recording
|
||||
feeds, start = Feed.index()
|
||||
feeds, start = Feed.index(user1)
|
||||
feeds.length.should == 1
|
||||
feeds[0].recording == claimed_recording.recording
|
||||
end
|
||||
|
|
@ -30,13 +30,13 @@ describe Feed do
|
|||
# verify the mess above only made one recording
|
||||
Recording.count.should == 1
|
||||
|
||||
feeds, start = Feed.index
|
||||
feeds, start = Feed.index(user1)
|
||||
feeds.length.should == 1
|
||||
end
|
||||
|
||||
it "one music session" do
|
||||
music_session = FactoryGirl.create(:music_session)
|
||||
feeds, start = Feed.index
|
||||
feeds, start = Feed.index(user1)
|
||||
feeds.length.should == 1
|
||||
feeds[0].music_session_history == music_session.music_session_history
|
||||
end
|
||||
|
|
@ -45,7 +45,7 @@ describe Feed do
|
|||
it "sorts by index (date) DESC" do
|
||||
claimed_recording = FactoryGirl.create(:claimed_recording)
|
||||
|
||||
feeds, start = Feed.index
|
||||
feeds, start = Feed.index(user1)
|
||||
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
|
||||
|
|
@ -57,13 +57,13 @@ describe Feed do
|
|||
|
||||
FactoryGirl.create(:recording_play, recording: claimed_recording1.recording, user:claimed_recording1.user)
|
||||
|
||||
feeds, start = Feed.index(:sort => 'plays')
|
||||
feeds, start = Feed.index(user1, :sort => 'plays')
|
||||
feeds.length.should == 4
|
||||
|
||||
FactoryGirl.create(:recording_play, recording: claimed_recording2.recording, user:claimed_recording1.user)
|
||||
FactoryGirl.create(:recording_play, recording: claimed_recording2.recording, user:claimed_recording2.user)
|
||||
|
||||
feeds, start = Feed.index(:sort => 'plays')
|
||||
feeds, start = Feed.index(user1, :sort => 'plays')
|
||||
feeds.length.should == 4
|
||||
feeds[0].recording.should == claimed_recording2.recording
|
||||
feeds[1].recording.should == claimed_recording1.recording
|
||||
|
|
@ -73,7 +73,7 @@ describe Feed do
|
|||
FactoryGirl.create(:music_session_play, music_session: claimed_recording1.recording.music_session.music_session_history, user: user3)
|
||||
|
||||
|
||||
feeds, start = Feed.index(:sort => 'plays')
|
||||
feeds, start = Feed.index(user1, :sort => 'plays')
|
||||
feeds.length.should == 4
|
||||
feeds[0].music_session_history.should == claimed_recording1.recording.music_session.music_session_history
|
||||
feeds[1].recording.should == claimed_recording2.recording
|
||||
|
|
@ -86,13 +86,13 @@ describe Feed do
|
|||
|
||||
FactoryGirl.create(:recording_like, recording: claimed_recording1.recording, user:claimed_recording1.user)
|
||||
|
||||
feeds, start = Feed.index(:sort => 'likes')
|
||||
feeds, start = Feed.index(user1, :sort => 'likes')
|
||||
feeds.length.should == 4
|
||||
|
||||
FactoryGirl.create(:recording_like, recording: claimed_recording2.recording, user:claimed_recording1.user)
|
||||
FactoryGirl.create(:recording_like, recording: claimed_recording2.recording, user:claimed_recording2.user)
|
||||
|
||||
feeds, start = Feed.index(:sort => 'likes')
|
||||
feeds, start = Feed.index(user1, :sort => 'likes')
|
||||
feeds.length.should == 4
|
||||
feeds[0].recording.should == claimed_recording2.recording
|
||||
feeds[1].recording.should == claimed_recording1.recording
|
||||
|
|
@ -101,7 +101,7 @@ describe Feed do
|
|||
FactoryGirl.create(:music_session_like, music_session_history: claimed_recording1.recording.music_session.music_session_history, user: user2)
|
||||
FactoryGirl.create(:music_session_like, music_session_history: claimed_recording1.recording.music_session.music_session_history, user: user3)
|
||||
|
||||
feeds, start = Feed.index(:sort => 'likes')
|
||||
feeds, start = Feed.index(user1, :sort => 'likes')
|
||||
feeds.length.should == 4
|
||||
feeds[0].music_session_history.should == claimed_recording1.recording.music_session.music_session_history
|
||||
feeds[1].recording.should == claimed_recording2.recording
|
||||
|
|
@ -114,7 +114,7 @@ describe Feed do
|
|||
# creates both recording and history record in feed
|
||||
claimed_recording1 = FactoryGirl.create(:claimed_recording)
|
||||
|
||||
feeds, start = Feed.index(:type => 'session')
|
||||
feeds, start = Feed.index(user1, :type => 'music_session_history')
|
||||
feeds.length.should == 1
|
||||
feeds[0].music_session_history == claimed_recording1.recording.music_session.music_session_history
|
||||
end
|
||||
|
|
@ -123,27 +123,78 @@ describe Feed do
|
|||
# creates both recording and history record in feed
|
||||
claimed_recording1 = FactoryGirl.create(:claimed_recording)
|
||||
|
||||
feeds, start = Feed.index(:type => 'session')
|
||||
feeds, start = Feed.index(user1, :type => 'music_session_history')
|
||||
feeds.length.should == 1
|
||||
feeds[0].music_session_history == claimed_recording1.recording.music_session.music_session_history
|
||||
end
|
||||
end
|
||||
|
||||
describe "time ranges" do
|
||||
it "month" do
|
||||
# creates both recording and history record in feed
|
||||
claimed_recording1 = FactoryGirl.create(:claimed_recording)
|
||||
|
||||
# move the feed entry created for the recording back more than a months ago
|
||||
claimed_recording1.recording.feed.created_at = 32.days.ago
|
||||
claimed_recording1.recording.feed.save!
|
||||
|
||||
feeds, start = Feed.index(user1, :type => 'recording')
|
||||
feeds.length.should == 0
|
||||
end
|
||||
|
||||
it "day" do
|
||||
# creates both recording and history record in feed
|
||||
claimed_recording1 = FactoryGirl.create(:claimed_recording)
|
||||
|
||||
# move the feed entry created for the recording back more than a months ago
|
||||
claimed_recording1.recording.feed.created_at = 25.hours.ago
|
||||
claimed_recording1.recording.feed.save!
|
||||
|
||||
feeds, start = Feed.index(user1, :type => 'recording', time_range: 'today')
|
||||
feeds.length.should == 0
|
||||
end
|
||||
|
||||
it "week" do
|
||||
# creates both recording and history record in feed
|
||||
claimed_recording1 = FactoryGirl.create(:claimed_recording)
|
||||
|
||||
# move the feed entry created for the recording back more than a months ago
|
||||
claimed_recording1.recording.feed.created_at = 8.days.ago
|
||||
claimed_recording1.recording.feed.save!
|
||||
|
||||
feeds, start = Feed.index(user1, :type => 'recording', time_range: 'week')
|
||||
feeds.length.should == 0
|
||||
end
|
||||
|
||||
it "all" do
|
||||
# creates both recording and history record in feed
|
||||
claimed_recording1 = FactoryGirl.create(:claimed_recording)
|
||||
|
||||
# move the feed entry created for the recording back more than a months ago
|
||||
claimed_recording1.recording.feed.created_at = 700.days.ago
|
||||
claimed_recording1.recording.feed.save!
|
||||
|
||||
feeds, start = Feed.index(user1, :type => 'recording', time_range: 'all')
|
||||
feeds.length.should == 1
|
||||
end
|
||||
end
|
||||
|
||||
describe "pagination" do
|
||||
it "supports date pagination" do
|
||||
claimed_recording = FactoryGirl.create(:claimed_recording)
|
||||
|
||||
options = {limit: 1}
|
||||
feeds, start = Feed.index(options)
|
||||
feeds, start = Feed.index(user1, options)
|
||||
feeds.length.should == 1
|
||||
feeds[0].recording.should == claimed_recording.recording
|
||||
|
||||
options[:start] = start
|
||||
feeds, start = Feed.index(options)
|
||||
feeds, start = Feed.index(user1, options)
|
||||
feeds.length.should == 1
|
||||
feeds[0].music_session_history.should == claimed_recording.recording.music_session.music_session_history
|
||||
|
||||
options[:start] = start
|
||||
feeds, start = Feed.index(options)
|
||||
feeds, start = Feed.index(user1, options)
|
||||
feeds.length.should == 0
|
||||
start.should be_nil
|
||||
end
|
||||
|
|
@ -154,17 +205,17 @@ describe Feed do
|
|||
FactoryGirl.create(:music_session_like, music_session_history: claimed_recording1.recording.music_session.music_session_history, user: user1)
|
||||
|
||||
options = {limit: 1, sort: 'likes'}
|
||||
feeds, start = Feed.index(options)
|
||||
feeds, start = Feed.index(user1, options)
|
||||
feeds.length.should == 1
|
||||
feeds[0].music_session_history.should == claimed_recording1.recording.music_session.music_session_history
|
||||
|
||||
options[:start] = start
|
||||
feeds, start = Feed.index(options)
|
||||
feeds, start = Feed.index(user1, options)
|
||||
feeds.length.should == 1
|
||||
feeds[0].recording.should == claimed_recording1.recording
|
||||
|
||||
options[:start] = start
|
||||
feeds, start = Feed.index(options)
|
||||
feeds, start = Feed.index(user1, options)
|
||||
feeds.length.should == 0
|
||||
start.should be_nil
|
||||
end
|
||||
|
|
@ -175,21 +226,22 @@ describe Feed do
|
|||
FactoryGirl.create(:music_session_play, music_session: claimed_recording1.recording.music_session.music_session_history, user: user1)
|
||||
|
||||
options = {limit: 1, sort: 'plays'}
|
||||
feeds, start = Feed.index(options)
|
||||
feeds, start = Feed.index(user1, options)
|
||||
feeds.length.should == 1
|
||||
feeds[0].music_session_history.should == claimed_recording1.recording.music_session.music_session_history
|
||||
|
||||
options[:start] = start
|
||||
feeds, start = Feed.index(options)
|
||||
feeds, start = Feed.index(user1, options)
|
||||
feeds.length.should == 1
|
||||
feeds[0].recording.should == claimed_recording1.recording
|
||||
|
||||
options[:start] = start
|
||||
feeds, start = Feed.index(options)
|
||||
feeds, start = Feed.index(user1, options)
|
||||
feeds.length.should == 0
|
||||
start.should be_nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ gem 'fog'
|
|||
gem 'haml-rails'
|
||||
gem 'unf' #optional fog dependency
|
||||
gem 'devise', '>= 1.1.2'
|
||||
#gem 'thin' # the presence of this gem on mac seems to prevent normal startup of rails.
|
||||
gem 'puma' # the presence of this gem on mac seems to prevent normal startup of rails.
|
||||
gem 'postgres-copy'
|
||||
#group :libv8 do
|
||||
# gem 'libv8', "~> 3.11.8"
|
||||
|
|
|
|||
|
|
@ -3,11 +3,12 @@ class ApiFeedsController < ApiController
|
|||
respond_to :json
|
||||
|
||||
def index
|
||||
@feeds, @next = Feed.index(user: current_user,
|
||||
@feeds, @next = Feed.index(current_user,
|
||||
start: params[:since],
|
||||
limit: params[:limit],
|
||||
sort: params[:sort],
|
||||
time_range: params[:time_range])
|
||||
time_range: params[:time_range],
|
||||
type: params[:type])
|
||||
|
||||
render "api_feeds/index", :layout => nil
|
||||
end
|
||||
|
|
|
|||
|
|
@ -41,11 +41,48 @@ describe ApiFeedsController do
|
|||
json = JSON.parse(response.body, :symbolize_names => true)
|
||||
json[:entries].length.should == 1
|
||||
|
||||
|
||||
music_session = json[:entries][0]
|
||||
music_session[:type].should == 'music_session_history'
|
||||
end
|
||||
|
||||
describe "time range" do
|
||||
|
||||
it "today and month find different results" do
|
||||
music_session.music_session_history.touch
|
||||
music_session.music_session_history.feed.created_at = 3.days.ago
|
||||
music_session.music_session_history.feed.save!
|
||||
|
||||
get :index
|
||||
json = JSON.parse(response.body, :symbolize_names => true)
|
||||
json[:entries].length.should == 1
|
||||
|
||||
get :index, { time_range:'today' }
|
||||
json = JSON.parse(response.body, :symbolize_names => true)
|
||||
json[:entries].length.should == 0
|
||||
end
|
||||
end
|
||||
|
||||
describe "type filter" do
|
||||
|
||||
it "can filter by type" do
|
||||
claimed_recording.touch
|
||||
|
||||
get :index
|
||||
json = JSON.parse(response.body, :symbolize_names => true)
|
||||
json[:entries].length.should == 2
|
||||
|
||||
get :index, { type: 'recording'}
|
||||
json = JSON.parse(response.body, :symbolize_names => true)
|
||||
json[:entries].length.should == 1
|
||||
json[:entries][0][:type].should == 'recording'
|
||||
|
||||
get :index, { type: 'music_session_history'}
|
||||
json = JSON.parse(response.body, :symbolize_names => true)
|
||||
json[:entries].length.should == 1
|
||||
json[:entries][0][:type].should == 'music_session_history'
|
||||
end
|
||||
end
|
||||
|
||||
describe "pagination" do
|
||||
|
||||
it "since parameter" do
|
||||
|
|
|
|||
|
|
@ -21,6 +21,10 @@ SpecDb::recreate_database(db_config)
|
|||
ActiveRecord::Base.establish_connection(YAML::load(File.open('config/database.yml'))["test"])
|
||||
require 'jam_ruby'
|
||||
|
||||
# uncomment this to see active record logs
|
||||
# ActiveRecord::Base.logger = Logger.new(STDOUT) if defined?(ActiveRecord::Base)
|
||||
|
||||
|
||||
include JamRuby
|
||||
|
||||
# put ActionMailer into test mode
|
||||
|
|
@ -63,6 +67,11 @@ Spork.prefork do
|
|||
config.visible_text_only = true
|
||||
end
|
||||
|
||||
Capybara.server do |app, port|
|
||||
require 'rack/handler/puma'
|
||||
Rack::Handler::Puma.run(app, :Port => port)
|
||||
end
|
||||
|
||||
# Requires supporting ruby files with custom matchers and macros, etc,
|
||||
# in spec/support/ and its subdirectories.
|
||||
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
|
||||
|
|
|
|||
Loading…
Reference in New Issue