Sha256: e4393826f52b2b0ad1f26231bbe45def75ee72616b2ea7882462b40460164c66

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

module Validateable

  [:save, :save!, :update_attribute].each{|attr| define_method(attr){}}

  def method_missing(symbol, *params)
    if(symbol.to_s =~ /(.*)_before_type_cast$/)
      send($1)
    end
  end

  def self.append_features(base)
    super
    base.send(:include, ActiveRecord::Validations)
  end

end

module ActiveRecord
  module Validations
    module ClassMethods

      def validates_value_of(*attr_names)
        configuration = {
          :message => 'value doesn\'t exist' 
        }
        configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)

        if configuration[:in].nil?
          validates_each(attr_names, configuration) do |record, attr_name, value|
            if (attr_names[1].to_s != record.send(attr_names[0].to_sym))
              record.errors.add(attr_name, configuration[:message])
            end
          end
        else
          validates_each(attr_names, configuration) do |record, attr_name, value|
            found = false
            record.send(configuration[:in]).each do |s|
              if (attr_names[1].to_s == s.send(attr_names[0].to_sym))
                found = true
                break
              end
            end
            record.errors.add(attr_name, configuration[:message]) unless found
          end
        end
      end

      def validates_as_email(*attr_names)
        configuration = {
          :message   => 'is an invalid email',
          :with      => RFC822::EmailAddress,
          :allow_nil => true }
        configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)

        validates_format_of attr_names, configuration
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
adaptation-0.0.5 lib/adaptation/validateable.rb
adaptation-0.0.6 lib/adaptation/validateable.rb