Commit c62890a7d80c411c72bbcf75d912dbbc2707f0db

Authored by Larissa Reis
1 parent 88c5d866

custom_forms: Adds support for textarea as an option for text field

plugins/custom_forms/db/migrate/20151008130948_create_text_field_type_in_custom_forms_plugin.rb 0 → 100644
@@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
  1 +class CreateTextFieldTypeInCustomFormsPlugin < ActiveRecord::Migration
  2 + def up
  3 + rename_column :custom_forms_plugin_fields, :select_field_type, :show_as
  4 + change_column :custom_forms_plugin_fields, :show_as, :string, :null => true, :default => nil
  5 + update("UPDATE custom_forms_plugin_fields SET show_as='text' WHERE type = 'CustomFormsPlugin::TextField'")
  6 + end
  7 +
  8 + def down
  9 + rename_column :custom_forms_plugin_fields, :show_as, :select_field_type
  10 + change_column :custom_forms_plugin_fields, :select_field_type, :string, :null => false, :default => 'radio'
  11 + update("UPDATE custom_forms_plugin_fields SET select_field_type='radio' WHERE type = 'CustomFormsPlugin::TextField'")
  12 + end
  13 +end
plugins/custom_forms/lib/custom_forms_plugin/field.rb
@@ -2,8 +2,9 @@ class CustomFormsPlugin::Field &lt; ActiveRecord::Base @@ -2,8 +2,9 @@ class CustomFormsPlugin::Field &lt; ActiveRecord::Base
2 self.table_name = :custom_forms_plugin_fields 2 self.table_name = :custom_forms_plugin_fields
3 3
4 validates_presence_of :name 4 validates_presence_of :name
  5 + validates_length_of :default_value, :maximum => 255
5 6
6 - attr_accessible :name, :form, :mandatory, :type, :position, :default_value, :select_field_type, :alternatives_attributes 7 + attr_accessible :name, :form, :mandatory, :type, :position, :default_value, :show_as, :alternatives_attributes
7 8
8 belongs_to :form, :class_name => 'CustomFormsPlugin::Form' 9 belongs_to :form, :class_name => 'CustomFormsPlugin::Form'
9 has_many :answers, :class_name => 'CustomFormsPlugin::Answer', :dependent => :destroy 10 has_many :answers, :class_name => 'CustomFormsPlugin::Answer', :dependent => :destroy
plugins/custom_forms/lib/custom_forms_plugin/helper.rb
@@ -85,7 +85,11 @@ module CustomFormsPlugin::Helper @@ -85,7 +85,11 @@ module CustomFormsPlugin::Helper
85 85
86 def display_text_field(field, answer, form) 86 def display_text_field(field, answer, form)
87 value = answer.present? ? answer.value : field.default_value 87 value = answer.present? ? answer.value : field.default_value
88 - text_field(form, "#{field.id}", :value => value, :disabled => display_disabled?(field, answer)) 88 + if field.show_as == 'textarea'
  89 + text_area(form, "#{field.id}", :value => value, :disabled => display_disabled?(field, answer))
  90 + else
  91 + text_field(form, "#{field.id}", :value => value, :disabled => display_disabled?(field, answer))
  92 + end
89 end 93 end
90 94
91 def default_selected(field, answer) 95 def default_selected(field, answer)
@@ -93,7 +97,7 @@ module CustomFormsPlugin::Helper @@ -93,7 +97,7 @@ module CustomFormsPlugin::Helper
93 end 97 end
94 98
95 def display_select_field(field, answer, form) 99 def display_select_field(field, answer, form)
96 - case field.select_field_type 100 + case field.show_as
97 when 'select' 101 when 'select'
98 selected = default_selected(field, answer) 102 selected = default_selected(field, answer)
99 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) 103 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 @@ -114,11 +118,11 @@ module CustomFormsPlugin::Helper
114 end 118 end
115 119
116 def radio_button?(field) 120 def radio_button?(field)
117 - type_for_options(field.class) == 'select_field' && field.select_field_type == 'radio' 121 + type_for_options(field.class) == 'select_field' && field.show_as == 'radio'
118 end 122 end
119 123
120 def check_box?(field) 124 def check_box?(field)
121 - type_for_options(field.class) == 'select_field' && field.select_field_type == 'check_box' 125 + type_for_options(field.class) == 'select_field' && field.show_as == 'check_box'
122 end 126 end
123 127
124 end 128 end
plugins/custom_forms/lib/custom_forms_plugin/select_field.rb
1 class CustomFormsPlugin::SelectField < CustomFormsPlugin::Field 1 class CustomFormsPlugin::SelectField < CustomFormsPlugin::Field
2 self.table_name = :custom_forms_plugin_fields 2 self.table_name = :custom_forms_plugin_fields
3 - validates_inclusion_of :select_field_type, :in => %w(radio check_box select multiple_select) 3 + validates_inclusion_of :show_as, :in => %w(radio check_box select multiple_select)
4 validates_length_of :alternatives, :minimum => 1, :message => 'can\'t be empty' 4 validates_length_of :alternatives, :minimum => 1, :message => 'can\'t be empty'
  5 +
  6 + after_initialize do
  7 + self.show_as ||= 'radio'
  8 + end
