From 622e2512484398b8879f3ef8aa1903a5a3b6fc8b Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Thu, 27 Dec 2012 16:49:56 -0300 Subject: [PATCH] creating author_id method in article class to return the author id when the author exists --- app/models/approve_article.rb | 2 +- app/models/article.rb | 4 ++++ test/unit/article_test.rb | 11 +++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/models/approve_article.rb b/app/models/approve_article.rb index 45f9eeb..7f7fa4a 100644 --- a/app/models/approve_article.rb +++ b/app/models/approve_article.rb @@ -48,7 +48,7 @@ class ApproveArticle < Task end def perform - article.copy!(:name => name, :abstract => abstract, :body => body, :profile => target, :reference_article => article, :parent => article_parent, :highlighted => highlighted, :source => article.source, :last_changed_by_id => article.author.id) + article.copy!(:name => name, :abstract => abstract, :body => body, :profile => target, :reference_article => article, :parent => article_parent, :highlighted => highlighted, :source => article.source, :last_changed_by_id => article.author_id) end def title diff --git a/app/models/article.rb b/app/models/article.rb index 2292602..57d3781 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -562,6 +562,10 @@ class Article < ActiveRecord::Base author ? author.url : nil end + def author_id + author ? author.id : nil + end + alias :active_record_cache_key :cache_key def cache_key(params = {}, the_profile = nil, language = 'en') active_record_cache_key+'-'+language + diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index ac7f39b..053c31f 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -1822,4 +1822,15 @@ class ArticleTest < ActiveSupport::TestCase assert_equal author_name, article.author_name end + 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) + 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) + assert_nil article.author_id + end + end -- libgit2 0.21.2