diff --git a/.travis.yml b/.travis.yml index 46fbb8f..336d1f8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,8 @@ language: ruby rvm: - 2.0.0 +env: + - SIMPLE_CONTACT_TO_EMAIL="to_me@example.com" SIMPLE_CONTACT_CC_EMAIL= "cc_me@example.com, cc_me2@example.com" SIMPLE_CONTACT_BCC_EMAIL="bcc_me@example.com, bcc_me2@example.com" SIMPLE_CONTACT_SUBJECT_PREFIX="[Simple Contact]" script: - bundle exec rake addons: diff --git a/README.md b/README.md index d372ac7..a86eb0a 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,23 @@ Simply mount it within your application's routes file where you want your contac mount SimpleContact::Engine => "/contact-me" ``` -Make sure you have an actionmailer method setup in your environment. +Make sure you have an Action Mailer method setup in your environment. + +Simple Contact uses Environment variables to setup your email's to, cc, bcc, and subject prefix lines +You can set them by simply setting environmant variables within your own app: + +``` + ENV['SIMPLE_CONTACT_TO_EMAIL'] = 'to_me@example.com' + ENV['SIMPLE_CONTACT_CC_EMAIL'] = 'cc_me@example.com, cc_me2@example.com' + ENV['SIMPLE_CONTACT_BCC_EMAIL'] = 'bcc_me@example.com, bcc_me2@example.com' + ENV['SIMPLE_CONTACT_SUBJECT_PREFIX'] = "[Simple Contact]" +``` + +## Customization + +Simple Contact messaging is entirely translated. The messages can all be overriden by translation files + + ## Contributing diff --git a/app/controllers/simple_contact/contact_controller.rb b/app/controllers/simple_contact/contact_controller.rb index afafe99..ef22b60 100644 --- a/app/controllers/simple_contact/contact_controller.rb +++ b/app/controllers/simple_contact/contact_controller.rb @@ -1,18 +1,21 @@ require_dependency "simple_contact/application_controller" class SimpleContact::ContactController < SimpleContact::ApplicationController + def new @message = SimpleContact::Message.new end def create @message = SimpleContact::Message.new(params[:message]) + if @message.valid? SimpleContact::ContactMailer.contact_message(@message).deliver redirect_to main_app.root_path, notice: I18n.translate(:mailer_success_message) else - flash.now.alert = "#{I18n.translate(:mailer_error_message)}" + flash.now.alert = I18n.translate(:mailer_error_message) render :new end end + end diff --git a/app/mailers/simple_contact/contact_mailer.rb b/app/mailers/simple_contact/contact_mailer.rb index 0c71f68..2d692db 100644 --- a/app/mailers/simple_contact/contact_mailer.rb +++ b/app/mailers/simple_contact/contact_mailer.rb @@ -1,6 +1,16 @@ class SimpleContact::ContactMailer < ActionMailer::Base + + # The to, cc, and bcc addresses can be customized in your own application by creating the appropriate environment variables ENV['SIMPLE_CONTACT_TO_EMAIL'], ENV['SIMPLE_CONTACT_CC_EMAIL'], ENV['SIMPLE_CONTACT_BCC_EMAIL'] respectively + # An optional subject prefix can be customized in your own application by creating the environment variable ENV['SIMPLE_CONTACT_SUBJECT_PREFIX']. e.g. if ENV['SIMPLE_CONTACT_SUBJECT_PREFIX'] = '[Our Cool Site]' the subject would be rendered as "[Our Cool Site] {subject of the message}" def contact_message(message) @message = message - mail(to: ENV['SIMPLE_CONTACT_TO_EMAIL'], from: @message.email, subject: "#{ENV['SIMPLE_CONTACT_SUBJECT_PREFIX']} #{@message.subject}" ) + mail( + to: ENV['SIMPLE_CONTACT_TO_EMAIL'], + cc: ENV['SIMPLE_CONTACT_CC_EMAIL'], + bcc: ENV['SIMPLE_CONTACT_BCC_EMAIL'], + from: @message.email, + subject: [ENV['SIMPLE_CONTACT_SUBJECT_PREFIX'], @message.subject].join(' ') + ) end + end diff --git a/app/models/simple_contact/message.rb b/app/models/simple_contact/message.rb index 6a314ba..b6e57db 100644 --- a/app/models/simple_contact/message.rb +++ b/app/models/simple_contact/message.rb @@ -1,14 +1,20 @@ -class SimpleContact::Message - +class SimpleContact::Message + # Simple Contact is a non-active record model, however we still use validations from active model as such. include ActiveModel::Validations include ActiveModel::Conversion extend ActiveModel::Naming + # Set's up the fields for our message attr_accessor :body, :email, :name, :subject + # Validates presence of all fields by default validates :body, :subject, :name, :email, presence: true - validates :email, format: { with: /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/ } + # Validates email format & displays a customizable error message from the translation file + validates :email, format: { with: /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/, message: I18n.translate(:invalid_email_message) } + + + # Setup code to make it work like an AR model would def initialize(attributes = {}) attributes.each do |name, value| send("#{name}=", value) diff --git a/app/views/simple_contact/contact/new.html.erb b/app/views/simple_contact/contact/new.html.erb index 206e504..1e91d33 100644 --- a/app/views/simple_contact/contact/new.html.erb +++ b/app/views/simple_contact/contact/new.html.erb @@ -1,53 +1,65 @@ +<%# Title of contact form %> + +
+ <%# Reduces the total width of the form & centers %>
<%= form_for @message, url: simple_contact.contact_path, html: {class: "form-horizontal" } do |f| %> - <% if @message.errors.any? %> - -

