[Git][noosfero/noosfero][stable-2.5] Merge branch 'custom-fields-fail-validate' into 'master'

Rodrigo Souto gitlab at mg.gitlab.com
Mon Sep 10 17:12:21 BRT 2018


Rodrigo Souto pushed to branch stable-2.5 at Noosfero / noosfero


Commits:
ba6dd51b by Rodrigo Souto at 2018-09-10T20:12:19Z
Merge branch 'custom-fields-fail-validate' into 'master'

Fix validation failure in custom fields

See merge request noosfero/noosfero!1606

(cherry picked from commit 902a980a1a5da36e4e389db072e81f2228df5d0b)

bf889066 Fix different treatment for custom fields in new and edit methods
19697ea4 Check if article has metadata custom_field before validate
adb36712 Add migrate to remove invalid custom_fields in article
84677144 Fix tratament to edit custom fields
- - - - -


2 changed files:

- app/models/article.rb
- + db/migrate/20180829134258_remove_empty_custon_fields_from_articles.rb


Changes:

=====================================
app/models/article.rb
=====================================
@@ -881,20 +881,24 @@ class Article < ApplicationRecord
   end
 
   def validate_custom_fields
-    custom_fields = metadata['custom_fields']
-    if custom_fields.present?
-      custom_fields.each do |key, field|
-        if field['value'].blank?
-          errors.add(:metadata, _('Custom fields must have values'))
+    if metadata.has_key?('custom_fields')
+      custom_fields = metadata['custom_fields']
+      if custom_fields.present?
+        custom_fields.each do |key, field|
+          if field['value'].blank?
+            errors.add(:metadata, _('Custom fields must have values'))
+          end
         end
       end
     end
   end
 
   def sanitize_custom_field_keys
-    custom_fields = metadata['custom_fields'] || {}
-    metadata['custom_fields'] = custom_fields.keys.map do |field|
-      [field.to_slug, metadata['custom_fields'][field]]
-    end.to_h
+    if metadata.has_key?('custom_fields')
+      custom_fields = metadata['custom_fields'] || {}
+      metadata['custom_fields'] = custom_fields.keys.map do |field|
+        [field.to_slug, metadata['custom_fields'][field]]
+      end.to_h
+    end
   end
 end


=====================================
db/migrate/20180829134258_remove_empty_custon_fields_from_articles.rb
=====================================
@@ -0,0 +1,10 @@
+class RemoveEmptyCustonFieldsFromArticles < ActiveRecord::Migration
+  def change
+    Article.find_each do |article|
+        if article.metadata.has_key?('custom_fields') && article.metadata['custom_fields'].empty?
+            article.metadata.delete('custom_fields')
+            article.save
+        end
+    end
+  end
+end



View it on GitLab: https://gitlab.com/noosfero/noosfero/commit/ba6dd51b0bbbc418f2de8854743879d7e7c73fd8

-- 
View it on GitLab: https://gitlab.com/noosfero/noosfero/commit/ba6dd51b0bbbc418f2de8854743879d7e7c73fd8
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20180910/f5992fd1/attachment-0001.html>


More information about the Noosfero-dev mailing list