Permalink
Browse files

Added email template management, removed fixtures and helper files

  • Loading branch information...
mikel committed May 19, 2009
1 parent 557ee07 commit 69850c4317651f0e5f33ba2ece92333afe23a1e2
Showing with 264 additions and 104 deletions.
  1. +2 −2 app/controllers/application_controller.rb
  2. +42 −0 app/controllers/email_templates_controller.rb
  3. +0 −2 app/helpers/home_helper.rb
  4. +0 −2 app/helpers/user_sessions_helper.rb
  5. +0 −2 app/helpers/users_helper.rb
  6. +8 −0 app/models/email_template.rb
  7. +2 −1 app/models/user.rb
  8. +3 −0 app/views/email_templates/_email_template.html.erb
  9. +5 −0 app/views/email_templates/_form.html.erb
  10. +6 −0 app/views/email_templates/edit.html.erb
  11. +4 −0 app/views/email_templates/index.html.erb
  12. +6 −0 app/views/email_templates/new.html.erb
  13. +13 −2 app/views/home/index.html.erb
  14. +4 −3 config/routes.rb
  15. +14 −0 db/migrate/20090518111443_create_email_templates.rb
  16. +9 −1 db/schema.rb
  17. +0 −4 features/home_page.feature
  18. +69 −0 features/manage_email_templates.feature
  19. +2 −2 features/manage_users.feature
  20. +4 −0 features/step_definitions/global_steps.rb
  21. +6 −0 features/step_definitions/manage_email_templates_steps.rb
  22. +15 −0 features/support/paths.rb
  23. +1 −1 lib/tasks/bootstrap.rake
  24. +10 −0 spec/controllers/email_templates_controller_spec.rb
  25. +0 −5 spec/controllers/home_controller_spec.rb
  26. +0 −5 spec/controllers/user_sessions_controller_spec.rb
  27. +0 −11 spec/controllers/users_controller_spec.rb
  28. +4 −0 spec/factories/email_template_factory.rb
  29. +0 −9 spec/fixtures/memberships.yml
  30. +0 −7 spec/fixtures/roles.yml
  31. +0 −7 spec/fixtures/users.yml
  32. +0 −11 spec/helpers/home_helper_spec.rb
  33. +0 −11 spec/helpers/user_sessions_helper_spec.rb
  34. +0 −11 spec/helpers/users_helper_spec.rb
  35. +27 −0 spec/models/email_template_spec.rb
  36. +0 −5 spec/models/membership_spec.rb
  37. +8 −0 spec/models/user_spec.rb
