Module: MailForm::Shim

Included in:
Base
Defined in:
lib/mail_form/shim.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/mail_form/shim.rb', line 6

def self.included(base)
  base.class_eval do
    extend ActiveModel::Naming
    extend ActiveModel::Translation
    extend ActiveModel::Callbacks
    include ActiveModel::Validations
    include ActiveModel::Conversion

    extend MailForm::Shim::ClassMethods
    define_model_callbacks :deliver
  end
end

Instance Method Details

#attributesObject

Returns a hash of attributes, according to the attributes existent in self.class.mail_attributes.



34
35
36
37
38
39
# File 'lib/mail_form/shim.rb', line 34

def attributes
  self.class.mail_attributes.inject({}) do |hash, attr|
    hash[attr.to_s] = send(attr)
    hash
  end
end

#deliverObject Also known as: save

Create just check validity, and if so, trigger callbacks.



56
57
58
59
60
61
62
# File 'lib/mail_form/shim.rb', line 56

def deliver
  if valid?
    _run_deliver_callbacks { true }
  else
    false
  end
end

#idObject

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



51
52
53
# File 'lib/mail_form/shim.rb', line 51

def id
  nil
end

#initialize(params = {}) ⇒ Object

Initialize assigning the parameters given as hash.



26
27
28
29
30
# File 'lib/mail_form/shim.rb', line 26

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

#new_record?Boolean

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

Returns:

  • (Boolean)


42
43
44
# File 'lib/mail_form/shim.rb', line 42

def new_record?
  true
end

#persisted?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/mail_form/shim.rb', line 46

def persisted?
  false
end