Permalink
Browse files

Added the add recipients and group pages to the create message wizard

  • Loading branch information...
1 parent b8768bf commit 2dd5bfa1b4dbf41486662c03a319d1fe2314dbf3 @mikel committed Jun 7, 2009
Showing with 707 additions and 87 deletions.
  1. +35 −0 app/controllers/groups_controller.rb
  2. +0 −1 app/controllers/messages_controller.rb
  3. +2 −2 app/controllers/recipients_controller.rb
  4. +2 −0 app/helpers/groups_helper.rb
  5. +7 −0 app/models/addressee.rb
  6. +4 −0 app/models/email_template.rb
  7. +11 −0 app/models/group.rb
  8. +83 −9 app/models/message.rb
  9. +14 −0 app/models/recipient.rb
  10. +6 −0 app/models/subscription.rb
  11. +1 −1 app/models/user.rb
  12. +4 −0 app/views/groups/_form.html.erb
  13. +1 −0 app/views/groups/_group.html.erb
  14. +9 −0 app/views/groups/index.html.erb
  15. +4 −0 app/views/groups/new.html.erb
  16. +4 −2 app/views/home/index.html.erb
  17. +1 −0 app/views/layouts/application.html.erb
  18. +6 −1 app/views/messages/_edit_content.html.erb
  19. +4 −0 app/views/messages/_group.html.erb
  20. +0 −4 app/views/messages/_plain_text.html.erb
  21. +4 −0 app/views/messages/_recipient.html.erb
  22. +2 −0 app/views/messages/_schedule_mailout.html.erb
  23. +17 −0 app/views/messages/_select_recipients.html.erb
  24. +5 −0 app/views/recipients/_group.html.erb
  25. +10 −0 app/views/recipients/edit.html.erb
  26. +11 −3 config/routes.rb
  27. +14 −0 db/migrate/20090603105339_create_groups.rb
  28. +14 −0 db/migrate/20090603123709_create_subscriptions.rb
  29. +15 −0 db/migrate/20090606215023_create_addressees.rb
  30. +23 −1 db/schema.rb
  31. +26 −0 features/manage_groups.feature
  32. +27 −1 features/manage_recipients.feature
  33. +67 −3 features/{manage_messages.feature → messages_wizard.feature}
  34. +7 −1 features/step_definitions/global_steps.rb
  35. +3 −0 features/step_definitions/manage_groups_steps.rb
  36. +2 −1 features/step_definitions/manage_recipients_steps.rb
  37. +17 −0 features/support/paths.rb
  38. +2 −0 lib/tasks/bootstrap.rake
  39. +10 −0 spec/controllers/groups_controller_spec.rb
  40. +1 −0 spec/factories/email_template_factory.rb
  41. +4 −0 spec/factories/group_factory.rb
  42. +0 −21 spec/fixtures/attachments.yml
  43. +0 −13 spec/fixtures/messages.yml
  44. +0 −7 spec/fixtures/organizations.yml
  45. +0 −15 spec/fixtures/recipients.yml
  46. +11 −0 spec/helpers/groups_helper_spec.rb
  47. +26 −0 spec/models/addressee_spec.rb
  48. +26 −0 spec/models/group_spec.rb
  49. +117 −1 spec/models/message_spec.rb
  50. +33 −0 spec/models/recipient_spec.rb
  51. +14 −0 spec/models/subscription_spec.rb
  52. +1 −0 spec/models/user_spec.rb
