Use an explicit per_page param instead of switching on format
This commit is contained in:
parent
58ac33607a
commit
d2b7168195
3 changed files with 24 additions and 38 deletions
|
|
@ -19,12 +19,13 @@ class NotificationsController < VannaController
|
|||
@aspect = :notification
|
||||
conditions = {:recipient_id => current_user.id}
|
||||
page = opts[:page] || 1
|
||||
notifications = WillPaginate::Collection.create(page, 25, Notification.where(conditions).count ) do |pager|
|
||||
per_page = opts[:per_page] || 25
|
||||
notifications = WillPaginate::Collection.create(page, per_page, Notification.where(conditions).count ) do |pager|
|
||||
result = Notification.find(:all,
|
||||
:conditions => conditions,
|
||||
:order => 'created_at desc',
|
||||
:include => [:target, {:actors => :profile}],
|
||||
:limit => request.format == :json ? 5 : pager.per_page,
|
||||
:limit => pager.per_page,
|
||||
:offset => pager.offset
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -13,12 +13,12 @@
|
|||
this.badgeLink.toggle(function(evt) {
|
||||
evt.preventDefault();
|
||||
evt.stopPropagation();
|
||||
|
||||
|
||||
self.ajaxLoader.show();
|
||||
self.badge.addClass("active");
|
||||
self.dropdown.css("display", "block");
|
||||
|
||||
self.getNotifications(function() {
|
||||
self.getNotifications(function() {
|
||||
self.renderNotifications();
|
||||
});
|
||||
}, function(evt) {
|
||||
|
|
@ -46,7 +46,7 @@
|
|||
};
|
||||
|
||||
this.getNotifications = function(callback) {
|
||||
$.getJSON("/notifications", function(notifications) {
|
||||
$.getJSON("/notifications?per_page=5", function(notifications) {
|
||||
self.notifications = notifications;
|
||||
callback.apply(self, []);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -48,45 +48,30 @@ describe NotificationsController do
|
|||
before do
|
||||
@post = Factory(:status_message)
|
||||
Factory(:notification, :recipient => @user, :target => @post)
|
||||
|
||||
@fake_request = ActionDispatch::Request.new({})
|
||||
@controller.stub!(:request).and_return(@fake_request)
|
||||
end
|
||||
|
||||
context "html request" do
|
||||
before do
|
||||
@fake_request.stub!(:format).and_return(:html)
|
||||
end
|
||||
it 'paginates the notifications' do
|
||||
25.times { Factory(:notification, :recipient => @user, :target => @post) }
|
||||
|
||||
it 'paginates the notifications' do
|
||||
25.times { Factory(:notification, :recipient => @user, :target => @post) }
|
||||
|
||||
@controller.index({})[:notifications].count.should == 25
|
||||
@controller.index(:page => 2)[:notifications].count.should == 1
|
||||
end
|
||||
|
||||
it "includes the actors" do
|
||||
Factory(:notification, :recipient => @user, :target => @post)
|
||||
response = @controller.index({})
|
||||
response[:notifications].first[:actors].first.should be_a(Person)
|
||||
end
|
||||
|
||||
it 'eager loads the target' do
|
||||
response = @controller.index({})
|
||||
response[:notifications].each { |note| note[:target].should be }
|
||||
end
|
||||
@controller.index({})[:notifications].count.should == 25
|
||||
@controller.index(:page => 2)[:notifications].count.should == 1
|
||||
end
|
||||
|
||||
context "json request" do
|
||||
before do
|
||||
@fake_request.stub!(:format).and_return(:json)
|
||||
end
|
||||
it "includes the actors" do
|
||||
Factory(:notification, :recipient => @user, :target => @post)
|
||||
response = @controller.index({})
|
||||
response[:notifications].first[:actors].first.should be_a(Person)
|
||||
end
|
||||
|
||||
it "returns just the first 5 notifications" do
|
||||
5.times { Factory(:notification, :recipient => @user, :target => @post) }
|
||||
response = @controller.index({})
|
||||
response[:notifications].length.should == 5
|
||||
end
|
||||
it 'eager loads the target' do
|
||||
response = @controller.index({})
|
||||
response[:notifications].each { |note| note[:target].should be }
|
||||
end
|
||||
|
||||
it "supports a limit per_page parameter" do
|
||||
5.times { Factory(:notification, :recipient => @user, :target => @post) }
|
||||
response = @controller.index({:per_page => 5})
|
||||
response[:notifications].length.should == 5
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue