From 1f9b24e758cfc2c61236701c55634f915eeaa86b Mon Sep 17 00:00:00 2001 From: Yannick Francois Date: Mon, 2 Sep 2013 15:42:24 +0200 Subject: [PATCH] Make notes more like article in feeds (with guid and title) --- app/models/note.rb | 3 +- .../shared/_atom_item_article.atom.builder | 7 +++- .../shared/_rss_item_article.rss.builder | 6 ++- spec/factories.rb | 1 + spec/models/note_spec.rb | 42 ++++++++++++------- spec/views/articles/index_atom_feed_spec.rb | 18 ++++++++ spec/views/articles/index_rss_feed_spec.rb | 19 +++++++++ 7 files changed, 79 insertions(+), 17 deletions(-) diff --git a/app/models/note.rb b/app/models/note.rb index 9384df093e..2c6b3ddd6b 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -14,10 +14,11 @@ class Note < Content belongs_to :user validates_presence_of :body - validates_uniqueness_of :permalink + validates_uniqueness_of :permalink, :guid attr_accessor :push_to_twitter, :twitter_message after_create :set_permalink, :shorten_url + before_create :create_guid default_scope order("published_at DESC") diff --git a/app/views/shared/_atom_item_article.atom.builder b/app/views/shared/_atom_item_article.atom.builder index d1eb3a2418..81e724dada 100644 --- a/app/views/shared/_atom_item_article.atom.builder +++ b/app/views/shared/_atom_item_article.atom.builder @@ -6,9 +6,14 @@ feed.entry item, :id => "urn:uuid:#{item.guid}", :url => item.permalink_url do | entry.email email if this_blog.link_to_author unless email.blank? end - entry.title item.title, "type"=>"html" + if item.is_a?(Note) + entry.title item.body, "type"=>"html" + else + entry.title item.title, "type"=>"html" + end if item.is_a?(Article) + item.categories.each do |category| entry.category "term" => category.permalink, "label" => category.name, "scheme" => category.permalink_url end diff --git a/app/views/shared/_rss_item_article.rss.builder b/app/views/shared/_rss_item_article.rss.builder index 93a888cc42..642ab29373 100644 --- a/app/views/shared/_rss_item_article.rss.builder +++ b/app/views/shared/_rss_item_article.rss.builder @@ -1,5 +1,9 @@ xm.item do - xm.title item.title + if item.is_a?(Note) + xm.title item.body + else + xm.title item.title + end content_html = if item.password_protected? "

This article is password protected. Please fill in your password to read it

