diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index e2f0914..e97dcdb 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -19,6 +19,12 @@ class ContentViewerController < PublicController end end + if @page.mime_type != 'text/html' + headers['Content-Type'] = @page.mime_type + render :text => @page.data, :layout => false + return + end + if request.post? && params[:comment] @comment = Comment.new(params[:comment]) @comment.author = user if logged_in? diff --git a/app/models/article.rb b/app/models/article.rb index 71fec9d..f2002f9 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -39,6 +39,12 @@ class Article < ActiveRecord::Base body end + # returns the data of the article. Must be overriden in each subclass to + # provide the correct content for the article. + def data + body + end + # provides the icon name to be used for this article. In this class this # method just returns 'text-html', but subclasses may (and should) override # to return their specific icons. diff --git a/app/models/rss_feed.rb b/app/models/rss_feed.rb new file mode 100644 index 0000000..94ec751 --- /dev/null +++ b/app/models/rss_feed.rb @@ -0,0 +1,43 @@ +class RssFeed < Article + + # store setting in body + serialize Hash, :body + + # TODO + def to_html + end + + # RSS feeds have type =text/xml=. + def mime_type + 'text/xml' + end + + # FIXME - feed real data into the RSS feed + def data + result = "" + xml = Builder::XmlMarkup.new(:target => result) + + xml.instruct! :xml, :version=>"1.0" + xml.rss(:version=>"2.0") do + xml.channel do + xml.title(_("%s's RSS feed") % (self.profile.name)) + xml.link("http://www.yourDomain.com") + xml.description('Description here') + xml.language("pt_BR") + for article in profile.recent_documents(10) + xml.item do + xml.title(article.name) + xml.description(article.abstract) + # rfc822 + xml.pubDate(article.created_on.rfc2822) + xml.link("http://www.yourDomain.com/linkToYourPost") + xml.guid("http://www.yourDomain.com/linkToYourPost") + end + end + end + end + + result + end + +end diff --git a/db/migrate/007_create_articles.rb b/db/migrate/007_create_articles.rb index ea6240c..bf562c9 100644 --- a/db/migrate/007_create_articles.rb +++ b/db/migrate/007_create_articles.rb @@ -23,6 +23,9 @@ class CreateArticles < ActiveRecord::Migration # acts as versioned t.column :version, :integer t.column :lock_version, :integer + + # single-table inheritance + t.column :type, :string end Article.create_versioned_table diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index 6d35e15..b0be9fa 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -85,5 +85,24 @@ class ContentViewerControllerTest < Test::Unit::TestCase end end + should 'produce a download-like when article is not text/html' do + + # for example, RSS feeds + profile = create_user('someone').person + page = profile.articles.build(:name => 'myarticle', :body => 'the body of the text') + page.save! + + feed = RssFeed.new(:name => 'feed') + feed.profile = profile + feed.save! + + get :view_page, :profile => 'someone', :page => [ 'feed' ] + + assert_response :success + assert_match /^text\/xml/, @response.headers['Content-Type'] + + assert_equal feed.data, @response.body + end + end diff --git a/test/unit/rss_feed_test.rb b/test/unit/rss_feed_test.rb new file mode 100644 index 0000000..2eb6caf --- /dev/null +++ b/test/unit/rss_feed_test.rb @@ -0,0 +1,50 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class RssFeedTest < Test::Unit::TestCase + + should 'indicate the correct mime/type' do + assert_equal 'text/xml', RssFeed.new.mime_type + end + + should 'store settings in a hash serialized into body field' do + flunk 'pending' + end + + should 'list recent articles of profile when top-level' do + profile = create_user('testuser').person + a1 = profile.articles.build(:name => 'article 1'); a1.save! + a2 = profile.articles.build(:name => 'article 2'); a2.save! + a3 = profile.articles.build(:name => 'article 3'); a3.save! + + feed = RssFeed.new(:name => 'feed') + feed.profile = profile + feed.save! + + rss = feed.data + assert_match /