Commit 35a1c4f5ccda3045955337407afe0bef3a94e5cd

Authored by MoisesMachado
1 parent 89736c33

ActionItem234: users can publish theirs artcles to its communities

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2452 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/my_profile/cms_controller.rb
... ... @@ -130,6 +130,23 @@ class CmsController < MyProfileController
130 130 render :partial => 'shared/select_categories', :locals => {:object_name => 'article', :multiple => true}, :layout => false
131 131 end
132 132  
  133 + def publish
  134 + @article = profile.articles.find(params[:id])
  135 + @groups = profile.memberships - [profile]
  136 + @marked_groups = []
  137 + groups_ids = profile.memberships.map{|m|m.id.to_s}
  138 + @marked_groups = params[:marked_groups].map do |item|
  139 + if groups_ids.include?(item[:group_id])
  140 + item.merge :group => Profile.find(item.delete(:group_id))
  141 + end
  142 + end.compact unless params[:marked_groups].nil?
  143 + if request.post?
  144 + @marked_groups.each do |item|
  145 + PublishedArticle.create!(:reference_article => @article, :profile => item[:group], :name => item[:name])
  146 + end
  147 + end
  148 + end
  149 +
133 150 protected
134 151  
135 152 def redirect_back
... ...
app/models/published_article.rb 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +class PublishedArticle < Article
  2 + belongs_to :reference_article, :class_name => "Article"
  3 +
  4 + def self.short_description
  5 + _('Reference to other article')
  6 + end
  7 +
  8 + def self.description
  9 + _('A reference to another article published in another profile')
  10 + end
  11 +
  12 + def body
  13 + reference_article.body
  14 + end
  15 +
  16 + before_validation_on_create :update_name
  17 + def update_name
  18 + self.name ||= self.reference_article.name
  19 + end
  20 +end
... ...
app/views/content_viewer/view_page.rhtml
... ... @@ -35,6 +35,9 @@
35 35 <%= link_to content_tag( 'span', _('Edit') ),
36 36 { :controller => 'cms', :action => 'edit', :id => @page },
37 37 :class => 'button with-text icon-edit' %>
  38 + <%= link_to content_tag( 'span', _('Publish') ),
  39 + { :controller => 'cms', :action => 'publish', :id => @page },
  40 + :class => 'button with-text icon-edit' %>
38 41 <%= lightbox_button(:new, _('New publication'), :controller => 'cms', :action => 'new', :parent_id => (@page.folder? ? @page : nil)) %>
39 42 </div>
40 43 <% end %>
... ...
db/migrate/054_add_reference_article.rb 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +class AddReferenceArticle < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :articles, :reference_article_id, :integer
  4 + add_column :article_versions, :reference_article_id, :integer
  5 + end
  6 +
  7 + def self.down
  8 + remove_column :articles, :reference_article_id
  9 + remove_column :article_versions, :reference_article_id
  10 + end
  11 +end
... ...
db/schema.rb
... ... @@ -9,14 +9,14 @@
9 9 #
10 10 # It's strongly recommended to check this file into your version control system.
11 11  
12   -ActiveRecord::Schema.define(:version => 53) do
  12 +ActiveRecord::Schema.define(:version => 54) do
13 13  
14 14 create_table "article_versions", :force => true do |t|
15 15 t.integer "article_id"
16 16 t.integer "version"
17 17 t.string "name"
18 18 t.string "slug"
19   - t.text "path", :default => ""
  19 + t.text "path", :default => ""
20 20 t.integer "parent_id"
21 21 t.text "body"
22 22 t.text "abstract"
... ... @@ -31,19 +31,20 @@ ActiveRecord::Schema.define(:version =&gt; 53) do
31 31 t.integer "width"
32 32 t.string "versioned_type"
33 33 t.integer "comments_count"
34   - t.boolean "advertise", :default => true
35   - t.boolean "published", :default => true
  34 + t.boolean "advertise", :default => true
  35 + t.boolean "published", :default => true
36 36 t.date "start_date"
37 37 t.date "end_date"
38   - t.integer "children_count", :default => 0
39   - t.boolean "public_article", :default => true
40   - t.boolean "accept_comments", :default => true
  38 + t.integer "children_count", :default => 0
  39 + t.boolean "public_article", :default => true
  40 + t.boolean "accept_comments", :default => true
  41 + t.integer "reference_article_id"
41 42 end
42 43  
43 44 create_table "articles", :force => true do |t|
44 45 t.string "name"
45 46 t.string "slug"
46   - t.text "path", :default => ""
  47 + t.text "path", :default => ""
