diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index f065f22..6762255 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -119,6 +119,7 @@ class CmsController < MyProfileController translations if @article.translatable? @article.profile = profile + @article.author = user @article.last_changed_by = user continue = params[:continue] diff --git a/app/models/article.rb b/app/models/article.rb index a99defd..018dcaa 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -13,6 +13,12 @@ class Article < ActiveRecord::Base # xss_terminate plugin can't sanitize array fields before_save :sanitize_tag_list + before_save do |article| + if article.author_id + article.author_name = Person.find(article.author_id).name + end + end + belongs_to :profile validates_presence_of :profile_id, :name validates_presence_of :slug, :path, :if => lambda { |article| !article.name.blank? } @@ -20,6 +26,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 :last_changed_by, :class_name => 'Person', :foreign_key => 'last_changed_by_id' + belongs_to :author, :class_name => 'Person', :foreign_key => 'author_id' has_many :comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc' @@ -278,7 +285,7 @@ class Article < ActiveRecord::Base if last_comment {:date => last_comment.created_at, :author_name => last_comment.author_name, :author_url => last_comment.author_url} else - {:date => updated_at, :author_name => author.name, :author_url => author.url} + {:date => updated_at, :author_name => author_name, :author_url => author ? author.url : nil} end end @@ -430,7 +437,7 @@ class Article < ActiveRecord::Base end def allow_post_content?(user = nil) - user && (user.has_permission?('post_content', profile) || allow_publish_content?(user) && (user == self.creator)) + user && (user.has_permission?('post_content', profile) || allow_publish_content?(user) && (user == author)) end def allow_publish_content?(user = nil) @@ -527,16 +534,8 @@ class Article < ActiveRecord::Base false end - def author - if reference_article - reference_article.author - else - last_changed_by || profile - end - end - def author_name - setting[:author_name].blank? ? author.name : setting[:author_name] + author ? author.name : setting[:author_name] end alias :active_record_cache_key :cache_key @@ -561,11 +560,6 @@ class Article < ActiveRecord::Base truncate sanitize_html(self.lead), :length => 170, :omission => '...' end - def creator - creator_id = versions[0][:last_changed_by_id] - creator_id && Profile.find(creator_id) - end - def notifiable? false end diff --git a/app/models/person.rb b/app/models/person.rb index 378ad3a..e01c3e4 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -54,6 +54,8 @@ class Person < Profile has_many :scraps_sent, :class_name => 'Scrap', :foreign_key => :sender_id, :dependent => :destroy + has_many :creations, :class_name => 'Article', :foreign_key => :author_id + named_scope :more_popular, :select => "#{Profile.qualified_column_names}, count(friend_id) as total", :group => Profile.qualified_column_names, diff --git a/db/migrate/20121024220349_add_author_id_to_article.rb b/db/migrate/20121024220349_add_author_id_to_article.rb new file mode 100644 index 0000000..ac10d9a --- /dev/null +++ b/db/migrate/20121024220349_add_author_id_to_article.rb @@ -0,0 +1,11 @@ +class AddAuthorIdToArticle < ActiveRecord::Migration + def self.up + add_column :articles, :author_id, :integer + add_column :article_versions, :author_id, :integer + end + + def self.down + remove_column :articles, :author_id + remove_column :article_versions, :author_id, :integer + end +end diff --git a/db/migrate/20121024222938_set_authors_based_on_article_versions.rb b/db/migrate/20121024222938_set_authors_based_on_article_versions.rb new file mode 100644 index 0000000..e63100d --- /dev/null +++ b/db/migrate/20121024222938_set_authors_based_on_article_versions.rb @@ -0,0 +1,9 @@ +class SetAuthorsBasedOnArticleVersions < ActiveRecord::Migration + def self.up + update('UPDATE articles SET author_id = (SELECT profiles.id FROM articles as a INNER JOIN article_versions ON a.id = article_versions.article_id INNER JOIN profiles ON profiles.id = article_versions.last_changed_by_id WHERE article_versions.version = 1 AND articles.id = a.id)') + end + + def self.down + say "Can not be revesed!" + end +end diff --git a/db/schema.rb b/db/schema.rb index ba8d295..439e828 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 => 20120825185219) do +ActiveRecord::Schema.define(:version => 20121024222938) do create_table "abuse_reports", :force => true do |t| t.integer "reporter_id" @@ -86,6 +86,7 @@ ActiveRecord::Schema.define(:version => 20120825185219) do t.string "language" t.string "source_name" t.integer "license_id" + t.integer "author_id" end create_table "articles", :force => true do |t| @@ -127,6 +128,7 @@ ActiveRecord::Schema.define(:version => 20120825185219) do t.string "language" t.string "source_name" t.integer "license_id" + t.integer "author_id" end add_index "articles", ["translation_of_id"], :name => "index_articles_on_translation_of_id" diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index 6b5aec2..19aa4e5 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -1556,6 +1556,16 @@ class CmsControllerTest < ActionController::TestCase assert_equal license, article.license end + should 'set author when creating article' do + login_as(profile.identifier) + + post :new, :type => 'TinyMceArticle', :profile => profile.identifier, :article => { :name => 'Sample Article', :body => 'content ...' } + + a = profile.articles.find_by_path('sample-article') + assert_not_nil a + assert_equal profile, a.author + end + protected # FIXME this is to avoid adding an extra dependency for a proper JSON parser. diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 60eb4a9..1f810dc 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -708,14 +708,6 @@ class ArticleTest < ActiveSupport::TestCase assert_equal a.url, a.view_url end - should 'know its author' do - assert_equal profile, Article.new(:last_changed_by => profile).author - end - - should 'use owning profile as author when we dont know who did the last change' do - assert_equal profile, Article.new(:last_changed_by => nil, :profile => profile).author - end - should 'have published_at' do assert_respond_to Article.new, :published_at end @@ -1372,7 +1364,7 @@ class ArticleTest < ActiveSupport::TestCase assert Article.method_defined?('author_name') end - should "the author_name returns the name od the article's author" do + should "the author_name returns the name of the article's author" do author = mock() author.expects(:name).returns('author name') a = Article.new @@ -1782,4 +1774,25 @@ class ArticleTest < ActiveSupport::TestCase assert_equal license, article.license end + should 'be able to have an author' do + author = fast_create(Person) + article = Article.new + assert_nothing_raised do + article.author = author + end + end + + should 'set author_name before saving article if there is an author' do + author = fast_create(Person) + article = fast_create(Article, :profile_id => fast_create(Profile).id) + article.author = author + article.save! + assert_equal author.name, article.author_name + + author_name = author.name + author.destroy + article.reload + assert_equal author_name, article.author_name + end + end diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index af7442a..9963732 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -1262,4 +1262,15 @@ class PersonTest < ActiveSupport::TestCase assert person.has_permission?('bli', Profile.new) end + + should 'have creations' do + person = create_user('some-user').person + a1 = fast_create(Article, :author_id => person.id) + a2 = fast_create(Article, :author_id => person.id) + a3 = fast_create(Article) + + assert_includes person.creations, a1 + assert_includes person.creations, a2 + assert_not_includes person.creations, a3 + end end -- libgit2 0.21.2