From d6bfb7fa4da4068c3e64f3fce16574de56fd72e9 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sun, 20 Jan 2008 21:29:51 +0000 Subject: [PATCH] Added default value for custom fields. Fixed javascript on custom field form for project and user custom fields. git-svn-id: http://redmine.rubyforge.org/svn/trunk@1090 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/custom_field.rb | 5 +++++ app/models/custom_value.rb | 6 ++++++ app/views/custom_fields/_form.rhtml | 22 +++++++++++++++++----- lang/bg.yml | 1 + lang/cs.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/it.yml | 1 + lang/ja.yml | 1 + lang/ko.yml | 1 + lang/lt.yml | 3 ++- lang/nl.yml | 3 ++- lang/pl.yml | 1 + lang/pt-br.yml | 1 + lang/pt.yml | 1 + lang/ro.yml | 1 + lang/ru.yml | 1 + lang/sr.yml | 1 + lang/sv.yml | 1 + lang/zh-tw.yml | 1 + lang/zh.yml | 1 + test/fixtures/custom_fields.yml | 5 +++++ test/functional/issues_controller_test.rb | 19 ++++++++++++++++--- test/integration/issues_test.rb | 14 ++++++++++++-- test/unit/custom_value_test.rb | 11 +++++++++++ 29 files changed, 96 insertions(+), 12 deletions(-) diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 5a134c4ec..6be081b0b 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -53,6 +53,11 @@ class CustomField < ActiveRecord::Base errors.add(:possible_values, :activerecord_error_blank) if self.possible_values.nil? || self.possible_values.empty? errors.add(:possible_values, :activerecord_error_invalid) unless self.possible_values.is_a? Array end + + # validate default value + v = CustomValue.new(:custom_field => self.dup, :value => default_value, :customized => nil) + v.custom_field.is_required = false + errors.add(:default_value, :activerecord_error_invalid) unless v.valid? end def <=>(field) diff --git a/app/models/custom_value.rb b/app/models/custom_value.rb index c3d6b7bb9..94b797bcc 100644 --- a/app/models/custom_value.rb +++ b/app/models/custom_value.rb @@ -19,6 +19,12 @@ class CustomValue < ActiveRecord::Base belongs_to :custom_field belongs_to :customized, :polymorphic => true + def after_initialize + if custom_field && new_record? && (customized_type.blank? || (customized && customized.new_record?)) + self.value ||= custom_field.default_value + end + end + protected def validate errors.add(:value, :activerecord_error_blank) and return if custom_field.is_required? and value.blank? diff --git a/app/views/custom_fields/_form.rhtml b/app/views/custom_fields/_form.rhtml index 915daab32..5e4eadf21 100644 --- a/app/views/custom_fields/_form.rhtml +++ b/app/views/custom_fields/_form.rhtml @@ -8,31 +8,42 @@ function toggle_custom_field_format() { p_regexp = $("custom_field_regexp"); p_values = $("custom_field_possible_values"); p_searchable = $("custom_field_searchable"); + p_default = $("custom_field_default_value"); + + p_default.setAttribute('type','text'); + Element.show(p_default.parentNode); + switch (format.value) { case "list": Element.hide(p_length.parentNode); Element.hide(p_regexp.parentNode); - Element.show(p_searchable.parentNode); + if (p_searchable) Element.show(p_searchable.parentNode); Element.show(p_values); break; - case "date": case "bool": + p_default.setAttribute('type','checkbox'); + Element.hide(p_length.parentNode); + Element.hide(p_regexp.parentNode); + if (p_searchable) Element.hide(p_searchable.parentNode); + Element.hide(p_values); + break; + case "date": Element.hide(p_length.parentNode); Element.hide(p_regexp.parentNode); - Element.hide(p_searchable.parentNode); + if (p_searchable) Element.hide(p_searchable.parentNode); Element.hide(p_values); break; case "float": case "int": Element.show(p_length.parentNode); Element.show(p_regexp.parentNode); - Element.hide(p_searchable.parentNode); + if (p_searchable) Element.hide(p_searchable.parentNode); Element.hide(p_values); break; default: Element.show(p_length.parentNode); Element.show(p_regexp.parentNode); - Element.show(p_searchable.parentNode); + if (p_searchable) Element.show(p_searchable.parentNode); Element.hide(p_values); break; } @@ -70,6 +81,7 @@ function deleteValueField(e) { <%= text_field_tag 'custom_field[possible_values][]', value, :size => 30 %> <%= image_to_function "delete.png", "deleteValueField(this);return false" %>
<% end %>

