Skip to content

Commit

Permalink
Adds more comments & documentation, moves env variables over to travi…
Browse files Browse the repository at this point in the history
…s, bumps to V 0.0.8
  • Loading branch information
beneggett committed Nov 24, 2013
1 parent 3a0e46e commit ee16c4b
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
simple_contact (0.0.7)
simple_contact (0.0.8)
rails (~> 3.2.15)

GEM
Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,21 @@ 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 standard conventions as described in [Rails I18n Guides](http://guides.rubyonrails.org/i18n.html)


## Contributing
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/simple_contact/contact_controller.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 11 additions & 1 deletion app/mailers/simple_contact/contact_mailer.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 9 additions & 3 deletions app/models/simple_contact/message.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
56 changes: 34 additions & 22 deletions app/views/simple_contact/contact/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,53 +1,65 @@
<%# Title of contact form %>
<div class="page-header">
<h1>
<%= t(:contact_me) %>
</h1>
</div>


<div class="row">
<%# Reduces the total width of the form & centers %>
<div class="col-sm-8 col-sm-offset-2">
<%= form_for @message, url: simple_contact.contact_path, html: {class: "form-horizontal" } do |f| %>
<% if @message.errors.any? %>
<script>
$(document).ready(function() {
$('.field_with_errors').parent().addClass('has-error');
});
</script>
<p><%= t(:message_errors_present) %></p>
<ul class='list-unstyled'>
<% @message.errors.full_messages.each do |error| %>
<li><%= error %></li>
<% end %>
</ul>
<% end %>

<div class="form-inputs">
<div class="form-group">
<%= f.label :email, t(:email_address_label).html_safe, class: "col-xs-2 control-label" %>
<div class="col-xs-10">
<%= f.email_field :email, placeholder: t(:email_address_placeholder).html_safe, class: "form-control" %>

<%# Lists any errors in the form %>
<% if @message.errors.any? %>
<div class='well text-center'>
<h3><%= t(:message_errors_present) %></h3>
<ul class='list-unstyled'>
<% @message.errors.full_messages.each do |error| %>
<li><%= error %></li>
<% end %>
</ul>
</div>
</div>
<div class="form-group">
<% end %>
<%# Message fields below %>
<div class="form-group <%= 'has-error' if @message.errors.present? %>">
<%= f.label :name, t(:email_name_label).html_safe, class: "col-xs-2 control-label" %>
<div class="col-xs-10">
<%= f.text_field :name, placeholder: t(:email_name_placeholder).html_safe, class: "form-control" %>
</div>
</div>
<div class="form-group">

<div class="form-group <%= 'has-error' if @message.errors.present? %>">
<%= f.label :email, t(:email_address_label).html_safe, class: "col-xs-2 control-label" %>
<div class="col-xs-10">
<%= f.email_field :email, placeholder: t(:email_address_placeholder).html_safe, class: "form-control" %>
</div>
</div>


<div class="form-group <%= 'has-error' if @message.errors.present? %>">
<%= f.label :subject, t(:email_subject_label).html_safe, class: "col-xs-2 control-label" %>
<div class="col-xs-10">
<%= f.text_field :subject, placeholder: t(:email_subject_placeholder).html_safe, class: "form-control" %>
</div>
</div>
<div class="form-group">

<div class="form-group <%= 'has-error' if @message.errors.present? %>">
<%= f.label :body, t(:email_body_label).html_safe, class: "col-xs-2 control-label" %>
<div class="col-xs-10">
<%= f.text_area :body, placeholder: t(:email_body_placeholder).html_safe, as: :text, rows: 10, class: "form-control" %>
</div>
</div>

</div>

<br />
<div class="text-center">
<button name='commit' class='btn btn-primary btn-lg'><%= t(:submit_message).html_safe %></button>
<button name='commit' class='btn btn-primary btn-lg'><%= t(:send_message).html_safe %></button>
</div>
<% end %>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<%# This message is left very basic by default. It is designed to be customized %>
<h3> You've received a new message from: <%= mail_to @message.email, @message.name %> </h3>

<hr />

<h3> <%=@message.subject %> </h3>

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

<br />
<hr />

<p> Please respond to <%= @message.name %> as soon as possible. </p>
5 changes: 3 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<i class='glyphicon glyphicon-envelope'></i> Send Message"
mailer_error_message: "Please correct the issues below"
send_message: "<i class='glyphicon glyphicon-envelope'></i> Send Message"
invalid_email_message: "must be a valid email address"
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_contact/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SimpleContact
VERSION = "0.0.7"
VERSION = "0.0.8"
end
14 changes: 10 additions & 4 deletions test/mailers/contact_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@ class SimpleContact::ContactMailerTest < ActionMailer::TestCase

before do
@routes = SimpleContact::Engine.routes
# Tests that single email addresses or multiple comma separated email address work as well.
ENV['SIMPLE_CONTACT_TO_EMAIL'] = 'test@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]"

@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
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

0 comments on commit ee16c4b

Please sign in to comment.