Commit 5192632458b4090d3971ea21a809e5984627b027

Authored by Lucas Melo
1 parent 5a911ba4

CustomFormsPlugin: order fields by creation time

ActionItem2593
plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb
@@ -70,7 +70,8 @@ class CustomFormsPluginMyprofileController < MyProfileController @@ -70,7 +70,8 @@ class CustomFormsPluginMyprofileController < MyProfileController
70 private 70 private
71 71
72 def new_fields(params) 72 def new_fields(params)
73 - result = params[:fields].map {|id, hash| hash.has_key?(:real_id) ? nil : hash}.compact 73 + keys = params[:fields].keys.sort{|a, b| a.to_i <=> b.to_i}
  74 + result = keys.map { |id| (hash = params[:fields][id]).has_key?(:real_id) ? nil : hash}.compact
74 result.delete_if {|field| field[:name].blank?} 75 result.delete_if {|field| field[:name].blank?}
75 result 76 result
76 end 77 end
plugins/custom_forms/db/migrate/20130702120732_add_position_to_custom_forms_plugin_fields.rb 0 → 100644
@@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
  1 +class AddPositionToCustomFormsPluginFields < ActiveRecord::Migration
  2 + def self.up
  3 + change_table :custom_forms_plugin_fields do |t|
  4 + t.integer :position, :default => 0
  5 + end
  6 + end
  7 +
  8 + def self.down
  9 + change_table :custom_forms_plugin_fields do |t|
  10 + t.remove :position
  11 + end
  12 + end
  13 +end
plugins/custom_forms/lib/custom_forms_plugin/field.rb
@@ -12,5 +12,11 @@ class CustomFormsPlugin::Field &lt; ActiveRecord::Base @@ -12,5 +12,11 @@ class CustomFormsPlugin::Field &lt; ActiveRecord::Base
12 before_validation do |field| 12 before_validation do |field|
13 field.slug = field.name.to_slug if field.name.present? 13 field.slug = field.name.to_slug if field.name.present?
14 end 14 end
  15 +
  16 + before_create do |field|
  17 + if field.form.fields.exists?
  18 + field.position = field.form.fields.order('position').last.position + 1
  19 + end
  20 + end
15 end 21 end
16 22
plugins/custom_forms/lib/custom_forms_plugin/form.rb
1 class CustomFormsPlugin::Form < Noosfero::Plugin::ActiveRecord 1 class CustomFormsPlugin::Form < Noosfero::Plugin::ActiveRecord
2 belongs_to :profile 2 belongs_to :profile
3 3
4 - has_many :fields, :class_name => 'CustomFormsPlugin::Field', :dependent => :destroy 4 + has_many :fields, :class_name => 'CustomFormsPlugin::Field', :dependent => :destroy, :order => 'position'
5 has_many :submissions, :class_name => 'CustomFormsPlugin::Submission' 5 has_many :submissions, :class_name => 'CustomFormsPlugin::Submission'
6 6
7 serialize :access 7 serialize :access
plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb
@@ -40,7 +40,7 @@ class CustomFormsPluginMyprofileControllerTest &lt; ActionController::TestCase @@ -40,7 +40,7 @@ class CustomFormsPluginMyprofileControllerTest &lt; ActionController::TestCase
40 should 'create a form' do 40 should 'create a form' do
41 format = '%Y-%m-%d %H:%M' 41 format = '%Y-%m-%d %H:%M'
42 begining = Time.now.strftime(format) 42 begining = Time.now.strftime(format)
43 - ending = (Time.now + 1.day).strftime('%Y-%m-%d %H:%M') 43 + ending = (Time.now + 1.day).strftime(format)
44 assert_difference CustomFormsPlugin::Form, :count, 1 do 44 assert_difference CustomFormsPlugin::Form, :count, 1 do
45 post :create, :profile => profile.identifier, 45 post :create, :profile => profile.identifier,
46 :form => { 46 :form => {
@@ -90,12 +90,42 @@ class CustomFormsPluginMyprofileControllerTest &lt; ActionController::TestCase @@ -90,12 +90,42 @@ class CustomFormsPluginMyprofileControllerTest &lt; ActionController::TestCase
90 assert f2.kind_of?(CustomFormsPlugin::SelectField) 90 assert f2.kind_of?(CustomFormsPlugin::SelectField)
91 end 91 end
92 92
  93 + should 'create fields in the order they are sent' do
  94 + format = '%Y-%m-%d %H:%M'
  95 + num_fields = 10
  96 + begining = Time.now.strftime(format)
  97 + ending = (Time.now + 1.day).strftime(format)
  98 + fields = {}
  99 + num_fields.times do |i|
  100 + fields[i.to_s] = {
  101 + :name => i.to_s,
  102 + :default_value => '',
  103 + :type => 'text_field'
  104 + }
  105 + end
  106 + assert_difference CustomFormsPlugin::Form, :count, 1 do
  107 + post :create, :profile => profile.identifier,
  108 + :form => {
  109 + :name => 'My Form',
  110 + :access => 'logged',
  111 + :begining => begining,
  112 + :ending => ending,
  113 + :description => 'Cool form'},
  114 + :fields => fields
  115 + end
  116 + form = CustomFormsPlugin::Form.find_by_name('My Form')
  117 + assert_equal num_fields, form.fields.count
  118 + form.fields.find_each do |f|
  119 + assert_equal f.position, f.name.to_i
  120 + end
  121 + end
  122 +
93 should 'edit a form' do 123 should 'edit a form' do
94 form = CustomFormsPlugin::Form.create!(:profile => profile, :name => 'Free Software') 124 form = CustomFormsPlugin::Form.create!(:profile => profile, :name => 'Free Software')
95 field = CustomFormsPlugin::TextField.create!(:form => form, :name => 'License') 125 field = CustomFormsPlugin::TextField.create!(:form => form, :name => 'License')
96 format = '%Y-%m-%d %H:%M' 126 format = '%Y-%m-%d %H:%M'
97 begining = Time.now.strftime(format) 127 begining = Time.now.strftime(format)
98 - ending = (Time.now + 1.day).strftime('%Y-%m-%d %H:%M') 128 + ending = (Time.now + 1.day).strftime(format)
99 129
100 post :edit, :profile => profile.identifier, :id => form.id, 130 post :edit, :profile => profile.identifier, :id => form.id,
101 :form => {:name => 'My Form', :access => 'logged', :begining => begining, :ending => ending, :description => 'Cool form'}, 131 :form => {:name => 'My Form', :access => 'logged', :begining => begining, :ending => ending, :description => 'Cool form'},
plugins/custom_forms/test/unit/custom_forms_plugin/field_test.rb
@@ -71,5 +71,15 @@ class CustomFormsPlugin::FieldTest &lt; ActiveSupport::TestCase @@ -71,5 +71,15 @@ class CustomFormsPlugin::FieldTest &lt; ActiveSupport::TestCase
71 end 71 end
72 assert_equal form.fields, [license_field] 72 assert_equal form.fields, [license_field]
73 end 73 end
  74 +
  75 + should 'give positions by creation order' do
  76 + form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => fast_create(Profile))
  77 + field_0 = CustomFormsPlugin::Field.create!(:name => 'License', :form => form)
  78 + field_1 = CustomFormsPlugin::Field.create!(:name => 'URL', :form => form)
  79 + field_2 = CustomFormsPlugin::Field.create!(:name => 'Wiki', :form => form)
  80 + assert_equal 0, field_0.position
  81 + assert_equal 1, field_1.position
  82 + assert_equal 2, field_2.position
  83 + end
74 end 84 end
75 85