" diff --git a/spec/factories.rb b/spec/factories.rb index 8ea981bb5d..f313a785a7 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -238,6 +238,7 @@ published true state 'published' text_filter {FactoryGirl.create(:markdown)} + guid end factory :unpublished_note, :parent => :note do |n| diff --git a/spec/models/note_spec.rb b/spec/models/note_spec.rb index 672f01eac5..b2a1632d0a 100644 --- a/spec/models/note_spec.rb +++ b/spec/models/note_spec.rb @@ -1,6 +1,20 @@ # coding: utf-8 require 'spec_helper' +describe Note do + let!(:blog) { create(:blog) } + + describe "validations" do + it { expect(create(:note)).to be_valid } + + context "with an existing note" do + let(:existing_note) { create(:note) } + + it { expect(build(:note, guid: existing_note.guid)).to be_invalid } + end + end +end + describe "Testing redirects" do it "a new published status gets a redirect" do FactoryGirl.create(:blog) @@ -20,8 +34,8 @@ note = FactoryGirl.create(:note, :body => "A test tweet with a #hashtag") text = note.html_preprocess(note.body, note.body) text.should == "A test tweet with a #hashtag" - end - + end + it "should replace a @mention by a proper URL to the twitter account" do note = FactoryGirl.create(:note, :body => "A test tweet with a @mention") text = note.html_preprocess(note.body, note.body) @@ -38,7 +52,7 @@ note = FactoryGirl.create(:note, :body => "A test tweet with a https://link.com") text = note.html_preprocess(note.body, note.body) text.should == "A test tweet with a https://link.com" - end + end end describe 'Testing notes scopes' do @@ -50,7 +64,7 @@ it 'Published scope should not bring unpublished statuses' do FactoryGirl.create(:note) FactoryGirl.create(:unpublished_note) - + notes = Note.published notes.count.should == 1 end @@ -58,7 +72,7 @@ it 'Published scope should not bring notes published in the future' do FactoryGirl.create(:note) FactoryGirl.create(:note, published_at: Time.now + 3.days ) - + notes = Note.published notes.count.should == 1 end @@ -75,7 +89,7 @@ subject { @note.permalink_url } it { should == "http://myblog.net/note/#{@note.id}-this-is-a-note" } end - + it "should give a sanitized title" do note = FactoryGirl.build(:note, :body => 'body with accents éèà') note.body.to_permalink.should == 'body-with-accents-eea' @@ -118,7 +132,7 @@ def valid_attributes @note.body = 'somebody' @note.should be_valid end - + it "should use sanitize title to set note name" do @note.attributes = valid_attributes.except(:body) @note.body = 'title with accents éèà' @@ -126,7 +140,7 @@ def valid_attributes @note.save @note.permalink.should == "#{@note.id}-title-with-accents-eea" end - + end describe 'Given a note page' do @@ -142,12 +156,12 @@ def valid_attributes note = FactoryGirl.build(:note, twitter_message: "A message without URL") note.instance_eval{ calculate_real_length }.should == 21 end - + it "A twitter message with a short http URL should have its URL expanded to 20 chars" do note = FactoryGirl.build(:note, twitter_message: "A message with a short URL http://foo.com") note.instance_eval{ calculate_real_length }.should == 47 end - + it "A twitter message with a short https URL should have its URL expanded to 21 chars" do note = FactoryGirl.build(:note, twitter_message: "A message with a short URL https://foo.com") note.instance_eval{ calculate_real_length }.should == 48 @@ -162,7 +176,7 @@ def valid_attributes note = FactoryGirl.build(:note, twitter_message: "A message with a long URL http://foobarsomething.com?blablablablabla") note.instance_eval{ calculate_real_length }.should == 46 end - + it "A twitter message with a short https URL should have its URL expanded to 21 chars" do note = FactoryGirl.build(:note, twitter_message: "A message with a long URL https://foobarsomething.com?blablablablabla") note.instance_eval{ calculate_real_length }.should == 47 @@ -172,7 +186,7 @@ def valid_attributes note = FactoryGirl.build(:note, twitter_message: "A message with a long URL ftp://foobarsomething.com?blablablablabla") note.instance_eval{ calculate_real_length }.should == 45 end -end +end describe 'Pushing a note to Twitter' do before :each do @@ -184,7 +198,7 @@ def valid_attributes note = FactoryGirl.build(:note, :push_to_twitter => 0) note.send_to_twitter.should == false end - + it 'a non configured blog and non configured user should not send a note to Twitter' do FactoryGirl.create(:blog) note = FactoryGirl.create(:note) @@ -197,7 +211,7 @@ def valid_attributes note = FactoryGirl.build(:note, user: user) note.send_to_twitter.should == false end - + it 'a non configured blog and a configured user should not send a note to Twitter' do FactoryGirl.build(:blog) user = FactoryGirl.build(:user, twitter_oauth_token: "12345", twitter_oauth_token_secret: "67890") diff --git a/spec/views/articles/index_atom_feed_spec.rb b/spec/views/articles/index_atom_feed_spec.rb index 4a32c06f19..dd3e53f24f 100644 --- a/spec/views/articles/index_atom_feed_spec.rb +++ b/spec/views/articles/index_atom_feed_spec.rb @@ -158,5 +158,23 @@ def rendered_entry parsed = Nokogiri::XML.parse(rendered) parsed.css("entry").first end + + describe :title do + + before(:each) do + assign(:articles, [article]) + render + end + + context "with a note" do + let(:article) { create(:note) } + it { expect(rendered_entry.css("title").text).to eq(article.body) } + end + + context "with an article" do + let(:article) { create(:article) } + it { expect(rendered_entry.css("title").text).to eq(article.title) } + end + end end diff --git a/spec/views/articles/index_rss_feed_spec.rb b/spec/views/articles/index_rss_feed_spec.rb index 6afa642dfa..6e3506c6d6 100644 --- a/spec/views/articles/index_rss_feed_spec.rb +++ b/spec/views/articles/index_rss_feed_spec.rb @@ -194,5 +194,24 @@ def rendered_entry parsed = Nokogiri::XML.parse(rendered) parsed.css("item").first end + + describe :title do + + before(:each) do + assign(:articles, [article]) + render + end + + context "with a note" do + let(:article) { create(:note) } + it { expect(rendered_entry.css("title").text).to eq(article.body) } + end + + context "with an article" do + let(:article) { create(:article) } + it { expect(rendered_entry.css("title").text).to eq(article.title) } + end + end + end