Commit d7da0420d71c19ce5327ccf5cc16b9409e7b30eb

Authored by Daniela Feitosa
1 parent bf80644c

Added methods to return data from specific version

(ActionItem2822)
app/controllers/public/content_viewer_controller.rb
... ... @@ -7,7 +7,7 @@ class ContentViewerController < ApplicationController
7 7  
8 8 def view_page
9 9 path = params[:page].join('/')
10   - version = params[:rev]
  10 + @version = params[:rev]
11 11  
12 12 if path.blank?
13 13 @page = profile.home_page
... ... @@ -44,8 +44,8 @@ class ContentViewerController < ApplicationController
44 44 return
45 45 end
46 46  
47   - if version
48   - @versioned_article = @page.versions.find_by_version(version)
  47 + if @version
  48 + @versioned_article = @page.versions.find_by_version(@version)
49 49 render :template => 'content_viewer/versioned_article.rhtml'
50 50 return
51 51 end
... ...
app/helpers/application_helper.rb
... ... @@ -1402,4 +1402,8 @@ module ApplicationHelper
1402 1402 content.nil? ? '' : content.id.to_s
1403 1403 end
1404 1404  
  1405 + def display_article_versions(article, version = nil)
  1406 + content_tag('ul', article.versions.map {|v| link_to("r#{v.version}", @page.url.merge(:rev => v.version))})
  1407 + end
  1408 +
1405 1409 end
... ...
app/models/article.rb
... ... @@ -604,17 +604,24 @@ class Article < ActiveRecord::Base
604 604 false
605 605 end
606 606  
607   - def author
608   - if versions.empty?
609   - last_changed_by
610   - else
611   - author_id = versions.first.last_changed_by_id
  607 + def author(version_number = nil)
  608 + if version_number
  609 + version = versions.find_by_version(version_number)
  610 + author_id = version.last_changed_by_id
612 611 Person.exists?(author_id) ? Person.find(author_id) : nil
  612 + else
  613 + if versions.empty?
  614 + last_changed_by
  615 + else
  616 + author_id = versions.first.last_changed_by_id
  617 + Person.exists?(author_id) ? Person.find(author_id) : nil
  618 + end
613 619 end
614 620 end
615 621  
616   - def author_name
617   - author ? author.name : (setting[:author_name] || _('Unknown'))
  622 + def author_name(version_number = nil)
  623 + person = version_number ? author(version_number) : author
  624 + person ? person.name : (setting[:author_name] || _('Unknown'))
618 625 end
619 626  
620 627 def author_url
... ... @@ -625,6 +632,11 @@ class Article < ActiveRecord::Base
625 632 author ? author.id : nil
626 633 end
627 634  
  635 + def version_license(version_number = nil)
  636 + return license if version_number.nil?
  637 + profile.environment.licenses.find_by_id(versions.find_by_version(version_number).license_id)
  638 + end
  639 +
628 640 alias :active_record_cache_key :cache_key
629 641 def cache_key(params = {}, the_profile = nil, language = 'en')
630 642 active_record_cache_key+'-'+language +
... ...
app/views/content_viewer/_article_versions.rhtml 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +<ul class='article-versions'>
  2 + <% @page.versions.each do |v| %>
  3 + <li><%= link_to("r#{v.version}", @page.url.merge(:rev => v.version)) %></li>
  4 + <% end %>
  5 +<ul>
... ...
app/views/content_viewer/versioned_article.rhtml
1 1 <div id="article" class="<%= @page.css_class_name %>">
2 2  
3   -<div id="article-toolbar"><%= _("This is the revision %s of this content") % @versioned_article.version %></div>
4   -
5   -<% if @page.display_hits? || @page.license.present? %>
6   - <div id='article-sub-header'>
7   - <% if @versioned_article.license.present? %>
8   - <div id="article-license">
9   - <%= _('Licensed under %s') % (@versioned_article.license.url.present? ? link_to(@versioned_article.license.name, @versioned_article.license.url, :target => '_blank') : @versioned_article.license.name) %>
10   - </div>
11   - <% end %>
  3 + <div id="article-header">
  4 + <h1 class='title'><%= @versioned_article.name %></h1>
  5 + <%= _("Version %{version} - %{author} on %{date}") % {:version => @version, :author => @page.author_name(@version), :date => show_time(@versioned_article.updated_at) } %>
