From 35a1c4f5ccda3045955337407afe0bef3a94e5cd Mon Sep 17 00:00:00 2001 From: MoisesMachado Date: Thu, 4 Sep 2008 13:57:55 +0000 Subject: [PATCH] ActionItem234: users can publish theirs artcles to its communities --- app/controllers/my_profile/cms_controller.rb | 17 +++++++++++++++++ app/models/published_article.rb | 20 ++++++++++++++++++++ app/views/content_viewer/view_page.rhtml | 3 +++ db/migrate/054_add_reference_article.rb | 11 +++++++++++ db/schema.rb | 30 ++++++++++++++++-------------- test/functional/cms_controller_test.rb | 22 ++++++++++++++++++++++ test/functional/content_viewer_controller_test.rb | 9 +++++++++ test/unit/published_article_test.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 140 insertions(+), 14 deletions(-) create mode 100644 app/models/published_article.rb create mode 100644 db/migrate/054_add_reference_article.rb create mode 100644 test/unit/published_article_test.rb diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index c94ebad..9988217 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -130,6 +130,23 @@ class CmsController < MyProfileController render :partial => 'shared/select_categories', :locals => {:object_name => 'article', :multiple => true}, :layout => false end + def publish + @article = profile.articles.find(params[:id]) + @groups = profile.memberships - [profile] + @marked_groups = [] + groups_ids = profile.memberships.map{|m|m.id.to_s} + @marked_groups = params[:marked_groups].map do |item| + if groups_ids.include?(item[:group_id]) + item.merge :group => Profile.find(item.delete(:group_id)) + end + end.compact unless params[:marked_groups].nil? + if request.post? + @marked_groups.each do |item| + PublishedArticle.create!(:reference_article => @article, :profile => item[:group], :name => item[:name]) + end + end + end + protected def redirect_back diff --git a/app/models/published_article.rb b/app/models/published_article.rb new file mode 100644 index 0000000..3363c82 --- /dev/null +++ b/app/models/published_article.rb @@ -0,0 +1,20 @@ +class PublishedArticle < Article + belongs_to :reference_article, :class_name => "Article" + + def self.short_description + _('Reference to other article') + end + + def self.description + _('A reference to another article published in another profile') + end + + def body + reference_article.body + end + + before_validation_on_create :update_name + def update_name + self.name ||= self.reference_article.name + end +end diff --git a/app/views/content_viewer/view_page.rhtml b/app/views/content_viewer/view_page.rhtml index e5f7fcd..3bd4d14 100644 --- a/app/views/content_viewer/view_page.rhtml +++ b/app/views/content_viewer/view_page.rhtml @@ -35,6 +35,9 @@ <%= link_to content_tag( 'span', _('Edit') ), { :controller => 'cms', :action => 'edit', :id => @page }, :class => 'button with-text icon-edit' %> + <%= link_to content_tag( 'span', _('Publish') ), + { :controller => 'cms', :action => 'publish', :id => @page }, + :class => 'button with-text icon-edit' %> <%= lightbox_button(:new, _('New publication'), :controller => 'cms', :action => 'new', :parent_id => (@page.folder? ? @page : nil)) %> <% end %> diff --git a/db/migrate/054_add_reference_article.rb b/db/migrate/054_add_reference_article.rb new file mode 100644 index 0000000..beb6208 --- /dev/null +++ b/db/migrate/054_add_reference_article.rb @@ -0,0 +1,11 @@ +class AddReferenceArticle < ActiveRecord::Migration + def self.up + add_column :articles, :reference_article_id, :integer + add_column :article_versions, :reference_article_id, :integer + end + + def self.down + remove_column :articles, :reference_article_id + remove_column :article_versions, :reference_article_id + end +end diff --git a/db/schema.rb b/db/schema.rb index cbe3725..e9cd418 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,14 +9,14 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 53) do +ActiveRecord::Schema.define(:version => 54) do create_table "article_versions", :force => true do |t| t.integer "article_id" t.integer "version" t.string "name" t.string "slug" - t.text "path", :default => "" + t.text "path", :default => "" t.integer "parent_id" t.text "body" t.text "abstract" @@ -31,19 +31,20 @@ ActiveRecord::Schema.define(:version => 53) do t.integer "width" t.string "versioned_type" t.integer "comments_count" - t.boolean "advertise", :default => true - t.boolean "published", :default => true + t.boolean "advertise", :default => true + t.boolean "published", :default => true t.date "start_date" t.date "end_date" - t.integer "children_count", :default => 0 - t.boolean "public_article", :default => true - t.boolean "accept_comments", :default => true + t.integer "children_count", :default => 0 + t.boolean "public_article", :default => true + t.boolean "accept_comments", :default => true + t.integer "reference_article_id" end create_table "articles", :force => true do |t| t.string "name" t.string "slug" - t.text "path", :default => "" + t.text "path", :default => "" t.integer "parent_id" t.text "body" t.text "abstract" @@ -59,14 +60,15 @@ ActiveRecord::Schema.define(:version => 53) do t.string "filename" t.integer "height" t.integer "width" - t.integer "comments_count", :default => 0 - t.boolean "advertise", :default => true - t.boolean "published", :default => true + t.integer "comments_count", :default => 0 + t.boolean "advertise", :default => true + t.boolean "published", :default => true t.date "start_date" t.date "end_date" - t.integer "children_count", :default => 0 - t.boolean "public_article", :default => true - t.boolean "accept_comments", :default => true + t.integer "children_count", :default => 0 + t.boolean "public_article", :default => true + t.boolean "accept_comments", :default => true + t.integer "reference_article_id" end create_table "articles_categories", :id => false, :force => true do |t| diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index 6e262e6..c9171fe 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -554,4 +554,26 @@ class CmsControllerTest < Test::Unit::TestCase assert !folder.children[0].public? end + should 'load communities for that the user belongs' do + c = Community.create!(:name => 'test comm', :identifier => 'test_comm') + c.affiliate(profile, Profile::Roles.all_roles) + a = profile.articles.create!(:name => 'something intresting', :body => 'ruby on rails') + + get :publish, :profile => profile.identifier, :id => a.id + + assert_equal [c], assigns(:groups) + assert_template 'publish' + end + + should 'publish the article in the selected community' do + c = Community.create!(:name => 'test comm', :identifier => 'test_comm') + c.affiliate(profile, Profile::Roles.all_roles) + a = profile.articles.create!(:name => 'something intresting', :body => 'ruby on rails') + + assert_difference PublishedArticle, :count do + post :publish, :profile => profile.identifier, :id => a.id, :marked_groups => [{:name => 'bli', :group_id => c.id.to_s}] + assert_equal [{'group' => c, 'name' => 'bli'}], assigns(:marked_groups) + end + end + end diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index defa90a..ac13a8d 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -404,4 +404,13 @@ class ContentViewerControllerTest < Test::Unit::TestCase end end + should 'show link to publication on view' do + page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text') + login_as(profile.identifier) + + get :view_page, :profile => profile.identifier, :page => ['myarticle'] + + assert_tag :tag => 'a', :attributes => {:href => ('/myprofile/' + profile.identifier + '/cms/publish/' + page.id.to_s)} + end + end diff --git a/test/unit/published_article_test.rb b/test/unit/published_article_test.rb new file mode 100644 index 0000000..474a060 --- /dev/null +++ b/test/unit/published_article_test.rb @@ -0,0 +1,42 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class PublishedArticleTest < ActiveSupport::TestCase + + def setup + @profile = create_user('test_user').person + @article = @profile.articles.create!(:name => 'test_article', :body => 'some trivial body') + end + + + should 'have a reference article and profile' do + prof = Community.create!(:name => 'test_comm', :identifier => 'test_comm') + p = PublishedArticle.create(:reference_article => @article, :profile => prof) + + assert p + assert_equal prof, p.profile + assert_equal @article, p.reference_article + end + + should 'have same content as reference article' do + prof = Community.create!(:name => 'test_comm', :identifier => 'test_comm') + p = PublishedArticle.create(:reference_article => @article, :profile => prof) + + assert_equal @article.body, p.body + end + + should 'have a different name than reference article' do + prof = Community.create!(:name => 'test_comm', :identifier => 'test_comm') + p = PublishedArticle.create(:reference_article => @article, :profile => prof, :name => 'other title') + + assert_equal 'other title', p.name + assert_not_equal @article.name, p.name + + end + + should 'use name of reference article a default name' do + prof = Community.create!(:name => 'test_comm', :identifier => 'test_comm') + p = PublishedArticle.create!(:reference_article => @article, :profile => prof) + + assert_equal @article.name, p.name + end +end -- libgit2 0.21.2