the previous and next posts are now supplied in json of the presenter; give the template creator a default value

This commit is contained in:
Maxwell Salzberg 2012-02-17 16:40:41 -08:00 committed by danielgrippi
parent cc0e04ea3f
commit 25cf776059
8 changed files with 85 additions and 29 deletions

View file

@ -68,6 +68,14 @@ class Post < ActiveRecord::Base
joins(:likes).where(:likes => {:author_id => person.id}) joins(:likes).where(:likes => {:author_id => person.id})
} }
def self.next(post)
where("posts.id > ?", post.id)
end
def self.previous(post)
where("posts.id < ?", post.id)
end
def post_type def post_type
self.class.name self.class.name
end end

View file

@ -1,16 +1,50 @@
require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'template_picker') require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'template_picker')
class PostPresenter class PostPresenter
attr_accessor :post attr_accessor :post, :current_user
def initialize(post) def initialize(post, current_user = nil)
self.post = post self.post = post
self.current_user = current_user
end end
def to_json(options = {}) def to_json(options = {})
{ {
:post => self.post.as_api_response(:backbone), :post => self.post.as_api_response(:backbone).update(
{ :next_post => next_post_url,
:previous_post => previous_post_url}),
:templateName => TemplatePicker.new(self.post).template_name :templateName => TemplatePicker.new(self.post).template_name
} }
end end
def next_post_url
if n = next_post
Rails.application.routes.url_helpers.post_path(n)
end
end
def previous_post_url
if p = previous_post
Rails.application.routes.url_helpers.post_path(p)
end
end
def next_post
post_base.next(post).first
end
def previous_post
post_base.previous(post).first
end
protected
def post_base
if current_user
Post.owned_or_visible_by_user(current_user)
else
Post.all_public
end
end
end end

View file

@ -6,24 +6,7 @@
= post_page_title @post = post_page_title @post
- content_for :head do - content_for :head do
=javascript_include_tag 'vendor/bootstrap/bootstrap-transition', 'vendor/bootstrap/bootstrap-modal' = javascript_include_tag 'vendor/bootstrap/bootstrap-transition', 'vendor/bootstrap/bootstrap-modal', 'posts-show'
:javascript
$(function(){
$(document).keypress(function(event){
$('#text').focus();
$('#comment').modal();
});
$(document).keydown(function(e){
if (e.keyCode == 37) {
window.location = $('#back').attr('href');
}else if(e.keyCode == 39) {
window.location = $('#forward').attr('href');
}
});
});
.header .header
.header-container .header-container
#post-author.media #post-author.media
@ -72,8 +55,8 @@
#post-content #post-content
= link_to image_tag('arrow-left.png'), post_path(@post.id-1), :class => 'nav-arrow left', :id => 'back' = link_to image_tag('arrow-left.png'), '#', :class => 'nav-arrow left', :id => 'back'
= link_to image_tag('arrow-right.png'), post_path(@post.id+1), :class => 'nav-arrow right', :id => 'forward' = link_to image_tag('arrow-right.png'), '#', :class => 'nav-arrow right', :id => 'forward'
#comment.modal.fade #comment.modal.fade
.modal-header .modal-header

View file

@ -52,6 +52,9 @@ module Diaspora
end end
end end
def diaspora_handle def diaspora_handle
read_attribute(:diaspora_handle) || self.author.diaspora_handle read_attribute(:diaspora_handle) || self.author.diaspora_handle
end end

View file

@ -14,9 +14,11 @@ class TemplatePicker
end end
def template_name def template_name
for template in TEMPLATES TEMPLATES.each do |template|
return template.gsub("_", '-') if self.send("#{template}?".to_sym) return template.gsub("_", '-') if self.send("#{template}?".to_sym)
end end
'status' #default
end end
def status_with_photo_backdrop? def status_with_photo_backdrop?
@ -36,7 +38,7 @@ class TemplatePicker
end end
def photo_backdrop? def photo_backdrop?
post.photos.size == 1 post.photos.size == 1
end end
def status? def status?

View file

@ -1,5 +1,11 @@
app.views.SinglePost = app.views.Post.extend({ app.views.SinglePost = app.views.Post.extend({
className : "loaded" className : "loaded",
next_arrow: $('#forward'),
previous_arrow: $('#back'),
postRenderTemplate : function() {
$('#forward').attr('href', this.model.get('next_post'));
$('#back').attr('href', this.model.get('previous_post'));
}
}); });

View file

@ -0,0 +1,14 @@
$(function(){
$(document).keypress(function(event){
$('#text').focus();
$('#comment').modal();
});
$(document).keydown(function(e){
if (e.keyCode == 37) {
window.location = $('#back').attr('href');
}else if(e.keyCode == 39) {
window.location = $('#forward').attr('href');
}
});
});

View file

@ -2,7 +2,10 @@ require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'template_picker')
describe TemplatePicker do describe TemplatePicker do
before do before do
@post_stubs = {:photos => stub(:present? => true, :size => 2), :o_embed_cache => stub(:present? => true), :text? => true, :text => stub(:length => 400)} @post_stubs = {:type => 'StatusMessage', :photos => stub(:size => 2),
:o_embed_cache => stub(:present? => true),
:text? => true, :text => stub(:length => 400)
}
end end
let(:post) { let(:post) {
@ -16,11 +19,13 @@ describe TemplatePicker do
describe '#template_name' do describe '#template_name' do
it 'returns the coolest template if the post has lots of cool stuff' do it 'returns the coolest template if the post has lots of cool stuff' do
puts TemplatePicker.new(post).template_name TemplatePicker.new(post).template_name.should_not be_nil
end end
end end
describe '#status_with_photo_backdrop?' do describe '#status_with_photo_backdrop?' do
it 'is true if the post contains a photo and text' do it 'is true if the post contains a single photo and text' do
@post_stubs.merge!(:photos => stub(:size => 1))
TemplatePicker.new(post).should be_status_with_photo_backdrop TemplatePicker.new(post).should be_status_with_photo_backdrop
end end
end end
@ -48,6 +53,7 @@ describe TemplatePicker do
@post_stubs.merge!(:photos => stub(:size => 1)) @post_stubs.merge!(:photos => stub(:size => 1))
TemplatePicker.new(post).should be_photo_backdrop TemplatePicker.new(post).should be_photo_backdrop
end end
end end
describe '#status?' do describe '#status?' do