@@ -0,0 +1,35 @@
+class GroupsController < ApplicationController
+
+ before_filter :require_user
+
+ def index
+ @groups = Group.find(:all)
+ end
+
+ def new
+ @group = Group.new
+ end
+
+ def create
+ @group = Group.new(params[:group])
+ @group.user = current_user
+ if @group.save
+ redirect_to groups_path
+ else
+ render :action => :new
+ end
+ end
+
+ def destroy
+ @group = Group.find(params[:id])
+ if params[:recipient_id]
+ @recipient = Recipient.find(params[:recipient_id])
+ @recipient.groups.delete(@group)
+ redirect_to edit_recipient_path(@recipient)
+ else
+ @group.destroy
+ redirect_to groups_path
+ end
+ end
+
+end
@@ -16,7 +16,6 @@ def update
case @message.next_step
when "finished"
-
else
render :action => :edit
end
@@ -31,9 +31,9 @@ def update
@recipient.organization = current_user.organization
if @recipient.save
flash[:notice] = "Recipient updated"
- redirect_to recipients_path
+ redirect_to :action => :edit
else
- render :action => :new
+ render :action => :edit
end
end
@@ -0,0 +1,2 @@
+module GroupsHelper
+end
View
@@ -0,0 +1,7 @@
+class Addressee < ActiveRecord::Base
+
+ belongs_to :group
+ belongs_to :recipient
+ belongs_to :message
+
+end
@@ -1,3 +1,7 @@
class EmailTemplate < Message
+
+ def copy_attributes
+ attributes.reject { |k,v| %w[title state email_template_id].include?(k) }
+ end
end
View
@@ -0,0 +1,11 @@
+class Group < ActiveRecord::Base
+
+ belongs_to :user
+
+ has_many :subscriptions
+ has_many :recipients, :through => :subscriptions
+
+ has_many :addressees
+ has_many :messages, :through => :addressees
+
+end
View
@@ -3,26 +3,51 @@ class Message < ActiveRecord::Base
require 'hpricot'
validates_presence_of :title
-
+
+ validate :must_have_recipients, :if => Proc.new { |message| message.state == 'select_recipients' }
+
+ def must_have_recipients
+ if no_recipients
+ self.state = 'select_recipients'
+ errors.add_to_base 'Please select at least one recipient'
+ false
+ else
+ true
+ end
+ end
+
has_many :attachments
+ has_many :addressees
+ has_many :groups, :through => :addressees
+ has_many :recipients, :through => :addressees
+
belongs_to :email_template
belongs_to :user
def next_step
- case state
+ return @state if @state
+ @state = case state
when nil
initial_step
when 'template_selected'
- self.html_part = email_template.html_part
- self.plain_part = email_template.plain_part
+ self.attributes = email_template.copy_attributes
email_template.attachments.each do |attachment|
self.attachments.create!(attachment.attributes)
end
'edit_content'
when 'file_uploaded'
- self.save
'edit_content'
+ when 'content_edited'
+ 'select_recipients'
+ when 'recipients_selected'
+ if @recipient_selected.blank? && @group_selected.blank? && must_have_recipients
+ 'schedule_mailout'
+ else
+ do_add_recipients unless @recipient_selected.blank?
+ do_add_group unless @group_selected.blank?
+ 'select_recipients'
+ end
else
state
end
@@ -31,10 +56,12 @@ def next_step
def initial_step
case source
when 'edit'
- 'plain_text'
+ self.update_attribute(:multipart, false)
+ 'edit_content'
when 'template'
'select_template'
when 'upload'
+ self.update_attribute(:multipart, true)
'select_files'
else
'new'
@@ -45,14 +72,61 @@ def html_file_data=(data)
self.html_part = data.read
self.plain_part = strip(html_part)
end
+
+ def zip_file_data=(data)
+ return unless data.respond_to?(:read)
+ extract_file_data(data)
+ end
+
+ def add_group_id
+ nil
+ end
+
+ def add_group_id=(group_selected)
+ @group_selected = group_selected
+ end
+
+ def add_recipient
+ nil
+ end
+
+ def add_recipient=(recipient_selected)
+ @recipient_selected = recipient_selected
+ end
+
+ private
+
+
+ def do_add_group
+ Addressee.create!(:message_id => self.id, :group_id => @group_selected)
+ self.state = 'select_recipients'
+ end
+
+ def do_add_recipients
+ case @recipient_selected
+ when /@/ # Probably an email address
+ @recipient = Recipient.find(:first, :conditions => {:email => @recipient_selected})
+ self.errors.add_to_base("No recipient found with '#{recipient_detail}'") unless @recipient
+ when /\s/
+ given, family = @recipient_selected.to_s.split(" ", 2)
+ @recipient = Recipient.find(:first, :conditions => {:given_name => given, :family_name => family})
+ self.errors.add_to_base("No recipient found with '#{recipient_detail}'") unless @recipient
+ end
+
+ if @recipient
+ Addressee.create!(:message_id => self.id, :recipient_id => @recipient.id)
+ end
+ end
+
+ def no_recipients
+ @no_recipients ||= (recipients.empty? && groups.empty?)
+ end
def strip(html)
Hpricot(html).to_plain_text.gsub(/\s+/, "\s").gsub(/\n\n+/, "\n")
end
- def zip_file_data=(data)
- return unless data.respond_to?(:read)
-
+ def extract_file_data(data)
filename = File.join(RAILS_ROOT, 'tmp', "#{Time.now.to_i}_#{data.original_filename}")
directory = data.original_filename.chomp(".zip")
path = File.join(RAILS_ROOT, 'tmp', "#{Time.now.to_i}_#{directory}")
View
@@ -2,6 +2,12 @@ class Recipient < ActiveRecord::Base
belongs_to :organization
+ has_many :subscriptions
+ has_many :groups, :through => :subscriptions
+
+ has_many :addressees
+ has_many :messages, :through => :addressees
+
validates_presence_of :organization_id
validates_associated :organization
@@ -17,4 +23,12 @@ def black_list
@black_list ||= (self.state == 'blacklisted')
end
+ def add_group_id=(group_id)
+ return if group_id.blank?
+ self.groups << Group.find(group_id)
+ end
+
+ def add_group_id
+ nil
+ end
end
@@ -0,0 +1,6 @@
+class Subscription < ActiveRecord::Base
+
+ belongs_to :group
+ belongs_to :recipient
+
+end
View
@@ -14,7 +14,7 @@ class User < ActiveRecord::Base
validate_on_update :check_last_admin?
validates_associated :organization
- validates_presence_of :organization_id
+ validates_presence_of :organization
def check_last_admin?
if @tried_to_remove_admin && last_admin?
@@ -0,0 +1,4 @@
+<p>
+ <%= form.label :name %>
+ <%= form.text_field :name %>
+</p>
@@ -0,0 +1 @@
+<li><%= group.name %></li>
@@ -0,0 +1,9 @@
+<%- if @groups.empty? -%>
+ No groups defined
+<% else -%>
+ <ul>
+ <%= render :partial => 'group', :collection => @groups %>
+ </ul>
+<%- end -%>
+
+<%= link_to "Make a new group", new_group_path %>
@@ -0,0 +1,4 @@
+<% form_for @group do |f| -%>
+ <%= render :partial => 'form', :object => f %>
+ <%= f.submit 'Create' %>
+<% end -%>
@@ -1,5 +1,3 @@
-<%= flash[:notice] %>
-
<h1>Welcome to Mailer</h1>
<h2>Email Templates</h2>
@@ -18,6 +16,10 @@
</li>
<li>
+ <%= link_to "Groups", groups_path %>
+ </li>
+
+ <li>
<%- if current_user.admin -%>
<%= link_to "Edit Users", users_path %><br />
<%- else -%>
@@ -15,6 +15,7 @@
<%= yield %>
<div id="footer">
+ <%= link_to "Home", root_path %><br />
<%= link_to "Logout", logout_path %>
</div>
</body>
@@ -1,15 +1,20 @@
+<%- if @message.multipart? -%>
<p>
<%= f.label :html_part, "HTML Part" %>
<%= f.text_area :html_part %>
</p>
+<%- end -%>
<p>
<%= f.label :plain_part, "Plain Text" %>
<%= f.text_area :plain_part %>
</p>
+<%= f.hidden_field :state, :value => 'content_edited' %>
+
<%- unless @message.attachments.empty? -%>
<h2>Attached Files:</h2>
<%= render :partial => 'attachments/attachment', :collection => @message.attachments %>
-<%- end -%>
+<%- end -%>
+
@@ -0,0 +1,4 @@
+<li>
+ <%= group.name %>,
+ <%= link_to "Remove", message_group_path(:id => group.to_param, :message_id => @message.to_param), :method => 'delete' %>
+</li>
@@ -1,4 +0,0 @@
-<p>
- <%= f.label :plain_part, "Plain Text" %>
- <%= f.text_area :plain_part %>
-</p>
@@ -0,0 +1,4 @@
+<li>
+ <%= recipient.name %>,
+ <%= link_to "Remove", message_recipient_path(:id => recipient.to_param, :message_id => @message.to_param), :method => 'delete' %>
+</li>
@@ -0,0 +1,2 @@
+<h2>Schedule Mailout</h2>
+
@@ -0,0 +1,17 @@
+<h2>Select Recipients</h2>
+
+<ul>
+ <%= render :partial => "group", :collection => @message.groups %>
+</ul>
+<%= f.label :add_group_id, "Add Group" %>
+<%= f.collection_select :add_group_id, Group.all(:order => 'name'), :id, :name, :include_blank => true -%>
+
+
+<ul>
+ <%= render :partial => "recipient", :collection => @message.recipients %>
+</ul>
+<%= f.label :add_recipient, "Add Recipient" %>
+<%= f.text_field :add_recipient -%>
+Enter name or email address
+
+<%= f.hidden_field :state, :value => 'recipients_selected' %>
@@ -0,0 +1,5 @@
+<li>
+ <%= group.name %>,
+ <%= link_to "Remove", recipient_group_path(:id => group.to_param, :recipient_id => @recipient.to_param), :method => 'delete' %>
+
+</li>
@@ -2,5 +2,15 @@
<% form_for @recipient do |f| %>
<%= f.error_messages %>
<%= render :partial => "form", :object => f %>
+
+ <h3>Groups:</h3>
+ <ul>
+ <%= render :partial => "group", :collection => @recipient.groups %>
+ </ul>
+
+ <br />
+ <%= f.label :add_group_id, "Add Group" %>
+ <%= f.collection_select :add_group_id, Group.all(:order => 'name'), :id, :name -%>
+
<%= f.submit "Update" %>
<% end %>
Oops, something went wrong.

0 comments on commit 2dd5bfa

Please sign in to comment.