diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 58911b5f73..f3a0f53f07 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -103,6 +103,18 @@ class CustomField < ActiveRecord::Base end end + def default_value + if self.default_value_is_current_date? + User.current.today.to_s + else + self[:default_value] + end + end + + def default_value_is_current_date? + self.field_format == 'date' && self[:default_value] == 'current_date' + end + def set_searchable # make sure these fields are not searchable self.searchable = false unless format.class.searchable_supported diff --git a/app/views/custom_fields/formats/_date.html.erb b/app/views/custom_fields/formats/_date.html.erb index b52c063105..e459ec202e 100644 --- a/app/views/custom_fields/formats/_date.html.erb +++ b/app/views/custom_fields/formats/_date.html.erb @@ -1,3 +1,15 @@ -
<%= f.date_field(:default_value, :value => @custom_field.default_value, :size => 10) %>
-<%= calendar_for('custom_field_default_value') %> ++ <%= f.label(:default_value) %> + + <%= date_field_tag 'custom_field[default_value]', !@custom_field.default_value_is_current_date? && @custom_field.default_value, :size => 10, :label => false, :id => nil, :class => 'custom_field_default_value_specific_date' %> + <%= f.hidden_field :default_value, :value => 'current_date', :disabled => @custom_field.default_value != 'current_date', :class => 'custom_field_default_value_current_date' %> + <%= calendar_for('custom_field_default_value') %> + +
<%= f.text_field :url_pattern, :size => 50, :label => :label_link_values_to %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index d8f09431f0..6efc6a0e46 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -378,6 +378,8 @@ en: field_full_width_layout: Full width layout field_digest: Checksum field_default_assigned_to: Default assignee + field_specific_date: Specific date + field_current_date: Current date setting_app_title: Application title setting_welcome_text: Welcome text diff --git a/lib/redmine/field_format.rb b/lib/redmine/field_format.rb index c4960c0f70..ed461706d8 100644 --- a/lib/redmine/field_format.rb +++ b/lib/redmine/field_format.rb @@ -534,7 +534,7 @@ module Redmine end def validate_single_value(custom_field, value, customized=nil) - if value =~ /^\d{4}-\d{2}-\d{2}$/ && (value.to_date rescue false) + if (value =~ /^\d{4}-\d{2}-\d{2}$/ && (value.to_date rescue false)) || value == 'current_date' [] else [::I18n.t('activerecord.errors.messages.not_a_date')] diff --git a/test/unit/custom_field_test.rb b/test/unit/custom_field_test.rb index 3eae54efae..efe76f2024 100644 --- a/test/unit/custom_field_test.rb +++ b/test/unit/custom_field_test.rb @@ -340,4 +340,17 @@ class CustomFieldTest < ActiveSupport::TestCase assert_equal 12.5, field.cast_value('+12.5') assert_equal -12.5, field.cast_value('-12.5') end + + def test_default_value_should_return_today_if_default_is_current_date + field = CustomField.new(:field_format => 'date', default_value: 'current_date') + assert_equal field.default_value, User.current.today.to_s + end + + def test_default_value_is_current_date? + field = CustomField.new(:field_format => 'date', default_value: 'current_date') + assert field.default_value_is_current_date? + + field = CustomField.new(:field_format => 'date', default_value: User.current.today.to_s) + assert_not field.default_value_is_current_date? + end end