Sha256: 46e22b8ff14155b76dd8e12d44eb13f376e1a66eb7ae942e81c1a87b808ea44a

Contents?: true

Size: 915 Bytes

Versions: 1

Compression:

Stored size: 915 Bytes

Contents

module SubdomainRoutes
  def self.valid_subdomain?(subdomain)
    subdomain.to_s =~ /^([a-z]|[a-z][a-z0-9]|[a-z]([a-z0-9]|\-[a-z0-9])*)$/
  # # TODO: could we use URI::parse instead?:
  #   URI.parse "http://#{subdomain}.example.com"
  # rescue URI::InvalidURIError
  #   false
  end
  
  module Validations
    module ClassMethods
      def validates_subdomain_format_of(*attr_names)
        configuration = { :on => :save }
        configuration.update(attr_names.extract_options!)
      
        validates_each(attr_names, configuration) do |record, attr_name, value|
          unless SubdomainRoutes.valid_subdomain?(value)
            record.errors.add(attr_name, :not_a_valid_subdomain, :default => configuration[:message], :value => value) 
          end
        end
      end
    end
  end
end

if defined? ActiveRecord::Base
  ActiveRecord::Base.send :extend, SubdomainRoutes::Validations::ClassMethods
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mholling-subdomain_routes-0.1.0 lib/subdomain_routes/validations.rb