Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Testing not-great looking front-end

  • Loading branch information...
commit f58d6df8cad94712bbe4e8168bfb6d4f188d283c 1 parent 705739d
@nahurst authored
View
10 app/controllers/mail_merge_controller.rb
@@ -1,10 +0,0 @@
-class MailMergeController < ApplicationController
- def show
- end
-
- def new
- end
-
- def create
- end
-end
View
83 app/controllers/mail_merges_controller.rb
@@ -0,0 +1,83 @@
+class MailMergesController < ApplicationController
+ # GET /mail_merges
+ # GET /mail_merges.json
+ def index
+ @mail_merges = MailMerge.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @mail_merges }
+ end
+ end
+
+ # GET /mail_merges/1
+ # GET /mail_merges/1.json
+ def show
+ @mail_merge = MailMerge.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render json: @mail_merge }
+ end
+ end
+
+ # GET /mail_merges/new
+ # GET /mail_merges/new.json
+ def new
+ @mail_merge = MailMerge.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @mail_merge }
+ end
+ end
+
+ # GET /mail_merges/1/edit
+ def edit
+ @mail_merge = MailMerge.find(params[:id])
+ end
+
+ # POST /mail_merges
+ # POST /mail_merges.json
+ def create
+ @mail_merge = MailMerge.new(params[:mail_merge])
+
+ respond_to do |format|
+ if @mail_merge.save
+ format.html { redirect_to @mail_merge, notice: 'Mail merge was successfully created.' }
+ format.json { render json: @mail_merge, status: :created, location: @mail_merge }
+ else
+ format.html { render action: "new" }
+ format.json { render json: @mail_merge.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /mail_merges/1
+ # PUT /mail_merges/1.json
+ def update
+ @mail_merge = MailMerge.find(params[:id])
+
+ respond_to do |format|
+ if @mail_merge.update_attributes(params[:mail_merge])
+ format.html { redirect_to @mail_merge, notice: 'Mail merge was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @mail_merge.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /mail_merges/1
+ # DELETE /mail_merges/1.json
+ def destroy
+ @mail_merge = MailMerge.find(params[:id])
+ @mail_merge.destroy
+
+ respond_to do |format|
+ format.html { redirect_to mail_merges_url }
+ format.json { head :no_content }
+ end
+ end
+end
View
2  app/helpers/application_helper.rb
@@ -1,2 +0,0 @@
-module ApplicationHelper
-end
View
9 app/models/mail_merge.rb
@@ -1,19 +1,20 @@
require 'mustache'
class MailMerge < ActiveRecord::Base
- attr_accessible :body_template, :data, :subject_template
+ attr_accessible :body_template, :data, :subject_template, :from
has_many :emails
- def generate_emails(from)
+ after_save :generate_emails
+
+ def generate_emails
ActiveSupport::JSON.decode(data).each do |context|
- email = self.emails.build(
+ email = self.emails.create(
:subject => Mustache.render(subject_template, context),
:body => Mustache.render(body_template, context),
:to => context['email'],
:from => from)
BulkMailer.basic(email).deliver
- email.save!
end
self.emails
end
View
29 app/views/mail_merges/_form.html.erb
@@ -0,0 +1,29 @@
+<%= form_for(@mail_merge) do |f| %>
+ <% if @mail_merge.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@mail_merge.errors.count, "error") %> prohibited this mail_merge from being saved:</h2>
+
+ <ul>
+ <% @mail_merge.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.label :data %><br />
+ <%= f.text_area :data %>
+ </div>
+ <div class="field">
+ <%= f.label :subject_template %><br />
+ <%= f.text_field :subject_template %>
+ </div>
+ <div class="field">
+ <%= f.label :body_template %><br />
+ <%= f.text_field :body_template %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
View
27 app/views/mail_merges/index.html.erb
@@ -0,0 +1,27 @@
+<h1>Listing mail_merges</h1>
+
+<table>
+ <tr>
+ <th>Data</th>
+ <th>Subject template</th>
+ <th>Body template</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @mail_merges.each do |mail_merge| %>
+ <tr>
+ <td><%= mail_merge.data %></td>
+ <td><%= mail_merge.subject_template %></td>
+ <td><%= mail_merge.body_template %></td>
+ <td><%= link_to 'Show', mail_merge %></td>
+ <td><%= link_to 'Edit', edit_mail_merge_path(mail_merge) %></td>
+ <td><%= link_to 'Destroy', mail_merge, method: :delete, data: { confirm: 'Are you sure?' } %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Mail merge', new_mail_merge_path %>
View
5 app/views/mail_merges/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New mail_merge</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', mail_merges_path %>
View
20 app/views/mail_merges/show.html.erb
@@ -0,0 +1,20 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <b>Data:</b>
+ <%= @mail_merge.data %>
+</p>
+
+<p>
+ <b>Subject template:</b>
+ <%= @mail_merge.subject_template %>
+</p>
+
+<p>
+ <b>Body template:</b>
+ <%= @mail_merge.body_template %>
+</p>
+
+
+<%= link_to 'Edit', edit_mail_merge_path(@mail_merge) %> |
+<%= link_to 'Back', mail_merges_path %>
View
4 config/routes.rb
@@ -1,4 +1,4 @@
HappyTo::Application.routes.draw do
- root :to => "mail_merge#new"
- resources :mail_merge
+ root :to => "mail_merges#new"
+ resources :mail_merges
end
View
5 db/migrate/20130318001548_add_from_to_mail_merge.rb
@@ -0,0 +1,5 @@
+class AddFromToMailMerge < ActiveRecord::Migration
+ def change
+ add_column :mail_merges, :from, :string
+ end
+end
View
3  db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20130317233716) do
+ActiveRecord::Schema.define(:version => 20130318001548) do
create_table "emails", :force => true do |t|
t.string "to"
@@ -31,6 +31,7 @@
t.text "body_template"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
+ t.string "from"
end
end
View
104 spec/controllers/mail_merges_controller_spec.rb
@@ -0,0 +1,104 @@
+require 'spec_helper'
+
+# This spec was generated by rspec-rails when you ran the scaffold generator.
+# It demonstrates how one might use RSpec to specify the controller code that
+# was generated by Rails when you ran the scaffold generator.
+#
+# It assumes that the implementation code is generated by the rails scaffold
+# generator. If you are using any extension libraries to generate different
+# controller code, this generated spec may or may not pass.
+#
+# It only uses APIs available in rails and/or rspec-rails. There are a number
+# of tools you can use to make these specs even more expressive, but we're
+# sticking to rails and rspec-rails APIs to keep things simple and stable.
+#
+# Compared to earlier versions of this generator, there is very limited use of
+# stubs and message expectations in this spec. Stubs are only used when there
+# is no simpler way to get a handle on the object needed for the example.
+# Message expectations are only used when there is no simpler way to specify
+# that an instance is receiving a specific message.
+
+describe MailMergesController do
+
+ # This should return the minimal set of attributes required to create a valid
+ # MailMerge. As you add validations to MailMerge, be sure to
+ # update the return value of this method accordingly.
+ def valid_attributes
+ tmp_mm = build(:mail_merge)
+ {
+ 'data' => tmp_mm.data,
+ 'body_template' => tmp_mm.body_template,
+ 'subject_template' => tmp_mm.subject_template
+ }
+ end
+
+ # This should return the minimal set of values that should be in the session
+ # in order to pass any filters (e.g. authentication) defined in
+ # MailMergesController. Be sure to keep this updated too.
+ def valid_session
+ {}
+ end
+
+ describe "GET index" do
+ it "assigns all mail_merges as @mail_merges" do
+ mail_merge = MailMerge.create! valid_attributes
+ get :index, {}, valid_session
+ assigns(:mail_merges).should eq([mail_merge])
+ end
+ end
+
+ describe "GET show" do
+ it "assigns the requested mail_merge as @mail_merge" do
+ mail_merge = MailMerge.create! valid_attributes
+ get :show, {:id => mail_merge.to_param}, valid_session
+ assigns(:mail_merge).should eq(mail_merge)
+ end
+ end
+
+ describe "GET new" do
+ it "assigns a new mail_merge as @mail_merge" do
+ get :new, {}, valid_session
+ assigns(:mail_merge).should be_a_new(MailMerge)
+ end
+ end
+
+ describe "POST create" do
+ describe "with valid params" do
+ it "creates a new MailMerge" do
+ expect {
+ post :create, {:mail_merge => valid_attributes}, valid_session
+ }.to change(MailMerge, :count).by(1)
+ end
+
+ it "assigns a newly created mail_merge as @mail_merge" do
+ post :create, {:mail_merge => valid_attributes}, valid_session
+ assigns(:mail_merge).should be_a(MailMerge)
+ assigns(:mail_merge).should be_persisted
+ assigns(:mail_merge).emails.count.should == 4
+ ActionMailer::Base.deliveries.count.should == 4
+ end
+
+ it "redirects to the created mail_merge" do
+ post :create, {:mail_merge => valid_attributes}, valid_session
+ response.should redirect_to(MailMerge.last)
+ end
+ end
+
+ describe "with invalid params" do
+ it "assigns a newly created but unsaved mail_merge as @mail_merge" do
+ # Trigger the behavior that occurs when invalid params are submitted
+ MailMerge.any_instance.stub(:save).and_return(false)
+ post :create, {:mail_merge => { "data" => "invalid value" }}, valid_session
+ assigns(:mail_merge).should be_a_new(MailMerge)
+ end
+
+ it "re-renders the 'new' template" do
+ # Trigger the behavior that occurs when invalid params are submitted
+ MailMerge.any_instance.stub(:save).and_return(false)
+ post :create, {:mail_merge => { "data" => "invalid value" }}, valid_session
+ response.should render_template("new")
+ end
+ end
+ end
+
+end
View
1  spec/models/mail_merge_spec.rb
@@ -5,7 +5,6 @@
it 'should generate emails for a complete mail merge' do
mm = create(:mail_merge)
- mm.generate_emails("splinter@example.com")
mm.emails.length.should == 4
email = mm.emails.first
Please sign in to comment.
Something went wrong with that request. Please try again.