noosfero | Fix: display error msgs when form submission fails

Daniela Feitosa gitlab at gitlab.com
Mon Mar 16 20:03:03 BRT 2015


Daniela Feitosa pushed to refs/heads/master at <a href="https://gitlab.com/noosfero/noosfero">Noosfero / noosfero</a>

Commits:
<a href="https://gitlab.com/noosfero/noosfero/commit/0b9f2d0d4c364c71b56c0a74a44b4bb6dd20976e">0b9f2d0d</a> by Daniela Feitosa
Fix: display error msgs when form submission fails

- - - - -


Changes:

=====================================
plugins/custom_forms/lib/custom_forms_plugin/submission.rb
=====================================
--- a/plugins/custom_forms/lib/custom_forms_plugin/submission.rb
+++ b/plugins/custom_forms/lib/custom_forms_plugin/submission.rb
@@ -5,7 +5,7 @@ class CustomFormsPlugin::Submission < Noosfero::Plugin::ActiveRecord
   # validation is done manually, see below
   has_many :answers, :class_name => 'CustomFormsPlugin::Answer', :dependent => :destroy, :validate => false
 
-  attr_accessible :form, :profile
+  attr_accessible :form, :profile, :author_name, :author_email
 
   validates_presence_of :form
   validates_presence_of :author_name, :author_email, :if => lambda {|submission| submission.profile.nil?}
@@ -13,13 +13,16 @@ class CustomFormsPlugin::Submission < Noosfero::Plugin::ActiveRecord
   validates_format_of :author_email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|submission| !submission.author_email.blank?})
   validate :check_answers
 
-  def self.human_attribute_name(attrib)
-    if /\d+/ =~ attrib and (f = CustomFormsPlugin::Field.find_by_id(attrib.to_i))
+  def self.human_attribute_name_with_customization(attrib, options={})
+    if /\d+/ =~ attrib and (f = CustomFormsPlugin::Field.find_by_id(attrib.to_s))
       f.name
     else
-      attrib
+      _(self.human_attribute_name_without_customization(attrib))
     end
   end
+  class << self
+    alias_method_chain :human_attribute_name, :customization
+  end
 
   before_create do |submission|
     if submission.profile

=====================================
plugins/custom_forms/test/functional/custom_forms_plugin_profile_controller_test.rb
=====================================
--- a/plugins/custom_forms/test/functional/custom_forms_plugin_profile_controller_test.rb
+++ b/plugins/custom_forms/test/functional/custom_forms_plugin_profile_controller_test.rb
@@ -40,6 +40,19 @@ class CustomFormsPluginProfileControllerTest < ActionController::TestCase
     assert_redirected_to :action => 'show'
   end
 
+  should 'display errors if user is not logged in and author_name is not uniq' do
+    logout
+    form = CustomFormsPlugin::Form.create(:profile => profile, :name => 'Free Software')
+    field = CustomFormsPlugin::TextField.create(:name => 'Name', :form => form)
+    submission = CustomFormsPlugin::Submission.create(:form => form, :author_name => "john", :author_email => 'john at example.com')
+
+    assert_no_difference 'CustomFormsPlugin::Submission.count' do
+      post :show, :profile => profile.identifier, :id => form.id, :author_name => "john", :author_email => 'john at example.com', :submission => {field.id.to_s => 'Noosfero'}
+    end
+    assert_equal "Submission could not be saved", session[:notice]
+    assert_tag :tag => 'div', :attributes => { :class => 'errorExplanation', :id => 'errorExplanation' }
+  end
+
   should 'disable fields if form expired' do
     form = CustomFormsPlugin::Form.create!(:profile => profile, :name => 'Free Software', :begining => Time.now + 1.day)
     form.fields << CustomFormsPlugin::TextField.create(:name => 'Field Name', :form => form, :default_value => "First Field")

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150316/091f1022/attachment.html>


More information about the Noosfero-dev mailing list