<%= t(:message_errors_present) %>

- - <% end %> +
-
- <%= f.label :email, t(:email_address_label).html_safe, class: "col-xs-2 control-label" %> -
- <%= f.email_field :email, placeholder: t(:email_address_placeholder).html_safe, class: "form-control" %> + + <%# Lists any errors in the form %> + <% if @message.errors.any? %> +
+

<%= t(:message_errors_present) %>

+
    + <% @message.errors.full_messages.each do |error| %> +
  • <%= error %>
  • + <% end %> +
-
-
+ <% end %> + + +
<%= f.label :name, t(:email_name_label).html_safe, class: "col-xs-2 control-label" %>
<%= f.text_field :name, placeholder: t(:email_name_placeholder).html_safe, class: "form-control" %>
-
+ +
+ <%= f.label :email, t(:email_address_label).html_safe, class: "col-xs-2 control-label" %> +
+ <%= f.email_field :email, placeholder: t(:email_address_placeholder).html_safe, class: "form-control" %> +
+
+ + +
<%= f.label :subject, t(:email_subject_label).html_safe, class: "col-xs-2 control-label" %>
<%= f.text_field :subject, placeholder: t(:email_subject_placeholder).html_safe, class: "form-control" %>
-
+ +
<%= f.label :body, t(:email_body_label).html_safe, class: "col-xs-2 control-label" %>
<%= f.text_area :body, placeholder: t(:email_body_placeholder).html_safe, as: :text, rows: 10, class: "form-control" %>
+
+
- +
<% end %>
diff --git a/app/views/simple_contact/contact_mailer/contact_message.html.erb b/app/views/simple_contact/contact_mailer/contact_message.html.erb index e579cda..1dc98ee 100644 --- a/app/views/simple_contact/contact_mailer/contact_message.html.erb +++ b/app/views/simple_contact/contact_mailer/contact_message.html.erb @@ -1,7 +1,13 @@ +<%# This message is left very basic by default. It is designed to be customized %>

You've received a new message from: <%= mail_to @message.email, @message.name %>

+
+

<%=@message.subject %>

+ <%= simple_format(@message.body) %> +

+

Please respond to <%= @message.name %> as soon as possible.

\ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 677b524..31a3273 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -10,5 +10,6 @@ en: email_body_placeholder: "Please write your message..." message_errors_present: "Please correct the following errors:" mailer_success_message: "Thanks for contacting us. We'll be in touch soon!" - mailer_error_message: "Please correct the errors below" - submit_message: " Send Message" \ No newline at end of file + mailer_error_message: "Please correct the issues below" + send_message: " Send Message" + invalid_email_message: "must be a valid email address" \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index bfcffc9..1b9e398 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ SimpleContact::Engine.routes.draw do - + # Routes are simple. All routes will point to the root_url, which will be where you mount the engine. get "/", to: 'contact#new', as: :contact post "/", to: 'contact#create', as: :contact root to: 'contact#new' diff --git a/test/mailers/contact_mailer_test.rb b/test/mailers/contact_mailer_test.rb index 18e807f..b0b7614 100644 --- a/test/mailers/contact_mailer_test.rb +++ b/test/mailers/contact_mailer_test.rb @@ -4,22 +4,23 @@ class SimpleContact::ContactMailerTest < ActionMailer::TestCase before do @routes = SimpleContact::Engine.routes - ENV['SIMPLE_CONTACT_TO_EMAIL'] = 'test@example.com' - ENV['SIMPLE_CONTACT_SUBJECT_PREFIX'] = "[Simple Contact]" @message = build(:message) @email = SimpleContact::ContactMailer.contact_message(@message).deliver end - it "is delivered to simple_contact_to_email" do - assert_equal @email.to.pop, ENV['SIMPLE_CONTACT_TO_EMAIL'] + it "is delivered to the appropriate addresses" do + # tests it works for single or multiple addresses in the fields + assert_equal @email.to.join(', '), ENV['SIMPLE_CONTACT_TO_EMAIL'] + assert_equal @email.cc.join(', '), ENV['SIMPLE_CONTACT_CC_EMAIL'] + assert_equal @email.bcc.join(', '), ENV['SIMPLE_CONTACT_BCC_EMAIL'] end it "is delivered from the senders email" do - assert_equal @email.from.pop, @message.email + assert_equal @email.from.first, @message.email end it "is delivered with an appropriate subject" do - assert_equal @email.subject, "#{ENV['SIMPLE_CONTACT_SUBJECT_PREFIX']} #{@message.subject}" + assert_equal @email.subject, [ENV['SIMPLE_CONTACT_SUBJECT_PREFIX'], @message.subject].join(' ') end end \ No newline at end of file