Commit b51a1f3a702db848836999ad00dfc5a0e8eddee6

Authored by Victor Costa
1 parent 094382c6

Allow proposals to be moderated

controllers/myprofile/proposals_discussion_plugin_myprofile_controller.rb
1 class ProposalsDiscussionPluginMyprofileController < MyProfileController 1 class ProposalsDiscussionPluginMyprofileController < MyProfileController
2 2
3 before_filter :check_edit_permission_to_proposal, :only => :publish_proposal 3 before_filter :check_edit_permission_to_proposal, :only => :publish_proposal
4 - before_filter :set_discussion, :only => [:select_topic, :new_proposal] 4 + before_filter :set_discussion, :only => [:select_topic, :new_proposal_with_topic, :new_proposal]
5 5
6 def select_topic 6 def select_topic
7 end 7 end
8 8
9 - def new_proposal  
10 - if params[:discussion].blank? || params[:discussion][:topic].blank? 9 + def new_proposal_with_topic
  10 + if params[:parent_id].blank?
11 session[:notice] = _('Please select a topic') 11 session[:notice] = _('Please select a topic')
12 render :file => 'proposals_discussion_plugin_myprofile/select_topic' 12 render :file => 'proposals_discussion_plugin_myprofile/select_topic'
13 else 13 else
14 - redirect_to :controller => 'cms', :action => 'new', :type => "ProposalsDiscussionPlugin::Proposal", :parent_id => params[:discussion][:topic] 14 + new_proposal
  15 + end
  16 + end
  17 +
  18 + def new_proposal
  19 + if @discussion.allow_create?(current_person)
  20 + redirect_to :controller => 'cms', :action => 'new', :type => "ProposalsDiscussionPlugin::Proposal", :parent_id => params[:parent_id]
  21 + else
  22 + render 'proposals_discussion_plugin_myprofile/suggest_proposal'
15 end 23 end
16 end 24 end
17 25
@@ -32,7 +40,7 @@ class ProposalsDiscussionPluginMyprofileController &lt; MyProfileController @@ -32,7 +40,7 @@ class ProposalsDiscussionPluginMyprofileController &lt; MyProfileController
32 end 40 end
33 41
34 def set_discussion 42 def set_discussion
35 - @discussion = profile.articles.find(params[:parent_id]) 43 + @discussion = profile.articles.find(params[:discussion_id]) if params[:discussion_id].present?
36 end 44 end
37 45
38 end 46 end
controllers/profile/proposals_discussion_plugin_profile_controller.rb
@@ -10,7 +10,7 @@ class ProposalsDiscussionPluginProfileController &lt; ProfileController @@ -10,7 +10,7 @@ class ProposalsDiscussionPluginProfileController &lt; ProfileController
10 10
11 def check_access_to_profile 11 def check_access_to_profile
12 @target = profile.articles.find(params[:article_id]) 12 @target = profile.articles.find(params[:article_id])
13 - render_access_denied(_('You are not allowed to export data from this article')) unless @target.allow_create?(user) 13 + render_access_denied(_('You are not allowed to export data from this article')) unless @target.allow_post_content?(user)
14 end 14 end
15 15
16 end 16 end
lib/proposals_discussion_plugin/discussion.rb
@@ -18,8 +18,9 @@ class ProposalsDiscussionPlugin::Discussion &lt; ProposalsDiscussionPlugin::Proposa @@ -18,8 +18,9 @@ class ProposalsDiscussionPlugin::Discussion &lt; ProposalsDiscussionPlugin::Proposa
18 settings_items :custom_body_label, :type => :string, :default => _('Body') 18 settings_items :custom_body_label, :type => :string, :default => _('Body')
19 settings_items :allow_topics, :type => :boolean, :default => true 19 settings_items :allow_topics, :type => :boolean, :default => true
20 settings_items :phase, :type => :string, :default => :proposals 20 settings_items :phase, :type => :string, :default => :proposals
  21 + settings_items :moderate_proposals, :type => :boolean, :default => false
21 22
22 - attr_accessible :custom_body_label, :allow_topics, :phase 23 + attr_accessible :custom_body_label, :allow_topics, :phase, :moderate_proposals
23 24
24 AVAILABLE_PHASES = {:proposals => _('Proposals'), :vote => 'Vote', :finish => 'Announcement'} 25 AVAILABLE_PHASES = {:proposals => _('Proposals'), :vote => 'Vote', :finish => 'Announcement'}
25 26
@@ -39,6 +40,10 @@ class ProposalsDiscussionPlugin::Discussion &lt; ProposalsDiscussionPlugin::Proposa @@ -39,6 +40,10 @@ class ProposalsDiscussionPlugin::Discussion &lt; ProposalsDiscussionPlugin::Proposa
39 phase.to_sym == :proposals 40 phase.to_sym == :proposals
40 end 41 end
41 42
  43 + def allow_create?(person)
  44 + !moderate_proposals || super
  45 + end
  46 +
