[Git][noosfero/noosfero][master] 3 commits: custom_forms: Remove obsolete partials

Bráulio Bhavamitra gitlab at mg.gitlab.com
Thu Mar 17 11:25:29 BRT 2016


Bráulio Bhavamitra pushed to branch master at Noosfero / noosfero


Commits:
88c5d866 by Larissa Reis at 2016-03-16T22:41:26-03:00
custom_forms: Remove obsolete partials

- - - - -
c62890a7 by Larissa Reis at 2016-03-16T22:41:34-03:00
custom_forms: Adds support for textarea as an option for text field

- - - - -
1891de67 by Bráulio Bhavamitra at 2016-03-17T14:24:11+00:00
Merge branch 'custom-form-text-area' into 'master'

Adds text area for custom forms

Add support for text area in custom forms plugin.

See merge request !702
- - - - -


14 changed files:

- + plugins/custom_forms/db/migrate/20151008130948_create_text_field_type_in_custom_forms_plugin.rb
- plugins/custom_forms/lib/custom_forms_plugin/field.rb
- plugins/custom_forms/lib/custom_forms_plugin/helper.rb
- plugins/custom_forms/lib/custom_forms_plugin/select_field.rb
- plugins/custom_forms/lib/custom_forms_plugin/text_field.rb
- plugins/custom_forms/public/style.css
- plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb
- plugins/custom_forms/test/unit/custom_forms_plugin/select_field_test.rb
- + plugins/custom_forms/test/unit/custom_forms_plugin/text_field_test.rb
- − plugins/custom_forms/views/custom_forms_plugin_myprofile/_edit_select.html.erb
- − plugins/custom_forms/views/custom_forms_plugin_myprofile/_empty_field.html.erb
- − plugins/custom_forms/views/custom_forms_plugin_myprofile/_empty_option.html.erb
- plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_select_field.html.erb
- plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_text_field.html.erb


Changes:

=====================================
plugins/custom_forms/db/migrate/20151008130948_create_text_field_type_in_custom_forms_plugin.rb
=====================================
--- /dev/null
+++ b/plugins/custom_forms/db/migrate/20151008130948_create_text_field_type_in_custom_forms_plugin.rb
@@ -0,0 +1,13 @@
+class CreateTextFieldTypeInCustomFormsPlugin < ActiveRecord::Migration
+  def up
+    rename_column :custom_forms_plugin_fields, :select_field_type, :show_as
+    change_column :custom_forms_plugin_fields, :show_as, :string, :null => true, :default => nil
+    update("UPDATE custom_forms_plugin_fields SET show_as='text' WHERE type = 'CustomFormsPlugin::TextField'")
+  end
+
+  def down
+    rename_column :custom_forms_plugin_fields, :show_as, :select_field_type
+    change_column :custom_forms_plugin_fields, :select_field_type, :string, :null => false, :default => 'radio'
+    update("UPDATE custom_forms_plugin_fields SET select_field_type='radio' WHERE type = 'CustomFormsPlugin::TextField'")
+  end
+end


=====================================
plugins/custom_forms/lib/custom_forms_plugin/field.rb
=====================================
--- a/plugins/custom_forms/lib/custom_forms_plugin/field.rb
+++ b/plugins/custom_forms/lib/custom_forms_plugin/field.rb
@@ -2,8 +2,9 @@ class CustomFormsPlugin::Field < ActiveRecord::Base
   self.table_name = :custom_forms_plugin_fields
 
   validates_presence_of :name
+  validates_length_of :default_value, :maximum => 255
 
-  attr_accessible :name, :form, :mandatory, :type, :position, :default_value, :select_field_type, :alternatives_attributes
+  attr_accessible :name, :form, :mandatory, :type, :position, :default_value, :show_as, :alternatives_attributes
 
   belongs_to :form, :class_name => 'CustomFormsPlugin::Form'
   has_many :answers, :class_name => 'CustomFormsPlugin::Answer', :dependent => :destroy


=====================================
plugins/custom_forms/lib/custom_forms_plugin/helper.rb
=====================================
--- a/plugins/custom_forms/lib/custom_forms_plugin/helper.rb
+++ b/plugins/custom_forms/lib/custom_forms_plugin/helper.rb
@@ -85,7 +85,11 @@ module CustomFormsPlugin::Helper
 
   def display_text_field(field, answer, form)
     value = answer.present? ? answer.value : field.default_value
