Skip to content

Commit

Permalink
Remove deprecated stuff and improve AR compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed May 26, 2010
1 parent c8669a9 commit 5e815e1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 43 deletions.
44 changes: 9 additions & 35 deletions lib/mail_form/delivery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,16 @@ module Delivery
class_attribute :mail_appendable
self.mail_appendable = []

before_create :not_spam?
after_create :deliver!

attr_accessor :request
alias :deliver :create

extend Deprecated
end

module Deprecated
def subject(duck=nil, &block)
ActiveSupport::Deprecation.warn "subject is deprecated. Please define a headers method " <<
"in your instance which returns a hash with :subject as key instead.", caller
end

def sender(duck=nil, &block)
ActiveSupport::Deprecation.warn "from/sender is deprecated. Please define a headers method " <<
"in your instance which returns a hash with :from as key instead.", caller
if respond_to?(:before_deliver) && respond_to?(:after_deliver)
before_deliver :not_spam?
after_deliver :deliver!
else # For ActiveRecord compatibility
before_create :not_spam?
after_create :deliver!
alias :deliver :save
end
alias :from :sender

def recipients(duck=nil, &block)
ActiveSupport::Deprecation.warn "to/recipients is deprecated. Please define a headers method " <<
"in your instance which returns a hash with :to as key instead.", caller
end
alias :to :recipients

def headers(hash)
ActiveSupport::Deprecation.warn "headers is deprecated. Please define a headers method " <<
"in your instance which returns the desired headers instead.", caller
end

def template(new_template)
ActiveSupport::Deprecation.warn "template is deprecated. Please define a headers method " <<
"in your instance which returns a hash with :template_name as key instead.", caller
end
attr_accessor :request
end

module ClassMethods
Expand Down Expand Up @@ -169,7 +143,7 @@ def not_spam?
!spam?
end

# Deliver the resource without checking any condition.
# Deliver the resource without running any validation.
def deliver!
MailForm::Notifier.contact(self).deliver
end
Expand Down
8 changes: 4 additions & 4 deletions lib/mail_form/shim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.included(base)
include ActiveModel::Conversion

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

Expand Down Expand Up @@ -53,13 +53,13 @@ def id
end

# Create just check validity, and if so, trigger callbacks.
def create
def deliver
if valid?
_run_create_callbacks { true }
_run_deliver_callbacks { true }
else
false
end
end
alias :save :create
alias :save :deliver
end
end
8 changes: 4 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ContactForm < MailForm::Base

attributes :created_at, :message, :validate => :callback

before_create :set_created_at
before_deliver :set_created_at

def headers
{ :to => 'my.email@my.domain.com' }
Expand Down Expand Up @@ -55,14 +55,14 @@ def headers

class FileForm < ContactForm
attribute :file, :attachment => true, :validate => true
recipients :set_recipient

def set_recipient
if file
def headers
to = if file
"contact_file@my.domain.com"
else
"contact@my.domain.com"
end
{ :to => to }
end
end

Expand Down

0 comments on commit 5e815e1

Please sign in to comment.