Class: SimpleForm

Inherits:
Object
  • Object
show all
Extended by:
DSL
Defined in:
lib/simple_form/errors.rb,
lib/simple_form.rb,
lib/simple_form/dsl.rb,
lib/simple_form/base.rb,
lib/simple_form/notifier.rb

Overview

This is the class responsable to send the e-mails.

Defined Under Namespace

Modules: DSL Classes: Errors, Notifier

Constant Summary collapse

ACCESSORS =
[ :form_attributes, :form_validatable, :form_subject, :form_attachments,
:form_recipients, :form_sender, :form_captcha, :form_headers, :form_appendable ]
DEFAULT_MESSAGES =
{ :blank => "can't be blank", :invalid => "is invalid" }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, request = nil) ⇒ SimpleForm

Initialize assigning the parameters given as hash (just as in ActiveRecord).

It also accepts the request object as second parameter which must be sent whenever :append is called.



9
10
11
12
13
14
# File 'lib/simple_form/base.rb', line 9

def initialize(params={}, request=nil)
  @request = request
  params.each_pair do |attr, value|
    self.send(:"#{attr}=", value)
  end unless params.blank?
end

Instance Attribute Details

#requestObject

Returns the value of attribute request.



2
3
4
# File 'lib/simple_form/base.rb', line 2

def request
  @request
end

Class Method Details

.human_attribute_name(attribute, options = {}) ⇒ Object

Add a human attribute name interface on top of I18n. If email is received as attribute, it will look for a translated name on:

simple_form:
  attributes:
    email: E-mail


103
104
105
# File 'lib/simple_form/base.rb', line 103

def self.human_attribute_name(attribute, options={})
  I18n.translate("attributes.#{attribute}", options.merge(:default => attribute.to_s.humanize, :scope => [:simple_form]))
end

.human_name(options = {}) ⇒ Object

Add a human name interface on top of I18n. If you have a model named SimpleForm, it will search for the localized name on:

simple_form:
  models:
    contact_form: Contact form


114
115
116
117
# File 'lib/simple_form/base.rb', line 114

def self.human_name(options={})
  underscored = self.name.demodulize.underscore
  I18n.translate("models.#{underscored}", options.merge(:default => underscored.humanize, :scope => [:simple_form]))
end

Instance Method Details

#deliver(run_validations = true) ⇒ Object Also known as: save

If is not spam and the form is valid, we send the e-mail and returns true. Otherwise returns false.



86
87
88
89
90
91
92
93
# File 'lib/simple_form/base.rb', line 86

def deliver(run_validations=true)
  if !run_validations || (self.not_spam? && self.valid?)
    SimpleForm::Notifier.deliver_contact(self)
    return true
  else
    return false
  end
end

#errorsObject

Return the errors in this form. The object returned as the same API as the ActiveRecord one.



122
123
124
# File 'lib/simple_form/base.rb', line 122

def errors
  @errors ||= SimpleForm::Errors.new(self)
end

#idObject

Always return nil so when using form_for, the default method will be post.



79
80
81
# File 'lib/simple_form/base.rb', line 79

def id
  nil
end

#invalid?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/simple_form/base.rb', line 67

def invalid?
  !valid?
end

#new_record?Boolean

Always return true so when using form_for, the default method will be post.

Returns:

  • (Boolean)


73
74
75
# File 'lib/simple_form/base.rb', line 73

def new_record?
  true
end

#not_spam?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/simple_form/base.rb', line 37

def not_spam?
  !spam?
end

#spam?Boolean

In development, raises an error if the captcha field is not blank. This is 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, returns false otherwise.

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/simple_form/base.rb', line 23

def spam?
  form_captcha.each do |field|
    next if send(field).blank?

    if RAILS_ENV == 'development'
      raise ScriptError, "The captcha field #{field} was supposed to be blank"
    else
      return true
    end
  end

  return false
end

#valid?Boolean

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.

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/simple_form/base.rb', line 49

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