[Git][noosfero/noosfero][master] 2 commits: CustomFormsPlugin: fixes update form with image

Rodrigo Souto gitlab at mg.gitlab.com
Thu Sep 21 18:23:09 BRT 2017


Rodrigo Souto pushed to branch master at Noosfero / noosfero


Commits:
26ec3c18 by Gabriel Silva at 2017-09-19T20:57:53Z
CustomFormsPlugin: fixes update form with image

Signed-off-by: Gabriel Silva <gabriel93.silva at gmail.com>

- - - - -
78285942 by Rodrigo Souto at 2017-09-21T21:22:58Z
Merge branch 'custom_forms_update' into 'master'

CustomFormsPlugin: fixes update form with image

See merge request noosfero/noosfero!1312
- - - - -


4 changed files:

- plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb
- plugins/custom_forms/lib/custom_forms_plugin/form.rb
- plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb
- plugins/custom_forms/test/unit/custom_forms_plugin/form_test.rb


Changes:

=====================================
plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb
=====================================
--- a/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb
+++ b/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb
@@ -28,8 +28,7 @@ class CustomFormsPluginMyprofileController < MyProfileController
     @form = CustomFormsPlugin::Form.new(params[:form])
     normalize_positions(@form)
 
-    form_with_image  = add_gallery_in_form(@form, profile, uploaded_data)
-
+    form_with_image = add_gallery_in_form(@form, profile, uploaded_data)
     respond_to do |format|
       if form_with_image
         session[:notice] = _("%s was successfully created") % @form.name
@@ -47,13 +46,14 @@ class CustomFormsPluginMyprofileController < MyProfileController
   end
 
   def update
+    uploaded_data = params[:form].delete(:image)
     @form = CustomFormsPlugin::Form.find(params[:id])
     @form.attributes = params[:form]
-
     normalize_positions(@form)
 
+    form_with_image = add_gallery_in_form(@form, profile, uploaded_data)
     respond_to do |format|
-      if @form.save!
+      if form_with_image
         session[:notice] = _("%s was successfully updated") % @form.name
         format.html { redirect_to(:action=>'index') }
       else
@@ -140,7 +140,9 @@ class CustomFormsPluginMyprofileController < MyProfileController
     form_settings = Noosfero::Plugin::Settings.new(profile, CustomFormsPlugin)
     form_gallery = Gallery.where(id: form_settings.gallery_id).first
     unless form_gallery
-      form_gallery = Gallery.new(profile: profile, name: _("Query Gallery"))
+      form_gallery = Gallery.create(profile: profile, name: _("Query Gallery"))
+      form_settings.gallery_id = form_gallery.id
+      form_settings.save!
     end
 
     form_gallery.images << form_image


=====================================
plugins/custom_forms/lib/custom_forms_plugin/form.rb
=====================================
--- a/plugins/custom_forms/lib/custom_forms_plugin/form.rb
+++ b/plugins/custom_forms/lib/custom_forms_plugin/form.rb
@@ -23,9 +23,9 @@ class CustomFormsPlugin::Form < ApplicationRecord
   # CustomFormsPlugin::Form schema.
   belongs_to :article, :class_name => 'UploadedFile', dependent: :destroy
 
-  attr_accessible :name, :profile, :for_admission, :access, :begining,
-    :ending, :description, :fields_attributes, :profile_id,
-    :on_membership, :identifier, :access_result_options, :kind
+  attr_accessible :name, :profile, :for_admission, :access, :begining, :kind,
+                  :ending, :description, :fields_attributes, :profile_id,
+                  :on_membership, :identifier, :access_result_options, :image
 
   KINDS = %w(survey poll)
   # Dynamic Translations
@@ -42,6 +42,7 @@ class CustomFormsPlugin::Form < ApplicationRecord
 
   before_validation do |form|
     form.slug = form.name.to_slug if form.name.present?
+    form.identifier = form.slug unless form.identifier.present?
     form.access = nil if form.access.blank?
   end
 


=====================================
plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb
=====================================
--- a/plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb
+++ b/plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb
@@ -335,6 +335,20 @@ class CustomFormsPluginMyprofileControllerTest < ActionController::TestCase
     assert_not_nil form.image
   end
 
+  should 'update a form with an uploaded image' do
+    form = CustomFormsPlugin::Form.create!(profile: profile,
+                                           name: 'Free?')
+    post :update, profile: profile.identifier, id: form.id,
+      form: {
+        name: 'Free as in freedon?',
+        description: 'Cool form',
+        image: fixture_file_upload('/files/rails.png', 'image/png')
+      }
+
+    form.reload
+    assert form.image.present?
+  end
+
   should 'create a galery to store form images' do
 
     post :create, :profile => profile.identifier,


=====================================
plugins/custom_forms/test/unit/custom_forms_plugin/form_test.rb
=====================================
--- a/plugins/custom_forms/test/unit/custom_forms_plugin/form_test.rb
+++ b/plugins/custom_forms/test/unit/custom_forms_plugin/form_test.rb
@@ -40,10 +40,11 @@ class CustomFormsPlugin::FormTest < ActiveSupport::TestCase
     assert_includes form.submissions, s2
   end
 
-  should 'set slug before validation based on name' do
+  should 'set slug amd identifier before validation based on name' do
     form = CustomFormsPlugin::Form.new(:name => 'Name', :identifier => 'name')
     form.valid?
     assert_equal form.name.to_slug, form.slug
+    assert_equal form.name.to_slug, form.identifier
   end
 
   should 'validates uniqueness of slug scoped on profile' do



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/a1148c07eaf16d53c18a04c9f4d9ca32d4df45ba...78285942c2e174a9a24c8b6f90c5af6c98f1d307

---
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/a1148c07eaf16d53c18a04c9f4d9ca32d4df45ba...78285942c2e174a9a24c8b6f90c5af6c98f1d307
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/20170921/0d2c2c37/attachment-0001.html>


More information about the Noosfero-dev mailing list