@@ -32,7 +32,7 @@ def require_user
unless current_user
store_location
flash[:notice] = "You must be logged in to access this page"
- redirect_to new_user_session_url
+ redirect_to login_url
return false
end
end
@@ -41,7 +41,7 @@ def require_no_user
if current_user
store_location
flash[:notice] = "You must be logged out to access this page"
- redirect_to account_url
+ redirect_to logout_url
return false
end
end
@@ -0,0 +1,42 @@
+class EmailTemplatesController < ApplicationController
+
+ before_filter :require_user
+
+ def index
+ end
+
+ def new
+ @email_template = EmailTemplate.new(:user => current_user)
+ end
+
+ def edit
+ @email_template = current_user.email_templates.find(params[:id])
+ end
+
+ def update
+ @email_template = current_user.email_templates.find(params[:id])
+ @email_template.attributes = params[:email_template]
+ if @email_template.save
+ redirect_to email_templates_path
+ else
+ render :action => :edit
+ end
+ end
+
+ def create
+ @email_template = EmailTemplate.new(params[:email_template])
+ @email_template.user = current_user
+ if @email_template.save
+ redirect_to email_templates_path
+ else
+ render :action => :new
+ end
+ end
+
+ def destroy
+ @email_template = current_user.email_templates.find(params[:id])
+ @email_template.destroy
+ redirect_to email_templates_path
+ end
+
+end
@@ -1,2 +0,0 @@
-module HomeHelper
-end
@@ -1,2 +0,0 @@
-module UserSessionsHelper
-end
@@ -1,2 +0,0 @@
-module UsersHelper
-end
@@ -0,0 +1,8 @@
+class EmailTemplate < ActiveRecord::Base
+
+ belongs_to :user
+
+ validates_presence_of :title
+ validates_presence_of :body
+
+end
View
@@ -1,6 +1,7 @@
class User < ActiveRecord::Base
acts_as_authentic
-
+
+ has_many :email_templates
has_many :memberships
has_many :roles, :through => :memberships
@@ -0,0 +1,3 @@
+<%= email_template.title %><br />
+<%= link_to "Edit", edit_email_template_path(email_template) %>
+<%= link_to "Delete", email_template_path(email_template), :method => 'delete' %>
@@ -0,0 +1,5 @@
+<%= form.label :title %><br />
+<%= form.text_field :title %><br />
+<br />
+<%= form.label :body %><br />
+<%= form.text_area :body %><br />
@@ -0,0 +1,6 @@
+<h2>Edit Email Template</h2>
+<% form_for @email_template do |f| %>
+ <%= f.error_messages %>
+ <%= render :partial => "form", :object => f %>
+ <%= f.submit "Update" %>
+<% end %>
@@ -0,0 +1,4 @@
+<h2>Current Email Templates</h2>
+<%- current_user.email_templates.each do |email_template| -%>
+ <%= render :partial => email_template %>
+<%- end -%>
@@ -0,0 +1,6 @@
+<h2>Create an Email Template</h2>
+<% form_for @email_template do |f| %>
+ <%= f.error_messages %>
+ <%= render :partial => "form", :object => f %>
+ <%= f.submit "Create" %>
+<% end %>
@@ -2,8 +2,19 @@
<h1>Welcome to Mailer</h1>
+<h2>Email Templates</h2>
+
+<%- if current_user.email_templates.empty? -%>
+ No email templates defined<br />
+ <%= link_to "Make a new email template", new_email_template_path %>
+<%- else -%>
+ <%- current_user.email_templates.each do |email_template| -%>
+ <%= render :partial => "email_template" %>
+ <%- end -%>
+<%- end -%>
+
<%- if current_user.admin -%>
- <%= link_to "Edit Users", users_path %>
+ <%= link_to "Edit Users", users_path %><br />
<%- else -%>
- <%= link_to "Edit Profile", edit_user_path(current_user) %>
+ <%= link_to "Edit Profile", edit_user_path(current_user) %><br />
<%- end -%>
View
@@ -36,9 +36,10 @@
# Note: These default routes make all actions in every controller accessible via GET requests. You should
# consider removing the them or commenting them out if you're using named routes and resources.
- map.resources :users
- map.resources :user_sessions
-
map.root :controller => 'home', :action => "index"
+ map.login '/login', :controller => 'user_sessions', :action => 'new', :method => 'get'
+ map.logout '/logout', :controller => 'user_sessions', :action => 'destroy', :method => 'delete'
+
+ map.resources :users, :user_sessions, :email_templates
end
@@ -0,0 +1,14 @@
+class CreateEmailTemplates < ActiveRecord::Migration
+ def self.up
+ create_table :email_templates do |t|
+ t.string :title
+ t.text :body
+ t.integer :user_id
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :email_templates
+ end
+end
View
@@ -9,7 +9,15 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20090510015902) do
+ActiveRecord::Schema.define(:version => 20090518111443) do
+
+ create_table "email_templates", :force => true do |t|
+ t.string "title"
+ t.text "body"
+ t.integer "user_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
create_table "memberships", :force => true do |t|
t.integer "user_id"
@@ -16,7 +16,3 @@ Feature: Home page
Then I should be on the homepage
And I should see "Welcome to Mailer"
And I should see "Edit Users"
-
-
-
-
@@ -0,0 +1,69 @@
+Feature: Manage Email Templates
+ In order to be able to communicate with our publics
+ As a user
+ I want to be able to create email templates to be mailed out
+
+ Scenario: Not being logged in and trying to make a template
+ Given I am logged out
+ When I go to the email templates page
+ Then I should be on the login page
+
+ Scenario: No templates in the system
+ Given I am logged in
+ When I go to the homepage
+ Then I should see "No email templates defined"
+ And I should see "Make a new email template"
+
+ Scenario: Making a new email template
+ Given I am logged in
+ When I go to the homepage
+ And I follow "Make a new email template"
+ Then I should be on the new email templates page
+
+ Scenario: Saving a new email template
+ Given I am logged in
+ When I go to the new email templates page
+ And I fill in "title" with "First email"
+ And I fill in "body" with "From mikel@me.com"
+ And I press "Create"
+ Then I should be on the email templates page
+ And I should see "First email"
+
+ Scenario: Attempting to save an invalid template
+ Given I am logged in
+ When I go to the new email templates page
+ And I fill in "body" with "From mikel@me.com"
+ And I press "Create"
+ Then I should see "prohibited this email template from being saved"
+
+ Scenario: Showing the existing templates for this user
+ Given I am logged in
+ And there is an email template I made in the system with title "Hello There"
+ When I go to the email templates page
+ Then I should see "Hello There"
+ And I should see "Edit"
+ And I should see "Delete"
+
+ Scenario: Selecting a template to edit
+ Given I am logged in
+ And there is an email template I made in the system with title "Hello There"
+ When I go to the email templates page
+ And I follow "Edit"
+ Then I should see "Edit Email Template"
+
+ Scenario: Updating a template
+ Given I am logged in
+ And there is an email template I made in the system with title "Hello There"
+ When I go to edit the first email template
+ And I fill in "title" with "Goodbye for now"
+ And I press "Update"
+ Then I should be on the email templates page
+ And I should see "Goodbye for now"
+
+ Scenario: Deleting an email template
+ Given I am logged in
+ And there is an email template I made in the system with title "Hello There"
+ When I go to the email templates page
+ And I follow "Delete"
+ Then I should be on the email templates page
+ And I should not see "Hello There"
@@ -8,12 +8,12 @@ Feature: Managing users
When I go to the users page
Then I should be on the homepage
And I should see "You must be an administrator to access this page"
-
+
Scenario: Only the admin user defined
Given I am logged in as an admin
When I go to the users page
Then I should see "bsmith"
-
+
Scenario: One user plus the admin user in the system
Given I am logged in as an admin
And a user with username "Charles" and password "PassWord"
@@ -6,6 +6,10 @@
click_button("Login")
end
+Given /^I am logged out$/ do
+ visit path_to("the logout page")
+end
+
Given /^I am logged in as an admin$/ do
user = login_user(:make_administrator)
visit path_to("the new user sessions page")
@@ -0,0 +1,6 @@
+Given /^there is an email template I made in the system with title "([^\"]*)"$/ do |title|
+ user = User.find(:first)
+ template = Factory(:email_template, :title => title)
+ template.user = user
+ template.save!
+end
View
@@ -29,7 +29,22 @@ def path_to(page_name)
when /the edit user page for "(\w+)"/
user = User.find_by_login($1)
edit_user_path(user.id)
+
+ when /the logout page/
+ logout_path
+
+ when /the login page/
+ login_path
+ when /the email templates page/
+ email_templates_path
+
+ when /the new email templates page/
+ new_email_template_path
+
+ when /edit the first email template/
+ edit_email_template_path(EmailTemplate.find(:first))
+
# Add more page name => path mappings here
else
View
@@ -9,6 +9,6 @@ task :bootstrap => :environment do
user.login = 'admin'
Role.create!(:name => 'admin')
Membership.create!(:user_id => 1, :role_id => 1)
+ user.admin = true
user.save!
- user.add_role!('admin')
end
@@ -0,0 +1,10 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe EmailTemplatesController do
+
+ #Delete this example and add some real ones
+ it "should use EmailTemplatesController" do
+ controller.should be_an_instance_of(EmailTemplatesController)
+ end
+
+end
@@ -2,9 +2,4 @@
describe HomeController do
- #Delete this example and add some real ones
- it "should use HomeController" do
- controller.should be_an_instance_of(HomeController)
- end
-
end
@@ -2,9 +2,4 @@
describe UserSessionsController do
- #Delete this example and add some real ones
- it "should use UserSessionsController" do
- controller.should be_an_instance_of(UserSessionsController)
- end
-
end
@@ -2,15 +2,4 @@
describe UsersController do
- it "should get a collection of users when asked for index" do
- User.should_receive(:find).with(:all)
- get :index
- end
-
- it "should assign the users to the view" do
- User.stub!(:find).and_return(['user'])
- get :index
- assigns(:users).should == ['user']
- end
-
end
@@ -0,0 +1,4 @@
+Factory.define :email_template do |t|
+ t.title "Email title"
+ t.body "This is the body of the email"
+end
@@ -1,9 +0,0 @@
-# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
-
-one:
- user_id: 1
- role_id: 1
-
-two:
- user_id: 1
- role_id: 1
View
@@ -1,7 +0,0 @@
-# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
-
-one:
- name: MyString
-
-two:
- name: MyString
Oops, something went wrong.

0 comments on commit 69850c4

Please sign in to comment.