Module: MailForm::Delivery::ClassMethods
- Defined in:
- lib/mail_form/delivery.rb
Instance Method Summary collapse
-
#append(*values) ⇒ Object
Values from request object to be appended to the contact form.
-
#attribute(*accessors) ⇒ Object
(also: #attributes)
Declare your form attributes.
Instance Method Details
#append(*values) ⇒ Object
Values from request object to be appended to the contact form. Whenever used, you have to send the request object when initializing the object:
@contact_form = ContactForm.new(params[:contact_form], request)
You can get the values to be appended from the AbstractRequest documentation (api.rubyonrails.org/classes/ActionController/AbstractRequest.html)
Examples
class ContactForm < MailForm
append :remote_ip, :user_agent, :session, :cookies
end
119 120 121 |
# File 'lib/mail_form/delivery.rb', line 119 def append(*values) self.mail_appendable += values end |
#attribute(*accessors) ⇒ Object Also known as: attributes
Declare your form attributes. All attributes declared here will be appended to the e-mail, except the ones captcha is true.
Options
-
:validate - A hook to validates_*_of. When true is given, validates the
presence of the attribute. When a regexp, validates format. When array, validates the inclusion of the attribute in the array. Whenever :validate is given, the presence is automatically checked. Give allow_blank: true to override. Finally, when :validate is a symbol, the method given as symbol will be called. Then you can add validations as you do in ActiveRecord (errors.add).
-
:attachment
- When given, expects a file to be sent and attaches it to the e-mail. Don’t forget to set your form to multitype. -
:captcha
- When true, validates the attributes must be blank This is a simple way to avoid spam
Examples
class ContactForm < MailForm
attributes :name, validate: true
attributes :email, validate: /\A[^@\s]+@[^@\s]+\z/i
attributes :type, validate: ["General", "Interface bug"]
attributes :message
attributes :screenshot, attachment: true, validate: :interface_bug?
attributes :nickname, captcha: true
def interface_bug?
if type == 'Interface bug' && screenshot.nil?
self.errors.add(:screenshot, "can't be blank when you are reporting an interface bug")
end
end
end
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/mail_form/delivery.rb', line 69 def attribute(*accessors) = accessors. # TODO: make this not depend on column_names columns_methods = self.respond_to?(:column_names) ? column_names.map(&:to_sym) : [] attr_accessor(*(accessors - instance_methods.map(&:to_sym) - columns_methods)) if [:attachment] self. += accessors elsif [:captcha] self.mail_captcha += accessors else self.mail_attributes += accessors end validation = .delete(:validate) return unless validation accessors.each do |accessor| case validation when Symbol, Class validate validation break 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 [:allow_blank] == true end end |