* VRFS-1135 add time filters and type filter

This commit is contained in:
Seth Call 2014-02-20 02:22:41 +00:00
parent 857dd27404
commit 6aae1ef0e6
5 changed files with 105 additions and 7 deletions

View File

@ -7,8 +7,8 @@ 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 = {})
limit = params[:limit]
@ -30,7 +30,7 @@ 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'
@ -53,9 +53,13 @@ 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')

View File

@ -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(:type => 'music_session_history')
feeds.length.should == 1
feeds[0].music_session_history == claimed_recording1.recording.music_session.music_session_history
end
@ -123,11 +123,62 @@ 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(: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(: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(: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(: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(: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)

View File

@ -7,7 +7,8 @@ class ApiFeedsController < ApiController
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

View File

@ -46,6 +46,44 @@ describe ApiFeedsController do
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

View File

@ -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