From 12792d8068d0ed0514e89267edefaab4df5cf2c3 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 17 Jan 2009 11:18:04 +0000 Subject: [PATCH] User custom fields can now be set as editable so that users can edit them on 'My account'. For existing user custom fields, this new attribute is set to false by default to preserve the prior behaviour (it can turned on by editing the custom field in admin area). Note: on the registration form, *required* custom fields will be displayed even if they are not defined as editable so that the account can be created. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2276 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/my_controller.rb | 1 + app/models/custom_value.rb | 8 +++++++ app/views/account/register.rhtml | 2 +- app/views/custom_fields/_form.rhtml | 1 + app/views/my/account.rhtml | 4 ++++ db/migrate/102_add_custom_fields_editable.rb | 9 ++++++++ db/migrate/103_set_custom_fields_editable.rb | 9 ++++++++ lang/bg.yml | 1 + lang/ca.yml | 1 + lang/cs.yml | 1 + lang/da.yml | 1 + lang/de.yml | 1 + lang/en.yml | 1 + lang/es.yml | 1 + lang/fi.yml | 1 + lang/fr.yml | 1 + lang/he.yml | 1 + lang/hu.yml | 1 + lang/it.yml | 1 + lang/ja.yml | 1 + lang/ko.yml | 1 + lang/lt.yml | 1 + lang/nl.yml | 1 + lang/no.yml | 1 + lang/pl.yml | 1 + lang/pt-br.yml | 1 + lang/pt.yml | 1 + lang/ro.yml | 1 + lang/ru.yml | 1 + lang/sk.yml | 1 + lang/sr.yml | 1 + lang/sv.yml | 1 + lang/th.yml | 1 + lang/tr.yml | 1 + lang/uk.yml | 1 + lang/vn.yml | 1 + lang/zh-tw.yml | 1 + lang/zh.yml | 1 + test/fixtures/custom_fields.yml | 6 +++++ test/functional/my_controller_test.rb | 23 +++++++++++++++++--- 40 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 db/migrate/102_add_custom_fields_editable.rb create mode 100644 db/migrate/103_set_custom_fields_editable.rb diff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rb index 1cfa3e531..5e1b9d2c8 100644 --- a/app/controllers/my_controller.rb +++ b/app/controllers/my_controller.rb @@ -19,6 +19,7 @@ class MyController < ApplicationController before_filter :require_login helper :issues + helper :custom_fields BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues, 'issuesreportedbyme' => :label_reported_issues, diff --git a/app/models/custom_value.rb b/app/models/custom_value.rb index 085d242fa..1f662baa7 100644 --- a/app/models/custom_value.rb +++ b/app/models/custom_value.rb @@ -30,6 +30,14 @@ class CustomValue < ActiveRecord::Base self.value == '1' end + def editable? + custom_field.editable? + end + + def required? + custom_field.is_required? + end + def to_s value.to_s end diff --git a/app/views/account/register.rhtml b/app/views/account/register.rhtml index 755a7ad4b..45719f055 100644 --- a/app/views/account/register.rhtml +++ b/app/views/account/register.rhtml @@ -29,7 +29,7 @@

<%= select("user", "language", lang_options_for_select) %>

-<% @user.custom_field_values.each do |value| %> +<% @user.custom_field_values.select {|v| v.editable? || v.required?}.each do |value| %>

<%= custom_field_tag_with_label :user, value %>

<% end %> diff --git a/app/views/custom_fields/_form.rhtml b/app/views/custom_fields/_form.rhtml index 0222ddb66..874c571bf 100644 --- a/app/views/custom_fields/_form.rhtml +++ b/app/views/custom_fields/_form.rhtml @@ -84,6 +84,7 @@ when "IssueCustomField" %> <% when "UserCustomField" %>

<%= f.check_box :is_required %>

+

<%= f.check_box :editable %>

<% when "ProjectCustomField" %>

<%= f.check_box :is_required %>

diff --git a/app/views/my/account.rhtml b/app/views/my/account.rhtml index f4b726f96..ef5b222e4 100644 --- a/app/views/my/account.rhtml +++ b/app/views/my/account.rhtml @@ -15,6 +15,10 @@

<%= f.text_field :lastname, :required => true %>

<%= f.text_field :mail, :required => true %>

<%= f.select :language, lang_options_for_select %>

+ +<% @user.custom_field_values.select(&:editable?).each do |value| %> +

<%= custom_field_tag_with_label :user, value %>

