Permalink
Browse files

initial version of plugin

  • Loading branch information...
0 parents commit d4ae3cce83836a9d6dcc41255cc01abc599e630b Gareth Townsend committed Jul 1, 2008
Showing with 132 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +16 −0 README
  3. +22 −0 Rakefile
  4. +3 −0 init.rb
  5. +1 −0 install.rb
  6. +31 −0 lib/validates_as_email.rb
  7. +26 −0 lib/validation_extentions.rb
  8. +4 −0 tasks/validates_as_email_tasks.rake
  9. +8 −0 test/validates_as_email_test.rb
  10. +1 −0 uninstall.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2008 Gareth Townsend
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16 README
@@ -0,0 +1,16 @@
+ValidatesAsEmail
+================
+
+Active Record extension for validating email addresses using TMail and MX records.
+
+Example
+=======
+
+class User < ActiveRecord::Base
+
+ validates_as_email :email_address
+
+end
+
+
+Copyright (c) 2008 Gareth Townsend, released under the MIT license
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the validates_as_email plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the validates_as_email plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'ValidatesAsEmail'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,3 @@
+# Include hook code here
+
+require 'validation_extentions'
@@ -0,0 +1 @@
+# Install hook code here
@@ -0,0 +1,31 @@
+# ValidatesAsEmail
+
+class ValidatesAsEmail
+
+ class EmailAddress
+ require 'resolv'
+
+ attr_accessor :address
+
+ def initialize(email = '')
+ self.address = email
+ end
+
+ def valid?
+ begin
+ @domain_name = TMail::Address.parse(address).domain
+ return true
+ rescue
+ return false
+ end
+ end
+
+ def valid_mx_record?
+ Resolv::DNS.open do |dns|
+ @mx = dns.getresources(@domain_name.to_s, Resolv::DNS::Resource::IN::MX)
+ end
+ !@mx.empty?
+ end
+
+ end
+end
@@ -0,0 +1,26 @@
+module ActiveRecord
+ module Validations
+ module ClassMethods
+
+ def validates_as_email(*attr_names)
+ configuration = { :message => ActiveRecord::Errors.default_error_messages[:invalid],
+ :on => :save,
+ :with => nil,
+ :check_mx_record => true, }
+ configuration.update(attr_names.extract_options!)
+
+ validates_each(attr_names, configuration) do |record, attr_name, value|
+ email = ValidatesAsEmail::EmailAddress.new(value)
+ message = :message unless email.valid?
+
+ if configuration[:check_mx_record] && !message
+ message = :message unless email.valid_mx_record?
+ end
+
+ record.errors.add(attr_name, configuration[message]) if message
+ end
+ end
+
+ end
+ end
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :validates_as_email do
+# # Task goes here
+# end
@@ -0,0 +1,8 @@
+require 'test/unit'
+
+class ValidatesAsEmailTest < Test::Unit::TestCase
+ # Replace this with your real tests.
+ def test_this_plugin
+ flunk
+ end
+end
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit d4ae3cc

Please sign in to comment.