Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Let ActiveModel do all the work.

  • Loading branch information...
commit c2fb45febf862a2dfceacd4200e2f75f843d3c64 1 parent 692a158
@josevalim josevalim authored
Showing with 19 additions and 45 deletions.
  1. +2 −3 lib/mail_form.rb
  2. +1 −38 lib/mail_form/base.rb
  3. +16 −4 lib/mail_form/dsl.rb
View
5 lib/mail_form.rb
@@ -6,9 +6,9 @@
class MailForm
extend MailForm::DSL
- ACCESSORS = [ :form_attributes, :form_validatable, :form_subject,
+ ACCESSORS = [ :form_attributes, :form_subject, :form_captcha,
:form_attachments, :form_recipients, :form_sender,
- :form_captcha, :form_headers, :form_template, :form_appendable ]
+ :form_headers, :form_template, :form_appendable ]
DEFAULT_MESSAGES = { :blank => "can't be blank", :invalid => "is invalid" }
@@ -21,7 +21,6 @@ class MailForm
write_inheritable_array :form_appendable, []
write_inheritable_array :form_attributes, []
write_inheritable_array :form_attachments, []
- write_inheritable_hash :form_validatable, {}
headers({})
sender {|c| c.email }
View
39 lib/mail_form/base.rb
@@ -22,7 +22,7 @@ def initialize(params={}, request=nil)
# is good to remember that the field should be hidden with CSS and shown only
# to robots.
#
- # In test and in production, it returns true if aall captcha field are blank,
+ # In test and in production, it returns true if all captcha fields are blank,
# returns false otherwise.
#
def spam?
@@ -43,36 +43,6 @@ def not_spam?
!spam?
end
- # To check if the form is valid, we run the validations.
- #
- # If the validation is true, we just check if the field is not blank. If it's
- # a regexp, we check if it is not blank AND if the Regexp matches.
- #
- # You can have totally custom validations by sending a symbol. Then the method
- # given as symbol will be called and then you cann hook your validations there.
- #
- def valid?
- return false unless errors.empty?
-
- form_validatable.each_pair do |field, validation|
- next unless validation
-
- if validation.is_a?(Symbol)
- send(validation)
- elsif send(field).blank?
- errors.add(field, :blank)
- elsif validation.is_a?(Regexp)
- errors.add(field, :invalid) unless send(field) =~ validation
- end
- end
-
- errors.empty?
- end
-
- def invalid?
- !valid?
- end
-
# Always return true so when using form_for, the default method will be post.
#
def new_record?
@@ -106,11 +76,4 @@ def self.lookup_ancestors
super - [MailForm]
end
- # Return the errors in this form. The object returned as the same API as the
- # ActiveRecord one.
- #
- def errors
- @errors ||= ActiveModel::Errors.new(self)
- end
-
end
View
20 lib/mail_form/dsl.rb
@@ -51,11 +51,23 @@ def attribute(*accessors)
write_inheritable_array(:form_attributes, accessors)
end
- if options[:validate]
- validations = {}
- accessors.each{ |a| validations[a] = options[:validate] }
+ validation = options.delete(:validate)
+ return unless validation
- write_inheritable_hash(:form_validatable, validations)
+ accessors.each do |accessor|
+ case validation
+ when Symbol, Class
+ validate validation
+ return
+ when Regexp
+ validates_format_of accessor, :with => validation, :allow_blank => true
+ when Array
+ validates_inclusion_of accessor, :in => validation, :allow_blank => true
+ when Range
+ validates_length_of accessor, :within => validation, :allow_blank => true
+ end
+
+ validates_presence_of accessor unless options[:allow_blank] == true
end
end
alias :attributes :attribute
Please sign in to comment.
Something went wrong with that request. Please try again.