From c224f8a93b63bb7c02398390218daba4303a41d1 Mon Sep 17 00:00:00 2001 From: Lucas Melo Date: Fri, 13 Sep 2013 14:53:00 -0300 Subject: [PATCH] custom_forms: use rails nested forms in form partial --- plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb | 137 +++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------ plugins/custom_forms/controllers/custom_forms_plugin_profile_controller.rb | 32 ++++++++++++++++++++++---------- plugins/custom_forms/db/migrate/20130823134700_create_custom_forms_plugin_alternatives.rb | 12 ++++++++++++ plugins/custom_forms/db/migrate/20130823135600_add_select_field_type_to_custom_forms_plugin_fields.rb | 29 +++++++++++++++++++++++++++++ plugins/custom_forms/db/migrate/20130823151900_associate_fields_to_alternatives.rb | 25 +++++++++++++++++++++++++ plugins/custom_forms/db/migrate/20131002155900_update_select_field_type_in_custom_forms_plugin_fields.rb | 9 +++++++++ plugins/custom_forms/db/migrate/20131007120600_add_selected_by_default_to_custom_forms_plugin_alternatives.rb | 14 ++++++++++++++ plugins/custom_forms/lib/custom_forms_plugin/alternative.rb | 8 ++++++++ plugins/custom_forms/lib/custom_forms_plugin/answer.rb | 2 +- plugins/custom_forms/lib/custom_forms_plugin/field.rb | 17 +++++++---------- plugins/custom_forms/lib/custom_forms_plugin/form.rb | 4 +++- plugins/custom_forms/lib/custom_forms_plugin/helper.rb | 54 +++++++++++++++++++++++++++++++----------------------- plugins/custom_forms/lib/custom_forms_plugin/select_field.rb | 4 +++- plugins/custom_forms/lib/custom_forms_plugin/submission.rb | 11 ++++++++++- plugins/custom_forms/public/field.js | 170 ++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------------------------- plugins/custom_forms/public/style.css | 24 ++++++++++++++++++++++++ plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb | 61 +++++++++++++++++++++++++++++++++---------------------------- plugins/custom_forms/test/functional/custom_forms_plugin_profile_controller_test.rb | 2 +- plugins/custom_forms/test/unit/custom_forms_plugin/answer_test.rb | 4 ++-- plugins/custom_forms/test/unit/custom_forms_plugin/field_test.rb | 61 ------------------------------------------------------------- plugins/custom_forms/test/unit/custom_forms_plugin/membership_survey_test.rb | 2 +- plugins/custom_forms/test/unit/custom_forms_plugin/select_field_test.rb | 20 +++++++++++++------- plugins/custom_forms/views/custom_forms_plugin_myprofile/_edit_select.html.erb | 5 ++--- plugins/custom_forms/views/custom_forms_plugin_myprofile/_edit_text.html.erb | 10 ---------- plugins/custom_forms/views/custom_forms_plugin_myprofile/_field.html.erb | 28 ++++++++++++++++------------ plugins/custom_forms/views/custom_forms_plugin_myprofile/_form.html.erb | 69 +++++++++++++++++++++++---------------------------------------------- plugins/custom_forms/views/custom_forms_plugin_myprofile/_option.html.erb | 7 ------- plugins/custom_forms/views/custom_forms_plugin_myprofile/create.html.erb | 2 -- plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_alternative.html.erb | 10 ++++++++++ plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_field.html.erb | 9 +++++++++ plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_select_field.html.erb | 31 +++++++++++++++++++++++++++++++ plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_text_field.html.erb | 6 ++++++ plugins/custom_forms/views/custom_forms_plugin_myprofile/edit.html.erb | 5 ++++- plugins/custom_forms/views/custom_forms_plugin_myprofile/index.html.erb | 2 +- plugins/custom_forms/views/custom_forms_plugin_myprofile/new.html.erb | 5 +++++ public/stylesheets/application.css | 4 ++-- 36 files changed, 420 insertions(+), 475 deletions(-) create mode 100644 plugins/custom_forms/db/migrate/20130823134700_create_custom_forms_plugin_alternatives.rb create mode 100644 plugins/custom_forms/db/migrate/20130823135600_add_select_field_type_to_custom_forms_plugin_fields.rb create mode 100644 plugins/custom_forms/db/migrate/20130823151900_associate_fields_to_alternatives.rb create mode 100644 plugins/custom_forms/db/migrate/20131002155900_update_select_field_type_in_custom_forms_plugin_fields.rb create mode 100644 plugins/custom_forms/db/migrate/20131007120600_add_selected_by_default_to_custom_forms_plugin_alternatives.rb create mode 100644 plugins/custom_forms/lib/custom_forms_plugin/alternative.rb delete mode 100644 plugins/custom_forms/views/custom_forms_plugin_myprofile/_edit_text.html.erb delete mode 100644 plugins/custom_forms/views/custom_forms_plugin_myprofile/_option.html.erb delete mode 100644 plugins/custom_forms/views/custom_forms_plugin_myprofile/create.html.erb create mode 100644 plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_alternative.html.erb create mode 100644 plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_field.html.erb create mode 100644 plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_select_field.html.erb create mode 100644 plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_text_field.html.erb create mode 100644 plugins/custom_forms/views/custom_forms_plugin_myprofile/new.html.erb diff --git a/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb b/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb index 18321c6..0663fe3 100644 --- a/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb +++ b/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb @@ -1,47 +1,46 @@ class CustomFormsPluginMyprofileController < MyProfileController - protect 'post_content', :profile + def index @forms = CustomFormsPlugin::Form.from(profile) end + def new + @form = CustomFormsPlugin::Form.new + + respond_to do |format| + format.html + end + end + def create - @form = CustomFormsPlugin::Form.new(:profile => profile) - @fields = [] - @empty_field = CustomFormsPlugin::Field.new - if request.post? - begin - @form.update_attributes!(params[:form]) - params[:fields] = format_kind(params[:fields]) - params[:fields] = format_choices(params[:fields]) - params[:fields] = set_form_id(params[:fields], @form.id) - create_fields(new_fields(params)) - session['notice'] = _('Form created') - redirect_to :action => 'index' - rescue Exception => exception - logger.error(exception.to_s) - session['notice'] = _('Form could not be created') + params[:form][:profile_id] = profile.id + @form = CustomFormsPlugin::Form.new(params[:form]) + + respond_to do |format| + if @form.save + flash[:notice] = _("Custom form #{@form.name} was successfully created.") + format.html { redirect_to(:action=>'index') } + else + format.html { render :action => 'new' } end end end def edit @form = CustomFormsPlugin::Form.find(params[:id]) - @fields = @form.fields - @empty_field = CustomFormsPlugin::TextField.new - if request.post? - begin - @form.update_attributes!(params[:form]) - params[:fields] = format_kind(params[:fields]) - params[:fields] = format_choices(params[:fields]) - remove_fields(params, @form) - create_fields(new_fields(params)) - update_fields(edited_fields(params)) - session['notice'] = _('Form updated') - redirect_to :action => 'index' - rescue Exception => exception - logger.error(exception.to_s) + end + + def update + @form = CustomFormsPlugin::Form.find(params[:id]) + + respond_to do |format| + if @form.update_attributes(params[:form]) + flash[:notice] = _("Custom form #{@form.name} was successfully updated.") + format.html { redirect_to(:action=>'index') } + else session['notice'] = _('Form could not be updated') + format.html { render :action => 'edit' } end end end @@ -67,82 +66,4 @@ class CustomFormsPluginMyprofileController < MyProfileController @form = @submission.form end - private - - def new_fields(params) - keys = params[:fields].keys.sort{|a, b| a.to_i <=> b.to_i} - result = keys.map { |id| (hash = params[:fields][id]).has_key?(:real_id) ? nil : hash}.compact - result.delete_if {|field| field[:name].blank?} - result - end - - def edited_fields(params) - params[:fields].map {|id, hash| hash.has_key?(:real_id) ? hash : nil}.compact - end - - def create_fields(fields) - fields.each do |field| - case field[:type] - when 'text_field' - CustomFormsPlugin::TextField.create!(field) - when 'select_field' - CustomFormsPlugin::SelectField.create!(field) - else - CustomFormsPlugin::Field.create!(field) - end - end - end - - def update_fields(fields) - fields.each do |field_attrs| - field = CustomFormsPlugin::Field.find(field_attrs.delete(:real_id)) - field.attributes = field_attrs - field.save! if field.changed? - end - end - - def format_kind(fields) - fields.each do |id, field| - next if field[:kind].blank? - kind = field.delete(:kind) - case kind - when 'radio' - field[:list] = false - field[:multiple] = false - when 'check_box' - field[:list] = false - field[:multiple] = true - when 'select' - field[:list] = true - field[:multiple] = false - when 'multiple_select' - field[:list] = true - field[:multiple] = true - end - end - fields - end - - def format_choices(fields) - fields.each do |id, field| - next if !field.has_key?(:choices) - field[:choices] = field[:choices].map {|key, value| value}.inject({}) do |result, choice| - hash = (choice[:name].blank? || choice[:value].blank?) ? {} : {choice[:name] => choice[:value]} - result.merge!(hash) - end - end - fields - end - - def remove_fields(params, form) - present_fields = params[:fields].map{|id, value| value}.collect {|field| field[:real_id]}.compact - form.fields.each {|field| field.destroy if !present_fields.include?(field.id.to_s) } - end - - def set_form_id(fields, form_id) - fields.each do |id, field| - field[:form_id] = form_id - end - fields - end end diff --git a/plugins/custom_forms/controllers/custom_forms_plugin_profile_controller.rb b/plugins/custom_forms/controllers/custom_forms_plugin_profile_controller.rb index 6bc0142..b040a1d 100644 --- a/plugins/custom_forms/controllers/custom_forms_plugin_profile_controller.rb +++ b/plugins/custom_forms/controllers/custom_forms_plugin_profile_controller.rb @@ -1,8 +1,9 @@ class CustomFormsPluginProfileController < ProfileController - before_filter :has_access, :show def show + extend(CustomFormsPlugin::Helper) + @form = CustomFormsPlugin::Form.find(params[:id]) if user @submission ||= CustomFormsPlugin::Submission.find_by_form_id_and_profile_id(@form.id,user.id) @@ -10,22 +11,33 @@ class CustomFormsPluginProfileController < ProfileController else @submission ||= CustomFormsPlugin::Submission.new(:form_id => @form.id) end + + # build the answers + @submission.answers.push(*(answers = build_answers(params[:submission], @form))) if params[:submission] + if request.post? begin - extend(CustomFormsPlugin::Helper) - answers = build_answers(params[:submission], @form) + raise 'Submission already present!' if CustomFormsPlugin::Submission.find_by_form_id_and_profile_id(@form.id,user.id) + + # @submission.answers for some reason has the same answer twice failed_answers = answers.select {|answer| !answer.valid? } + if failed_answers.empty? - if !user - @submission.author_name = params[:author_name] - @submission.author_email = params[:author_email] + # Save the submission + ActiveRecord::Base.transaction do + if !user + @submission.author_name = params[:author_name] + @submission.author_email = params[:author_email] + end + @submission.save! end - @submission.save! - answers.map {|answer| answer.submission = @submission; answer.save!} else - @submission.valid? + @submission.errors.clear failed_answers.each do |answer| - @submission.errors.add(answer.field.name.to_sym, answer.errors[answer.field.slug.to_sym]) + answer.valid? + answer.errors.each do |attribute, msg| + @submission.errors.add(answer.field.id.to_s.to_sym, msg) + end end raise 'Submission failed: answers not valid' end diff --git a/plugins/custom_forms/db/migrate/20130823134700_create_custom_forms_plugin_alternatives.rb b/plugins/custom_forms/db/migrate/20130823134700_create_custom_forms_plugin_alternatives.rb new file mode 100644 index 0000000..5a936af --- /dev/null +++ b/plugins/custom_forms/db/migrate/20130823134700_create_custom_forms_plugin_alternatives.rb @@ -0,0 +1,12 @@ +class CreateCustomFormsPluginAlternatives < ActiveRecord::Migration + def self.up + create_table :custom_forms_plugin_alternatives do |t| + t.string :label + t.references :field + end + end + + def self.down + drop_table :custom_forms_plugin_alternatives + end +end diff --git a/plugins/custom_forms/db/migrate/20130823135600_add_select_field_type_to_custom_forms_plugin_fields.rb b/plugins/custom_forms/db/migrate/20130823135600_add_select_field_type_to_custom_forms_plugin_fields.rb new file mode 100644 index 0000000..93545fa --- /dev/null +++ b/plugins/custom_forms/db/migrate/20130823135600_add_select_field_type_to_custom_forms_plugin_fields.rb @@ -0,0 +1,29 @@ +class AddSelectFieldTypeToCustomFormsPluginFields < ActiveRecord::Migration + def self.up + change_table :custom_forms_plugin_fields do |t| + t.remove :position + t.string :select_field_type + end + + CustomFormsPlugin::Field.find_each do |f| + if !f.list && !f.multiple + f.select_field_type = :radio + elsif !f.list && f.multiple + f.select_field_type = :check_box + elsif f.list && !f.multiple + f.select_field_type = :select + else + f.select_field_type = :multiple_select + end + f.save! + end + + change_table :custom_forms_plugin_fields do |t| + t.remove :multiple, :list + end + end + + def self.down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/plugins/custom_forms/db/migrate/20130823151900_associate_fields_to_alternatives.rb b/plugins/custom_forms/db/migrate/20130823151900_associate_fields_to_alternatives.rb new file mode 100644 index 0000000..5ecf512 --- /dev/null +++ b/plugins/custom_forms/db/migrate/20130823151900_associate_fields_to_alternatives.rb @@ -0,0 +1,25 @@ +class AssociateFieldsToAlternatives < ActiveRecord::Migration + class CustomFormsPlugin::Field < ActiveRecord::Base + set_table_name :custom_forms_plugin_fields + has_many :alternatives, :class_name => 'CustomFormsPlugin::Alternative' + serialize :choices, Hash + end + + def self.up + CustomFormsPlugin::Field.reset_column_information + + CustomFormsPlugin::Field.find_each do |f| + f.choices.each do |key, value| + CustomFormsPlugin::Alternative.create!(:label => key, :field_id => f.id) + end + end + + change_table :custom_forms_plugin_fields do |t| + t.remove :choices + end + end + + def self.down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/plugins/custom_forms/db/migrate/20131002155900_update_select_field_type_in_custom_forms_plugin_fields.rb b/plugins/custom_forms/db/migrate/20131002155900_update_select_field_type_in_custom_forms_plugin_fields.rb new file mode 100644 index 0000000..db9564d --- /dev/null +++ b/plugins/custom_forms/db/migrate/20131002155900_update_select_field_type_in_custom_forms_plugin_fields.rb @@ -0,0 +1,9 @@ +class UpdateSelectFieldTypeInCustomFormsPluginFields < ActiveRecord::Migration + def self.up + change_column :custom_forms_plugin_fields, :select_field_type, :string, :null => false, :default => 'radio' + end + + def self.down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/plugins/custom_forms/db/migrate/20131007120600_add_selected_by_default_to_custom_forms_plugin_alternatives.rb b/plugins/custom_forms/db/migrate/20131007120600_add_selected_by_default_to_custom_forms_plugin_alternatives.rb new file mode 100644 index 0000000..e09e8b8 --- /dev/null +++ b/plugins/custom_forms/db/migrate/20131007120600_add_selected_by_default_to_custom_forms_plugin_alternatives.rb @@ -0,0 +1,14 @@ +class AddSelectedByDefaultToCustomFormsPluginAlternatives < ActiveRecord::Migration + def self.up + add_column :custom_forms_plugin_alternatives, :selected_by_default, :boolean, :null => false, :default => false + CustomFormsPlugin::Field.find_each do |f| + f.alternatives.each do |a| + a.update_attribute(:selected_by_default, true) if a.label == f.default_value + end + end + end + + def self.down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/plugins/custom_forms/lib/custom_forms_plugin/alternative.rb b/plugins/custom_forms/lib/custom_forms_plugin/alternative.rb new file mode 100644 index 0000000..81aee63 --- /dev/null +++ b/plugins/custom_forms/lib/custom_forms_plugin/alternative.rb @@ -0,0 +1,8 @@ +class CustomFormsPlugin::Alternative < ActiveRecord::Base + set_table_name :custom_forms_plugin_alternatives + + validates_presence_of :label + + belongs_to :field, :class_name => 'CustomFormsPlugin::Field' +end + diff --git a/plugins/custom_forms/lib/custom_forms_plugin/answer.rb b/plugins/custom_forms/lib/custom_forms_plugin/answer.rb index 418ef60..f66561f 100644 --- a/plugins/custom_forms/lib/custom_forms_plugin/answer.rb +++ b/plugins/custom_forms/lib/custom_forms_plugin/answer.rb @@ -7,7 +7,7 @@ class CustomFormsPlugin::Answer < Noosfero::Plugin::ActiveRecord def value_mandatory if field.mandatory && value.blank? - errors.add(field.slug.to_sym, _("is mandatory.").fix_i18n) + errors.add(:value, _("is mandatory.").fix_i18n) end 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 f9b493e..6f70f08 100644 --- a/plugins/custom_forms/lib/custom_forms_plugin/field.rb +++ b/plugins/custom_forms/lib/custom_forms_plugin/field.rb @@ -1,26 +1,23 @@ class CustomFormsPlugin::Field < ActiveRecord::Base set_table_name :custom_forms_plugin_fields - validates_presence_of :form, :name - validates_uniqueness_of :slug, :scope => :form_id + validates_presence_of :name belongs_to :form, :class_name => 'CustomFormsPlugin::Form' has_many :answers, :class_name => 'CustomFormsPlugin::Answer' - serialize :choices, Hash + has_many :alternatives, :class_name => 'CustomFormsPlugin::Alternative' + accepts_nested_attributes_for :alternatives, :allow_destroy => true before_validation do |field| field.slug = field.name.to_slug if field.name.present? end - before_create do |field| - if field.form.fields.exists? - field.position = field.form.fields.order(:position).last.position + 1 - end - end + private - def position - self[:position] || 0 + def attributes_protected_by_default + super - [self.class.inheritance_column] end + end diff --git a/plugins/custom_forms/lib/custom_forms_plugin/form.rb b/plugins/custom_forms/lib/custom_forms_plugin/form.rb index c9b19bf..853c91b 100644 --- a/plugins/custom_forms/lib/custom_forms_plugin/form.rb +++ b/plugins/custom_forms/lib/custom_forms_plugin/form.rb @@ -1,7 +1,9 @@ class CustomFormsPlugin::Form < Noosfero::Plugin::ActiveRecord belongs_to :profile - has_many :fields, :class_name => 'CustomFormsPlugin::Field', :dependent => :destroy, :order => 'position' + has_many :fields, :class_name => 'CustomFormsPlugin::Field', :dependent => :destroy + accepts_nested_attributes_for :fields, :allow_destroy => true + has_many :submissions, :class_name => 'CustomFormsPlugin::Submission' serialize :access diff --git a/plugins/custom_forms/lib/custom_forms_plugin/helper.rb b/plugins/custom_forms/lib/custom_forms_plugin/helper.rb index 27948ad..7458a4e 100644 --- a/plugins/custom_forms/lib/custom_forms_plugin/helper.rb +++ b/plugins/custom_forms/lib/custom_forms_plugin/helper.rb @@ -1,4 +1,11 @@ module CustomFormsPlugin::Helper + def html_for_field(builder, association, klass) + new_object = klass.new + builder.fields_for(association, new_object, :child_index => "new_#{association}") do |f| + render(partial_for_class(klass), :f => f) + end + end + def access_text(form) return _('Public') if form.access.nil? return _('Logged users') if form.access == 'logged' @@ -48,8 +55,8 @@ module CustomFormsPlugin::Helper def type_to_label(type) map = { - 'text_field' => _('Text'), - 'select_field' => _('Select') + 'text_field' => _('Text field'), + 'select_field' => _('Select field') } map[type_for_options(type)] end @@ -61,52 +68,53 @@ module CustomFormsPlugin::Helper def display_custom_field(field, submission, form) answer = submission.answers.select{|answer| answer.field == field}.first field_tag = send("display_#{type_for_options(field.class)}",field, answer, form) - if field.mandatory? && !radio_button?(field) && !check_box?(field) && submission.id.nil? + if field.mandatory? && submission.id.nil? required(labelled_form_field(field.name, field_tag)) - else + else labelled_form_field(field.name, field_tag) end end def display_text_field(field, answer, form) value = answer.present? ? answer.value : field.default_value - text_field(form, field.name.to_slug, :value => value, :disabled => answer.present?) + text_field(form, "#{field.id}", :value => value, :disabled => answer.present? && answer.id.present?) end def display_select_field(field, answer, form) - if field.list && field.multiple - selected = answer.present? ? answer.value.split(',') : [] - select_tag "#{form}[#{field.name.to_slug}]", options_for_select(field.choices.to_a, selected), :multiple => true, :size => field.choices.size, :disabled => answer.present? - elsif !field.list && field.multiple - field.choices.map do |name, value| - default = answer.present? ? answer.value.split(',').include?(value) : false - labelled_check_box name, "#{form}[#{field.name.to_slug}][#{value}]", '1', default, :disabled => answer.present? + case field.select_field_type + when 'multiple_select' + selected = answer.present? ? answer.value.split(',') : field.alternatives.select {|a| a.selected_by_default}.map{|a| a.id.to_s} + select_tag "submission[#{field.id}]", options_for_select(field.alternatives.map{|a| [a.label, a.id.to_s]}, selected), :multiple => true, :size => field.alternatives.size, :disabled => answer.present? && answer.id.present? + when 'check_box' + field.alternatives.map do |alternative| + default = answer.present? ? answer.value.split(',').include?(alternative.id.to_s) : alternative.selected_by_default + labelled_check_box alternative.label, "submission[#{field.id}][#{alternative.id}]", '1', default, :disabled => answer.present? && answer.id.present? end.join("\n") - elsif field.list && !field.multiple - selected = answer.present? ? answer.value.split(',') : [] - select_tag "#{form}[#{field.name.to_slug}]", options_for_select([['','']] + field.choices.to_a, selected), :disabled => answer.present? - elsif !field.list && !field.multiple - field.choices.map do |name, value| - default = answer.present? ? answer.value == value : true - labelled_radio_button name, "#{form}[#{field.name.to_slug}]", value, default, :disabled => answer.present? + when 'select' + selected = answer.present? ? answer.value.split(',') : field.alternatives.select {|a| a.selected_by_default}.map{|a| a.id.to_s} + select_tag "submission[#{field.id}]", options_for_select([['','']] + field.alternatives.map {|a| [a.label, a.id.to_s]}, selected), :disabled => answer.present? && answer.id.present? + when 'radio' + field.alternatives.map do |alternative| + default = answer.present? ? answer.value == alternative.id.to_s : alternative.selected_by_default + labelled_radio_button alternative.label, "submission[#{field.id}]", alternative.id, default, :disabled => answer.present? && answer.id.present? end.join("\n") end end def radio_button?(field) - type_for_options(field.class) == 'select_field' && !field.list && !field.multiple + type_for_options(field.class) == 'select_field' && field.select_field_type == 'radio' end def check_box?(field) - type_for_options(field.class) == 'select_field' && !field.list && field.multiple + type_for_options(field.class) == 'select_field' && field.select_field_type == 'check_box' end def build_answers(submission, form) answers = [] form.fields.each do |field| final_value = '' - if submission.has_key?(field.slug) - value = submission[field.slug] + if submission.has_key?(field.id.to_s) + value = submission[field.id.to_s] if value.kind_of?(String) final_value = value elsif value.kind_of?(Array) 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 d5bf84e..c9084ee 100644 --- a/plugins/custom_forms/lib/custom_forms_plugin/select_field.rb +++ b/plugins/custom_forms/lib/custom_forms_plugin/select_field.rb @@ -1,4 +1,6 @@ class CustomFormsPlugin::SelectField < CustomFormsPlugin::Field set_table_name :custom_forms_plugin_fields - validates_presence_of :choices + + validates_length_of :select_field_type, :minimum => 1, :allow_nil => false + validates_inclusion_of :select_field_type, :in => %w(radio check_box select multiple_select) end diff --git a/plugins/custom_forms/lib/custom_forms_plugin/submission.rb b/plugins/custom_forms/lib/custom_forms_plugin/submission.rb index 371cb4b..752872b 100644 --- a/plugins/custom_forms/lib/custom_forms_plugin/submission.rb +++ b/plugins/custom_forms/lib/custom_forms_plugin/submission.rb @@ -2,11 +2,20 @@ class CustomFormsPlugin::Submission < Noosfero::Plugin::ActiveRecord belongs_to :form, :class_name => 'CustomFormsPlugin::Form' belongs_to :profile - has_many :answers, :class_name => 'CustomFormsPlugin::Answer' + has_many :answers, :class_name => 'CustomFormsPlugin::Answer', :dependent => :destroy validates_presence_of :form validates_presence_of :author_name, :author_email, :if => lambda {|submission| submission.profile.nil?} validates_uniqueness_of :author_email, :scope => :form_id, :allow_nil => true validates_format_of :author_email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|submission| !submission.author_email.blank?}) + + def self.human_attribute_name(attrib) + if /\d+/ =~ attrib and (f = CustomFormsPlugin::Field.find_by_id(attrib.to_i)) + f.name + else + attrib + end + end + end diff --git a/plugins/custom_forms/public/field.js b/plugins/custom_forms/public/field.js index 6e811cb..80afb8f 100644 --- a/plugins/custom_forms/public/field.js +++ b/plugins/custom_forms/public/field.js @@ -1,140 +1,38 @@ -jQuery('.icon-edit').live('click', function() { - elem = this; - jQuery.fn.colorbox({ - overlayClose: false, - escKey: false, - inline: true, - href: function(){ - id = jQuery(elem).attr('field_id'); - type = jQuery('#fields_'+id+'_type').val().split('_')[0]; - selector = '#edit-'+type+'-'+id - return selector +var customFormsPlugin = { + removeFieldBox: function (button, confirmMsg) { + if (confirm(confirmMsg)) { + fb = jQuery(button).closest('.field-box'); + jQuery('input.destroy-field', fb).val(1); + jQuery('> div', fb).slideUp({easing:'linear', complete:function(){fb.slideUp({easing:'linear', duration:250})}}); } - }); - return false; -}); - -jQuery('.remove-field').live('click', function(){ - id = jQuery(this).attr('field_id'); - jQuery('#field-'+id).slideDown(function(){ - jQuery('#field-'+id).remove(); - }); - return false -}); - -jQuery('.remove-option').live('click', function(){ - field_id = jQuery(this).attr('field_id'); - option_id = jQuery(this).attr('option_id'); - selector = '#field-'+field_id+'-option-'+option_id - jQuery(selector).slideDown(function(){ - jQuery(selector).remove(); - jQuery.colorbox.resize(); - }); - return false -}); - -function updateEditText(id){ - new_id = id+1 - jQuery('#edit-text-'+id).attr('id', 'edit-text-'+new_id); - input = jQuery('#edit-text-'+new_id+' input'); - jQuery('#edit-text-'+new_id+' .colorbox-ok-button').attr('div_id', 'edit-text-'+new_id); - input.attr('id', input.attr('id').replace(id,new_id)); - input.attr('name', input.attr('name').replace(id,new_id)); - label = jQuery('#edit-text-'+new_id+' label'); - label.attr('for', label.attr('for').replace(id,new_id)); -} - -function updateEditSelect(id){ - new_id = id+1 - jQuery('#edit-select-'+id).attr('id', 'edit-select-'+new_id); - jQuery('#edit-select-'+new_id+' .colorbox-ok-button').attr('div_id', 'edit-select-'+new_id); - jQuery('tr[id^=field-'+id+'-option').each(function(id, element){ - jQuery(element).attr('id', jQuery(element).attr('id').replace('field-'+id,'field-'+new_id)); - }); - jQuery('#edit-select-'+new_id+' label').each(function(index, element){ - label = jQuery(element); - label.attr('for', label.attr('for').replace(id,new_id)); - }); - jQuery('#edit-select-'+new_id+' input').each(function(index, element){ - input = jQuery(element); - input.attr('id', input.attr('id').replace(id,new_id)); - input.attr('name', input.attr('name').replace(id,new_id)); - }); - jQuery('#edit-select-'+new_id+' .remove-option').each(function(index, element){ - jQuery(element).attr('field_id',new_id); - }); - jQuery('#edit-select-'+new_id+' .new-option').attr('field_id',new_id); - jQuery('#edit-select-'+new_id+' #empty-option-'+id).attr('id','empty-option-'+new_id); -} - -function updateEmptyField(id){ - id = parseInt(id); - empty_field = jQuery('#empty-field'); - empty_field.attr('last_id', (id + 1).toString()); - jQuery('#empty-field input').each(function(index, element){ - new_id = jQuery(element).attr('id').replace(id,id+1); - jQuery(element).attr('id', new_id); - new_name = jQuery(element).attr('name').replace(id,id+1); - jQuery(element).attr('name', new_name); - }); - jQuery('#empty-field select').each(function(index, element){ - new_id = jQuery(element).attr('id').replace(id,id+1); - jQuery(element).attr('id', new_id); - new_name = jQuery(element).attr('name').replace(id,id+1); - jQuery(element).attr('name', new_name); - }); - jQuery('#empty-field a').each(function(index, element){ - jQuery(element).attr('field_id', id+1); - }); - updateEditText(id); - updateEditSelect(id); -} - -function updateEmptyOption(field_id, option_id){ - field_id = parseInt(field_id); - option_id = parseInt(option_id); - new_option_id = option_id+1; - empty_option = jQuery('#empty-option-'+field_id); - empty_option.attr('option_id',new_option_id); - jQuery('#empty-option-'+field_id+' .remove-option').attr('option_id', new_option_id); + }, + + removeAlternative: function (button, confirmMsg) { + if (confirm(confirmMsg)) { + alt = jQuery(button).closest('tr.alternative'); + jQuery('input.destroy-field', alt).val(1); + alt.fadeOut(500, function() { + customFormsPlugin.checkHeaderDisplay(jQuery(button).closest('table')); + }); + } + }, - name_id = ' #fields_'+field_id+'_choices_'+option_id+'_name'; - jQuery('#empty-option-'+field_id+name_id).attr('name', 'fields['+field_id+'][choices]['+new_option_id+'][name]'); - jQuery('#empty-option-'+field_id+name_id).attr('id', 'fields_'+field_id+'_choices_'+new_option_id+'_name'); + addFields: function (button, association, content) { + var new_id = new Date().getTime(); + var regexp = new RegExp("new_" + association, "g"); + jQuery(content.replace(regexp, new_id)).insertBefore(jQuery(button).closest('.addition-buttons')).hide().slideDown(); - value_id = ' #fields_'+field_id+'_choices_'+option_id+'_value'; - jQuery('#empty-option-'+field_id+value_id).attr('name', 'fields['+field_id+'][choices]['+new_option_id+'][value]'); - jQuery('#empty-option-'+field_id+value_id).attr('id', 'fields_'+field_id+'_choices_'+new_option_id+'_value'); + if(association == 'alternatives') { + jQuery(button).closest('table').find('tr:first').show(); + } + }, + + checkHeaderDisplay: function(table) { + trs =jQuery('tr:visible', table); + if (trs.length <= 2) { + trs[0].style.display = 'none'; + } else { + trs[0].style.display = 'table-row'; + } + } } - -jQuery('#new-field').live('click', function(){ - empty_field = jQuery('#empty-field'); - id = empty_field.attr('last_id'); - edit_text = jQuery('#edit-text-'+id); - edit_select = jQuery('#edit-select-'+id); - new_field = empty_field.clone(); - new_field.attr('id','field-'+id); - new_field.insertBefore(empty_field).slideDown(); - edit_text.clone().insertAfter(edit_text); - edit_select.clone().insertAfter(edit_select); - updateEmptyField(id); - return false -}); - -jQuery('.new-option').live('click', function(){ - field_id = jQuery(this).attr('field_id'); - empty_option = jQuery('#empty-option-'+field_id); - option_id = empty_option.attr('option_id'); - new_option = empty_option.clone(); - new_option.attr('id','field-'+field_id+'-option-'+option_id); - new_option.insertBefore(empty_option).slideDown(); - jQuery.colorbox.resize(); - updateEmptyOption(field_id, option_id); - return false -}); - -jQuery('.colorbox-ok-button').live('click', function(){ - jQuery('#'+jQuery(this).attr('div_id')).hide(); - jQuery.colorbox.close(); - return false -}); diff --git a/plugins/custom_forms/public/style.css b/plugins/custom_forms/public/style.css index ed2b339..c52c719 100644 --- a/plugins/custom_forms/public/style.css +++ b/plugins/custom_forms/public/style.css @@ -31,3 +31,27 @@ #colorbox .edit-information { display: block; } + +.field-box { + margin: 10px 0; +} + +.field-box > div { + overflow: hide; +} + +.field-box .button { + margin-left: 15px; +} + +.field-box .addition-buttons .button { + margin: 0px; +} + +.field-select-type { + margin: 10px 0; +} + +.field-text-default { + margin-top: 10px; +} 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 ce5fc1b..3861f6c 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 @@ -48,21 +48,22 @@ class CustomFormsPluginMyprofileControllerTest < ActionController::TestCase :access => 'logged', :begining => begining, :ending => ending, - :description => 'Cool form'}, - :fields => { - 1 => { - :name => 'Name', - :default_value => 'Jack', - :type => 'text_field' - }, - 2 => { - :name => 'Color', - :list => '1', - :type => 'select_field', - :choices => { - 1 => {:name => 'Red', :value => 'red'}, - 2 => {:name => 'Blue', :value => 'blue'}, - 3 => {:name => 'Black', :value => 'black'} + :description => 'Cool form', + :fields_attributes => { + 1 => { + :name => 'Name', + :default_value => 'Jack', + :type => 'CustomFormsPlugin::TextField' + }, + 2 => { + :name => 'Color', + :select_field_type => 'radio', + :type => 'CustomFormsPlugin::SelectField', + :alternatives_attributes => { + 1 => {:label => 'Red'}, + 2 => {:label => 'Blue'}, + 3 => {:label => 'Black'} + } } } } @@ -83,10 +84,8 @@ class CustomFormsPluginMyprofileControllerTest < ActionController::TestCase assert f1.kind_of?(CustomFormsPlugin::TextField) assert_equal 'Color', f2.name - assert_equal 'red', f2.choices['Red'] - assert_equal 'blue', f2.choices['Blue'] - assert_equal 'black', f2.choices['Black'] - assert f2.list + assert_equal f2.alternatives.map(&:label).sort, ['Red', 'Blue', 'Black'].sort + assert_equal f2.select_field_type, 'radio' assert f2.kind_of?(CustomFormsPlugin::SelectField) end @@ -100,7 +99,7 @@ class CustomFormsPluginMyprofileControllerTest < ActionController::TestCase fields[i.to_s] = { :name => i.to_s, :default_value => '', - :type => 'text_field' + :type => 'CustomFormsPlugin::TextField' } end assert_difference CustomFormsPlugin::Form, :count, 1 do @@ -110,29 +109,35 @@ class CustomFormsPluginMyprofileControllerTest < ActionController::TestCase :access => 'logged', :begining => begining, :ending => ending, - :description => 'Cool form'}, - :fields => fields + :description => 'Cool form', + :fields_attributes => fields + } end form = CustomFormsPlugin::Form.find_by_name('My Form') assert_equal num_fields, form.fields.count + lst = -1 form.fields.find_each do |f| - assert_equal f.position, f.name.to_i + assert f.name.to_i > lst + lst = f.name.to_i end end should 'edit a form' do form = CustomFormsPlugin::Form.create!(:profile => profile, :name => 'Free Software') - field = CustomFormsPlugin::TextField.create!(:form => form, :name => 'License') format = '%Y-%m-%d %H:%M' begining = Time.now.strftime(format) ending = (Time.now + 1.day).strftime(format) - post :edit, :profile => profile.identifier, :id => form.id, - :form => {:name => 'My Form', :access => 'logged', :begining => begining, :ending => ending, :description => 'Cool form'}, - :fields => {1 => {:real_id => field.id.to_s, :name => 'Source'}} + assert_equal form.fields.length, 0 + + post :update, :profile => profile.identifier, :id => form.id, + :form => {:name => 'My Form', :access => 'logged', :begining => begining, :ending => ending, :description => 'Cool form', + :fields_attributes => {1 => {:name => 'Source'}}} form.reload - field.reload + assert_equal form.fields.length, 1 + + field = form.fields.last assert_equal 'logged', form.access assert_equal begining, form.begining.strftime(format) diff --git a/plugins/custom_forms/test/functional/custom_forms_plugin_profile_controller_test.rb b/plugins/custom_forms/test/functional/custom_forms_plugin_profile_controller_test.rb index 78ac5c0..df54bb8 100644 --- a/plugins/custom_forms/test/functional/custom_forms_plugin_profile_controller_test.rb +++ b/plugins/custom_forms/test/functional/custom_forms_plugin_profile_controller_test.rb @@ -23,7 +23,7 @@ class CustomFormsPluginProfileControllerTest < ActionController::TestCase field2 = CustomFormsPlugin::TextField.create(:name => 'License', :form => form) assert_difference CustomFormsPlugin::Submission, :count, 1 do - post :show, :profile => profile.identifier, :id => form.id, :submission => {field1.name.to_slug => 'Noosfero', field2.name.to_slug => 'GPL'} + post :show, :profile => profile.identifier, :id => form.id, :submission => {field1.id.to_s => 'Noosfero', field2.id.to_s => 'GPL'} end assert !session[:notice].include?('not saved') assert_redirected_to :action => 'show' diff --git a/plugins/custom_forms/test/unit/custom_forms_plugin/answer_test.rb b/plugins/custom_forms/test/unit/custom_forms_plugin/answer_test.rb index 5e8b224..827642c 100644 --- a/plugins/custom_forms/test/unit/custom_forms_plugin/answer_test.rb +++ b/plugins/custom_forms/test/unit/custom_forms_plugin/answer_test.rb @@ -27,11 +27,11 @@ class CustomFormsPlugin::AnswerTest < ActiveSupport::TestCase field = CustomFormsPlugin::Field.create!(:name => 'License', :form => form, :mandatory => true) answer = CustomFormsPlugin::Answer.new(:field => field) answer.valid? - assert answer.errors.invalid?(field.slug.to_sym) + assert answer.errors.invalid?(:value) answer.value = "GPL" answer.valid? - assert !answer.errors.invalid?(field.slug.to_sym) + assert !answer.errors.invalid?(:value) end end diff --git a/plugins/custom_forms/test/unit/custom_forms_plugin/field_test.rb b/plugins/custom_forms/test/unit/custom_forms_plugin/field_test.rb index 5c672d1..ce2f42e 100644 --- a/plugins/custom_forms/test/unit/custom_forms_plugin/field_test.rb +++ b/plugins/custom_forms/test/unit/custom_forms_plugin/field_test.rb @@ -1,40 +1,12 @@ require File.dirname(__FILE__) + '/../../../../../test/test_helper' class CustomFormsPlugin::FieldTest < ActiveSupport::TestCase - should 'validate presence of form' do - field = CustomFormsPlugin::Field.new - field.valid? - assert field.errors.invalid?(:form) - assert field.errors.invalid?(:name) - - form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => fast_create(Profile)) - field.form = form - field.name = 'License' - field.valid? - assert !field.errors.invalid?(:form) - assert !field.errors.invalid?(:name) - end - should 'set slug before validation based on name' do field = CustomFormsPlugin::Field.new(:name => 'Name') field.valid? assert_equal field.name.to_slug, field.slug end - should 'validate uniqueness of slug scoped on the form' do - form1 = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => fast_create(Profile)) - form2 = CustomFormsPlugin::Form.create!(:name => 'Open Source', :profile => fast_create(Profile)) - f1 = CustomFormsPlugin::Field.create!(:name => 'License', :form => form1) - f2 = CustomFormsPlugin::Field.new(:name => 'License', :form => form1) - f3 = CustomFormsPlugin::Field.new(:name => 'License', :form => form2) - - f2.valid? - f3.valid? - - assert f2.errors.invalid?(:slug) - assert !f3.errors.invalid?(:slug) - end - should 'set mandatory field as false by default' do field = CustomFormsPlugin::Field.new assert !field.mandatory @@ -50,17 +22,6 @@ class CustomFormsPlugin::FieldTest < ActiveSupport::TestCase assert_includes field.answers, a2 end - should 'serialize choices into a hash' do - form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => fast_create(Profile)) - field = CustomFormsPlugin::Field.create!(:name => 'License', :form => form) - field.choices = {'First' => 1, 'Second' => 2, 'Third' => 3} - field.save! - - assert_equal 1, field.choices['First'] - assert_equal 2, field.choices['Second'] - assert_equal 3, field.choices['Third'] - end - should 'not destroy form after removing a field' do form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => fast_create(Profile)) license_field = CustomFormsPlugin::Field.create!(:name => 'License', :form => form) @@ -71,27 +32,5 @@ class CustomFormsPlugin::FieldTest < ActiveSupport::TestCase end assert_equal form.fields, [license_field] end - - should 'give positions by creation order' do - form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => fast_create(Profile)) - field_0 = CustomFormsPlugin::Field.create!(:name => 'License', :form => form) - field_1 = CustomFormsPlugin::Field.create!(:name => 'URL', :form => form) - field_2 = CustomFormsPlugin::Field.create!(:name => 'Wiki', :form => form) - assert_equal 0, field_0.position - assert_equal 1, field_1.position - assert_equal 2, field_2.position - end - - should 'not crash when adding new fields on a form with fields without position' do - form = CustomFormsPlugin::Form.create(:name => 'Free Software', :profile => fast_create(Profile)) - field_0 = CustomFormsPlugin::Field.create(:name => 'License', :form => form) - field_0.position = nil - field_0.save - - assert_nothing_raised do - field_1 = CustomFormsPlugin::Field.create!(:name => 'URL', :form => form) - end - end - end diff --git a/plugins/custom_forms/test/unit/custom_forms_plugin/membership_survey_test.rb b/plugins/custom_forms/test/unit/custom_forms_plugin/membership_survey_test.rb index 72cd63a..a47a264 100644 --- a/plugins/custom_forms/test/unit/custom_forms_plugin/membership_survey_test.rb +++ b/plugins/custom_forms/test/unit/custom_forms_plugin/membership_survey_test.rb @@ -16,7 +16,7 @@ class CustomFormsPlugin::MembershipSurveyTest < ActiveSupport::TestCase person = fast_create(Person) form = CustomFormsPlugin::Form.create!(:name => 'Simple Form', :profile => profile) field = CustomFormsPlugin::Field.create!(:name => 'Name', :form => form) - task = CustomFormsPlugin::MembershipSurvey.create!(:form_id => form.id, :submission => {'name' => 'Jack'}, :target => person, :requestor => profile) + task = CustomFormsPlugin::MembershipSurvey.create!(:form_id => form.id, :submission => {field.id.to_s => 'Jack'}, :target => person, :requestor => profile) assert_difference CustomFormsPlugin::Submission, :count, 1 do task.finish 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 73de468..2cfc0e1 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 @@ -1,13 +1,19 @@ require File.dirname(__FILE__) + '/../../../../../test/test_helper' class CustomFormsPlugin::SelectFieldTest < ActiveSupport::TestCase - should 'validate presence of choices, multiple and list' do - select = CustomFormsPlugin::SelectField.new - select.valid? - assert select.errors.invalid?(:choices) + should 'validate type' do + select = CustomFormsPlugin::SelectField.new(:name => 'select_field001' ) - select.choices = {'label' => 'value'} - select.valid? - assert !select.errors.invalid?(:choices) + select.update_attribute(:select_field_type, 'random') + assert select.invalid? + + select.update_attribute(:select_field_type, 'radio') + assert select.valid? + select.update_attribute(:select_field_type, 'check_box') + assert select.valid? + select.update_attribute(:select_field_type, 'select') + assert select.valid? + select.update_attribute(:select_field_type, 'multiple_select') + assert select.valid? end end diff --git a/plugins/custom_forms/views/custom_forms_plugin_myprofile/_edit_select.html.erb b/plugins/custom_forms/views/custom_forms_plugin_myprofile/_edit_select.html.erb index 03af42e..c45ea5c 100644 --- a/plugins/custom_forms/views/custom_forms_plugin_myprofile/_edit_select.html.erb +++ b/plugins/custom_forms/views/custom_forms_plugin_myprofile/_edit_select.html.erb @@ -1,11 +1,10 @@ -<% elem_id = "edit-select-#{counter}" %> -
class='edit-information'> +