-    text_field(form, "#{field.id}", :value => value, :disabled => display_disabled?(field, answer))
+    if field.show_as == 'textarea'
+      text_area(form, "#{field.id}", :value => value, :disabled => display_disabled?(field, answer))
+    else
+      text_field(form, "#{field.id}", :value => value, :disabled => display_disabled?(field, answer))
+    end
   end
 
   def default_selected(field, answer)
@@ -93,7 +97,7 @@ module CustomFormsPlugin::Helper
   end
 
   def display_select_field(field, answer, form)
-    case field.select_field_type
+    case field.show_as
     when 'select'
       selected = default_selected(field, answer)
       select_tag form.to_s + "[#{field.id}]", options_for_select([['','']] + field.alternatives.map {|a| [a.label, a.id.to_s]}, selected), :disabled => display_disabled?(field, answer)
@@ -114,11 +118,11 @@ module CustomFormsPlugin::Helper
   end
 
   def radio_button?(field)
-    type_for_options(field.class) == 'select_field' && field.select_field_type == 'radio'
+    type_for_options(field.class) == 'select_field' && field.show_as == 'radio'
   end
 
   def check_box?(field)
-    type_for_options(field.class) == 'select_field' && field.select_field_type == 'check_box'
+    type_for_options(field.class) == 'select_field' && field.show_as == 'check_box'
   end
 
 end


=====================================
plugins/custom_forms/lib/custom_forms_plugin/select_field.rb
=====================================
--- a/plugins/custom_forms/lib/custom_forms_plugin/select_field.rb
+++ b/plugins/custom_forms/lib/custom_forms_plugin/select_field.rb
@@ -1,5 +1,9 @@
 class CustomFormsPlugin::SelectField < CustomFormsPlugin::Field
   self.table_name = :custom_forms_plugin_fields
-  validates_inclusion_of :select_field_type, :in => %w(radio check_box select multiple_select)
+  validates_inclusion_of :show_as, :in => %w(radio check_box select multiple_select)
   validates_length_of :alternatives, :minimum => 1, :message => 'can\'t be empty'
+
+  after_initialize do
+    self.show_as ||= 'radio'
+  end
 end


=====================================
plugins/custom_forms/lib/custom_forms_plugin/text_field.rb
=====================================
--- a/plugins/custom_forms/lib/custom_forms_plugin/text_field.rb
+++ b/plugins/custom_forms/lib/custom_forms_plugin/text_field.rb
@@ -2,4 +2,9 @@ class CustomFormsPlugin::TextField < CustomFormsPlugin::Field
 
   self.table_name = :custom_forms_plugin_fields
 
+  validates_inclusion_of :show_as, :in => %w(text textarea)
+
+  after_initialize do
+    self.show_as ||= 'text'
+  end
 end


=====================================
plugins/custom_forms/public/style.css
=====================================
--- a/plugins/custom_forms/public/style.css
+++ b/plugins/custom_forms/public/style.css
@@ -95,6 +95,11 @@ tr.addition-buttons {
   color: rgba(0,0,0,0.5);
 }
 
+#custom-forms-plugin_submission textarea {
+  width: 100%;
+  height: 10em;
+}
+
 #custom-forms-plugin_submission-view th {
   border: none;
   text-align: right;