+<% end %> <%= submit_tag l(:button_save) %> diff --git a/db/migrate/102_add_custom_fields_editable.rb b/db/migrate/102_add_custom_fields_editable.rb new file mode 100644 index 000000000..949f9db9d --- /dev/null +++ b/db/migrate/102_add_custom_fields_editable.rb @@ -0,0 +1,9 @@ +class AddCustomFieldsEditable < ActiveRecord::Migration + def self.up + add_column :custom_fields, :editable, :boolean, :default => true + end + + def self.down + remove_column :custom_fields, :editable + end +end diff --git a/db/migrate/103_set_custom_fields_editable.rb b/db/migrate/103_set_custom_fields_editable.rb new file mode 100644 index 000000000..465c929ec --- /dev/null +++ b/db/migrate/103_set_custom_fields_editable.rb @@ -0,0 +1,9 @@ +class SetCustomFieldsEditable < ActiveRecord::Migration + def self.up + UserCustomField.update_all('editable = false') + end + + def self.down + UserCustomField.update_all('editable = true') + end +end diff --git a/lang/bg.yml b/lang/bg.yml index d0df0bd9e..bb80f1708 100644 --- a/lang/bg.yml +++ b/lang/bg.yml @@ -701,3 +701,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/ca.yml b/lang/ca.yml index 26c309ee5..b0bbee2eb 100644 --- a/lang/ca.yml +++ b/lang/ca.yml @@ -702,3 +702,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/cs.yml b/lang/cs.yml index ce32e3637..ab0624860 100644 --- a/lang/cs.yml +++ b/lang/cs.yml @@ -706,3 +706,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/da.yml b/lang/da.yml index d46d60fef..b0df54468 100644 --- a/lang/da.yml +++ b/lang/da.yml @@ -702,3 +702,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/de.yml b/lang/de.yml index c49aba0f1..e8a9d937c 100644 --- a/lang/de.yml +++ b/lang/de.yml @@ -703,3 +703,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/en.yml b/lang/en.yml index 3dbfd851d..36e454692 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -186,6 +186,7 @@ field_searchable: Searchable field_default_value: Default value field_comments_sorting: Display comments field_parent_title: Parent page +field_editable: Editable setting_app_title: Application title setting_app_subtitle: Application subtitle diff --git a/lang/es.yml b/lang/es.yml index 9c6a08e43..c6e6234e8 100644 --- a/lang/es.yml +++ b/lang/es.yml @@ -686,3 +686,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/fi.yml b/lang/fi.yml index e80a5c705..dce1192ea 100644 --- a/lang/fi.yml +++ b/lang/fi.yml @@ -701,3 +701,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/fr.yml b/lang/fr.yml index ab77094a8..84f18e839 100644 --- a/lang/fr.yml +++ b/lang/fr.yml @@ -186,6 +186,7 @@ field_searchable: Utilisé pour les recherches field_default_value: Valeur par défaut field_comments_sorting: Afficher les commentaires field_parent_title: Page parent +field_editable: Modifiable setting_app_title: Titre de l'application setting_app_subtitle: Sous-titre de l'application diff --git a/lang/he.yml b/lang/he.yml index 447c84933..2fae88e80 100644 --- a/lang/he.yml +++ b/lang/he.yml @@ -701,3 +701,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/hu.yml b/lang/hu.yml index 765c503bc..eb1670c34 100644 --- a/lang/hu.yml +++ b/lang/hu.yml @@ -702,3 +702,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/it.yml b/lang/it.yml index 8838ab62b..a5d645694 100644 --- a/lang/it.yml +++ b/lang/it.yml @@ -701,3 +701,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/ja.yml b/lang/ja.yml index 31ba66268..f6b696fb4 100644 --- a/lang/ja.yml +++ b/lang/ja.yml @@ -702,3 +702,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/ko.yml b/lang/ko.yml index d56460ff1..8da9ac6d0 100644 --- a/lang/ko.yml +++ b/lang/ko.yml @@ -701,3 +701,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/lt.yml b/lang/lt.yml index e8fb5176e..9bd57f8c6 100644 --- a/lang/lt.yml +++ b/lang/lt.yml @@ -703,3 +703,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/nl.yml b/lang/nl.yml index b0db9f5e2..e223d13a6 100644 --- a/lang/nl.yml +++ b/lang/nl.yml @@ -703,3 +703,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/no.yml b/lang/no.yml index 2154f56a9..0e848772e 100644 --- a/lang/no.yml +++ b/lang/no.yml @@ -702,3 +702,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/pl.yml b/lang/pl.yml index a9c78da63..508216bf7 100644 --- a/lang/pl.yml +++ b/lang/pl.yml @@ -720,3 +720,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/pt-br.yml b/lang/pt-br.yml index 7c494cfe3..d25260ffd 100644 --- a/lang/pt-br.yml +++ b/lang/pt-br.yml @@ -702,3 +702,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/pt.yml b/lang/pt.yml index 1ec65d23b..980a6e5e0 100644 --- a/lang/pt.yml +++ b/lang/pt.yml @@ -703,3 +703,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/ro.yml b/lang/ro.yml index de89d093c..bf1f11769 100644 --- a/lang/ro.yml +++ b/lang/ro.yml @@ -701,3 +701,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/ru.yml b/lang/ru.yml index 0d54f8a7d..62ea8e340 100644 --- a/lang/ru.yml +++ b/lang/ru.yml @@ -735,3 +735,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/sk.yml b/lang/sk.yml index 853aff646..3f7c7dc72 100644 --- a/lang/sk.yml +++ b/lang/sk.yml @@ -707,3 +707,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/sr.yml b/lang/sr.yml index fd8f3ebac..455173d0c 100644 --- a/lang/sr.yml +++ b/lang/sr.yml @@ -702,3 +702,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/sv.yml b/lang/sv.yml index 878d1b9f6..4a85f1504 100644 --- a/lang/sv.yml +++ b/lang/sv.yml @@ -702,3 +702,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/th.yml b/lang/th.yml index a2b4d292b..8d39fb5db 100644 --- a/lang/th.yml +++ b/lang/th.yml @@ -704,3 +704,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/tr.yml b/lang/tr.yml index e6dfad2d5..80655926f 100644 --- a/lang/tr.yml +++ b/lang/tr.yml @@ -702,3 +702,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/uk.yml b/lang/uk.yml index 70fa21956..75c6639cb 100644 --- a/lang/uk.yml +++ b/lang/uk.yml @@ -703,3 +703,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/vn.yml b/lang/vn.yml index 90b8965b8..0bff54b57 100644 --- a/lang/vn.yml +++ b/lang/vn.yml @@ -704,3 +704,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/zh-tw.yml b/lang/zh-tw.yml index 72e5358ec..028457e87 100644 --- a/lang/zh-tw.yml +++ b/lang/zh-tw.yml @@ -703,3 +703,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/lang/zh.yml b/lang/zh.yml index 3bb139ab1..d25963941 100644 --- a/lang/zh.yml +++ b/lang/zh.yml @@ -703,3 +703,4 @@ warning_attachments_not_saved: "%d file(s) could not be saved." button_create_and_continue: Create and continue text_custom_field_possible_values_info: 'One line for each value' label_display: Display +field_editable: Editable diff --git a/test/fixtures/custom_fields.yml b/test/fixtures/custom_fields.yml index b7718b169..a17827473 100644 --- a/test/fixtures/custom_fields.yml +++ b/test/fixtures/custom_fields.yml @@ -15,6 +15,7 @@ custom_fields_001: is_required: false field_format: list default_value: "" + editable: true custom_fields_002: name: Searchable field min_length: 1 @@ -28,6 +29,7 @@ custom_fields_002: field_format: string searchable: true default_value: "Default string" + editable: true custom_fields_003: name: Development status min_length: 0 @@ -45,6 +47,7 @@ custom_fields_003: is_required: true field_format: list default_value: "" + editable: true custom_fields_004: name: Phone number min_length: 0 @@ -57,6 +60,7 @@ custom_fields_004: is_required: false field_format: string default_value: "" + editable: true custom_fields_005: name: Money min_length: 0 @@ -69,6 +73,7 @@ custom_fields_005: is_required: false field_format: float default_value: "" + editable: true custom_fields_006: name: Float field min_length: 0 @@ -81,4 +86,5 @@ custom_fields_006: is_required: false field_format: float default_value: "" + editable: true \ No newline at end of file diff --git a/test/functional/my_controller_test.rb b/test/functional/my_controller_test.rb index c1349ace4..997340096 100644 --- a/test/functional/my_controller_test.rb +++ b/test/functional/my_controller_test.rb @@ -22,7 +22,7 @@ require 'my_controller' class MyController; def rescue_action(e) raise e end; end class MyControllerTest < Test::Unit::TestCase - fixtures :users, :issues, :issue_statuses, :trackers, :enumerations + fixtures :users, :issues, :issue_statuses, :trackers, :enumerations, :custom_fields def setup @controller = MyController.new @@ -43,20 +43,37 @@ class MyControllerTest < Test::Unit::TestCase assert_template 'page' end - def test_get_account + def test_my_account_should_show_editable_custom_fields get :account assert_response :success assert_template 'account' assert_equal User.find(2), assigns(:user) + + assert_tag :input, :attributes => { :name => 'user[custom_field_values][4]'} + end + + def test_my_account_should_not_show_non_editable_custom_fields + UserCustomField.find(4).update_attribute :editable, false + + get :account + assert_response :success + assert_template 'account' + assert_equal User.find(2), assigns(:user) + + assert_no_tag :input, :attributes => { :name => 'user[custom_field_values][4]'} end def test_update_account - post :account, :user => {:firstname => "Joe", :login => "root", :admin => 1} + post :account, :user => {:firstname => "Joe", + :login => "root", + :admin => 1, + :custom_field_values => {"4" => "0100562500"}} assert_redirected_to 'my/account' user = User.find(2) assert_equal user, assigns(:user) assert_equal "Joe", user.firstname assert_equal "jsmith", user.login + assert_equal "0100562500", user.custom_value_for(4).value assert !user.admin? end