From 5ef630cf86faf6f74e5df9d34767c3064ed6a20d Mon Sep 17 00:00:00 2001 From: Seth Call Date: Mon, 17 Feb 2014 00:30:08 +0000 Subject: [PATCH] * in progress on next parameter VRFS-1135 --- db/manifest | 3 ++- db/up/feed_autoincrement_primary_key.sql | 9 +++++++ ruby/lib/jam_ruby/models/recording.rb | 2 +- ruby/spec/jam_ruby/models/feed_spec.rb | 22 ++++++++++++++++ web/app/controllers/api_feeds_controller.rb | 9 ++++++- .../api_invited_users_controller.rb | 2 +- web/app/views/api_feeds/index.rabl | 1 - .../controllers/api_feeds_controller_spec.rb | 26 +++++++++++++++++++ 8 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 db/up/feed_autoincrement_primary_key.sql diff --git a/db/manifest b/db/manifest index 24b1ab8bf..c5711632d 100755 --- a/db/manifest +++ b/db/manifest @@ -108,4 +108,5 @@ recordings_via_admin_web.sql relax_band_model_varchar.sql add_piano.sql feed.sql -feed_use_recording.sql \ No newline at end of file +feed_use_recording.sql +feed_autoincrement_primary_key.sql \ No newline at end of file diff --git a/db/up/feed_autoincrement_primary_key.sql b/db/up/feed_autoincrement_primary_key.sql new file mode 100644 index 000000000..9c481a2e9 --- /dev/null +++ b/db/up/feed_autoincrement_primary_key.sql @@ -0,0 +1,9 @@ +DROP TABLE feeds; + +CREATE TABLE feeds ( + id BIGSERIAL PRIMARY KEY, + recording_id VARCHAR(64) UNIQUE REFERENCES recordings(id) ON DELETE CASCADE, + music_session_id VARCHAR(64) UNIQUE REFERENCES music_sessions_history(id) ON DELETE CASCADE, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/recording.rb b/ruby/lib/jam_ruby/models/recording.rb index 8efd4e73f..75c5bc6a5 100644 --- a/ruby/lib/jam_ruby/models/recording.rb +++ b/ruby/lib/jam_ruby/models/recording.rb @@ -12,7 +12,7 @@ module JamRuby has_many :comments, :class_name => "JamRuby::RecordingComment", :foreign_key => "recording_id" has_many :likes, :class_name => "JamRuby::RecordingLiker", :foreign_key => "recording_id" has_many :plays, :class_name => "JamRuby::RecordingPlay", :foreign_key => "recording_id" - has_one :feed, :class_name => "JamRuby::Feed", :inverse_of => :recording, :foreign_key => 'recording_id', :dependent => :destroy + has_one :feed, :class_name => "JamRuby::Feed", :inverse_of => :recording, :foreign_key => 'recording', :dependent => :destroy belongs_to :owner, :class_name => "JamRuby::User", :inverse_of => :owned_recordings, :foreign_key => 'owner_id' belongs_to :band, :class_name => "JamRuby::Band", :inverse_of => :recordings diff --git a/ruby/spec/jam_ruby/models/feed_spec.rb b/ruby/spec/jam_ruby/models/feed_spec.rb index 96d584e75..7b81ba384 100644 --- a/ruby/spec/jam_ruby/models/feed_spec.rb +++ b/ruby/spec/jam_ruby/models/feed_spec.rb @@ -35,4 +35,26 @@ describe Feed do feeds[0].music_session_history == music_session.music_session_history 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) + feeds.length.should == 1 + feeds[0].recording.should == claimed_recording.recording + + options[:start] = 1 + feeds = Feed.index(options) + feeds.length.should == 1 + feeds[0].music_session_history.should == claimed_recording.recording.music_session.music_session_history + + options[:start] = 2 + feeds = Feed.index(options) + feeds.length.should == 0 + end + end + end diff --git a/web/app/controllers/api_feeds_controller.rb b/web/app/controllers/api_feeds_controller.rb index 09bf72c59..753a1970c 100644 --- a/web/app/controllers/api_feeds_controller.rb +++ b/web/app/controllers/api_feeds_controller.rb @@ -1,6 +1,9 @@ class ApiFeedsController < ApiController + respond_to :json + def index + puts params.inspect # parse out since parameter since = params[:since] if since @@ -12,13 +15,17 @@ class ApiFeedsController < ApiController limit = 20 end + limit = params[:limit].to_i if params[:limit] # override limit if specified + @feeds = Feed.index({user: current_user, start: start, limit: limit}) if @feeds.length < limit @next = nil + else + @next = "#{start + limit}:#{limit}" end - @feeds + render "api_feeds/index", :layout => nil end end \ No newline at end of file diff --git a/web/app/controllers/api_invited_users_controller.rb b/web/app/controllers/api_invited_users_controller.rb index bba99ec2f..903da69c2 100644 --- a/web/app/controllers/api_invited_users_controller.rb +++ b/web/app/controllers/api_invited_users_controller.rb @@ -1,4 +1,4 @@ -class ApiInvitedUsersController < ApiController + class ApiInvitedUsersController < ApiController # have to be signed in currently to see this screen before_filter :api_signed_in_user diff --git a/web/app/views/api_feeds/index.rabl b/web/app/views/api_feeds/index.rabl index fe1aac33b..5b7b8d9c3 100644 --- a/web/app/views/api_feeds/index.rabl +++ b/web/app/views/api_feeds/index.rabl @@ -1,4 +1,3 @@ - node :next do |page| @next end diff --git a/web/spec/controllers/api_feeds_controller_spec.rb b/web/spec/controllers/api_feeds_controller_spec.rb index 64b659168..5f111aacb 100644 --- a/web/spec/controllers/api_feeds_controller_spec.rb +++ b/web/spec/controllers/api_feeds_controller_spec.rb @@ -46,4 +46,30 @@ describe ApiFeedsController do music_session[:type].should == 'music_session_history' end + describe "pagination" do + + it "since parameter" do + claimed_recording.touch + claimed_recording.recording.created_at = 3.days.ago + claimed_recording.recording.save! + + get :index, { limit: 1 } + json = JSON.parse(response.body, :symbolize_names => true) + json[:entries].length.should == 1 + _next = json[:next] + _next.should_not be_nil + + get :index, { since: _next } + json = JSON.parse(response.body, :symbolize_names => true) + json[:entries].length.should == 1 + _next = json[:next] + _next.should_not be_nil + + get :index, { since: _next } + json = JSON.parse(response.body, :symbolize_names => true) + json[:entries].length.should == 0 + _next = json[:next] + _next.should be_nil + end + end end