42 def to_html(options = {}) 47 def to_html(options = {})
43 discussion = self 48 discussion = self
44 proc do 49 proc do
lib/proposals_discussion_plugin/discussion_helper.rb
@@ -3,7 +3,7 @@ module ProposalsDiscussionPlugin::DiscussionHelper @@ -3,7 +3,7 @@ module ProposalsDiscussionPlugin::DiscussionHelper
3 def link_to_new_proposal(discussion) 3 def link_to_new_proposal(discussion)
4 return '' unless discussion.allow_new_proposals? 4 return '' unless discussion.allow_new_proposals?
5 5
6 - url = {:parent_id => discussion.id, :profile => discussion.profile.identifier} 6 + url = {:discussion_id => discussion.id, :profile => discussion.profile.identifier}
7 if discussion.allow_topics 7 if discussion.allow_topics
8 url.merge!(:controller => 'proposals_discussion_plugin_myprofile', :action => 'select_topic') 8 url.merge!(:controller => 'proposals_discussion_plugin_myprofile', :action => 'select_topic')
9 else 9 else
lib/proposals_discussion_plugin/topic.rb
@@ -26,7 +26,7 @@ class ProposalsDiscussionPlugin::Topic &lt; ProposalsDiscussionPlugin::ProposalsHo @@ -26,7 +26,7 @@ class ProposalsDiscussionPlugin::Topic &lt; ProposalsDiscussionPlugin::ProposalsHo
26 end 26 end
27 27
28 def allow_create?(user) 28 def allow_create?(user)
29 - true 29 + !discussion.moderate_proposals
30 end 30 end
31 31
32 end 32 end
test/functional/cms_controller_test.rb
@@ -35,4 +35,9 @@ class CmsControllerTest &lt; ActionController::TestCase @@ -35,4 +35,9 @@ class CmsControllerTest &lt; ActionController::TestCase
35 assert_tag :tag => 'select', :attributes => {:id => 'article_phase'} 35 assert_tag :tag => 'select', :attributes => {:id => 'article_phase'}
36 end 36 end
37 37
  38 + should 'display moderate proposals config option' do
  39 + get :edit, :id => discussion.id, :profile => profile.identifier
  40 + assert_select '#article_moderate_proposals'
  41 + end
  42 +
38 end 43 end
test/functional/proposals_discussion_plugin_myprofile_controller_test.rb
@@ -14,17 +14,17 @@ class ProposalsDiscussionPluginMyprofileControllerTest &lt; ActionController::TestC @@ -14,17 +14,17 @@ class ProposalsDiscussionPluginMyprofileControllerTest &lt; ActionController::TestC
14 14
15 should 'list topics for selection' do 15 should 'list topics for selection' do
16 3.times {fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => discussion.id, :profile_id => profile.id)} 16 3.times {fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => discussion.id, :profile_id => profile.id)}
17 - get :select_topic, :profile => profile.identifier, :parent_id => discussion.id 17 + get :select_topic, :profile => profile.identifier, :discussion_id => discussion.id
18 assert_equal discussion, assigns(:discussion) 18 assert_equal discussion, assigns(:discussion)
19 assert_select 'div#topics' do 19 assert_select 'div#topics' do
20 assert_select 'div.content', discussion.topics.count 20 assert_select 'div.content', discussion.topics.count
21 - assert_select "input[name='discussion[topic]']", discussion.topics.count 21 + assert_select "input[name='parent_id']", discussion.topics.count
22 end 22 end
23 - assert_tag :form, :attributes => {:action => "/myprofile/#{profile.identifier}/plugin/proposals_discussion/myprofile/new_proposal"} 23 + assert_tag :form, :attributes => {:action => "/myprofile/#{profile.identifier}/plugin/proposals_discussion/myprofile/new_proposal_with_topic?discussion_id=#{discussion.id}"}
24 end 24 end
25 25
26 should 'new_proposal redirect to cms controller' do 26 should 'new_proposal redirect to cms controller' do
27 - get :new_proposal, :profile => profile.identifier, :discussion => {:topic => topic.id}, :parent_id => discussion.id 27 + get :new_proposal, :profile => profile.identifier,:parent_id => topic.id, :discussion_id => discussion.id
28 assert_redirected_to :controller => 'cms', :action => 'new', :type => "ProposalsDiscussionPlugin::Proposal", :parent_id => topic.id 28 assert_redirected_to :controller => 'cms', :action => 'new', :type => "ProposalsDiscussionPlugin::Proposal", :parent_id => topic.id
29 end 29 end
30 30
@@ -41,8 +41,8 @@ class ProposalsDiscussionPluginMyprofileControllerTest &lt; ActionController::TestC @@ -41,8 +41,8 @@ class ProposalsDiscussionPluginMyprofileControllerTest &lt; ActionController::TestC
41 assert !proposal.reload.published 41 assert !proposal.reload.published
42 end 42 end
43 43
44 - should 'new_proposal without a topic redirect to back' do  
45 - get :new_proposal, :profile => profile.identifier, :parent_id => discussion.id 44 + should 'new_proposal without a topic redirect to select_topic' do
  45 + get :new_proposal_with_topic, :profile => profile.identifier, :discussion_id => discussion.id
