public
Clone URL: git://github.com/collectiveidea/calendar_builder.git
Search Repo:
Click here to lend your support to: calendar_builder and make a donation at www.pledgie.com !
calendar_builder / lib / active_record / event_validations.rb
100644 26 lines (22 sloc) 0.819 kb
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
module CollectiveIdea
  module EventValidations
    
    def self.included(base)
      base.extend(ClassMethods)
    end
    
    module ClassMethods
      # Validates date/time fields in chronological order.
      def validates_chronological(*attr_names)
        options = {:allow_nil => true, :message => "is not in chronological order", :on => :save }
        options.update(attr_names.extract_options!).symbolize_keys
                
        send(validation_method(options[:on]), options) do |record|
          previous = record.send(attr_names.first)
          attr_names.each do |attr|
            value = record.send(attr)
            next if (value.nil? && options[:allow_nil]) || value >= previous
            record.errors.add(attr, options[:message])
          end
        end
      end
    end
 
  end
end