Browse files

Bump tiny and release 1.2.1.

  • Loading branch information...
1 parent 89ba919 commit eac538ea2edbaedb5542a7f99fbcf8f4810c2fba @josevalim josevalim committed Apr 3, 2010
View
24 lib/mail_form.rb
@@ -1,27 +1,7 @@
-class MailForm < ActionMailer::Base
+module MailForm
autoload :Base, 'mail_form/base'
autoload :Callbacks, 'mail_form/callbacks'
autoload :Delivery, 'mail_form/delivery'
+ autoload :Notifier, 'mail_form/notifier'
autoload :Shim, 'mail_form/shim'
-
- append_view_path File.expand_path('../views', __FILE__)
-
- def contact(resource)
- if resource.request.nil? && resource.class.mail_appendable.any?
- raise ScriptError, "You set :append values but forgot to give me the request object"
- end
-
- @resource = @form = resource
-
- resource.class.mail_attachments.each do |attribute|
- value = resource.send(attribute)
- next unless value.respond_to?(:read)
- attachments[value.original_filename] = value.read
- end
-
- headers = resource.headers
- headers[:from] ||= resource.email
- headers[:subject] ||= resource.class.human_name
- mail(headers)
- end
end
View
12 lib/mail_form/base.rb
@@ -1,8 +1,10 @@
-class MailForm::Base
- include MailForm::Shim
- include MailForm::Delivery
+module MailForm
+ class Base
+ include MailForm::Shim
+ include MailForm::Delivery
- def self.lookup_ancestors
- super - [MailForm::Base]
+ def self.lookup_ancestors
+ super - [MailForm::Base]
+ end
end
end
View
296 lib/mail_form/delivery.rb
@@ -1,175 +1,177 @@
-module MailForm::Delivery
- extend ActiveSupport::Concern
+module MailForm
+ module Delivery
+ extend ActiveSupport::Concern
- included do
- class_attribute :mail_attributes
- self.mail_attributes = []
+ included do
+ class_attribute :mail_attributes
+ self.mail_attributes = []
- class_attribute :mail_captcha
- self.mail_captcha = []
+ class_attribute :mail_captcha
+ self.mail_captcha = []
- class_attribute :mail_attachments
- self.mail_attachments = []
+ class_attribute :mail_attachments
+ self.mail_attachments = []
- class_attribute :mail_appendable
- self.mail_appendable = []
+ class_attribute :mail_appendable
+ self.mail_appendable = []
- before_create :not_spam?
- after_create :deliver!
+ before_create :not_spam?
+ after_create :deliver!
- attr_accessor :request
- alias :deliver :create
+ 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
+ extend Deprecated
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
- end
- alias :from :sender
+ 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 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 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
+ end
+ alias :from :sender
- def headers(hash)
- ActiveSupport::Deprecation.warn "to/recipients is deprecated. Please define a headers method " <<
- "in your instance which returns the desired headers instead.", caller
- end
+ 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 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
- end
+ 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
- module ClassMethods
- # 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).
- #
- # * <tt>:attachment</tt> - When given, expects a file to be sent and attaches
- # it to the e-mail. Don't forget to set your form to multitype.
- #
- # * <tt>:captcha</tt> - 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-z0-9]+\.)+[a-z]{2,})$/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
- #
- def attribute(*accessors)
- options = accessors.extract_options!
- attr_accessor *(accessors - instance_methods.map(&:to_sym))
-
- if options[:attachment]
- self.mail_attachments += accessors
- elsif options[:captcha]
- self.mail_captcha += accessors
- else
- self.mail_attributes += accessors
+ 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
+ end
- validation = options.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
+ module ClassMethods
+ # 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).
+ #
+ # * <tt>:attachment</tt> - When given, expects a file to be sent and attaches
+ # it to the e-mail. Don't forget to set your form to multitype.
+ #
+ # * <tt>:captcha</tt> - 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-z0-9]+\.)+[a-z]{2,})$/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
+ #
+ def attribute(*accessors)
+ options = accessors.extract_options!
+ attr_accessor *(accessors - instance_methods.map(&:to_sym))
+
+ if options[:attachment]
+ self.mail_attachments += accessors
+ elsif options[:captcha]
+ self.mail_captcha += accessors
+ else
+ self.mail_attributes += accessors
end
- validates_presence_of accessor unless options[:allow_blank] == true
+ validation = options.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 options[:allow_blank] == true
+ end
+ end
+ alias :attributes :attribute
+
+ # 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 (http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html)
+ #
+ # == Examples
+ #
+ # class ContactForm < MailForm
+ # append :remote_ip, :user_agent, :session, :cookies
+ # end
+ #
+ def append(*values)
+ self.mail_appendable += values
end
end
- alias :attributes :attribute
- # 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)
+ # 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.
#
- # You can get the values to be appended from the AbstractRequest
- # documentation (http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html)
+ # In test and in production, it returns true if all captcha fields are blank,
+ # returns false otherwise.
#
- # == Examples
- #
- # class ContactForm < MailForm
- # append :remote_ip, :user_agent, :session, :cookies
- # end
- #
- def append(*values)
- self.mail_appendable += values
- end
- end
-
- # 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 all captcha fields are blank,
- # returns false otherwise.
- #
- def spam?
- self.class.mail_captcha.each do |field|
- next if send(field).blank?
-
- if defined?(Rails) && Rails.env.development?
- raise ScriptError, "The captcha field #{field} was supposed to be blank"
- else
- return true
+ def spam?
+ self.class.mail_captcha.each do |field|
+ next if send(field).blank?
+
+ if defined?(Rails) && Rails.env.development?
+ raise ScriptError, "The captcha field #{field} was supposed to be blank"
+ else
+ return true
+ end
end
- end
- false
- end
+ false
+ end
- def not_spam?
- !spam?
- end
+ def not_spam?
+ !spam?
+ end
- # Deliver the resource without checking any condition.
- def deliver!
- MailForm.contact(self).deliver
+ # Deliver the resource without checking any condition.
+ def deliver!
+ MailForm::Notifier.contact(self).deliver
+ end
end
end
View
25 lib/mail_form/notifier.rb
@@ -0,0 +1,25 @@
+module MailForm
+ class Notifier < ActionMailer::Base
+ self.mailer_name = "mail_form"
+ append_view_path File.expand_path('../views', __FILE__)
+
+ def contact(resource)
+ if resource.request.nil? && resource.class.mail_appendable.any?
+ raise ScriptError, "You set :append values but forgot to give me the request object"
+ end
+
+ @resource = @form = resource
+
+ resource.class.mail_attachments.each do |attribute|
+ value = resource.send(attribute)
+ next unless value.respond_to?(:read)
+ attachments[value.original_filename] = value.read
+ end
+
+ headers = resource.headers
+ headers[:from] ||= resource.email
+ headers[:subject] ||= resource.class.model_name.human
+ mail(headers)
+ end
+ end
+end
View
90 lib/mail_form/shim.rb
@@ -1,59 +1,61 @@
require 'active_model'
# This the module which makes any class behave like ActiveModel.
-module MailForm::Shim
- 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 :create
+module MailForm
+ module Shim
+ 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 :create
+ end
end
- end
- module ClassMethods
- def i18n_scope
- :mail_form
+ module ClassMethods
+ def i18n_scope
+ :mail_form
+ end
end
- end
- # Initialize assigning the parameters given as hash.
- def initialize(params={})
- params.each_pair do |attr, value|
- self.send(:"#{attr}=", value)
- end unless params.blank?
- end
+ # Initialize assigning the parameters given as hash.
+ def initialize(params={})
+ params.each_pair do |attr, value|
+ self.send(:"#{attr}=", value)
+ end unless params.blank?
+ end
- # Returns a hash of attributes, according to the attributes existent in
- # self.class.mail_attributes.
- def attributes
- self.class.mail_attributes.inject({}) do |hash, attr|
- hash[attr.to_s] = send(attr)
- hash
+ # Returns a hash of attributes, according to the attributes existent in
+ # self.class.mail_attributes.
+ def attributes
+ self.class.mail_attributes.inject({}) do |hash, attr|
+ hash[attr.to_s] = send(attr)
+ hash
+ end
end
- end
- # Always return true so when using form_for, the default method will be post.
- def new_record?
- true
- end
+ # Always return true so when using form_for, the default method will be post.
+ def new_record?
+ true
+ end
- # Always return nil so when using form_for, the default method will be post.
- def id
- nil
- end
+ # Always return nil so when using form_for, the default method will be post.
+ def id
+ nil
+ end
- # Create just check validity, and if so, trigger callbacks.
- def create
- if valid?
- _run_create_callbacks { true }
- else
- false
+ # Create just check validity, and if so, trigger callbacks.
+ def create
+ if valid?
+ _run_create_callbacks { true }
+ else
+ false
+ end
end
+ alias :save :create
end
- alias :save :create
end
View
2 lib/mail_form/version.rb
@@ -1,3 +1,3 @@
module MailForm
- VERSION = "1.2.0".freeze
+ VERSION = "1.2.1".freeze
end
View
0 lib/views/mail_form/contact.erb → lib/mail_form/views/mail_form/contact.erb
File renamed without changes.
View
9 mail_form.gemspec
@@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = %q{mail_form}
- s.version = "1.2.0"
+ s.version = "1.2.1"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Jos\303\251 Valim", "Carlos Ant\303\264nio"]
- s.date = %q{2010-03-03}
+ s.date = %q{2010-04-03}
s.description = %q{Send e-mail straight from forms in Rails with I18n, validations, attachments and request information.}
s.email = %q{contact@plataformatec.com.br}
s.extra_rdoc_files = [
@@ -23,14 +23,15 @@ Gem::Specification.new do |s|
"lib/mail_form.rb",
"lib/mail_form/base.rb",
"lib/mail_form/delivery.rb",
+ "lib/mail_form/notifier.rb",
"lib/mail_form/shim.rb",
"lib/mail_form/version.rb",
- "lib/views/mail_form/contact.erb"
+ "lib/mail_form/views/mail_form/contact.erb"
]
s.homepage = %q{http://github.com/plataformatec/mail_form}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
- s.rubygems_version = %q{1.3.5}
+ s.rubygems_version = %q{1.3.6}
s.summary = %q{Send e-mail straight from forms in Rails with I18n, validations, attachments and request information.}
s.test_files = [
"test/mail_form_test.rb",

0 comments on commit eac538e

Please sign in to comment.