Sha256: 6ef13da96e712c66efa35a5fa1669b2d8ff21bedb69a1f00dc9602cbacb8ee48

Contents?: true

Size: 1.03 KB

Versions: 7

Compression:

Stored size: 1.03 KB

Contents

require 'active_support/core_ext/hash/reverse_merge'

module ValidatesFormattingOf
  module ModelAdditions

    # Using validates_formatting_of is as simple as using Rails' built-in
    # validation methods in models.
    #
    # class User < ActiveRecord::Base
    #   validates_formatting_of :email, :using => :email
    # end
    #
    # If your column name is idencital to any of the built-in methods, you
    # may leave off the `:using` option and validates_formatting_of will
    # automatically use the validation with the matching name.
    #
    # class User < ActiveRecord::Base
    #   validates_formatting_of :email
    # end
    #
    # You can also pass conditions and options for Rails to use
    # * :if
    # * :unless
    # * :allow_nil
    # * :allow_blank
    # * :on
    def validates_formatting_of(attribute, options = {})
      validation = Method.find(attribute, options)
      options.reverse_merge!(:with => validation.regex, :message => validation.message)
      self.validates_format_of(attribute, options)
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
validates_formatting_of-0.9.0 lib/validates_formatting_of/model_additions.rb
validates_formatting_of-0.8.1 lib/validates_formatting_of/model_additions.rb
validates_formatting_of-0.8.0 lib/validates_formatting_of/model_additions.rb
validates_formatting_of-0.7.2 lib/validates_formatting_of/model_additions.rb
validates_formatting_of-0.7.1 lib/validates_formatting_of/model_additions.rb
validates_formatting_of-0.7.0 lib/validates_formatting_of/model_additions.rb
validates_formatting_of-0.6.9 lib/validates_formatting_of/model_additions.rb