<%= _('Options') %>

- + <% option_counter = 1 %> <% (field.choices || {}).each do |name, value| %> diff --git a/plugins/custom_forms/views/custom_forms_plugin_myprofile/_edit_text.html.erb b/plugins/custom_forms/views/custom_forms_plugin_myprofile/_edit_text.html.erb deleted file mode 100644 index c84aeaf..0000000 --- a/plugins/custom_forms/views/custom_forms_plugin_myprofile/_edit_text.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -<% elem_id = "edit-text-#{counter}" %> -
class='edit-information'> -

<%= _('Default value') %>

- <%= labelled_form_field _('Default value'), text_field("fields[#{counter}]", :default_value, :value => field.default_value) %> - - <% button_bar do %> - <%= button :ok, _('Ok'), '#', :class => 'colorbox-ok-button', :div_id => elem_id %> - <% end %> -
- diff --git a/plugins/custom_forms/views/custom_forms_plugin_myprofile/_field.html.erb b/plugins/custom_forms/views/custom_forms_plugin_myprofile/_field.html.erb index 573e9c7..efaa266 100644 --- a/plugins/custom_forms/views/custom_forms_plugin_myprofile/_field.html.erb +++ b/plugins/custom_forms/views/custom_forms_plugin_myprofile/_field.html.erb @@ -1,12 +1,16 @@ -> - - - <%= hidden_field "fields[#{counter}]", :type, :value => type_for_options(field.class) %> - - <%= hidden_field "fields[#{counter}]", :real_id, :value => field.id %> - <%= hidden_field "fields[#{counter}]", :form_id, :value => @form.id %> - - +
> + <%= type_to_label(f.object.type) %> +
+ <%= f.label :name, _('Name:') %> + <%= f.text_field :name, :size => 25 %> + + <%= f.hidden_field :type %> + + <%= f.check_box :mandatory %> + <%= f.label :mandatory, _('Mandatory') %> + + <%= f.hidden_field :_destroy, :class => 'destroy-field' %> + <%= button_to_function :delete, _('Remove field'), "customFormsPlugin.removeFieldBox(this, #{_('Are you sure you want to remove this field?').to_json})" %> + <%= yield %> +
+
diff --git a/plugins/custom_forms/views/custom_forms_plugin_myprofile/_form.html.erb b/plugins/custom_forms/views/custom_forms_plugin_myprofile/_form.html.erb index c14a851..a4f1c98 100644 --- a/plugins/custom_forms/views/custom_forms_plugin_myprofile/_form.html.erb +++ b/plugins/custom_forms/views/custom_forms_plugin_myprofile/_form.html.erb @@ -1,56 +1,33 @@ <% self.extend(CustomFormsPlugin::Helper) %> -<%= error_messages_for :form %> - -<% form_for :form, @form do |f| %> - <%= required labelled_form_field _('Name'), f.text_field(:name) %> - <%= labelled_form_field(_('Period'), ( - date_range_field('form[begining]', 'form[ending]', @form.begining, @form.ending, - '%Y-%m-%d %H:%M', - { :time => true, :change_month => true, :change_year => true, - :date_format => 'yy-mm-dd', :time_format => 'hh:mm' }, +<%= f.error_messages %> +<%= required labelled_form_field _('Name'), f.text_field(:name) %> +<%= labelled_form_field(_('Period'), ( + date_range_field('form[begining]', 'form[ending]', @form.begining, @form.ending, + '%Y-%m-%d %H:%M', + { :time => true, :change_month => true, :change_year => true, + :date_format => 'yy-mm-dd', :time_format => 'hh:mm' }, { :size => 14 }) - )) %> - <%= labelled_form_field _('Access'), f.select(:access, access_options(profile))%> - <% if profile.organization? %> - <%= labelled_form_field _('Triggered on membership'), f.check_box(:on_membership) %> - <% end %> - <%= labelled_form_field _('Description'), f.text_area(:description, :style => 'width: 100%') %> +)) %> +<%= labelled_form_field _('Access'), f.select(:access, access_options(profile))%> +<% if profile.organization? %> + <%= labelled_form_field _('Triggered on membership'), f.check_box(:on_membership) %> +<% end %> +<%= labelled_form_field _('Description'), f.text_area(:description, :style => 'width: 100%') %> -

<%= _('Fields') %>

-
<%= _('Name') %> <%= _('Value') %><%= _('Actions') %><%= _('Delete') %>
<%= text_field "fields[#{counter}]", :name, :value => field.name, :size => 25 %><%= type_to_label(field.type) %><%= check_box "fields[#{counter}]", :mandatory, :checked => field.mandatory %> - <%= button_without_text :edit, _('Edit'), '#', :field_id => counter %> - <%= button_without_text :remove, _('Remove'), '#', :class => 'remove-field', :field_id => counter, :confirm => _('Are you sure you want to remove this field?') %> -
- - - - - - - <% counter = 1 %> - <% @fields.each do |field| %> - <%= render :partial => 'field', :locals => {:field => field, :counter => counter} %> - <% counter += 1 %> - <% end %> - <%= render :partial => 'empty_field', :locals => {:field => @empty_field, :counter => counter} %> - - - -
<%= _('Name') %><%= _('Type') %><%= _('Mandatory') %><%= _('Actions') %>
- <%= button(:add, _('Add a new field'), '#', :id => 'new-field')%> -
+

<%= _('Fields') %>

- <% counter = 1 %> - <% @fields.each do |field| %> - <%= render :partial => 'edit_text', :locals => {:field => field, :counter => counter} %> - <%= render :partial => 'edit_select', :locals => {:field => field, :counter => counter} %> - <% counter += 1 %> - <% end %> +<% f.fields_for :fields do |builder| %> + <%= render partial_for_class(builder.object.class), :f => builder %> +<% end %> - <%= render :partial => 'edit_text', :locals => {:field => @empty_field, :counter => counter} %> - <%= render :partial => 'edit_select', :locals => {:field => @empty_field, :counter => counter} %> +
+ <%= button(:add, _('Add a new text field'), '#', :onclick => "customFormsPlugin.addFields(this, 'fields', #{html_for_field(f, :fields, CustomFormsPlugin::TextField).to_json}); return false")%> + <%= button(:add, _('Add a new select field'), '#', :onclick => "customFormsPlugin.addFields(this, 'fields', #{html_for_field(f, :fields, CustomFormsPlugin::SelectField).to_json}); return false")%> +
- <% button_bar do %> - <%= submit_button :save, _('Save'), :cancel => {:action => 'index'}%> - <% end %> +<% button_bar do %> + <%= submit_button :save, _('Save'), :cancel => {:action => 'index'}%> <% end %> <%= javascript_include_tag '../plugins/custom_forms/field' %> diff --git a/plugins/custom_forms/views/custom_forms_plugin_myprofile/_option.html.erb b/plugins/custom_forms/views/custom_forms_plugin_myprofile/_option.html.erb deleted file mode 100644 index b6278c4..0000000 --- a/plugins/custom_forms/views/custom_forms_plugin_myprofile/_option.html.erb +++ /dev/null @@ -1,7 +0,0 @@ - style="display: auto;"> - <%= text_field_tag("fields[#{counter}][choices][#{option_counter}][name]", name) %> - <%= text_field_tag("fields[#{counter}][choices][#{option_counter}][value]", value) %> - - <%= button_without_text :remove, _('Remove'), '#', :class => 'remove-option', :field_id => counter, :option_id => option_counter, :confirm => _('Are you sure you want to remove this option?') %> - - diff --git a/plugins/custom_forms/views/custom_forms_plugin_myprofile/create.html.erb b/plugins/custom_forms/views/custom_forms_plugin_myprofile/create.html.erb deleted file mode 100644 index 77e947e..0000000 --- a/plugins/custom_forms/views/custom_forms_plugin_myprofile/create.html.erb +++ /dev/null @@ -1,2 +0,0 @@ -

<%= _('New form') %>

-<%= render :partial => 'form' %> diff --git a/plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_alternative.html.erb b/plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_alternative.html.erb new file mode 100644 index 0000000..dc9d248 --- /dev/null +++ b/plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_alternative.html.erb @@ -0,0 +1,10 @@ + + <%= f.text_field(:label) %> + + <%= f.check_box(:selected_by_default) %> + + + <%= f.hidden_field :_destroy, :class => 'destroy-field' %> + <%= button_to_function_without_text :remove, _('Remove alternative'), "customFormsPlugin.removeAlternative(this, #{_('Are you sure you want to remove this alternative?').to_json})", :class => 'remove-field', :title => _('Remove alternative') %> + +
diff --git a/plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_field.html.erb b/plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_field.html.erb new file mode 100644 index 0000000..13a84a6 --- /dev/null +++ b/plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_field.html.erb @@ -0,0 +1,9 @@ +<%-# Small hack necessary for fields_for when validation fails on #new %> +<% case f.object.type + when 'CustomFormsPlugin::SelectField' %> + <%= render partial_for_class(CustomFormsPlugin::SelectField), :f => f %> +<% when 'CustomFormsPlugin::TextField' %> + <%= render partial_for_class(CustomFormsPlugin::TextField), :f => f %> +<% else %> + <% raise 'This field has no valid type' %> +<% 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 new file mode 100644 index 0000000..1c6f1a0 --- /dev/null +++ b/plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_select_field.html.erb @@ -0,0 +1,31 @@ +<% 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.fields_for :alternatives do |builder| %> + <%= render partial_for_class(builder.object.class), :f => builder %> + <% end %> + + + +
<%= _('Alternative') %><%= _('Preselected') %><%= _('Remove') %>
+ <%= button(:add, _('Add a new alternative'), '#', :onclick => "customFormsPlugin.addFields(this, 'alternatives', #{html_for_field(f, :alternatives, CustomFormsPlugin::Alternative).to_json}); return false") %> +
+ + +<% end %> 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 new file mode 100644 index 0000000..f062819 --- /dev/null +++ b/plugins/custom_forms/views/custom_forms_plugin_myprofile/custom_forms_plugin/_text_field.html.erb @@ -0,0 +1,6 @@ +<% render :layout => 'field', :locals => { :f => f } do %> +
+ <%= f.label(:default_value, _('Default value:')) %> + <%= f.text_field(:default_value) %> +
+<% end %> diff --git a/plugins/custom_forms/views/custom_forms_plugin_myprofile/edit.html.erb b/plugins/custom_forms/views/custom_forms_plugin_myprofile/edit.html.erb index 1aad8ee..52fd305 100644 --- a/plugins/custom_forms/views/custom_forms_plugin_myprofile/edit.html.erb +++ b/plugins/custom_forms/views/custom_forms_plugin_myprofile/edit.html.erb @@ -1,2 +1,5 @@

<%= _('Edit form') %>

-<%= render :partial => 'form' %> + +<% form_for :form, @form, :url => { :action => 'update', :id => params[:id] } do |f| %> + <%= render :partial => 'form', :locals => {:f => f} %> +<% end %> diff --git a/plugins/custom_forms/views/custom_forms_plugin_myprofile/index.html.erb b/plugins/custom_forms/views/custom_forms_plugin_myprofile/index.html.erb index cbb3427..9b5dea9 100644 --- a/plugins/custom_forms/views/custom_forms_plugin_myprofile/index.html.erb +++ b/plugins/custom_forms/views/custom_forms_plugin_myprofile/index.html.erb @@ -22,7 +22,7 @@ <% end %> - <%= button(:add, _('Add a new form'), :action => 'create')%> + <%= button(:add, _('Add a new form'), :action => 'new')%> diff --git a/plugins/custom_forms/views/custom_forms_plugin_myprofile/new.html.erb b/plugins/custom_forms/views/custom_forms_plugin_myprofile/new.html.erb new file mode 100644 index 0000000..ed77fa2 --- /dev/null +++ b/plugins/custom_forms/views/custom_forms_plugin_myprofile/new.html.erb @@ -0,0 +1,5 @@ +

<%= _('New form') %>

+ +<% form_for :form, @form, :url => { :action => 'create', :id => params[:id] } do |f| %> + <%= render :partial => 'form', :locals => {:f => f} %> +<% end %> diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index 2daf771..dfdfeb8 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -2471,11 +2471,11 @@ div#activation_enterprise label, div#activation_enterprise input, div#activation } /*** REQUIRED FIELDS ***/ -.required-field label { +.required-field label.formlabel { font-weight: bold; color: #c00; } -.required-field label:after { +.required-field label.formlabel:after { content: ' (*)'; } .login-box { -- libgit2 0.21.2