Commit 1891de67bef4eb312e91293e1e6a280308d0659e
Exists in
web_steps_improvements
and in
9 other branches
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
Showing
14 changed files
with
84 additions
and
71 deletions
Show diff stats
plugins/custom_forms/db/migrate/20151008130948_create_text_field_type_in_custom_forms_plugin.rb
0 → 100644
... | ... | @@ -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 < ActiveRecord::Base |
2 | 2 | self.table_name = :custom_forms_plugin_fields |
3 | 3 | |
4 | 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 | 9 | belongs_to :form, :class_name => 'CustomFormsPlugin::Form' |
9 | 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 | 85 | |
86 | 86 | def display_text_field(field, answer, form) |
87 | 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 | 93 | end |
90 | 94 | |
91 | 95 | def default_selected(field, answer) |
... | ... | @@ -93,7 +97,7 @@ module CustomFormsPlugin::Helper |
93 | 97 | end |
94 | 98 | |
95 | 99 | def display_select_field(field, answer, form) |
96 | - case field.select_field_type | |
100 | + case field.show_as | |
97 | 101 | when 'select' |
98 | 102 | selected = default_selected(field, answer) |
99 | 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 | 118 | end |
115 | 119 | |
116 | 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 | 122 | end |
119 | 123 | |
120 | 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 | 126 | end |
123 | 127 | |
124 | 128 | end | ... | ... |
plugins/custom_forms/lib/custom_forms_plugin/select_field.rb
1 | 1 | class CustomFormsPlugin::SelectField < CustomFormsPlugin::Field |
2 | 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 | 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 | 9 | end | ... | ... |
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
... | ... | @@ -54,7 +54,7 @@ class CustomFormsPluginMyprofileControllerTest < ActionController::TestCase |
54 | 54 | }, |
55 | 55 | 2 => { |
56 | 56 | :name => 'Color', |
57 | - :select_field_type => 'radio', | |
57 | + :show_as => 'radio', | |
58 | 58 | :type => 'CustomFormsPlugin::SelectField', |
59 | 59 | :alternatives_attributes => { |
60 | 60 | 1 => {:label => 'Red'}, |
... | ... | @@ -82,7 +82,7 @@ class CustomFormsPluginMyprofileControllerTest < ActionController::TestCase |
82 | 82 | |
83 | 83 | assert_equal 'Color', f2.name |
84 | 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 | 86 | assert f2.kind_of?(CustomFormsPlugin::SelectField) |
87 | 87 | end |
88 | 88 | ... | ... |
plugins/custom_forms/test/unit/custom_forms_plugin/select_field_test.rb
... | ... | @@ -13,16 +13,16 @@ class CustomFormsPlugin::SelectFieldTest < ActiveSupport::TestCase |
13 | 13 | select = CustomFormsPlugin::SelectField.new(:name => 'select_field001' ) |
14 | 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 | 17 | assert select.invalid? |
18 | 18 | |
19 | - select.update_attribute(:select_field_type, 'radio') | |
19 | + select.update_attribute(:show_as, 'radio') | |
20 | 20 | assert select.valid? |
21 | - select.update_attribute(:select_field_type, 'check_box') | |
21 | + select.update_attribute(:show_as, 'check_box') | |
22 | 22 | assert select.valid? |
23 | - select.update_attribute(:select_field_type, 'select') | |
23 | + select.update_attribute(:show_as, 'select') | |
24 | 24 | assert select.valid? |
25 | - select.update_attribute(:select_field_type, 'multiple_select') | |
25 | + select.update_attribute(:show_as, 'multiple_select') | |
26 | 26 | assert select.valid? |
27 | 27 | end |
28 | 28 | end | ... | ... |
plugins/custom_forms/test/unit/custom_forms_plugin/text_field_test.rb
0 → 100644
... | ... | @@ -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/_edit_select.html.erb
... | ... | @@ -1,32 +0,0 @@ |
1 | -<div class='edit-information edit-select'> | |
2 | - <h2><%= c_('Options') %></h2> | |
3 | - <table class='action-table' style='width: 420px'> | |
4 | - <tr> | |
5 | - <th style='width: 40%'><%= _('Name') %></th> | |
6 | - <th style='width: 40%'><%= _('Value') %></th> | |
7 | - <th style='width: 20%'><%= c_('Delete') %></th> | |
8 | - </tr> | |
9 | - <% option_counter = 1 %> | |
10 | - <% (field.choices || {}).each do |name, value| %> | |
11 | - <%= render :partial => 'option', :locals => {:name => name, :value => value, :counter => counter, :option_counter => option_counter} %> | |
12 | - <% option_counter += 1 %> | |
13 | - <% end %> | |
14 | - <%= render :partial => 'empty_option', :locals => {:counter => counter, :option_counter => option_counter} %> | |
15 | - <tr class='new-item'> | |
16 | - <td colspan='3'> | |
17 | - <%= button(:add, _('Add a new option'), '#', :class => 'new-option', :field_id => counter)%> | |
18 | - </td> | |
19 | - </tr> | |
20 | - </table> | |
21 | - | |
22 | - <h3><%= c_('Type') %></h3> | |
23 | - <%= labelled_radio_button 'Radio', "fields[#{counter}][kind]", 'radio', !field.multiple && !field.list %><br /> | |
24 | - <%= labelled_radio_button 'Checkbox', "fields[#{counter}][kind]", 'check_box', field.multiple && !field.list %><br /> | |
25 | - <%= labelled_radio_button 'Select', "fields[#{counter}][kind]", 'select', !field.multiple && field.list %><br /> | |
26 | - <%= labelled_radio_button 'Multiple Select', "fields[#{counter}][kind]", 'multiple_select', field.multiple && field.list %><br /> | |
27 | - | |
28 | - <% button_bar do %> | |
29 | - <%= button :ok, _('Ok'), '#', :div_id => elem_id %> | |
30 | - <% end %> | |
31 | -</div> | |
32 | - |
plugins/custom_forms/views/custom_forms_plugin_myprofile/_empty_field.html.erb
... | ... | @@ -1,10 +0,0 @@ |
1 | -<tr id="empty-field" style='display: none' last_id=<%= counter %>> | |
2 | - <td style="text-align: left"><%= text_field "fields[#{counter}]", :name, :size => 25 %></td> | |
3 | - <td><%= select "fields[#{counter}]", :type, type_options, :selected => type_for_options(field.class) %></td> | |
4 | - <td><%= check_box "fields[#{counter}]", :mandatory %></td> | |
5 | - <%= hidden_field "fields[#{counter}]", :form_id, :value => @form.id %> | |
6 | - <td class='actions'> | |
7 | - <%= button_without_text :edit, c_('Edit'), '', :field_id => counter %> | |
8 | - <%= button_without_text :remove, c_('Remove'), '#', class: 'remove-field', field_id: counter, data: {confirm: _('Are you sure you want to remove this field?')} %> | |
9 | - </td> | |
10 | -</tr> |
plugins/custom_forms/views/custom_forms_plugin_myprofile/_empty_option.html.erb
... | ... | @@ -1,8 +0,0 @@ |
1 | -<tr id=<%= "empty-option-#{counter}" %> option_id=<%= option_counter %> style="display: none;"> | |
2 | - <td><%= text_field_tag("fields[#{counter}][choices][#{option_counter}][name]") %></td> | |
3 | - <td><%= text_field_tag("fields[#{counter}][choices][#{option_counter}][value]") %></td> | |
4 | - <td class='actions'> | |
5 | - <%= 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?')} %> | |
6 | - </td> | |
7 | -</tr> | |
8 | - |
plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_select_field.html.erb
1 | 1 | <%= render :layout => 'field', :locals => { :f => f } do %> |
2 | 2 | <div class="field-select-type"> |
3 | 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 | 12 | </div> |
13 | 13 | |
14 | 14 | <table> | ... | ... |
plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_text_field.html.erb
1 | 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 | 10 | <div class="field-text-default"> |
3 | 11 | <%= f.label(:default_value, _('Default text:')) %> |
4 | 12 | <%= f.text_field(:default_value) %> |
13 | + <small><%= _('Maximum of 255 characters') %></small> | |
5 | 14 | </div> |
6 | 15 | <% end %> | ... | ... |