DG MS; determine what template to use on the client from the server

This commit is contained in:
danielgrippi 2012-02-15 16:30:45 -08:00
parent d54ff5f341
commit ff875a5b3c
7 changed files with 127 additions and 2 deletions

View file

@ -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'

View file

@ -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

View file

@ -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

View file

@ -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

45
lib/template_picker.rb Normal file
View file

@ -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

View file

@ -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)

View file

@ -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