46 assert_template 'select_topic' 46 assert_template 'select_topic'
47 end 47 end
48 48
test/functional/proposals_discussion_plugin_profile_controller_test.rb
@@ -6,7 +6,7 @@ class ProposalsDiscussionPluginProfileControllerTest &lt; ActionController::TestCas @@ -6,7 +6,7 @@ class ProposalsDiscussionPluginProfileControllerTest &lt; ActionController::TestCas
6 @profile = fast_create(Community) 6 @profile = fast_create(Community)
7 @discussion = ProposalsDiscussionPlugin::Discussion.create!(:profile => @profile, :name => 'discussion',:allow_topics => true) 7 @discussion = ProposalsDiscussionPlugin::Discussion.create!(:profile => @profile, :name => 'discussion',:allow_topics => true)
8 @topic = fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => @discussion.id, :profile_id => @profile.id) 8 @topic = fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => @discussion.id, :profile_id => @profile.id)
9 - @person = create_user_with_permission('testinguser', 'post_content') 9 + @person = create_user_with_permission('testinguser', 'post_content', @profile)
10 login_as :testinguser 10 login_as :testinguser
11 end 11 end
12 12
@@ -30,6 +30,7 @@ class ProposalsDiscussionPluginProfileControllerTest &lt; ActionController::TestCas @@ -30,6 +30,7 @@ class ProposalsDiscussionPluginProfileControllerTest &lt; ActionController::TestCas
30 end 30 end
31 31
32 should 'deny access to export when user has no permission' do 32 should 'deny access to export when user has no permission' do
  33 + login_as(create_user('testuser').login)
33 get :export, :format => :csv, :article_id => discussion.id, :profile => profile.identifier 34 get :export, :format => :csv, :article_id => discussion.id, :profile => profile.identifier
34 assert_template 'access_denied' 35 assert_template 'access_denied'
35 end 36 end
test/unit/discussion_test.rb
@@ -49,4 +49,19 @@ class DiscussionTest &lt; ActiveSupport::TestCase @@ -49,4 +49,19 @@ class DiscussionTest &lt; ActiveSupport::TestCase
49 assert !discussion.allow_new_proposals? 49 assert !discussion.allow_new_proposals?
50 end 50 end
51 51
  52 + should 'not allow proposal creation by normal users if discussion is moderated' do
  53 + discussion.moderate_proposals = true
  54 + person = fast_create(Person)
  55 + proposal = ProposalsDiscussionPlugin::Proposal.create!(:parent => discussion, :profile => profile, :name => "proposal1", :abstract => 'abstract')
  56 + assert !discussion.allow_create?(person)
  57 + end
  58 +
  59 + should 'allow proposal creation by admin users even when discussion is moderated' do
  60 + discussion.moderate_proposals = true
  61 + person = fast_create(Person)
  62 + give_permission(person, 'post_content', profile)
  63 + proposal = ProposalsDiscussionPlugin::Proposal.create!(:parent => discussion, :profile => profile, :name => "proposal1", :abstract => 'abstract')
  64 + assert discussion.allow_create?(person)
  65 + end
  66 +
