Sha256: 6405af6765cf531066f24b9c097780ff7a1c6f07e007b91e606de115097778ff

Contents?: true

Size: 788 Bytes

Versions: 11

Compression:

Stored size: 788 Bytes

Contents

class SlugPatternValidator < ActiveModel::EachValidator
  def validate_each record, attribute, value
		if value.blank?
			record.errors[attribute] << "cannot be blank"
		else
			return string_replaced value
		end
  end

	def string_replaced value
		email_removal_for_morons value
		intelligently_de_dot value
		finish_normalizing value
		value
	end

private

	def finish_normalizing str
		str.gsub!(/\W/,'-')
		str.gsub!(/(\W\-|\-\W|\-)+/,'-')
		str.gsub!(/(\A\-|\-\Z)/,'')
		str
	end

	def intelligently_de_dot str
		str.gsub!(/([A-Z])\.([a-z])/,'\1-\2')
		str.gsub!(/([A-Z])\./,'\1')
		str
	end

	def email_removal_for_morons str
		str.gsub(/^(.*)?@([^@]*)?\.([^@\.]*)$/, '\1' )
		pattern = ("" == $1) ? '\2-\3' : '\1'
		str.gsub!(/^(.*)?@([^@]*)?\.([^@\.]*)$/, pattern )
		str
	end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
stripe_local-0.2.5 app/validators/slug_pattern_validator.rb
stripe_local-0.2.4 app/validators/slug_pattern_validator.rb
stripe_local-0.2.3 app/validators/slug_pattern_validator.rb
stripe_local-0.2.2 app/validators/slug_pattern_validator.rb
stripe_local-0.2.1 app/validators/slug_pattern_validator.rb
stripe_local-0.2.0 app/validators/slug_pattern_validator.rb
stripe_local-0.1.3 app/validators/slug_pattern_validator.rb
stripe_local-0.1.2 app/validators/slug_pattern_validator.rb
stripe_local-0.1.1 app/validators/slug_pattern_validator.rb
stripe_local-0.1.0 app/validators/slug_pattern_validator.rb
stripe_local-0.0.2 app/validators/slug_pattern_validator.rb