=====================================
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
@@ -54,7 +54,7 @@ class CustomFormsPluginMyprofileControllerTest < ActionController::TestCase
             },
             2 => {
               :name => 'Color',
-              :select_field_type => 'radio',
+              :show_as => 'radio',
               :type => 'CustomFormsPlugin::SelectField',
               :alternatives_attributes => {
                 1 => {:label => 'Red'},
@@ -82,7 +82,7 @@ class CustomFormsPluginMyprofileControllerTest < ActionController::TestCase
 
     assert_equal 'Color', f2.name
     assert_equal f2.alternatives.map(&:label).sort, ['Red', 'Blue', 'Black'].sort
-    assert_equal f2.select_field_type, 'radio'
+    assert_equal f2.show_as, 'radio'
     assert f2.kind_of?(CustomFormsPlugin::SelectField)
   end
 


=====================================
plugins/custom_forms/test/unit/custom_forms_plugin/select_field_test.rb
=====================================
--- a/plugins/custom_forms/test/unit/custom_forms_plugin/select_field_test.rb
+++ b/plugins/custom_forms/test/unit/custom_forms_plugin/select_field_test.rb
@@ -13,16 +13,16 @@ class CustomFormsPlugin::SelectFieldTest < ActiveSupport::TestCase
     select = CustomFormsPlugin::SelectField.new(:name => 'select_field001' )
     select.alternatives << CustomFormsPlugin::Alternative.new(:label => 'option')
 
-    select.update_attribute(:select_field_type, 'random')
+    select.update_attribute(:show_as, 'random')
     assert select.invalid?
 
-    select.update_attribute(:select_field_type, 'radio')
+    select.update_attribute(:show_as, 'radio')
     assert select.valid?
-    select.update_attribute(:select_field_type, 'check_box')
+    select.update_attribute(:show_as, 'check_box')
     assert select.valid?
-    select.update_attribute(:select_field_type, 'select')
+    select.update_attribute(:show_as, 'select')
     assert select.valid?
-    select.update_attribute(:select_field_type, 'multiple_select')
+    select.update_attribute(:show_as, 'multiple_select')
     assert select.valid?
   end
 end


=====================================
plugins/custom_forms/test/unit/custom_forms_plugin/text_field_test.rb
=====================================
--- /dev/null
+++ b/plugins/custom_forms/test/unit/custom_forms_plugin/text_field_test.rb
@@ -0,0 +1,22 @@
+require File.dirname(__FILE__) + '/../../../../../test/test_helper'
+
+class CustomFormsPlugin::TextFieldTest < ActiveSupport::TestCase
+  should 'validate type' do
+    text = CustomFormsPlugin::TextField.new(:name => 'text-field-010' )
+
+    text.update_attribute(:show_as, 'random')
+    assert text.invalid?
+    text.update_attribute(:show_as, 'radio')
+    assert text.invalid?
+
+    text.update_attribute(:show_as, 'text')
+    assert text.valid?
+    text.update_attribute(:show_as, 'textarea')
+    assert text.valid?
+  end
+
+  should 'field type defaults to text when initialized' do
+    text = CustomFormsPlugin::TextField.new(:name => 'text_field001' )
+    assert_equal 'text', text.show_as
+  end
+end


=====================================
plugins/custom_forms/views/custom_forms_plugin_myprofile/_edit_select.html.erb deleted
=====================================
--- a/plugins/custom_forms/views/custom_forms_plugin_myprofile/_edit_select.html.erb
+++ /dev/null
@@ -1,32 +0,0 @@
-<div class='edit-information edit-select'>
-  <h2><%= c_('Options') %></h2>
-  <table class='action-table' style='width: 420px'>
-    <tr>
-      <th style='width: 40%'><%= _('Name') %></th>
-      <th style='width: 40%'><%= _('Value') %></th>
-      <th style='width: 20%'><%= c_('Delete') %></th>
-    </tr>
-    <% option_counter = 1 %>
-    <% (field.choices || {}).each do |name, value| %>
-      <%= render :partial => 'option', :locals => {:name => name, :value => value, :counter => counter, :option_counter => option_counter} %>
-      <% option_counter += 1 %>
-    <% end %>
-    <%= render :partial => 'empty_option', :locals => {:counter => counter, :option_counter => option_counter} %>
-    <tr class='new-item'>
-      <td colspan='3'>
-        <%= button(:add, _('Add a new option'), '#', :class => 'new-option', :field_id => counter)%>
-      </td>
-    </tr>
-  </table>
-
-  <h3><%= c_('Type') %></h3>
-  <%= labelled_radio_button 'Radio', "fields[#{counter}][kind]", 'radio', !field.multiple && !field.list %><br />
-  <%= labelled_radio_button 'Checkbox', "fields[#{counter}][kind]", 'check_box', field.multiple && !field.list %><br />
-  <%= labelled_radio_button 'Select', "fields[#{counter}][kind]", 'select', !field.multiple && field.list %><br />
-  <%= labelled_radio_button 'Multiple Select', "fields[#{counter}][kind]", 'multiple_select', field.multiple && field.list %><br />
-
-  <% button_bar do %>
-    <%= button :ok, _('Ok'), '#', :div_id => elem_id %>
-  <% end %>
-</div>
-


=====================================
plugins/custom_forms/views/custom_forms_plugin_myprofile/_empty_field.html.erb deleted
=====================================
--- a/plugins/custom_forms/views/custom_forms_plugin_myprofile/_empty_field.html.erb
+++ /dev/null
@@ -1,10 +0,0 @@
-<tr id="empty-field" style='display: none' last_id=<%= counter %>>
-  <td style="text-align: left"><%= text_field "fields[#{counter}]", :name, :size => 25 %></td>
-  <td><%= select "fields[#{counter}]", :type, type_options, :selected => type_for_options(field.class) %></td>
-  <td><%= check_box "fields[#{counter}]", :mandatory %></td>
-  <%= hidden_field "fields[#{counter}]", :form_id, :value => @form.id %>
-  <td class='actions'>
-    <%= button_without_text :edit, c_('Edit'), '', :field_id => counter %>
-    <%= button_without_text :remove, c_('Remove'), '#', class: 'remove-field', field_id: counter, data: {confirm: _('Are you sure you want to remove this field?')} %>
-  </td>
-</tr>


=====================================
plugins/custom_forms/views/custom_forms_plugin_myprofile/_empty_option.html.erb deleted
=====================================
--- a/plugins/custom_forms/views/custom_forms_plugin_myprofile/_empty_option.html.erb
+++ /dev/null
@@ -1,8 +0,0 @@
-<tr id=<%= "empty-option-#{counter}" %> option_id=<%= option_counter %> style="display: none;">
-  <td><%= text_field_tag("fields[#{counter}][choices][#{option_counter}][name]") %></td>
-  <td><%= text_field_tag("fields[#{counter}][choices][#{option_counter}][value]") %></td>
-  <td class='actions'>
-    <%= button_without_text :remove, c_('Remove'), '#', class: 'remove-option', field_id: counter, option_id: option_counter, data: {confirm: _('Are you sure you want to remove this option?')} %>
-  </td>
-</tr>
-


=====================================
plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_select_field.html.erb
=====================================
--- a/plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_select_field.html.erb
+++ b/plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_select_field.html.erb
@@ -1,14 +1,14 @@
 <%= render :layout => 'field', :locals => { :f => f } do %>
   <div class="field-select-type">
     <%= _('Type:') %>
-    <%= f.radio_button(:select_field_type, 'radio') %>
-    <%= f.label(:select_field_type, _('Radio'), :value => 'radio') %>
-    <%= f.radio_button(:select_field_type, 'check_box') %>
-    <%= f.label(:select_field_type, _('Checkbox'), :value => 'check_box') %>
-    <%= f.radio_button(:select_field_type, 'select') %>
-    <%= f.label(:select_field_type, _('Drop down'), :value => 'select') %>
-    <%= f.radio_button(:select_field_type, 'multiple_select') %>
-    <%= f.label(:select_field_type, _('Multiple Select'), :value => 'multiple_select') %>
+    <%= f.radio_button(:show_as, 'radio') %>
+    <%= f.label(:show_as, _('Radio'), :value => 'radio') %>
+    <%= f.radio_button(:show_as, 'check_box') %>
+    <%= f.label(:show_as, _('Checkbox'), :value => 'check_box') %>
+    <%= f.radio_button(:show_as, 'select') %>
+    <%= f.label(:show_as, _('Drop down'), :value => 'select') %>
+    <%= f.radio_button(:show_as, 'multiple_select') %>
+    <%= f.label(:show_as, _('Multiple Select'), :value => 'multiple_select') %>
   </div>
 
   <table>


=====================================
plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_text_field.html.erb
=====================================
--- a/plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_text_field.html.erb
+++ b/plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_text_field.html.erb
@@ -1,6 +1,15 @@
 <%= render :layout => 'field', :locals => { :f => f } do %>
+  <div class="field-select-type">
+    <%= _('Type:') %>
+    <%= f.radio_button(:show_as, 'text') %>
+    <%= f.label(:show_as, _('One-line text'), :value => 'text') %>
+    <%= f.radio_button(:show_as, 'textarea') %>
+    <%= f.label(:show_as, _('Multiline text'), :value => 'textarea') %>
+  </div>
+
   <div class="field-text-default">
     <%= f.label(:default_value, _('Default text:')) %>
     <%= f.text_field(:default_value) %>
+    <small><%= _('Maximum of 255 characters') %></small>
   </div>
 <% end %>



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/51517ea4b1ec9cc26ec729bdb482bf4f5fd4202f...1891de67bef4eb312e91293e1e6a280308d0659e
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20160317/b90ba5fb/attachment-0001.html>


More information about the Noosfero-dev mailing list