[Git][noosfero/noosfero][master] 5 commits: Fix different treatment for custom fields in new and edit methods

Rodrigo Souto gitlab at mg.gitlab.com
Wed Sep 5 09:16:02 BRT 2018


Rodrigo Souto pushed to branch master at Noosfero / noosfero


Commits:
bf889066 by Iasmin Mendes at 2018-08-31T12:20:08Z
Fix different treatment for custom fields in new and edit methods

- - - - -
19697ea4 by Iasmin Mendes at 2018-08-31T12:20:08Z
Check if article has metadata custom_field before validate

- - - - -
adb36712 by Iasmin Mendes at 2018-08-31T12:20:08Z
Add migrate to remove invalid custom_fields in article

- - - - -
84677144 by Iasmin Mendes at 2018-08-31T14:09:57Z
Fix tratament to edit custom fields

- - - - -
902a980a by Rodrigo Souto at 2018-09-05T12:15:59Z
Merge branch 'custom-fields-fail-validate' into 'master'

Fix validation failure in custom fields

See merge request noosfero/noosfero!1606
- - - - -


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/compare/b8bebbeb7ea9a43803aaec582f7d204cc24c0c10...902a980a1a5da36e4e389db072e81f2228df5d0b

-- 
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/b8bebbeb7ea9a43803aaec582f7d204cc24c0c10...902a980a1a5da36e4e389db072e81f2228df5d0b
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/20180905/389833e2/attachment-0001.html>


More information about the Noosfero-dev mailing list