blob: 5c66cb1a9c5fa30d58a2d1953f0c38552e0e935b (
plain)
| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 | module DateSupport
  extend ActiveSupport::Concern
  included do
    after_initialize :init_dates
    def init_dates
      self.dates ||= []
    end
    ### Calendar::DateValue
    # Required by coocon
    def build_date_value
      Calendar::DateValue.new
    end
    def date_values
      @date_values ||= init_date_values
    end
    def init_date_values
      if dates
        dates.each_with_index.map { |d, index| Calendar::DateValue.from_date(index, d) }
      else
        []
      end
    end
    private :init_date_values
    validate :validate_date_values
    def validate_date_values
      date_values_are_valid = date_values.all?(&:valid?)
      date_values.each do |date_value|
        if date_values.count { |d| d.value == date_value.value } > 1
          date_value.errors.add(:base, I18n.t('activerecord.errors.models.calendar.attributes.dates.date_in_dates'))
          date_values_are_valid = false
        end
        date_ranges.each do |date_range|
          if date_range.cover?(date_value.value)
            excluded_day = self.respond_to?(:valid_day?) && !self.valid_day?(date_value.value.wday)
            unless excluded_day
              date_value.errors.add(:base, I18n.t('activerecord.errors.models.calendar.attributes.dates.date_in_date_ranges'))
              date_values_are_valid = false
            end
          end
        end
      end
      unless date_values_are_valid
        errors.add(:date_values, :invalid)
      end
    end
    def date_values_attributes=(attributes = {})
      @date_values = []
      attributes.each do |index, date_value_attribute|
        date_value_attribute['value'] = flatten_date_array(date_value_attribute, 'value')
        date_value = Calendar::DateValue.new(date_value_attribute.merge(id: index))
        @date_values << date_value unless date_value.marked_for_destruction?
      end
      dates_will_change!
    end
    before_validation :fill_dates
    def fill_dates
      if @date_values
        self.dates = @date_values.map(&:value).compact.sort
      end
    end
    after_save :clear_date_values
    def clear_date_values
      @date_values = nil
    end
    private :clear_date_values
  end
end
 |