From 25cf776059fdacf370a0c3b384fa0a8690f98ec1 Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Fri, 17 Feb 2012 16:40:41 -0800 Subject: [PATCH] the previous and next posts are now supplied in json of the presenter; give the template creator a default value --- app/models/post.rb | 8 ++++ app/presenters/post_presenter.rb | 40 +++++++++++++++++-- app/views/posts/show.html.haml | 23 ++--------- lib/diaspora/shareable.rb | 3 ++ lib/template_picker.rb | 6 ++- .../javascripts/app/views/single_post_view.js | 8 +++- public/javascripts/posts-show.js | 14 +++++++ spec/lib/template_picker_spec.rb | 12 ++++-- 8 files changed, 85 insertions(+), 29 deletions(-) create mode 100644 public/javascripts/posts-show.js diff --git a/app/models/post.rb b/app/models/post.rb index f171edd8c..e8af5ca54 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -68,6 +68,14 @@ class Post < ActiveRecord::Base 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 self.class.name end diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb index 9e36b4d6c..22243f48f 100644 --- a/app/presenters/post_presenter.rb +++ b/app/presenters/post_presenter.rb @@ -1,16 +1,50 @@ require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'template_picker') class PostPresenter - attr_accessor :post + attr_accessor :post, :current_user - def initialize(post) + def initialize(post, current_user = nil) self.post = post + self.current_user = current_user end 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 } 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 \ No newline at end of file diff --git a/app/views/posts/show.html.haml b/app/views/posts/show.html.haml index bd8500dab..c91de91a7 100644 --- a/app/views/posts/show.html.haml +++ b/app/views/posts/show.html.haml @@ -6,24 +6,7 @@ = post_page_title @post - content_for :head do - =javascript_include_tag 'vendor/bootstrap/bootstrap-transition', 'vendor/bootstrap/bootstrap-modal' - -: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'); - } - }); - }); - + = javascript_include_tag 'vendor/bootstrap/bootstrap-transition', 'vendor/bootstrap/bootstrap-modal', 'posts-show' .header .header-container #post-author.media @@ -72,8 +55,8 @@ #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-right.png'), post_path(@post.id+1), :class => 'nav-arrow right', :id => 'forward' += link_to image_tag('arrow-left.png'), '#', :class => 'nav-arrow left', :id => 'back' += link_to image_tag('arrow-right.png'), '#', :class => 'nav-arrow right', :id => 'forward' #comment.modal.fade .modal-header diff --git a/lib/diaspora/shareable.rb b/lib/diaspora/shareable.rb index fb7f3c12e..5220f74a8 100644 --- a/lib/diaspora/shareable.rb +++ b/lib/diaspora/shareable.rb @@ -52,6 +52,9 @@ module Diaspora end end + + + def diaspora_handle read_attribute(:diaspora_handle) || self.author.diaspora_handle end diff --git a/lib/template_picker.rb b/lib/template_picker.rb index c0a7a73a1..81080b512 100644 --- a/lib/template_picker.rb +++ b/lib/template_picker.rb @@ -14,9 +14,11 @@ class TemplatePicker end def template_name - for template in TEMPLATES + TEMPLATES.each do |template| return template.gsub("_", '-') if self.send("#{template}?".to_sym) end + + 'status' #default end def status_with_photo_backdrop? @@ -36,7 +38,7 @@ class TemplatePicker end def photo_backdrop? - post.photos.size == 1 + post.photos.size == 1 end def status? diff --git a/public/javascripts/app/views/single_post_view.js b/public/javascripts/app/views/single_post_view.js index a6eca8ba7..e0d476343 100644 --- a/public/javascripts/app/views/single_post_view.js +++ b/public/javascripts/app/views/single_post_view.js @@ -1,5 +1,11 @@ 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')); + } }); diff --git a/public/javascripts/posts-show.js b/public/javascripts/posts-show.js new file mode 100644 index 000000000..9404b264b --- /dev/null +++ b/public/javascripts/posts-show.js @@ -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'); + } + }); +}); \ No newline at end of file diff --git a/spec/lib/template_picker_spec.rb b/spec/lib/template_picker_spec.rb index b0f0169c1..38b0ff7f8 100644 --- a/spec/lib/template_picker_spec.rb +++ b/spec/lib/template_picker_spec.rb @@ -2,7 +2,10 @@ require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'template_picker') describe TemplatePicker 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 let(:post) { @@ -16,11 +19,13 @@ describe TemplatePicker do describe '#template_name' 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 + 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 end end @@ -48,6 +53,7 @@ describe TemplatePicker do @post_stubs.merge!(:photos => stub(:size => 1)) TemplatePicker.new(post).should be_photo_backdrop end + end describe '#status?' do