47 48 t.integer "parent_id"
48 49 t.text "body"
49 50 t.text "abstract"
... ... @@ -59,14 +60,15 @@ ActiveRecord::Schema.define(:version =&gt; 53) do
59 60 t.string "filename"
60 61 t.integer "height"
61 62 t.integer "width"
62   - t.integer "comments_count", :default => 0
63   - t.boolean "advertise", :default => true
64   - t.boolean "published", :default => true
  63 + t.integer "comments_count", :default => 0
  64 + t.boolean "advertise", :default => true
  65 + t.boolean "published", :default => true
65 66 t.date "start_date"
66 67 t.date "end_date"
67   - t.integer "children_count", :default => 0
68   - t.boolean "public_article", :default => true
69   - t.boolean "accept_comments", :default => true
  68 + t.integer "children_count", :default => 0
  69 + t.boolean "public_article", :default => true
  70 + t.boolean "accept_comments", :default => true
  71 + t.integer "reference_article_id"
70 72 end
71 73  
72 74 create_table "articles_categories", :id => false, :force => true do |t|
... ...
test/functional/cms_controller_test.rb
... ... @@ -554,4 +554,26 @@ class CmsControllerTest &lt; Test::Unit::TestCase
554 554 assert !folder.children[0].public?
555 555 end
556 556  
  557 + should 'load communities for that the user belongs' do
  558 + c = Community.create!(:name => 'test comm', :identifier => 'test_comm')
  559 + c.affiliate(profile, Profile::Roles.all_roles)
  560 + a = profile.articles.create!(:name => 'something intresting', :body => 'ruby on rails')
  561 +
  562 + get :publish, :profile => profile.identifier, :id => a.id
  563 +
  564 + assert_equal [c], assigns(:groups)
  565 + assert_template 'publish'
  566 + end
  567 +
  568 + should 'publish the article in the selected community' do
  569 + c = Community.create!(:name => 'test comm', :identifier => 'test_comm')
  570 + c.affiliate(profile, Profile::Roles.all_roles)
  571 + a = profile.articles.create!(:name => 'something intresting', :body => 'ruby on rails')
  572 +
  573 + assert_difference PublishedArticle, :count do
  574 + post :publish, :profile => profile.identifier, :id => a.id, :marked_groups => [{:name => 'bli', :group_id => c.id.to_s}]
  575 + assert_equal [{'group' => c, 'name' => 'bli'}], assigns(:marked_groups)
  576 + end
  577 + end
  578 +
557 579 end
... ...
test/functional/content_viewer_controller_test.rb
... ... @@ -404,4 +404,13 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
404 404 end
405 405 end
406 406  
  407 + should 'show link to publication on view' do
  408 + page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text')
  409 + login_as(profile.identifier)
  410 +
  411 + get :view_page, :profile => profile.identifier, :page => ['myarticle']
  412 +
  413 + assert_tag :tag => 'a', :attributes => {:href => ('/myprofile/' + profile.identifier + '/cms/publish/' + page.id.to_s)}
  414 + end
  415 +
407 416 end
... ...
test/unit/published_article_test.rb 0 → 100644
... ... @@ -0,0 +1,42 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class PublishedArticleTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + @profile = create_user('test_user').person
  7 + @article = @profile.articles.create!(:name => 'test_article', :body => 'some trivial body')
  8 + end
  9 +
  10 +
  11 + should 'have a reference article and profile' do
  12 + prof = Community.create!(:name => 'test_comm', :identifier => 'test_comm')
  13 + p = PublishedArticle.create(:reference_article => @article, :profile => prof)
  14 +
  15 + assert p
  16 + assert_equal prof, p.profile
  17 + assert_equal @article, p.reference_article
  18 + end
  19 +
  20 + should 'have same content as reference article' do
  21 + prof = Community.create!(:name => 'test_comm', :identifier => 'test_comm')
  22 + p = PublishedArticle.create(:reference_article => @article, :profile => prof)
  23 +
  24 + assert_equal @article.body, p.body
  25 + end
  26 +
  27 + should 'have a different name than reference article' do
  28 + prof = Community.create!(:name => 'test_comm', :identifier => 'test_comm')
  29 + p = PublishedArticle.create(:reference_article => @article, :profile => prof, :name => 'other title')
  30 +
  31 + assert_equal 'other title', p.name
  32 + assert_not_equal @article.name, p.name
  33 +
  34 + end
  35 +
  36 + should 'use name of reference article a default name' do
  37 + prof = Community.create!(:name => 'test_comm', :identifier => 'test_comm')
  38 + p = PublishedArticle.create!(:reference_article => @article, :profile => prof)
  39 +
  40 + assert_equal @article.name, p.name
  41 + end
  42 +end
... ...