diff --git a/Gemfile b/Gemfile index 5aed8adde..4455d6a32 100644 --- a/Gemfile +++ b/Gemfile @@ -152,6 +152,8 @@ group :development do gem 'ruby-debug', :platforms => :mri_18 gem 'yard', :require => false + # rails 3.2 goodness + gem 'active_reload' # for tracing AR object instantiation and memory usage per request gem 'oink' diff --git a/Gemfile.lock b/Gemfile.lock index 496428559..a67c80347 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -61,6 +61,7 @@ GEM rack-mount (~> 0.6.14) rack-test (~> 0.5.7) tzinfo (~> 0.3.23) + active_reload (0.6.1) activemodel (3.0.11) activesupport (= 3.0.11) builder (~> 2.1.2) @@ -430,6 +431,7 @@ PLATFORMS DEPENDENCIES SystemTimer (= 1.2.3) + active_reload activerecord-import acts-as-taggable-on! acts_as_api diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 49669558e..59a372585 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -2,6 +2,8 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. +require Rails.root.join("app", "presenters", "post_presenter") + class PostsController < ApplicationController before_filter :authenticate_user!, :except => :show before_filter :set_format_if_malformed_from_status_net, :only => :show @@ -31,7 +33,7 @@ class PostsController < ApplicationController respond_to do |format| format.xml{ render :xml => @post.to_diaspora_xml } format.mobile{render 'posts/show.mobile.haml'} - format.json{ render :json => {:posts => @post.as_api_response(:backbone)}, :status => 201 } + format.json{ render :json => PostPresenter.new(@post).to_json } format.any{render 'posts/show.html.haml'} end diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb new file mode 100644 index 000000000..9e36b4d6c --- /dev/null +++ b/app/presenters/post_presenter.rb @@ -0,0 +1,16 @@ +require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'template_picker') + +class PostPresenter + attr_accessor :post + + def initialize(post) + self.post = post + end + + def to_json(options = {}) + { + :post => self.post.as_api_response(:backbone), + :templateName => TemplatePicker.new(self.post).template_name + } + end +end \ No newline at end of file diff --git a/lib/template_picker.rb b/lib/template_picker.rb new file mode 100644 index 000000000..c0a7a73a1 --- /dev/null +++ b/lib/template_picker.rb @@ -0,0 +1,45 @@ +class TemplatePicker + attr_accessor :post + + TEMPLATES = %w{ status_with_photo_backdrop + note + rich_media + multi_photo + photo_backdrop + status + } + + def initialize(post) + self.post = post + end + + def template_name + for template in TEMPLATES + return template.gsub("_", '-') if self.send("#{template}?".to_sym) + end + end + + def status_with_photo_backdrop? + status? && photo_backdrop? + end + + def note? + self.status? && post.text.length > 300 + end + + def rich_media? + post.o_embed_cache.present? + end + + def multi_photo? + post.photos.size > 1 + end + + def photo_backdrop? + post.photos.size == 1 + end + + def status? + post.text? + end +end \ No newline at end of file diff --git a/public/javascripts/app/router.js b/public/javascripts/app/router.js index 2f6e6dc23..eba879f3e 100644 --- a/public/javascripts/app/router.js +++ b/public/javascripts/app/router.js @@ -39,7 +39,7 @@ app.Router = Backbone.Router.extend({ singlePost : function(id) { new app.models.Post({id : id}).fetch({success : function(resp){ - var postAttrs = resp.get("posts"); + var postAttrs = resp.get("post"); var view = new app.views.Post({ model : new app.models.Post(postAttrs) diff --git a/spec/lib/template_picker_spec.rb b/spec/lib/template_picker_spec.rb new file mode 100644 index 000000000..b0f0169c1 --- /dev/null +++ b/spec/lib/template_picker_spec.rb @@ -0,0 +1,58 @@ +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)} + end + + let(:post) { + stub(@post_stubs) + } + + it 'has a post' do + t = TemplatePicker.new(post) + t.post.should_not be_nil + end + + describe '#template_name' do + it 'returns the coolest template if the post has lots of cool stuff' do + puts TemplatePicker.new(post).template_name + end + end + describe '#status_with_photo_backdrop?' do + it 'is true if the post contains a photo and text' do + TemplatePicker.new(post).should be_status_with_photo_backdrop + end + end + + describe '#note?' do + it 'is true if the post contains text more than 300 characters long' do + TemplatePicker.new(post).should be_note + end + end + + describe '#rich_media?' do + it 'is true if the post contains an o_embed object' do + TemplatePicker.new(post).should be_rich_media + end + end + + describe 'multi_photo?' do + it 'is true if the post contains more than one photo' do + TemplatePicker.new(post).should be_multi_photo + end + end + + describe '#photo_backdrop?' do + it 'is true if the post contains only one photo' do + @post_stubs.merge!(:photos => stub(:size => 1)) + TemplatePicker.new(post).should be_photo_backdrop + end + end + + describe '#status?' do + it 'is true if the post contains text' do + TemplatePicker.new(post).should be_status + end + end +end \ No newline at end of file