Project

General

Profile

Patch #6450 ยป scvmc_redmine_668p1_custom_field_dates.diff

Stuart Cianos, 2010-09-21 03:13

View differences:

app/models/custom_value.rb (working copy)
16 16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 17

  
18 18
class CustomValue < ActiveRecord::Base
19
  include Redmine::I18n
20
  
19 21
  belongs_to :custom_field
20 22
  belongs_to :customized, :polymorphic => true
21 23

  
24
  before_save :transform
25

  
22 26
  def after_initialize
23 27
    if new_record? && custom_field && (customized_type.blank? || (customized && customized.new_record?))
24 28
      self.value ||= custom_field.default_value
......
41 45
  def to_s
42 46
    value.to_s
43 47
  end
44
  
48
 
45 49
protected
46 50
  def validate
47 51
    if value.blank?
......
58 62
      when 'float'
59 63
        begin; Kernel.Float(value); rescue; errors.add(:value, :invalid) end
60 64
      when 'date'
61
        errors.add(:value, :not_a_date) unless value =~ /^\d{4}-\d{2}-\d{2}$/
65
        errors.add(:value, :not_a_date) unless parse_user_date(value)
62 66
      when 'list'
63 67
        errors.add(:value, :inclusion) unless custom_field.possible_values.include?(value)
64 68
      end
65 69
    end
66 70
  end
71

  
72
private
73

  
74
  def transform
75
    self.value = parse_user_date(value) if custom_field.field_format == 'date'
76
  end
77

  
67 78
end
lib/redmine/i18n.rb (working copy)
39 39
      return nil unless date
40 40
      Setting.date_format.blank? ? ::I18n.l(date.to_date) : date.strftime(Setting.date_format)
41 41
    end
42

  
43
    def parse_user_date(datestring)
44
      return nil unless datestring
45
      begin
46
        if datestring =~ /^\d{4}-\d{2}-\d{2}$/
47
          r = Date.strptime(datestring, '%F')
48
        elsif
49
          r = Setting.date_format.blank? ? Date.parse(datestring) : Date.strptime(datestring, Setting.date_format)
50
        end
51
      rescue
52
        r = nil
53
      end
54
      return r
55
    end
42 56
    
43 57
    def format_time(time, include_date = true)
44 58
      return nil unless time
    (1-1/1)