52 end 67 end
test/unit/topic_test.rb
@@ -8,7 +8,7 @@ class TopicTest &lt; ActiveSupport::TestCase @@ -8,7 +8,7 @@ class TopicTest &lt; ActiveSupport::TestCase
8 @topic = ProposalsDiscussionPlugin::Topic.new(:name => 'test', :profile => @profile, :parent => @discussion) 8 @topic = ProposalsDiscussionPlugin::Topic.new(:name => 'test', :profile => @profile, :parent => @discussion)
9 end 9 end
10 10
11 - attr_reader :profile, :topic 11 + attr_reader :profile, :topic, :discussion
12 12
13 should 'return list of proposals' do 13 should 'return list of proposals' do
14 topic.save! 14 topic.save!
@@ -17,10 +17,15 @@ class TopicTest &lt; ActiveSupport::TestCase @@ -17,10 +17,15 @@ class TopicTest &lt; ActiveSupport::TestCase
17 assert_equivalent [proposal1, proposal2], topic.proposals 17 assert_equivalent [proposal1, proposal2], topic.proposals
18 end 18 end
19 19
20 - should 'allow any user to create proposals in a topic' do 20 + should 'allow any user to create proposals in a topic when discussion is not moderated' do
21 assert topic.allow_create?(Person.new) 21 assert topic.allow_create?(Person.new)
22 end 22 end
23 23
  24 + should 'do not allow normal users to create proposals in a topic when discussion is moderated' do
  25 + discussion.moderate_proposals = true
  26 + assert !topic.allow_create?(Person.new)
  27 + end
  28 +
24 should 'return list of comments' do 29 should 'return list of comments' do
25 topic.save! 30 topic.save!
26 proposal = fast_create(ProposalsDiscussionPlugin::Proposal, :parent_id => topic.id) 31 proposal = fast_create(ProposalsDiscussionPlugin::Proposal, :parent_id => topic.id)
views/cms/proposals_discussion_plugin/_discussion.html.erb
@@ -11,3 +11,4 @@ @@ -11,3 +11,4 @@
11 <%= f.text_field(:custom_body_label) %> 11 <%= f.text_field(:custom_body_label) %>
12 <%= labelled_form_field _('Current Phase'), f.select(:phase, ProposalsDiscussionPlugin::Discussion::AVAILABLE_PHASES.map{|k,v| [v,k]} ) %> 12 <%= labelled_form_field _('Current Phase'), f.select(:phase, ProposalsDiscussionPlugin::Discussion::AVAILABLE_PHASES.map{|k,v| [v,k]} ) %>
13 <%= labelled_form_field check_box(:article, :allow_topics) + _('Allow topics'), '' %> 13 <%= labelled_form_field check_box(:article, :allow_topics) + _('Allow topics'), '' %>
  14 +<%= labelled_form_field check_box(:article, :moderate_proposals) + _('Moderate proposals'), '' %>
views/proposals_discussion_plugin_myprofile/select_topic.html.erb
@@ -11,14 +11,14 @@ @@ -11,14 +11,14 @@
11 11
12 <div class="proposals-discussion-plugin select-topic"> 12 <div class="proposals-discussion-plugin select-topic">
13 <h1><%= @discussion.title %></h1> 13 <h1><%= @discussion.title %></h1>
14 - <%= form_for :discussion, :url => {:controller => 'proposals_discussion_plugin_myprofile', :action => 'new_proposal'} do %> 14 + <%= form_for :discussion, :url => {:controller => 'proposals_discussion_plugin_myprofile', :action => 'new_proposal_with_topic', :discussion_id => @discussion.id } do %>
15 15
16 <h3><%= _('Select topic') %></h3> 16 <h3><%= _('Select topic') %></h3>
17 <div id="topics"> 17 <div id="topics">
18 18
19 <% @discussion.topics.each do |topic| %> 19 <% @discussion.topics.each do |topic| %>
20 <h4> 20 <h4>
21 - <%= radio_button_tag('discussion[topic]', topic.id) %> 21 + <%= radio_button_tag('parent_id', topic.id) %>
22 <%= topic.title %> 22 <%= topic.title %>
23 </h4> 23 </h4>
24 <div class="content"> 24 <div class="content">
@@ -29,8 +29,6 @@ @@ -29,8 +29,6 @@
29 <div class="clear"></div> 29 <div class="clear"></div>
30 </div> 30 </div>
31 31
32 - <%= hidden_field_tag :parent_id, @discussion.id %>  
33 -  
34 <div class="actions"> 32 <div class="actions">
35 <%= submit_button(:next, _('Next')) %> 33 <%= submit_button(:next, _('Next')) %>
36 <%= button :cancel, _('Cancel'), @discussion.view_url %> 34 <%= button :cancel, _('Cancel'), @discussion.view_url %>