Sha256: 868b86f40e98ae825b6fbbcf51f290ffb19b7fe2fb91047daf5fff76e228dad2

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module ValidatesTimeliness
  module ORM
    module Mongoid
      extend ActiveSupport::Concern
      # You need define the fields before you define the validations.
      # It is best to use the plugin parser to avoid errors on a bad
      # field value in Mongoid. Parser will return nil rather than error.

      module ClassMethods 
        def timeliness_validation_for(attr_names, type)
          super
          attr_names.each { |attr_name| define_timeliness_write_method(attr_name, type, false) }
        end

        def define_timeliness_write_method(attr_name, type, timezone_aware)
          method_body, line = <<-EOV, __LINE__ + 1
            def #{attr_name}=(value)
              @attributes_cache ||= {}
              @attributes_cache["_#{attr_name}_before_type_cast"] = value
              #{ "value = ValidatesTimeliness::Parser.parse(value, :#{type}) if value.is_a?(String)" if ValidatesTimeliness.use_plugin_parser }
              write_attribute(:#{attr_name}, value)
            end
          EOV
          class_eval(method_body, __FILE__, line)
        end
      end
    end
  end
end
 
module Mongoid::Document
  include ValidatesTimeliness::HelperMethods
  include ValidatesTimeliness::AttributeMethods
  include ValidatesTimeliness::ORM::Mongoid
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
validates_timeliness-3.0.0.beta.3 lib/validates_timeliness/orm/mongoid.rb