5 end 9 end
plugins/custom_forms/lib/custom_forms_plugin/text_field.rb
@@ -2,4 +2,9 @@ class CustomFormsPlugin::TextField &lt; CustomFormsPlugin::Field @@ -2,4 +2,9 @@ class CustomFormsPlugin::TextField &lt; CustomFormsPlugin::Field
2 2
3 self.table_name = :custom_forms_plugin_fields 3 self.table_name = :custom_forms_plugin_fields
4 4
  5 + validates_inclusion_of :show_as, :in => %w(text textarea)
  6 +
  7 + after_initialize do
  8 + self.show_as ||= 'text'
  9 + end
5 end 10 end
plugins/custom_forms/public/style.css
@@ -95,6 +95,11 @@ tr.addition-buttons { @@ -95,6 +95,11 @@ tr.addition-buttons {
95 color: rgba(0,0,0,0.5); 95 color: rgba(0,0,0,0.5);
96 } 96 }
97 97
  98 +#custom-forms-plugin_submission textarea {
  99 + width: 100%;
  100 + height: 10em;
  101 +}
  102 +
98 #custom-forms-plugin_submission-view th { 103 #custom-forms-plugin_submission-view th {
99 border: none; 104 border: none;
100 text-align: right; 105 text-align: right;
plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb
@@ -54,7 +54,7 @@ class CustomFormsPluginMyprofileControllerTest &lt; ActionController::TestCase @@ -54,7 +54,7 @@ class CustomFormsPluginMyprofileControllerTest &lt; ActionController::TestCase
54 }, 54 },
55 2 => { 55 2 => {
56 :name => 'Color', 56 :name => 'Color',
57 - :select_field_type => 'radio', 57 + :show_as => 'radio',
58 :type => 'CustomFormsPlugin::SelectField', 58 :type => 'CustomFormsPlugin::SelectField',
59 :alternatives_attributes => { 59 :alternatives_attributes => {
60 1 => {:label => 'Red'}, 60 1 => {:label => 'Red'},
@@ -82,7 +82,7 @@ class CustomFormsPluginMyprofileControllerTest &lt; ActionController::TestCase @@ -82,7 +82,7 @@ class CustomFormsPluginMyprofileControllerTest &lt; ActionController::TestCase
82 82
83 assert_equal 'Color', f2.name 83 assert_equal 'Color', f2.name
84 assert_equal f2.alternatives.map(&:label).sort, ['Red', 'Blue', 'Black'].sort 84 assert_equal f2.alternatives.map(&:label).sort, ['Red', 'Blue', 'Black'].sort
85 - assert_equal f2.select_field_type, 'radio' 85 + assert_equal f2.show_as, 'radio'
86 assert f2.kind_of?(CustomFormsPlugin::SelectField) 86 assert f2.kind_of?(CustomFormsPlugin::SelectField)
87 end 87 end
88 88
plugins/custom_forms/test/unit/custom_forms_plugin/select_field_test.rb
@@ -13,16 +13,16 @@ class CustomFormsPlugin::SelectFieldTest &lt; ActiveSupport::TestCase @@ -13,16 +13,16 @@ class CustomFormsPlugin::SelectFieldTest &lt; ActiveSupport::TestCase
13 select = CustomFormsPlugin::SelectField.new(:name => 'select_field001' ) 13 select = CustomFormsPlugin::SelectField.new(:name => 'select_field001' )
14 select.alternatives << CustomFormsPlugin::Alternative.new(:label => 'option') 14 select.alternatives << CustomFormsPlugin::Alternative.new(:label => 'option')
15 15
16 - select.update_attribute(:select_field_type, 'random') 16 + select.update_attribute(:show_as, 'random')
17 assert select.invalid? 17 assert select.invalid?
18 18
19 - select.update_attribute(:select_field_type, 'radio') 19 + select.update_attribute(:show_as, 'radio')
20 assert select.valid? 20 assert select.valid?
21 - select.update_attribute(:select_field_type, 'check_box') 21 + select.update_attribute(:show_as, 'check_box')
22 assert select.valid? 22 assert select.valid?
23 - select.update_attribute(:select_field_type, 'select') 23 + select.update_attribute(:show_as, 'select')
24 assert select.valid? 24 assert select.valid?
25 - select.update_attribute(:select_field_type, 'multiple_select') 25 + select.update_attribute(:show_as, 'multiple_select')
26 assert select.valid? 26 assert select.valid?
27 end 27 end
28 end 28 end
plugins/custom_forms/test/unit/custom_forms_plugin/text_field_test.rb 0 → 100644
@@ -0,0 +1,22 @@ @@ -0,0 +1,22 @@
  1 +require File.dirname(__FILE__) + '/../../../../../test/test_helper'
  2 +
  3 +class CustomFormsPlugin::TextFieldTest < ActiveSupport::TestCase
  4 + should 'validate type' do
  5 + text = CustomFormsPlugin::TextField.new(:name => 'text-field-010' )
  6 +
  7 + text.update_attribute(:show_as, 'random')
  8 + assert text.invalid?
  9 + text.update_attribute(:show_as, 'radio')
  10 + assert text.invalid?
  11 +
  12 + text.update_attribute(:show_as, 'text')
  13 + assert text.valid?
  14 + text.update_attribute(:show_as, 'textarea')
  15 + assert text.valid?
  16 + end
  17 +
  18 + should 'field type defaults to text when initialized' do
  19 + text = CustomFormsPlugin::TextField.new(:name => 'text_field001' )
  20 + assert_equal 'text', text.show_as
  21 + end
  22 +end
plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_select_field.html.erb
1 <%= render :layout => 'field', :locals => { :f => f } do %> 1 <%= render :layout => 'field', :locals => { :f => f } do %>
2 <div class="field-select-type"> 2 <div class="field-select-type">
3 <%= _('Type:') %> 3 <%= _('Type:') %>
4 - <%= f.radio_button(:select_field_type, 'radio') %>  
5 - <%= f.label(:select_field_type, _('Radio'), :value => 'radio') %>  
6 - <%= f.radio_button(:select_field_type, 'check_box') %>  
7 - <%= f.label(:select_field_type, _('Checkbox'), :value => 'check_box') %>  
8 - <%= f.radio_button(:select_field_type, 'select') %>  
9 - <%= f.label(:select_field_type, _('Drop down'), :value => 'select') %>  
10 - <%= f.radio_button(:select_field_type, 'multiple_select') %>  
11 - <%= f.label(:select_field_type, _('Multiple Select'), :value => 'multiple_select') %> 4 + <%= f.radio_button(:show_as, 'radio') %>
  5 + <%= f.label(:show_as, _('Radio'), :value => 'radio') %>
  6 + <%= f.radio_button(:show_as, 'check_box') %>
  7 + <%= f.label(:show_as, _('Checkbox'), :value => 'check_box') %>
  8 + <%= f.radio_button(:show_as, 'select') %>
  9 + <%= f.label(:show_as, _('Drop down'), :value => 'select') %>
  10 + <%= f.radio_button(:show_as, 'multiple_select') %>
  11 + <%= f.label(:show_as, _('Multiple Select'), :value => 'multiple_select') %>
12 </div> 12 </div>
13 13
14 <table> 14 <table>
plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_text_field.html.erb
1 <%= render :layout => 'field', :locals => { :f => f } do %> 1 <%= render :layout => 'field', :locals => { :f => f } do %>
  2 + <div class="field-select-type">
  3 + <%= _('Type:') %>
  4 + <%= f.radio_button(:show_as, 'text') %>
  5 + <%= f.label(:show_as, _('One-line text'), :value => 'text') %>
  6 + <%= f.radio_button(:show_as, 'textarea') %>
  7 + <%= f.label(:show_as, _('Multiline text'), :value => 'textarea') %>
  8 + </div>
  9 +
2 <div class="field-text-default"> 10 <div class="field-text-default">
3 <%= f.label(:default_value, _('Default text:')) %> 11 <%= f.label(:default_value, _('Default text:')) %>
4 <%= f.text_field(:default_value) %> 12 <%= f.text_field(:default_value) %>
  13 + <small><%= _('Maximum of 255 characters') %></small>
5 </div> 14 </div>
6 <% end %> 15 <% end %>