Commit 6be0e715e72e548f74eb767c704cc7c0dec69a15

Authored by Rodrigo Souto
2 parents 6cac22b2 01ffe8db

Merge commit 'refs/merge-requests/363' of git://gitorious.org/noosfero/noosfero …

…into merge-requests/363
plugins/custom_forms/db/migrate/20130822001407_set_position_to_existent_custom_forms_plugin_fields.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +class SetPositionToExistentCustomFormsPluginFields < ActiveRecord::Migration
  2 + def self.up
  3 + update("UPDATE custom_forms_plugin_fields SET position = 0 WHERE position IS NULL")
  4 + end
  5 +
  6 + def self.down
  7 + say("Nothing to undo (cannot recover the data)")
  8 + end
  9 +end
... ...
plugins/custom_forms/lib/custom_forms_plugin/field.rb
... ... @@ -15,8 +15,12 @@ class CustomFormsPlugin::Field &lt; ActiveRecord::Base
15 15  
16 16 before_create do |field|
17 17 if field.form.fields.exists?
18   - field.position = field.form.fields.order('position').last.position + 1
  18 + field.position = field.form.fields.order(:position).last.position + 1
19 19 end
20 20 end
  21 +
  22 + def position
  23 + self[:position] || 0
  24 + end
21 25 end
22 26  
... ...
plugins/custom_forms/test/unit/custom_forms_plugin/field_test.rb
... ... @@ -81,5 +81,17 @@ class CustomFormsPlugin::FieldTest &lt; ActiveSupport::TestCase
81 81 assert_equal 1, field_1.position
82 82 assert_equal 2, field_2.position
83 83 end
  84 +
  85 + should 'not crash when adding new fields on a form with fields without position' do
  86 + form = CustomFormsPlugin::Form.create(:name => 'Free Software', :profile => fast_create(Profile))
  87 + field_0 = CustomFormsPlugin::Field.create(:name => 'License', :form => form)
  88 + field_0.position = nil
  89 + field_0.save
  90 +
  91 + assert_nothing_raised do
  92 + field_1 = CustomFormsPlugin::Field.create!(:name => 'URL', :form => form)
  93 + end
  94 + end
  95 +
84 96 end
85 97  
... ...