Commit 9de0f762e26660ae2c3c5bbff83468fd12253293

Authored by Victor Costa
1 parent 55e057ab

proposals_discussion: display an error when trying to create a proposal without a topic

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 5
5 def select_topic 6 def select_topic
6 - @discussion = profile.articles.find(params[:parent_id])  
7 - render :file => 'select_topic'  
8 end 7 end
9 8
10 def new_proposal 9 def new_proposal
11 - redirect_to :controller => 'cms', :action => 'new', :type => "ProposalsDiscussionPlugin::Proposal", :parent_id => params[:discussion][:topic] 10 + if params[:discussion].blank? || params[:discussion][:topic].blank?
  11 + session[:notice] = _('Please select a topic')
  12 + render :file => 'proposals_discussion_plugin_myprofile/select_topic'
  13 + else
  14 + redirect_to :controller => 'cms', :action => 'new', :type => "ProposalsDiscussionPlugin::Proposal", :parent_id => params[:discussion][:topic]
  15 + end
12 end 16 end
13 17
14 def publish_proposal 18 def publish_proposal
@@ -27,4 +31,8 @@ class ProposalsDiscussionPluginMyprofileController &lt; MyProfileController @@ -27,4 +31,8 @@ class ProposalsDiscussionPluginMyprofileController &lt; MyProfileController
27 render_access_denied unless @proposal.allow_edit?(user) 31 render_access_denied unless @proposal.allow_edit?(user)
28 end 32 end
29 33
  34 + def set_discussion
  35 + @discussion = profile.articles.find(params[:parent_id])
  36 + end
  37 +
30 end 38 end
test/functional/proposals_discussion_plugin_myprofile_controller_test.rb
@@ -24,7 +24,7 @@ class ProposalsDiscussionPluginMyprofileControllerTest &lt; ActionController::TestC @@ -24,7 +24,7 @@ class ProposalsDiscussionPluginMyprofileControllerTest &lt; ActionController::TestC
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} 27 + get :new_proposal, :profile => profile.identifier, :discussion => {:topic => topic.id}, :parent_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,4 +41,9 @@ class ProposalsDiscussionPluginMyprofileControllerTest &lt; ActionController::TestC @@ -41,4 +41,9 @@ 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
  46 + assert_template 'select_topic'
  47 + end
  48 +
44 end 49 end
views/proposals_discussion_plugin_myprofile/select_topic.html.erb 0 → 100644
@@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
  1 +<script>
  2 + jQuery(document).ready(function($){
  3 + $("#topics").accordion();
  4 + $('#topics input[type=radio],label').on('click',function(e){e.stopPropagation();});
  5 + $('#topics h4').click(function(e){
  6 + e.stopPropagation();
  7 + $(this).find('input:radio').prop('checked', true);
  8 + });
  9 + });
  10 +</script>
  11 +
  12 +<div class="proposals-discussion-plugin select-topic">
  13 + <h1><%= @discussion.title %></h1>
  14 + <%= form_for :discussion, :url => {:controller => 'proposals_discussion_plugin_myprofile', :action => 'new_proposal'} do %>
  15 +
  16 + <h3><%= _('Select topic') %></h3>
  17 + <div id="topics">
  18 +
  19 + <% @discussion.topics.each do |topic| %>
  20 + <h4>
  21 + <%= radio_button_tag('discussion[topic]', topic.id) %>
  22 + <%= topic.title %>
  23 + </h4>
  24 + <div class="content">
  25 + <%= topic.body %>
  26 + </div>
  27 + <% end %>
  28 +
  29 + <div class="clear"></div>
  30 + </div>
  31 +
  32 + <%= hidden_field_tag :parent_id, @discussion.id %>
  33 +
  34 + <div class="actions">
  35 + <%= submit_button(:next, _('Next')) %>
  36 + <%= button :cancel, _('Cancel'), @discussion.view_url %>
  37 + </div>
  38 + <% end %>
  39 +</div>
views/select_topic.html.erb
@@ -1,37 +0,0 @@ @@ -1,37 +0,0 @@
1 -<script>  
2 - jQuery(document).ready(function($){  
3 - $("#topics").accordion();  
4 - $('#topics input[type=radio],label').on('click',function(e){e.stopPropagation();});  
5 - $('#topics h4').click(function(e){  
6 - e.stopPropagation();  
7 - $(this).find('input:radio').prop('checked', true);  
8 - });  
9 - });  
10 -</script>  
11 -  
12 -<div class="proposals-discussion-plugin select-topic">  
13 - <h1><%= @discussion.title %></h1>  
14 - <%= form_for :discussion, :url => {:controller => 'proposals_discussion_plugin_myprofile', :action => 'new_proposal'} do %>  
15 -  
16 - <h3><%= _('Select topic') %></h3>  
17 - <div id="topics">  
18 -  
19 - <% @discussion.topics.each do |topic| %>  
20 - <h4>  
21 - <%= radio_button_tag('discussion[topic]', topic.id) %>  
22 - <%= topic.title %>  
23 - </h4>  
24 - <div class="content">  
25 - <%= topic.body %>  
26 - </div>  
27 - <% end %>  
28 -  
29 - <div class="clear"></div>  
30 - </div>  
31 -  
32 - <div class="actions">  
33 - <%= submit_button(:next, _('Next')) %>  
34 - <%= button :cancel, _('Cancel'), @discussion.view_url %>  
35 - </div>  
36 - <% end %>  
37 -</div>