public
Clone URL: git://github.com/collectiveidea/calendar_builder.git
Click here to lend your support to: calendar_builder and make a donation at www.pledgie.com !
Mon Mar 31 14:56:59 -0700 2008
commit  f2be039c39fbc477f79794376a4ce97f0687047d
tree    b76555942d9fad5677412ec2c1516ffe27a7c276
parent  1fc8a12d392d78e76a9754f31f5a4729bc8e998d
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