+

<%= @custom_field.field_format == 'bool' ? f.check_box(:default_value) : f.text_field(:default_value) %>

diff --git a/lang/bg.yml b/lang/bg.yml index 7f994acd4..2c83b966f 100644 --- a/lang/bg.yml +++ b/lang/bg.yml @@ -161,6 +161,7 @@ field_delay: Отместване field_assignable: Възможно е възлагане на задачи за тази роля field_redirect_existing_links: Пренасочване на съществуващи линкове field_estimated_hours: Изчислено време +field_default_value: Статус по подразбиране setting_app_title: Заглавие setting_app_subtitle: Описание diff --git a/lang/cs.yml b/lang/cs.yml index 6e904c106..cde58fe17 100644 --- a/lang/cs.yml +++ b/lang/cs.yml @@ -159,6 +159,7 @@ field_is_filter: Used as a filter field_issue_to_id: Vztažený požadavek field_delay: Zpoždění field_assignable: Požadavky mohou být přiřazeny této roli +field_default_value: Výchozí stav setting_app_title: Titulek aplikace setting_app_subtitle: Podtitulek aplikace diff --git a/lang/de.yml b/lang/de.yml index 27ca40a3d..a23e9c533 100644 --- a/lang/de.yml +++ b/lang/de.yml @@ -161,6 +161,7 @@ field_delay: Pufferzeit field_assignable: Tickets können dieser Rolle zugewiesen werden field_redirect_existing_links: Existierende Links umleiten field_estimated_hours: Geschätzter Aufwand +field_default_value: Default setting_app_title: Applikations-Titel setting_app_subtitle: Applikations-Untertitel diff --git a/lang/en.yml b/lang/en.yml index 6580d398a..dc7b04dd2 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -174,6 +174,7 @@ field_estimated_hours: Estimated time field_column_names: Columns field_time_zone: Time zone field_searchable: Searchable +field_default_value: Default value setting_app_title: Application title setting_app_subtitle: Application subtitle diff --git a/lang/es.yml b/lang/es.yml index 1c6776555..8e9ffaa2f 100644 --- a/lang/es.yml +++ b/lang/es.yml @@ -155,6 +155,7 @@ field_identifier: Identificador field_is_filter: Usado como filtro field_issue_to_id: Petición Relacionada field_delay: Retraso +field_default_value: Estado por defecto setting_app_title: Título de la aplicación setting_app_subtitle: Subtítulo de la aplicación diff --git a/lang/fi.yml b/lang/fi.yml index 7eba540a7..497647ad9 100644 --- a/lang/fi.yml +++ b/lang/fi.yml @@ -174,6 +174,7 @@ field_estimated_hours: Arvioitu aika field_column_names: Saraketta field_time_zone: Aikavyöhyke field_searchable: Haettava +field_default_value: Vakio arvo setting_app_title: Ohjelman otsikko setting_app_subtitle: Ohjelman alaotsikko diff --git a/lang/fr.yml b/lang/fr.yml index ce3817d3c..854ed5859 100644 --- a/lang/fr.yml +++ b/lang/fr.yml @@ -174,6 +174,7 @@ field_estimated_hours: Temps estimé field_column_names: Colonnes field_time_zone: Fuseau horaire field_searchable: Utilisé pour les recherches +field_default_value: Valeur par défaut 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 928c68baa..3e8ad4160 100644 --- a/lang/he.yml +++ b/lang/he.yml @@ -164,6 +164,7 @@ field_assignable: ניתן להקצות נושאים לתפקיד זה field_redirect_existing_links: העבר קישורים קיימים field_estimated_hours: זמן משוער field_column_names: עמודות +field_default_value: ערך ברירת מחדל setting_app_title: כותרת ישום setting_app_subtitle: תת-כותרת ישום diff --git a/lang/it.yml b/lang/it.yml index a16228149..ca4b3391a 100644 --- a/lang/it.yml +++ b/lang/it.yml @@ -161,6 +161,7 @@ field_delay: Delay field_assignable: Issues can be assigned to this role field_redirect_existing_links: Redirect existing links field_estimated_hours: Estimated time +field_default_value: Stato predefinito setting_app_title: Titolo applicazione setting_app_subtitle: Sottotitolo applicazione diff --git a/lang/ja.yml b/lang/ja.yml index 56f846947..713af6b46 100644 --- a/lang/ja.yml +++ b/lang/ja.yml @@ -162,6 +162,7 @@ field_delay: 遅延 field_assignable: 問題はこのロールに割り当てることができます field_redirect_existing_links: 既存のリンクをリダイレクトする field_estimated_hours: 予定工数 +field_default_value: デフォルトのステータス setting_app_title: アプリケーションのタイトル setting_app_subtitle: アプリケーションのサブタイトル diff --git a/lang/ko.yml b/lang/ko.yml index 7a509478e..564e67253 100644 --- a/lang/ko.yml +++ b/lang/ko.yml @@ -164,6 +164,7 @@ field_assignable: 이 역할에 할당될수 있는 티켓 field_redirect_existing_links: Redirect existing links field_estimated_hours: 추정시간 field_column_names: 컬럼 +field_default_value: 기본값 setting_app_title: 레드마인 제목 setting_app_subtitle: 레드마인 부제목 diff --git a/lang/lt.yml b/lang/lt.yml index 8455260f1..d95da7469 100644 --- a/lang/lt.yml +++ b/lang/lt.yml @@ -171,7 +171,8 @@ field_estimated_hours: Apskaičiuotas laikas field_column_names: Skiltys field_time_zone: Laiko juosta field_searchable: Randamas - +field_default_value: Numatytoji vertė + setting_app_title: Programos pavadinimas setting_app_subtitle: Programos paantraštė setting_welcome_text: Pasveikinimas diff --git a/lang/nl.yml b/lang/nl.yml index 92e5a5971..8d29ab902 100644 --- a/lang/nl.yml +++ b/lang/nl.yml @@ -109,7 +109,7 @@ field_issue: Issue field_status: Status field_notes: Notities field_is_closed: Issue gesloten -field_is_default: Default status +field_is_default: Default field_tracker: Tracker field_subject: Onderwerp field_due_date: Verwachte datum gereed @@ -161,6 +161,7 @@ field_delay: Vertraging field_assignable: Issues can be assigned to this role field_redirect_existing_links: Redirect existing links field_estimated_hours: Estimated time +field_default_value: Default value setting_app_title: Applicatie titel setting_app_subtitle: Applicatie ondertitel diff --git a/lang/pl.yml b/lang/pl.yml index 548917a36..69bf5306a 100644 --- a/lang/pl.yml +++ b/lang/pl.yml @@ -155,6 +155,7 @@ field_identifier: Identifikator field_is_filter: Atrybut filtrowania field_issue_to_id: Powiązania zagadnienia field_delay: Opóźnienie +field_default_value: Domyślny setting_app_title: Tytuł aplikacji setting_app_subtitle: Podtytuł aplikacji diff --git a/lang/pt-br.yml b/lang/pt-br.yml index 45e66cd52..926c913a0 100644 --- a/lang/pt-br.yml +++ b/lang/pt-br.yml @@ -161,6 +161,7 @@ field_delay: Delay field_assignable: Issues can be assigned to this role field_redirect_existing_links: Redirect existing links field_estimated_hours: Estimated time +field_default_value: Padrao setting_app_title: Titulo da aplicacao setting_app_subtitle: Sub-titulo da aplicacao diff --git a/lang/pt.yml b/lang/pt.yml index 54b74c47c..88fd9cf74 100644 --- a/lang/pt.yml +++ b/lang/pt.yml @@ -161,6 +161,7 @@ field_delay: Atraso field_assignable: Issues can be assigned to this role field_redirect_existing_links: Redirect existing links field_estimated_hours: Estimated time +field_default_value: Padrão setting_app_title: Título da aplicação setting_app_subtitle: Sub-título da aplicação diff --git a/lang/ro.yml b/lang/ro.yml index c3427d261..2eb17609c 100644 --- a/lang/ro.yml +++ b/lang/ro.yml @@ -161,6 +161,7 @@ field_delay: Intarziere field_assignable: La acest rol se poate atribui tichete field_redirect_existing_links: Redirectare linkuri existente field_estimated_hours: Timpul estimat +field_default_value: Default value setting_app_title: Titlul aplicatiei setting_app_subtitle: Subtitlul aplicatiei diff --git a/lang/ru.yml b/lang/ru.yml index 7bf47c8fc..62799bdf8 100644 --- a/lang/ru.yml +++ b/lang/ru.yml @@ -166,6 +166,7 @@ field_assignable: Задача может быть назначена этой field_redirect_existing_links: Перенаправить существующие ссылки field_estimated_hours: Оцененное время field_column_names: Колонки +field_default_value: Default value setting_app_title: Название приложения setting_app_subtitle: Подзаголовок приложения diff --git a/lang/sr.yml b/lang/sr.yml index 6c486a83f..6d88d34ff 100644 --- a/lang/sr.yml +++ b/lang/sr.yml @@ -166,6 +166,7 @@ field_assignable: Kartice mogu biti dodeljene ovoj ulozi field_redirect_existing_links: Redirekcija postojećih linkova field_estimated_hours: Procenjeno vreme field_column_names: Kolone +field_default_value: Default value setting_app_title: Naziv aplikacije setting_app_subtitle: Podnaslov aplikacije diff --git a/lang/sv.yml b/lang/sv.yml index 0cb9a2d67..5f4a01126 100644 --- a/lang/sv.yml +++ b/lang/sv.yml @@ -161,6 +161,7 @@ field_delay: Delay field_assignable: Issues can be assigned to this role field_redirect_existing_links: Redirect existing links field_estimated_hours: Estimated time +field_default_value: Default value setting_app_title: Applikationstitel setting_app_subtitle: Applicationsunderrubrik diff --git a/lang/zh-tw.yml b/lang/zh-tw.yml index 465855b58..2a431266e 100644 --- a/lang/zh-tw.yml +++ b/lang/zh-tw.yml @@ -174,6 +174,7 @@ field_estimated_hours: 預估工時 field_column_names: Columns field_time_zone: 時區 field_searchable: 可用做搜尋條件 +field_default_value: Default value setting_app_title: 標題 setting_app_subtitle: 副標題 diff --git a/lang/zh.yml b/lang/zh.yml index c42c60fac..b87a1515d 100644 --- a/lang/zh.yml +++ b/lang/zh.yml @@ -164,6 +164,7 @@ field_delay: Delay field_assignable: Issues can be assigned to this role field_redirect_existing_links: Redirect existing links field_estimated_hours: Estimated time +field_default_value: Default value setting_app_title: 应用程序标题 setting_app_subtitle: 应用程序子标题 diff --git a/test/fixtures/custom_fields.yml b/test/fixtures/custom_fields.yml index e73e6de96..e58d8e3dc 100644 --- a/test/fixtures/custom_fields.yml +++ b/test/fixtures/custom_fields.yml @@ -10,6 +10,7 @@ custom_fields_001: id: 1 is_required: false field_format: list + default_value: "" custom_fields_002: name: Searchable field min_length: 1 @@ -22,6 +23,7 @@ custom_fields_002: is_required: false field_format: string searchable: true + default_value: "Default string" custom_fields_003: name: Development status min_length: 0 @@ -33,6 +35,7 @@ custom_fields_003: id: 3 is_required: true field_format: list + default_value: "" custom_fields_004: name: Phone number min_length: 0 @@ -44,6 +47,7 @@ custom_fields_004: id: 4 is_required: false field_format: string + default_value: "" custom_fields_005: name: Money min_length: 0 @@ -55,4 +59,5 @@ custom_fields_005: id: 5 is_required: false field_format: float + default_value: "" \ No newline at end of file diff --git a/test/functional/issues_controller_test.rb b/test/functional/issues_controller_test.rb index 05bfc96e3..f4c99f1ed 100644 --- a/test/functional/issues_controller_test.rb +++ b/test/functional/issues_controller_test.rb @@ -34,7 +34,10 @@ class IssuesControllerTest < Test::Unit::TestCase :enabled_modules, :enumerations, :attachments, - :workflows + :workflows, + :custom_fields, + :custom_values, + :custom_fields_trackers def setup @controller = IssuesController.new @@ -132,6 +135,9 @@ class IssuesControllerTest < Test::Unit::TestCase get :new, :project_id => 1, :tracker_id => 1 assert_response :success assert_template 'new' + + assert_tag :tag => 'input', :attributes => { :name => 'custom_fields[2]', + :value => 'Default string' } end def test_get_new_without_tracker_id @@ -162,9 +168,16 @@ class IssuesControllerTest < Test::Unit::TestCase :issue => {:tracker_id => 1, :subject => 'This is the test_new issue', :description => 'This is the description', - :priority_id => 5} + :priority_id => 5}, + :custom_fields => {'2' => 'Value for field 2'} assert_redirected_to 'projects/ecookbook/issues' - assert Issue.find_by_subject('This is the test_new issue') + + issue = Issue.find_by_subject('This is the test_new issue') + assert_not_nil issue + assert_equal 2, issue.author_id + v = issue.custom_values.find_by_custom_field_id(2) + assert_not_nil v + assert_equal 'Value for field 2', v.value end def test_copy_issue diff --git a/test/integration/issues_test.rb b/test/integration/issues_test.rb index 81d27c30f..7249ed3da 100644 --- a/test/integration/issues_test.rb +++ b/test/integration/issues_test.rb @@ -1,7 +1,16 @@ require "#{File.dirname(__FILE__)}/../test_helper" class IssuesTest < ActionController::IntegrationTest - fixtures :projects, :users, :trackers, :issue_statuses, :issues, :enumerations + fixtures :projects, + :users, + :trackers, + :projects_trackers, + :issue_statuses, + :issues, + :enumerations, + :custom_fields, + :custom_values, + :custom_fields_trackers # create an issue def test_add_issue @@ -18,7 +27,8 @@ class IssuesTest < ActionController::IntegrationTest :description => "new issue", :done_ratio => "0", :due_date => "", - :assigned_to_id => "" } + :assigned_to_id => "" }, + :custom_fields => {'2' => 'Value for field 2'} # find created issue issue = Issue.find_by_subject("new test issue") assert_kind_of Issue, issue diff --git a/test/unit/custom_value_test.rb b/test/unit/custom_value_test.rb index 24d09fe49..4d488bae2 100644 --- a/test/unit/custom_value_test.rb +++ b/test/unit/custom_value_test.rb @@ -31,4 +31,15 @@ class CustomValueTest < Test::Unit::TestCase v.value = '6a' assert !v.save end + + def test_default_value + field = CustomField.find_by_default_value('Default string') + assert_not_nil field + + v = CustomValue.new(:custom_field => field) + assert_equal 'Default string', v.value + + v = CustomValue.new(:custom_field => field, :value => 'Not empty') + assert_equal 'Not empty', v.value + end end -- 2.39.5