From 5192632458b4090d3971ea21a809e5984627b027 Mon Sep 17 00:00:00 2001 From: Lucas Melo Date: Tue, 2 Jul 2013 10:16:56 -0300 Subject: [PATCH] CustomFormsPlugin: order fields by creation time --- plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb | 3 ++- plugins/custom_forms/db/migrate/20130702120732_add_position_to_custom_forms_plugin_fields.rb | 13 +++++++++++++ plugins/custom_forms/lib/custom_forms_plugin/field.rb | 6 ++++++ plugins/custom_forms/lib/custom_forms_plugin/form.rb | 2 +- plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb | 34 ++++++++++++++++++++++++++++++++-- plugins/custom_forms/test/unit/custom_forms_plugin/field_test.rb | 10 ++++++++++ 6 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 plugins/custom_forms/db/migrate/20130702120732_add_position_to_custom_forms_plugin_fields.rb 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 b0a19ef..18321c6 100644 --- a/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb +++ b/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb @@ -70,7 +70,8 @@ class CustomFormsPluginMyprofileController < MyProfileController private def new_fields(params) - result = params[:fields].map {|id, hash| hash.has_key?(:real_id) ? nil : hash}.compact + 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 diff --git a/plugins/custom_forms/db/migrate/20130702120732_add_position_to_custom_forms_plugin_fields.rb b/plugins/custom_forms/db/migrate/20130702120732_add_position_to_custom_forms_plugin_fields.rb new file mode 100644 index 0000000..23d5d6f --- /dev/null +++ b/plugins/custom_forms/db/migrate/20130702120732_add_position_to_custom_forms_plugin_fields.rb @@ -0,0 +1,13 @@ +class AddPositionToCustomFormsPluginFields < ActiveRecord::Migration + def self.up + change_table :custom_forms_plugin_fields do |t| + t.integer :position, :default => 0 + end + end + + def self.down + change_table :custom_forms_plugin_fields do |t| + t.remove :position + 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 bc7f6a3..004f88b 100644 --- a/plugins/custom_forms/lib/custom_forms_plugin/field.rb +++ b/plugins/custom_forms/lib/custom_forms_plugin/field.rb @@ -12,5 +12,11 @@ class CustomFormsPlugin::Field < ActiveRecord::Base 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 end diff --git a/plugins/custom_forms/lib/custom_forms_plugin/form.rb b/plugins/custom_forms/lib/custom_forms_plugin/form.rb index 984d4de..c9b19bf 100644 --- a/plugins/custom_forms/lib/custom_forms_plugin/form.rb +++ b/plugins/custom_forms/lib/custom_forms_plugin/form.rb @@ -1,7 +1,7 @@ class CustomFormsPlugin::Form < Noosfero::Plugin::ActiveRecord belongs_to :profile - has_many :fields, :class_name => 'CustomFormsPlugin::Field', :dependent => :destroy + has_many :fields, :class_name => 'CustomFormsPlugin::Field', :dependent => :destroy, :order => 'position' has_many :submissions, :class_name => 'CustomFormsPlugin::Submission' serialize :access 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 7c12989..ce5fc1b 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 @@ -40,7 +40,7 @@ class CustomFormsPluginMyprofileControllerTest < ActionController::TestCase should 'create a form' do format = '%Y-%m-%d %H:%M' begining = Time.now.strftime(format) - ending = (Time.now + 1.day).strftime('%Y-%m-%d %H:%M') + ending = (Time.now + 1.day).strftime(format) assert_difference CustomFormsPlugin::Form, :count, 1 do post :create, :profile => profile.identifier, :form => { @@ -90,12 +90,42 @@ class CustomFormsPluginMyprofileControllerTest < ActionController::TestCase assert f2.kind_of?(CustomFormsPlugin::SelectField) end + should 'create fields in the order they are sent' do + format = '%Y-%m-%d %H:%M' + num_fields = 10 + begining = Time.now.strftime(format) + ending = (Time.now + 1.day).strftime(format) + fields = {} + num_fields.times do |i| + fields[i.to_s] = { + :name => i.to_s, + :default_value => '', + :type => 'text_field' + } + end + assert_difference CustomFormsPlugin::Form, :count, 1 do + post :create, :profile => profile.identifier, + :form => { + :name => 'My Form', + :access => 'logged', + :begining => begining, + :ending => ending, + :description => 'Cool form'}, + :fields => fields + end + form = CustomFormsPlugin::Form.find_by_name('My Form') + assert_equal num_fields, form.fields.count + form.fields.find_each do |f| + assert_equal f.position, 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('%Y-%m-%d %H:%M') + 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'}, 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 34edab1..1bf35b8 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 @@ -71,5 +71,15 @@ 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 end -- libgit2 0.21.2