diff --git a/plugins/custom_forms/db/migrate/20151008130948_create_text_field_type_in_custom_forms_plugin.rb b/plugins/custom_forms/db/migrate/20151008130948_create_text_field_type_in_custom_forms_plugin.rb new file mode 100644 index 0000000..d3e8ee7 --- /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 diff --git a/plugins/custom_forms/lib/custom_forms_plugin/field.rb b/plugins/custom_forms/lib/custom_forms_plugin/field.rb index c96070e..ae3d2d0 100644 --- 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 diff --git a/plugins/custom_forms/lib/custom_forms_plugin/helper.rb b/plugins/custom_forms/lib/custom_forms_plugin/helper.rb index 6995fd1..7984b6c 100644 --- 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 diff --git a/plugins/custom_forms/lib/custom_forms_plugin/select_field.rb b/plugins/custom_forms/lib/custom_forms_plugin/select_field.rb index d91bbea..db7aa37 100644 --- 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 diff --git a/plugins/custom_forms/lib/custom_forms_plugin/text_field.rb b/plugins/custom_forms/lib/custom_forms_plugin/text_field.rb index 7f694b9..54a8c0d 100644 --- 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 diff --git a/plugins/custom_forms/public/style.css b/plugins/custom_forms/public/style.css index 635aeb6..4f03693 100644 --- 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; diff --git 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 index 66cecd6..0dd8070 100644 --- 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 diff --git 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 index 7e255e6..94795d4 100644 --- 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 diff --git a/plugins/custom_forms/test/unit/custom_forms_plugin/text_field_test.rb b/plugins/custom_forms/test/unit/custom_forms_plugin/text_field_test.rb new file mode 100644 index 0000000..2e1b79a --- /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 diff --git 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 index ebb221b..bf8af8e 100644 --- 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 %>
<%= _('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') %>
diff --git 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 index f1d0e11..8a89368 100644 --- 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 %> +
+ <%= _('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') %> +
+
<%= f.label(:default_value, _('Default text:')) %> <%= f.text_field(:default_value) %> + <%= _('Maximum of 255 characters') %>
<% end %> -- libgit2 0.21.2