Project

General

Profile

Feature #25035 » add_current_date.patch

Mizuki ISHIKAWA, 2018-08-30 09:42

View differences:

app/models/custom_field.rb
103 103
    end
104 104
  end
105 105

  
106
  def default_value
107
    if self.default_value_is_current_date?
108
      User.current.today.to_s
109
    else
110
      self[:default_value]
111
    end
112
  end
113

  
114
  def default_value_is_current_date?
115
    self.field_format == 'date' && self[:default_value] == 'current_date'
116
  end
117

  
106 118
  def set_searchable
107 119
    # make sure these fields are not searchable
108 120
    self.searchable = false unless format.class.searchable_supported
app/views/custom_fields/formats/_date.html.erb
1
<p><%= f.date_field(:default_value, :value => @custom_field.default_value, :size => 10) %></p>
2
<%= calendar_for('custom_field_default_value') %>
1
<p>
2
  <%= f.label(:default_value) %>
3
    <label class="block">
4
    <%= radio_button_tag :default_value_type, 'specific_date', @custom_field.default_value.present? && !@custom_field.default_value_is_current_date?, {:checked => true, :data => {:disables => 'input.custom_field_default_value_current_date', :enables => 'input.custom_field_default_value_specific_date'}} %>
5
    <%= l(:field_specific_date) %>
6
  </label>
7
  <%= 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' %>
8
  <%= f.hidden_field :default_value, :value => 'current_date', :disabled => @custom_field.default_value != 'current_date', :class => 'custom_field_default_value_current_date' %>
9
  <%= calendar_for('custom_field_default_value') %>
10
  <label class="block">
11
    <%= radio_button_tag :default_value_type, 'current_date', @custom_field.default_value.present? && @custom_field.default_value_is_current_date?, {:data => {:disables => 'input.custom_field_default_value_specific_date', :enables => 'input.custom_field_default_value_current_date'}} %>
12
    <%= l(:field_current_date) %>
13
  </label>
14
</p>
3 15
<p><%= f.text_field :url_pattern, :size => 50, :label => :label_link_values_to %></p>
config/locales/en.yml
378 378
  field_full_width_layout: Full width layout
379 379
  field_digest: Checksum
380 380
  field_default_assigned_to: Default assignee
381
  field_specific_date: Specific date
382
  field_current_date: Current date
381 383

  
382 384
  setting_app_title: Application title
383 385
  setting_welcome_text: Welcome text
lib/redmine/field_format.rb
534 534
      end
535 535

  
536 536
      def validate_single_value(custom_field, value, customized=nil)
537
        if value =~ /^\d{4}-\d{2}-\d{2}$/ && (value.to_date rescue false)
537
        if (value =~ /^\d{4}-\d{2}-\d{2}$/ && (value.to_date rescue false)) || value == 'current_date'
538 538
          []
539 539
        else
540 540
          [::I18n.t('activerecord.errors.messages.not_a_date')]
test/unit/custom_field_test.rb
340 340
    assert_equal 12.5, field.cast_value('+12.5')
341 341
    assert_equal -12.5, field.cast_value('-12.5')
342 342
  end
343

  
344
  def test_default_value_should_return_today_if_default_is_current_date
345
    field = CustomField.new(:field_format => 'date', default_value: 'current_date')
346
    assert_equal field.default_value, User.current.today.to_s
347
  end
348

  
349
  def test_default_value_is_current_date?
350
    field = CustomField.new(:field_format => 'date', default_value: 'current_date')
351
    assert field.default_value_is_current_date?
352

  
353
    field = CustomField.new(:field_format => 'date', default_value: User.current.today.to_s)
354
    assert_not field.default_value_is_current_date?
355
  end
343 356
end
(2-2/2)