Permalink
Browse files

Changed next_step in message.rb into an indempotent method

  • Loading branch information...
1 parent 2dd5bfa commit 6681ee7f70d0add1e15623d2fbae4e9b484d35e3 @mikel committed Jun 7, 2009
@@ -26,6 +26,10 @@ def destroy
@recipient = Recipient.find(params[:recipient_id])
@recipient.groups.delete(@group)
redirect_to edit_recipient_path(@recipient)
+ elsif params[:message_id]
+ @message = Message.find(params[:message_id])
+ @message.groups.delete(@group)
+ redirect_to edit_message_path(@message)
else
@group.destroy
redirect_to groups_path
@@ -10,9 +10,14 @@ def create
render :action => :edit
end
+ def edit
+ @message = Message.find(params[:id])
+ end
+
def update
@message = Message.find(params[:id])
@message.attributes = params[:message]
+ @message.save
case @message.next_step
when "finished"
View
@@ -8,7 +8,6 @@ class Message < ActiveRecord::Base
def must_have_recipients
if no_recipients
- self.state = 'select_recipients'
errors.add_to_base 'Please select at least one recipient'
false
else
@@ -24,46 +23,42 @@ def must_have_recipients
belongs_to :email_template
belongs_to :user
+
+ def before_save
+ update_template if changes.include?('email_template') || changes.include?('email_template_id')
+ self.multipart = true if state == 'new' && source == 'upload'
+ do_add_recipients if @recipient_selected
+ do_add_group if @group_selected
+ end
+
+ def update_template
+ self.attributes = email_template.copy_attributes
+ email_template.attachments.each do |attachment|
+ self.attachments.create!(attachment.attributes)
+ end
+ end
def next_step
- return @state if @state
- @state = case state
- when nil
- initial_step
- when 'template_selected'
- self.attributes = email_template.copy_attributes
- email_template.attachments.each do |attachment|
- self.attachments.create!(attachment.attributes)
- end
- 'edit_content'
- when 'file_uploaded'
+ case
+ when state == 'new' && source == 'edit'
'edit_content'
- when 'content_edited'
+ when state == 'new' && source == 'template'
+ 'select_template'
+ when state == 'new' && source == 'upload'
+ 'select_files'
+ when state == 'content_edited'
'select_recipients'
- when 'recipients_selected'
+ when state == 'file_uploaded'
+ 'edit_content'
+ when state == 'template_selected'
+ 'edit_content'
+ when state == '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
- end
-
- def initial_step
- case source
- when 'edit'
- self.update_attribute(:multipart, false)
- 'edit_content'
- when 'template'
- 'select_template'
- when 'upload'
- self.update_attribute(:multipart, true)
- 'select_files'
- else
'new'
end
end
@@ -98,8 +93,9 @@ def add_recipient=(recipient_selected)
def do_add_group
- Addressee.create!(:message_id => self.id, :group_id => @group_selected)
- self.state = 'select_recipients'
+ unless Addressee.find(:first, :conditions => {:message_id => self.id, :group_id => @group_selected})
+ Addressee.create!(:message_id => self.id, :group_id => @group_selected)
+ end
end
def do_add_recipients
@@ -113,7 +109,7 @@ def do_add_recipients
self.errors.add_to_base("No recipient found with '#{recipient_detail}'") unless @recipient
end
- if @recipient
+ if @recipient && !recipients.include?(@recipient)
Addressee.create!(:message_id => self.id, :recipient_id => @recipient.id)
end
end
@@ -15,6 +15,7 @@
<%= f.label :source_upload, "From Uploaded Files" %>
<%= f.radio_button("source", "upload") %>
</p>
+ <%= f.hidden_field :state, :value => 'new' %>
<%= f.submit "Next" %>
<% end -%>
@@ -134,7 +134,7 @@ Feature: Messages Wizard
And the "message" with "title" of "My new email" should have a "group" with "name" of "Public"
And the "message" with "title" of "My new email" should have a "recipient" with "given_name" of "Mikel"
- Scenario: Selecting no recipients for a message
+ Scenario: Trying to add the same recipient twice
Given I am logged in
And there is a group in the system called "Public"
And there is a recipient I added in the system called "Mikel Lindsaar"
@@ -144,30 +144,60 @@ Feature: Messages Wizard
And I press "Next"
And I fill in "Plain Text" with "This is the email I am sending out"
And I press "Next"
+ And I fill in "Add Recipient" with "Mikel Lindsaar"
+ And I press "Next"
+ And I fill in "Add Recipient" with "Mikel Lindsaar"
And I press "Next"
Then I should see "Select Recipients"
- And I should see "Please select at least one recipient"
+ And the message entitled "My new email" should have one recipient
- Scenario: Saving a created message
+ Scenario: Trying to add the same group twice
Given I am logged in
And there is a group in the system called "Public"
- And there is a recipient I added in the system called "Mikel Lindsaar"
When I follow "New Message"
And I fill in "Title" with "My new email"
And I choose "Plain Text Only"
And I press "Next"
And I fill in "Plain Text" with "This is the email I am sending out"
And I press "Next"
- And I fill in "Add Recipient" with "Mikel Lindsaar"
+ And I select "Public" from "Add Group"
+ And I press "Next"
+ And I select "Public" from "Add Group"
+ And I press "Next"
+ Then I should see "Select Recipients"
+ And the message entitled "My new email" should have one group
+
+ Scenario: Selecting no recipients for a message
+ Given I am logged in
+ And there is a message called "My Email" in the system
+ When I go to the edit page for "Message" with a "title" of "My Email"
And I press "Next"
+ Then I should see "Select Recipients"
+ And I should see "Please select at least one recipient"
+
+ Scenario: Saving a created message
+ Given I am logged in
+ And there is a recipient I added in the system called "Mikel Lindsaar"
+ And there is a message called "My Email" in the system with "Mikel Lindsaar" as a recipient
+ When I go to the edit page for "Message" with a "title" of "My Email"
And I press "Next"
Then I should see "Schedule Mailout"
Scenario: Removing a recipient from a created message
+ Given I am logged in
+ And there is a recipient I added in the system called "Mikel Lindsaar"
+ And there is a message called "My Email" in the system with "Mikel Lindsaar" as a recipient
+ When I go to the edit page for "Message" with a "title" of "My Email"
+ And I follow "Remove"
+ Then I should see "Select Recipients"
+ And the message called "My email" should have no recipients
Scenario: Removing a group from a created message
-
-
-
-
-
+ Given I am logged in
+ And there is a group in the system called "Public"
+ And there is a message called "My Email" in the system with "Public" as a group
+ When I go to the edit page for "Message" with a "title" of "My Email"
+ And I follow "Remove"
+ Then I should see "Select Recipients"
+ And the message called "My email" should have no groups
+
@@ -0,0 +1,35 @@
+When /^there is a message called "([^\"]*)" in the system with "([^\"]*)" as a recipient$/ do |title, recipient|
+ message = Factory(:message, :title => title, :state => 'content_edited')
+ given, family = recipient.split(" ", 2)
+ recipient = Factory(:recipient, :given_name => given, :family_name => family)
+ message.save!
+ message.recipients << recipient
+end
+
+When /^there is a message called "([^\"]*)" in the system with "([^\"]*)" as a group$/ do |title, group|
+ message = Factory(:message, :title => title, :state => 'content_edited')
+ group = Factory(:group, :name => group, :user => User.find(:first))
+ message.save!
+ message.groups << group
+end
+
+When /^there is a message called "([^\"]*)" in the system$/ do |title|
+ message = Factory(:message, :title => title, :state => 'content_edited')
+ message.save!
+end
+
+Then /^the message entitled "([^\"]*)" should have one recipient/ do |title|
+ Message.find_by_title(title).recipients.count.should == 1
+end
+
+Then /^the message entitled "([^\"]*)" should have one group/ do |title|
+ Message.find_by_title(title).groups.count.should == 1
+end
+
+Then /^the message called "([^\"]*)" should have no recipients$/ do |title|
+ Message.find_by_title(title).recipients.count.should == 0
+end
+
+Then /^the message called "([^\"]*)" should have no groups$/ do |title|
+ Group.find_by_title(title).groups.count.should == 0
+end
@@ -84,8 +84,9 @@ def path_to(page_name)
klass = $1
attribute = $2
value = $3
- obj = klass.constantize.find(:first, :conditions => {attribute => value})
- instance_eval("edit_#{klass.downcase}_path(#{obj.to_param})")
+ obj = klass.capitalize.constantize.find(:first, :conditions => {attribute => value})
+ raise ActiveRecord::RecordNotFound unless obj
+ instance_eval("edit_#{klass.downcase}_path(obj)")
else
@@ -67,12 +67,14 @@
@message.id = 10
Addressee.should_receive(:create!).with(:message_id => @message.id, :group_id => 5)
@message.add_group_id = 5
+ @message.save
end
it "should set the state to select_recipients" do
@message = Message.new
Addressee.stub!(:create!)
@message.add_group_id = 5
+ @message.save
@message.state.should == 'select_recipients'
end
end
@@ -119,7 +121,7 @@
Recipient.stub!(:find).and_return(@recipient)
Addressee.stub!(:create!)
@message.add_recipient = 'mikel@me.com'
- @message.state.should == 'select_recipients'
+ @message.next_step.should == 'select_recipients'
end
it "should not try adding an addressee if no recipient was found" do

0 comments on commit 6681ee7

Please sign in to comment.