12 6 </div>
13   -<% end %>
14 7  
15   -<% cache(@page.cache_key(params, user, language)) do %>
16   - <div class="<%="article-body article-body-" + @page.css_class_name %>">
17   - <% options = @page.image? ? {:gallery_view => true} : {} %>
18   - <%= article_to_html(@page, options) %>
19   - <br style="clear:both" />
20   - </div> <!-- end class="article-body" -->
21   -<% end %>
  8 + <% if @page.version_license(@version).present? %>
  9 + <div id='article-sub-header'>
  10 + <% if @page.version_license(@version).present? %>
  11 + <div id="article-license">
  12 + <%= _('Licensed under %s') % (@page.version_license(@version).url.present? ? link_to(@page.version_license(@version).name, @page.version_license(@version).url, :target => '_blank') : @page.version_license(@version).name) %>
  13 + </div>
  14 + <% end %>
  15 + </div>
  16 + <% end %>
22 17  
  18 + <% cache(@page.cache_key(params, user, language)) do %>
  19 + <div class="<%="article-body article-body-" + @page.css_class_name %>">
  20 + <%= @versioned_article.body %>
  21 + <br style="clear:both" />
  22 + </div> <!-- end class="article-body" -->
  23 + <% end %>
23 24  
24   -<div id="article-versions">
25   - <%= #TODO Review this %>
26   - <%= @page.versions.map {|v| link_to("r#{v.version}", @page.url.merge(:rev => v.version))} %>
27   -</div>
  25 + <div id="article-versions">
  26 + <%= render :partial => 'article_versions' %>
  27 + </div>
28 28  
29   -<%= display_source_info(@versioned_article) %>
  29 + <%= display_source_info(@page) %>
30 30  
31 31 </div><!-- end id="article" -->
32 32 <%= add_zoom_to_article_images %>
... ...
test/unit/article_test.rb
... ... @@ -1693,6 +1693,16 @@ class ArticleTest &lt; ActiveSupport::TestCase
1693 1693 assert_equal license, article.license
1694 1694 end
1695 1695  
  1696 + should 'return license from a specific version' do
  1697 + cc = License.create!(:name => 'CC (by)', :environment => Environment.default)
  1698 + gpl = License.create!(:name => 'GPLv3', :environment => Environment.default)
  1699 + article = Article.create!(:name => 'first version', :profile => profile, :license => cc)
  1700 + article.license = gpl
  1701 + article.save
  1702 + assert_equal cc, article.version_license(1)
  1703 + assert_equal gpl, article.version_license(2)
  1704 + end
  1705 +
1696 1706 should 'update path if parent is changed' do
1697 1707 f1 = Folder.create!(:name => 'Folder 1', :profile => profile)
1698 1708 f2 = Folder.create!(:name => 'Folder 2', :profile => profile)
... ... @@ -1751,6 +1761,28 @@ class ArticleTest &lt; ActiveSupport::TestCase
1751 1761 assert_nil article.author_id
1752 1762 end
1753 1763  
  1764 + should "return the author of a specific version" do
  1765 + author1 = fast_create(Person)
  1766 + author2 = fast_create(Person)
  1767 + article = Article.create!(:name => 'first version', :profile => profile, :last_changed_by => author1)
  1768 + article.name = 'second version'
  1769 + article.last_changed_by = author2
  1770 + article.save
  1771 + assert_equal author1, article.author(1)
  1772 + assert_equal author2, article.author(2)
  1773 + end
  1774 +
  1775 + should "return the author_name of a specific version" do
  1776 + author1 = fast_create(Person)
  1777 + author2 = fast_create(Person)
  1778 + article = Article.create!(:name => 'first version', :profile => profile, :last_changed_by => author1)
  1779 + article.name = 'second version'
  1780 + article.last_changed_by = author2
  1781 + article.save
  1782 + assert_equal author1.name, article.author_name(1)
  1783 + assert_equal author2.name, article.author_name(2)
  1784 + end
  1785 +
1754 1786 should 'identify if belongs to forum' do
1755 1787 p = create_user('user_forum_test').person
1756 1788 forum = fast_create(Forum, :name => 'Forum test', :profile_id => p.id)
... ...