From 177f608276c1e9bbae6e74338580db2cec72d8fd Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Sun, 15 Jan 2012 15:56:34 -0800 Subject: [PATCH] fix oembed issues in backbone land --- app/models/o_embed_cache.rb | 6 ++++++ app/models/reshare.rb | 4 ++++ app/views/templates/reshare.jst | 4 +--- app/views/templates/status_message.jst | 4 +--- public/javascripts/app/views/content_view.js | 8 +++++++- spec/javascripts/app/views/post_view_spec.js | 20 ++++++++++++++++++++ 6 files changed, 39 insertions(+), 7 deletions(-) diff --git a/app/models/o_embed_cache.rb b/app/models/o_embed_cache.rb index 17c4bfe6e..dd1e70cbd 100644 --- a/app/models/o_embed_cache.rb +++ b/app/models/o_embed_cache.rb @@ -5,6 +5,12 @@ class OEmbedCache < ActiveRecord::Base has_many :posts + # NOTE API V1 to be extracted + acts_as_api + api_accessible :backbone do |t| + t.add :data + end + def self.find_or_create_by_url(url) cache = OEmbedCache.find_or_initialize_by_url(url) return cache if cache.persisted? diff --git a/app/models/reshare.rb b/app/models/reshare.rb index b4559b8fc..f5468a5b2 100644 --- a/app/models/reshare.rb +++ b/app/models/reshare.rb @@ -29,6 +29,10 @@ class Reshare < Post self.root.author.diaspora_handle end + def o_embed_cache + self.root ? root.o_embed_cache : super + end + def raw_message self.root ? root.raw_message : super end diff --git a/app/views/templates/reshare.jst b/app/views/templates/reshare.jst index 3513e4f19..95716f6d8 100644 --- a/app/views/templates/reshare.jst +++ b/app/views/templates/reshare.jst @@ -39,9 +39,7 @@ <%= text %> - <% if(o_embed_cache) { %> - <%= root.o_embed_cache.posts.data.html %> - <% } %> + <%= o_embed_html %> diff --git a/app/views/templates/status_message.jst b/app/views/templates/status_message.jst index eb4e306d6..06d04dd4d 100644 --- a/app/views/templates/status_message.jst +++ b/app/views/templates/status_message.jst @@ -14,6 +14,4 @@ <%= text %> -<% if(o_embed_cache) { %> - <%= o_embed_cache.posts.data.html %> -<% } %> +<%= o_embed_html %> diff --git a/public/javascripts/app/views/content_view.js b/public/javascripts/app/views/content_view.js index fee5cb09d..1d321d590 100644 --- a/public/javascripts/app/views/content_view.js +++ b/public/javascripts/app/views/content_view.js @@ -2,7 +2,8 @@ app.views.Content = app.views.StreamObject.extend({ presenter : function(){ var model = this.model return _.extend(this.defaultPresenter(), { - text : metafyText(model.get("text")) + text : metafyText(model.get("text")), + o_embed_html : embedHTML(model) }) function metafyText(text) { @@ -48,6 +49,11 @@ app.views.Content = app.views.StreamObject.extend({ return "" + url + "" }) } + + function embedHTML(model){ + if(!model.get("o_embed_cache")) { return ""; } + return model.get("o_embed_cache").data.html + } } }) diff --git a/spec/javascripts/app/views/post_view_spec.js b/spec/javascripts/app/views/post_view_spec.js index 93f392411..6dd2678dc 100644 --- a/spec/javascripts/app/views/post_view_spec.js +++ b/spec/javascripts/app/views/post_view_spec.js @@ -167,6 +167,26 @@ describe("app.views.Post", function(){ }) }) + context("embed_html", function(){ + it("provides oembed html from the model response", function(){ + this.statusMessage.set({"o_embed_cache" : { + "data" : { + "html" : "some html" + } + }}) + + var view = new app.views.Content({model : this.statusMessage}); + expect(view.presenter().o_embed_html).toContain("some html") + }) + + it("does not provide oembed html from the model response if none is present", function(){ + this.statusMessage.set({"o_embed_cache" : null}) + + var view = new app.views.Content({model : this.statusMessage}); + expect(view.presenter().o_embed_html).toBe(""); + }) + }) + context("user not signed in", function(){ it("does not provide a Feedback view", function(){ logout()