diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index 3125033..a40f6bf 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -144,6 +144,7 @@ class CmsController < MyProfileController end @article.profile = profile + @article.author = user @article.last_changed_by = user translations if @article.translatable? diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index 8f62722..0ea88a9 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -83,7 +83,7 @@ class ContentViewerController < ApplicationController @page.posts end - posts = posts.includes(:parent, {:profile => [:domains, :environment]}) + posts = posts.includes(:parent, {:profile => [:domains, :environment]}, :author) #FIXME Need to run this before the pagination because this version of # will_paginate returns a will_paginate collection instead of a diff --git a/app/helpers/content_viewer_helper.rb b/app/helpers/content_viewer_helper.rb index 1aa06fc..2620b7f 100644 --- a/app/helpers/content_viewer_helper.rb +++ b/app/helpers/content_viewer_helper.rb @@ -26,7 +26,7 @@ module ContentViewerHelper end title << content_tag('span', content_tag('span', show_date(article.published_at), :class => 'date') + - content_tag('span', [_(", by %s") % link_to(article.author_name, article.author_url)], :class => 'author') + + content_tag('span', [_(", by %s") % (article.author ? link_to(article.author_name, article.author_url) : article.author_name)], :class => 'author') + content_tag('span', comments, :class => 'comments'), :class => 'created-at' ) diff --git a/app/models/article.rb b/app/models/article.rb index e183453..a0d4055 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -39,8 +39,8 @@ class Article < ActiveRecord::Base before_save :sanitize_tag_list before_create do |article| - if article.last_changed_by_id - article.author_name = Person.find(article.last_changed_by_id).name + if article.author + article.author_name = article.author.name end end @@ -52,6 +52,7 @@ class Article < ActiveRecord::Base validates_uniqueness_of :slug, :scope => ['profile_id', 'parent_id'], :message => N_('The title (article name) is already being used by another article, please use another title.'), :if => lambda { |article| !article.slug.blank? } + belongs_to :author, :class_name => 'Person' belongs_to :last_changed_by, :class_name => 'Person', :foreign_key => 'last_changed_by_id' has_many :comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc' @@ -449,7 +450,7 @@ class Article < ActiveRecord::Base ['TextArticle', 'TextileArticle', 'TinyMceArticle'] end - named_scope :published, :conditions => { :published => true } + named_scope :published, :conditions => ['articles.published = ?', true] named_scope :folders, lambda {|profile|{:conditions => ['articles.type IN (?)', profile.folder_types] }} named_scope :no_folders, lambda {|profile|{:conditions => ['articles.type NOT IN (?)', profile.folder_types]}} named_scope :galleries, :conditions => [ "articles.type IN ('Gallery')" ] @@ -462,7 +463,7 @@ class Article < ActiveRecord::Base named_scope :more_recent, :order => "created_at DESC" def self.display_filter(user, profile) - return {:conditions => ['published = ?', true]} if !user + return {:conditions => ['articles.published = ?', true]} if !user {:conditions => [" articles.published = ? OR articles.last_changed_by_id = ? OR articles.profile_id = ? OR @@ -621,39 +622,36 @@ class Article < ActiveRecord::Base can_display_versions? && display_versions end - def author(version_number = nil) - if version_number - version = versions.find_by_version(version_number) - author_id = version.last_changed_by_id if version - Person.exists?(author_id) ? Person.find(author_id) : nil - else - if versions.empty? - last_changed_by - else - author_id = versions.first.last_changed_by_id - Person.exists?(author_id) ? Person.find(author_id) : nil - end - end + def get_version(version_number = nil) + version_number ? versions.find(:first, :order => 'version', :offset => version_number - 1) : versions.earliest + end + + def author_by_version(version_number = nil) + version_number ? profile.environment.people.find_by_id(get_version(version_number).last_changed_by_id) : author end def author_name(version_number = nil) - person = author(version_number) - person ? person.name : (setting[:author_name] || _('Unknown')) + person = author_by_version(version_number) + if version_number + person ? person.name : _('Unknown') + else + person ? person.name : (setting[:author_name] || _('Unknown')) + end end def author_url(version_number = nil) - person = author(version_number) + person = author_by_version(version_number) person ? person.url : nil end def author_id(version_number = nil) - person = author(version_number) + person = author_by_version(version_number) person ? person.id : nil end def version_license(version_number = nil) return license if version_number.nil? - profile.environment.licenses.find_by_id(versions.find_by_version(version_number).license_id) + profile.environment.licenses.find_by_id(get_version(version_number).license_id) end alias :active_record_cache_key :cache_key diff --git a/db/migrate/20140709224246_create_real_relation_between_article_and_author.rb b/db/migrate/20140709224246_create_real_relation_between_article_and_author.rb new file mode 100644 index 0000000..d15f9ce --- /dev/null +++ b/db/migrate/20140709224246_create_real_relation_between_article_and_author.rb @@ -0,0 +1,14 @@ +class CreateRealRelationBetweenArticleAndAuthor < ActiveRecord::Migration + def self.up + add_column :articles, :author_id, :integer + add_column :article_versions, :author_id, :integer + + # Set article's author as the first version's last_changed_by_id. + execute "update articles set author_id = (select article_versions.last_changed_by_id from article_versions where article_versions.article_id = articles.id and article_versions.version = 1 limit 1)" + end + + def self.down + remove_column :articles, :author_id + remove_column :article_versions, :author_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 5c97896..17aa804 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20140709212646) do +ActiveRecord::Schema.define(:version => 20140709224246) do create_table "abuse_reports", :force => true do |t| t.integer "reporter_id" @@ -94,6 +94,7 @@ ActiveRecord::Schema.define(:version => 20140709212646) do t.integer "image_id" t.integer "position" t.integer "spam_comments_count", :default => 0 + t.integer "author_id" end add_index "article_versions", ["article_id"], :name => "index_article_versions_on_article_id" @@ -140,6 +141,7 @@ ActiveRecord::Schema.define(:version => 20140709212646) do t.integer "image_id" t.integer "position" t.integer "spam_comments_count", :default => 0 + t.integer "author_id" end add_index "articles", ["comments_count"], :name => "index_articles_on_comments_count" diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index 144100c..d3d1832 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -767,7 +767,7 @@ class ContentViewerControllerTest < ActionController::TestCase c = Community.create!(:name => 'test_com') u = create_user_with_permission('test_user', 'publish_content', c) login_as u.identifier - a = c.articles.create!(:name => 'test-article', :last_changed_by => u, :published => false) + a = c.articles.create!(:name => 'test-article', :author => u, :published => false) get :view_page, :profile => c.identifier, :page => a.explode_path @@ -779,7 +779,7 @@ class ContentViewerControllerTest < ActionController::TestCase c = Community.create!(:name => 'test_com') u = create_user_with_permission('test_user', 'publish_content', c) login_as u.identifier - a = c.articles.create!(:name => 'test-article', :last_changed_by => profile, :published => true) + a = c.articles.create!(:name => 'test-article', :author => profile, :published => true) xhr :get, :view_page, :profile => c.identifier, :page => a.explode_path, :toolbar => true @@ -926,7 +926,7 @@ class ContentViewerControllerTest < ActionController::TestCase community.add_member(author) forum = Forum.create(:profile => community, :name => 'Forum test', :body => 'Forum test') - post = fast_create(TextileArticle, :name => 'First post', :profile_id => community.id, :parent_id => forum.id, :last_changed_by_id => author.id) + post = fast_create(TextileArticle, :name => 'First post', :profile_id => community.id, :parent_id => forum.id, :author_id => author.id) login_as(author.identifier) get :view_page, :profile => community.identifier, :page => post.path.split('/') @@ -942,7 +942,7 @@ class ContentViewerControllerTest < ActionController::TestCase community.add_member(author) forum = Forum.create(:profile => community, :name => 'Forum test', :body => 'Forum test') - post = fast_create(TextileArticle, :name => 'First post', :profile_id => community.id, :parent_id => forum.id, :last_changed_by_id => author.id) + post = fast_create(TextileArticle, :name => 'First post', :profile_id => community.id, :parent_id => forum.id, :author_id => author.id) login_as(author.identifier) get :view_page, :profile => community.identifier, :page => post.path.split('/') diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index d931263..1c940f3 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -802,7 +802,7 @@ class ArticleTest < ActiveSupport::TestCase should 'allow author to edit if is publisher' do c = fast_create(Community) p = create_user_with_permission('test_user', 'publish_content', c) - a = c.articles.create!(:name => 'a test article', :last_changed_by => p) + a = c.articles.create!(:name => 'a test article', :author => p) assert a.allow_post_content?(p) end @@ -1378,7 +1378,7 @@ class ArticleTest < ActiveSupport::TestCase should "the author_name returns the name of the article's author" do author = fast_create(Person) - a = profile.articles.create!(:name => 'a test article', :last_changed_by => author) + a = profile.articles.create!(:name => 'a test article', :author => author) assert_equal author.name, a.author_name author.destroy a.reload @@ -1388,7 +1388,7 @@ class ArticleTest < ActiveSupport::TestCase should 'retrieve latest info from topic when has no comments' do forum = fast_create(Forum, :name => 'Forum test', :profile_id => profile.id) - post = fast_create(TextileArticle, :name => 'First post', :profile_id => profile.id, :parent_id => forum.id, :updated_at => Time.now, :last_changed_by_id => profile.id) + post = fast_create(TextileArticle, :name => 'First post', :profile_id => profile.id, :parent_id => forum.id, :updated_at => Time.now, :author_id => profile.id) assert_equal post.updated_at, post.info_from_last_update[:date] assert_equal profile.name, post.info_from_last_update[:author_name] assert_equal profile.url, post.info_from_last_update[:author_url] @@ -1668,7 +1668,7 @@ class ArticleTest < ActiveSupport::TestCase author = fast_create(Person) community.add_member(author) forum = Forum.create(:profile => community, :name => 'Forum test', :body => 'Forum test') - post = fast_create(TextileArticle, :name => 'First post', :profile_id => community.id, :parent_id => forum.id, :last_changed_by_id => author.id) + post = fast_create(TextileArticle, :name => 'First post', :profile_id => community.id, :parent_id => forum.id, :author_id => author.id) assert post.allow_edit?(author) end @@ -1753,7 +1753,7 @@ class ArticleTest < ActiveSupport::TestCase should 'set author_name before creating article if there is an author' do author = fast_create(Person) - article = Article.create!(:name => 'Test', :profile => profile, :last_changed_by => author) + article = Article.create!(:name => 'Test', :profile => profile, :author => author) assert_equal author.name, article.author_name author_name = author.name @@ -1764,12 +1764,12 @@ class ArticleTest < ActiveSupport::TestCase should "author_id return the author id of the article's author" do author = fast_create(Person) - article = Article.create!(:name => 'Test', :profile => profile, :last_changed_by => author) + article = Article.create!(:name => 'Test', :profile => profile, :author => author) assert_equal author.id, article.author_id end should "author_id return nil if there is no article's author" do - article = Article.create!(:name => 'Test', :profile => profile, :last_changed_by => nil) + article = Article.create!(:name => 'Test', :profile => profile, :author => nil) assert_nil article.author_id end @@ -1780,8 +1780,8 @@ class ArticleTest < ActiveSupport::TestCase article.name = 'second version' article.last_changed_by = author2 article.save - assert_equal author1, article.author(1) - assert_equal author2, article.author(2) + assert_equal author1, article.author_by_version(1) + assert_equal author2, article.author_by_version(2) end should "return the author_name of a specific version" do @@ -1837,4 +1837,33 @@ class ArticleTest < ActiveSupport::TestCase assert_equivalent [c3], Article.with_types(['Event']) end + should 'get specific version' do + article = Article.create!(:name => 'first version', :profile => profile) + article.name = 'second version' + article.save! + article.name = 'third version' + article.save! + + assert_equal 'first version', article.get_version(1).name + assert_equal 'second version', article.get_version(2).name + assert_equal 'third version', article.get_version(3).name + end + + should 'get author by version' do + p1 = fast_create(Person) + p2 = fast_create(Person) + p3 = fast_create(Person) + article = Article.create!(:name => 'first version', :profile => profile, :last_changed_by => p1) + article.name = 'second version' + article.last_changed_by = p2 + article.save! + article.last_changed_by = p3 + article.name = 'third version' + article.save! + + assert_equal p1, article.author_by_version(1) + assert_equal p2, article.author_by_version(2) + assert_equal p3, article.author_by_version(3) + end + end -- libgit2 0.21.2