diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index e6417b9..0d21d9f 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -357,6 +357,7 @@ class CmsController < MyProfileController @task.ip_address = request.remote_ip @task.user_agent = request.user_agent @task.referrer = request.referrer + @task.requestor = current_person if logged_in? if verify_recaptcha(:model => @task, :message => _('Please type the words correctly')) && @task.save session[:notice] = _('Thanks for your suggestion. The community administrators were notified.') redirect_to @back_to diff --git a/app/models/suggest_article.rb b/app/models/suggest_article.rb index b62be70..24a3aa5 100644 --- a/app/models/suggest_article.rb +++ b/app/models/suggest_article.rb @@ -1,6 +1,7 @@ class SuggestArticle < Task - validates_presence_of :target_id, :article_name, :email, :name, :article_body + validates_presence_of :target_id, :article_name, :article_body + validates_presence_of :email, :name, :if => Proc.new { |task| task.requestor.blank? } settings_items :email, :type => String settings_items :name, :type => String @@ -24,7 +25,7 @@ class SuggestArticle < Task include Noosfero::Plugin::HotSpot def sender - "#{name} (#{email})" + requestor ? "#{requestor.name}" : "#{name} (#{email})" end def perform @@ -50,8 +51,9 @@ class SuggestArticle < Task end def information - { :message => _('%{sender} suggested the publication of the article: %{subject}.'), - :variables => {:sender => sender} } + variables = requestor.blank? ? {:requestor => sender} : {} + { :message => _('%{requestor} suggested the publication of the article: %{subject}.'), + :variables => variables } end def accept_details @@ -63,8 +65,8 @@ class SuggestArticle < Task end def target_notification_description - _('%{sender} suggested the publication of the article: %{article}.') % - {:sender => sender, :article => article_name} + _('%{requestor} suggested the publication of the article: %{article}.') % + {:requestor => sender, :article => article_name} end def target_notification_message diff --git a/app/views/cms/suggest_an_article.html.erb b/app/views/cms/suggest_an_article.html.erb index 17da89c..14ea5dd 100644 --- a/app/views/cms/suggest_an_article.html.erb +++ b/app/views/cms/suggest_an_article.html.erb @@ -12,9 +12,10 @@ <%= labelled_form_field(_('Source URL'), text_field(:task, 'source')) %> - <%= required labelled_form_field(_('Your name'), text_field(:task, 'name')) %> - - <%= required labelled_form_field(_('Email'), text_field(:task, 'email')) %> + <% unless logged_in? %> + <%= required labelled_form_field(_('Your name'), text_field(:task, 'name')) %> + <%= required labelled_form_field(_('Email'), text_field(:task, 'email')) %> + <% end %> <%= render :partial => 'shared/lead_and_body', :locals => {:tiny_mce => true, :object => :task, :abstract_method => 'article_abstract', :body_method => 'article_body'} %> diff --git a/app/views/tasks/_suggest_article_accept_details.html.erb b/app/views/tasks/_suggest_article_accept_details.html.erb index 759e165..ecf7f3e 100644 --- a/app/views/tasks/_suggest_article_accept_details.html.erb +++ b/app/views/tasks/_suggest_article_accept_details.html.erb @@ -1,7 +1,9 @@ <%= render :file => 'shared/tiny_mce' %> -<%= labelled_form_field(_("Sent by: "), f.text_field(:name)) %> -

<%= label_tag(_("Email: %s") % task.email) %>

+<% unless task.requestor %> + <%= labelled_form_field(_("Sent by: "), f.text_field(:name)) %> +

<%= label_tag(_("Email: %s") % task.email) %>

+<% end %> <%= required labelled_form_field(_('Title'), f.text_field(:article_name, :size => 50)) %> <%= labelled_form_field(_('Source'), f.text_field(:source_name)) %> <%= labelled_form_field(_("Source URL"), f.text_field(:source)) %> diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index b3de376..7f86a79 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -1407,6 +1407,21 @@ class CmsControllerTest < ActionController::TestCase assert_template 'suggest_an_article' end + should 'display name and email when a not logged in user suggest an article' do + logout + get :suggest_an_article, :profile => profile.identifier, :back_to => 'action_view' + + assert_select '#task_name' + assert_select '#task_email' + end + + should 'do not display name and email when a logged in user suggest an article' do + get :suggest_an_article, :profile => profile.identifier, :back_to => 'action_view' + + assert_select '#task_name', 0 + assert_select '#task_email', 0 + end + should 'render TinyMce Editor on suggestion of article' do logout get :suggest_an_article, :profile => profile.identifier @@ -1423,6 +1438,13 @@ class CmsControllerTest < ActionController::TestCase end end + should 'create suggest task with logged in user as the article author' do + c = Community.create!(:name => 'test comm', :identifier => 'test_comm', :moderated_articles => true) + + post :suggest_an_article, :profile => c.identifier, :back_to => 'action_view', :task => {:article_name => 'some name', :article_body => 'some body'} + assert_equal profile, SuggestArticle.last.requestor + end + should 'suggest an article from a profile' do c = Community.create!(:name => 'test comm', :identifier => 'test_comm', :moderated_articles => true) get :suggest_an_article, :profile => c.identifier, :back_to => c.identifier diff --git a/test/functional/tasks_controller_test.rb b/test/functional/tasks_controller_test.rb index 9d3f8b3..8af8765 100644 --- a/test/functional/tasks_controller_test.rb +++ b/test/functional/tasks_controller_test.rb @@ -305,6 +305,28 @@ class TasksControllerTest < ActionController::TestCase assert_equal 'new source', TinyMceArticle.find(:first).source_name end + should "display name from article suggestion when requestor was not setted" do + Task.destroy_all + c = fast_create(Community) + c.add_admin profile + @controller.stubs(:profile).returns(c) + t = SuggestArticle.create!(:article_name => 'test name', :article_abstract => 'test abstract', :article_body => 'test body', :name => 'some name', :email => 'test@localhost.com', :target => c) + + get :index + assert_select "#tasks_#{t.id}_task_name" + end + + should "not display name from article suggestion when requestor was setted" do + Task.destroy_all + c = fast_create(Community) + c.add_admin profile + @controller.stubs(:profile).returns(c) + t = SuggestArticle.create!(:article_name => 'test name', :article_abstract => 'test abstract', :article_body => 'test body', :requestor => fast_create(Person), :target => c) + + get :index + assert_select "#tasks_#{t.id}_task_name", 0 + end + should "not crash if accessing close without tasks parameter" do assert_nothing_raised do post :close diff --git a/test/unit/suggest_article_test.rb b/test/unit/suggest_article_test.rb index acfab41..fbed706 100644 --- a/test/unit/suggest_article_test.rb +++ b/test/unit/suggest_article_test.rb @@ -225,4 +225,17 @@ class SuggestArticleTest < ActiveSupport::TestCase assert_match "SuggestArticle-id: #{t.id} IP: 192.168.0.1", log.read end + should 'not require name and email when requestor is present' do + t = SuggestArticle.new(:requestor => fast_create(Person)) + t.valid? + assert t.errors[:email].blank? + assert t.errors[:name].blank? + end + + should 'return name as sender when requestor is setted' do + person = fast_create(Person) + t = SuggestArticle.new(:requestor => person) + assert_equal person.name, t.sender + end + end -- libgit2 0.21.2