Skip to content

Commit

Permalink
move validator class to standalone folder.
Browse files Browse the repository at this point in the history
  • Loading branch information
saberma committed Feb 2, 2012
1 parent 0749017 commit 17d931d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 39 deletions.
10 changes: 10 additions & 0 deletions app/validators/email_format_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# encoding: utf-8
class EmailFormatValidator < ActiveModel::EachValidator #用于邮件校验

def validate_each(object, attribute, value)
unless value =~ /\A[^@]+@([^@\.]+\.)+[^@\.]+\z/
object.errors[attribute] << (options[:message] || "格式不对")
end
end

end
12 changes: 12 additions & 0 deletions app/validators/sku_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# encoding: utf-8
class SkuValidator < ActiveModel::Validator #用于验证sku数量超过商店限制后不能再新增商品和款式 issues#282

def validate(record)
shop = record.shop || record.product.try(:shop)
if !shop.nil? && shop.plan_type.skus <= shop.variants.size
record.errors[:base] << "超过商店限制"
end
end

end

39 changes: 0 additions & 39 deletions config/initializers/add_method_to_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,42 +30,3 @@ def smart_fetch(name, options = {}, &blk)
return val
end
end

#增加subdomain属性
#rails 3.1默认支持subdomain和domain
#module UrlHelper
# extend ActionDispatch::Routing::UrlFor
#
# def with_subdomain(subdomain)
# subdomain = (subdomain || "")
# subdomain += "." unless subdomain.empty?
# [subdomain, request.domain, request.port_string].join
# end
#
# def url_for(options = nil)
# if options.kind_of?(Hash) && options.has_key?(:subdomain)
# options[:host] = with_subdomain(options.delete(:subdomain))
# end
# super
# end
#
#end

#用于邮件校验
class EmailFormatValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
unless value =~ /\A[^@]+@([^@\.]+\.)+[^@\.]+\z/
object.errors[attribute] << (options[:message] || "格式不对")
end
end
end

#用于验证sku数量超过商店限制后不能再新增商品和款式 issues#282
class SkuValidator < ActiveModel::Validator
def validate(record)
shop = record.shop || record.product.try(:shop)
if !shop.nil? && shop.plan_type.skus <= shop.variants.size
record.errors[:base] << "超过商店限制"
end
end
end

0 comments on commit 17d931d

Please sign in to comment.