Browse files

Swapped out my own hack job for Acts As State Machine gem

  • Loading branch information...
1 parent ca80a7f commit a564e76f914c38f5c0be2f91471ec5e90af6d574 @mikel committed Jul 28, 2009
View
13 app/controllers/messages_controller.rb
@@ -10,8 +10,11 @@ def new
def create
@message = Message.new(params[:message])
- @message.save
- render :action => :edit
+ if @message.next!
+ render :action => :edit
+ else
+ render :action => :new
+ end
end
def edit
@@ -21,9 +24,9 @@ def edit
def update
@message = Message.find(params[:id])
@message.attributes = params[:message]
- @message.save
- case @message.next_step
- when "ready_to_send"
+ @message.next!
+ case @message.state
+ when "confirmed"
redirect_to messages_path
else
render :action => :edit
View
109 app/models/message.rb
@@ -1,22 +1,71 @@
class Message < ActiveRecord::Base
-
+
require 'hpricot'
-
- validates_presence_of :title
- validate :must_have_recipients, :if => Proc.new { |m| m.state == 'recipients_selected' }
+ # Use state machine for state transitions
+ include AASM
+ aasm_column :aasm_state
+ aasm_initial_state :new
+
+ aasm_state :new
+ aasm_state :select_html
+ aasm_state :select_template
+ aasm_state :edit_content
+ aasm_state :select_recipients
+ aasm_state :schedule_mailout
+ aasm_state :confirm_mailout
+ aasm_state :confirmed
+
+ aasm_event :next do
+ transitions :to => :edit_content, :from => [:new],
+ :guard => Proc.new { |m| m.source == 'plain' }
+ transitions :to => :select_html, :from => [:new],
+ :guard => Proc.new { |m| m.source == 'html' }
+ transitions :to => :select_template, :from => [:new],
+ :guard => Proc.new { |m| m.source == 'template' }
+ transitions :to => :edit_content, :from => [:select_html],
+ :on_transition => Proc.new { |m| m.multipart = true }
+ transitions :to => :edit_content, :from => [:select_template],
+ :on_transition => Proc.new { |m| m.update_template }
+ transitions :to => :select_recipients, :from => [:edit_content]
+ transitions :to => :schedule_mailout, :from => [:select_recipients],
+ :guard => Proc.new { |m| m.must_have_recipients }
+ transitions :to => :confirm_mailout, :from => [:schedule_mailout]
+ transitions :to => :confirmed, :from => [:confirm_mailout]
+ end
+
+ aasm_event :previous do
+ transitions :to => :confirm_mailout, :from => [:confirmed]
+ transitions :to => :schedule_mailout, :from => [:confirm_mailout]
+ transitions :to => :select_recipients, :from => [:schedule_mailout]
+ transitions :to => :edit_content, :from => [:select_recipients]
+ transitions :to => :new, :from => [:select_html, :select_template]
+ transitions :to => :new, :from => [:edit_content],
+ :guard => Proc.new { |m| m.source == 'plain' }
+ transitions :to => :select_html, :from => [:edit_content],
+ :guard => Proc.new { |m| m.source == 'html' }
+ transitions :to => :select_template, :from => [:edit_content],
+ :guard => Proc.new { |m| m.source == 'template' }
+ end
+
+ validates_presence_of :title
def must_have_recipients
- true == true
case
+ # When no recipients and we are not trying to add anyone
when no_recipients && @recipient_selected.blank? && @group_selected.blank?
errors.add_to_base 'Please select at least one recipient'
false
+ # When no recipients even though we tried to add someone
when no_recipients
errors.add_to_base "No recipient found with '#{@recipient_selected}'"
false
- else
+ # We have recipients and are not trying to add anyone else
+ when have_recipients && @recipient_selected.blank? && @group_selected.blank?
true
+ else
+ self.save
+ false
end
end
@@ -29,51 +78,21 @@ def must_have_recipients
belongs_to :email_template
belongs_to :user
- def before_validation
- self.multipart = true if state == 'new' && source == 'upload'
- end
-
- def after_save
- update_template if changes.include?('email_template') || changes.include?('email_template_id')
+ def state
+ aasm_state
end
def organization
user.organization
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
- case
- when state == 'new' && source == 'edit'
- 'edit_content'
- when state == 'new' && source == 'template'
- 'select_template'
- when state == 'new' && source == 'upload'
- 'select_files'
- when state == 'content_edited'
- 'select_recipients'
- 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
- 'select_recipients'
+ if changes.include?('email_template') || changes.include?('email_template_id')
+ self.attributes = email_template.copy_attributes
+ self.save
+ email_template.attachments.each do |attachment|
+ self.attachments.create!(attachment.attributes)
end
- when state == 'date_scheduled'
- 'confirm'
- when state == 'confirmed'
- 'ready_to_send'
- else
- 'new'
end
end
@@ -128,6 +147,10 @@ def do_add_recipients
end
+ def have_recipients
+ !no_recipients
+ end
+
def no_recipients
(recipients.length == 0) && (groups.length == 0)
end
View
20 app/models/recipient.rb
@@ -1,5 +1,13 @@
class Recipient < ActiveRecord::Base
+ # Use state machine for state transitions
+ include AASM
+ aasm_column :aasm_state
+ aasm_initial_state :ok
+
+ aasm_state :ok
+ aasm_state :black_listed
+
belongs_to :organization
has_many :subscriptions
@@ -15,12 +23,8 @@ def name
"#{given_name} #{family_name}"
end
- def black_list!
- self.update_attribute(:state, 'blacklisted')
- end
-
- def black_list
- @black_list ||= (self.state == 'blacklisted')
+ aasm_event :black_list do
+ transitions :to => :black_listed, :from => [:ok]
end
def add_group_id=(group_id)
@@ -31,4 +35,8 @@ def add_group_id=(group_id)
def add_group_id
nil
end
+
+ def state
+ aasm_state
+ end
end
View
2 app/views/home/index.html.erb
@@ -4,7 +4,7 @@
<ul>
<li>
- <%= link_to "Email Templates", new_email_template_path %>
+ <%= link_to "Email Templates", email_templates_path %>
</li>
<li>
View
1 app/views/messages/_confirm.html.erb → app/views/messages/_confirm_mailout.html.erb
@@ -19,6 +19,5 @@
<%= render :partial => "recipient", :collection => message.recipients, :locals => {:message => message} %>
</ul>
</p>
-<%= f.hidden_field :state, :value => 'confirmed' %>
<%= f.submit "Confirm Mailout" %>
View
2 app/views/messages/_edit_content.html.erb
@@ -10,8 +10,6 @@
<%= 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 %>
View
2 app/views/messages/_schedule_mailout.html.erb
@@ -2,6 +2,4 @@
<%= f.datetime_select :date_scheduled, :default => Time.now %>
-<%= f.hidden_field :state, :value => 'date_scheduled' %>
-
<%= f.submit "Next" %>
View
1 app/views/messages/_select_files.html.erb → app/views/messages/_select_html.html.erb
@@ -6,6 +6,5 @@
<%= f.label :file_data, "Zip File" %>:
<%= f.file_field :zip_file_data %>
</p>
-<%= f.hidden_field :state, :value => 'file_uploaded' %>
<%= f.submit "Next" %>
View
2 app/views/messages/_select_recipients.html.erb
@@ -14,6 +14,4 @@
<%= f.text_field :add_recipient -%>
Enter name or email address
-<%= f.hidden_field :state, :value => 'recipients_selected' %>
-
<%= f.submit "Next" %>
View
2 app/views/messages/_select_template.html.erb
@@ -2,6 +2,4 @@
<%= f.label :email_template_id, "Select Template" %>
<%= f.collection_select :email_template_id, current_user.email_templates.all(:order => 'title'), :id, :title, :include_blank => 'Please select template' %><br />
</p>
-<%= f.hidden_field :state, :value => 'template_selected' %>
-
<%= f.submit "Next" %>
View
10 app/views/messages/edit.html.erb
@@ -1,6 +1,10 @@
-<% form_for @message, :html => { :multipart => (@message.next_step == 'select_files') } do |f| -%>
+<% form_for @message, :html => { :multipart => (@message.multipart) } do |f| -%>
<%= f.error_messages %>
- <%= render :partial => @message.next_step, :locals => { :f => f, :message => @message } %>
+ <%= render :partial => @message.state, :locals => { :f => f, :message => @message } %>
-<% end -%>
+<% end -%>
+
+<p>
+ Step: <%= @message.state %>
+</p>
View
18 app/views/messages/new.html.erb
@@ -1,21 +1,23 @@
-<% form_for @message, :html => { :multipart => (@message.next_step == 'select_files') } do |f| -%>
+<% form_for @message, :html => { :multipart => (@message.multipart) } do |f| -%>
<%= f.error_messages %>
+ <h2>
+ Enter Title and Type of Mailout
+ </h2>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
<br />
- <%= f.label :source_edit, "Plain Text Only" %>
- <%= f.radio_button("source", "edit") %>
+ <%= f.label :source_plain, "Plain Text Only" %>
+ <%= f.radio_button("source", "plain") %>
+ <br />
+ <%= f.label :source_html, "HTML and Plain Text" %>
+ <%= f.radio_button("source", "html") %>
<%- if current_user.email_templates.count > 0 -%>
<br />
<%= f.label :source_template, "From Template" %>
<%= f.radio_button("source", "template") %>
<%- end -%>
- <br />
- <%= f.label :source_upload, "From Uploaded Files" %>
- <%= f.radio_button("source", "upload") %>
</p>
- <%= f.hidden_field :state, :value => 'new' %>
<%= f.submit "Next" %>
-<% end -%>
+<% end -%>
View
17 config/environment.rb
@@ -14,16 +14,13 @@
# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{RAILS_ROOT}/extras )
- # Specify gems that this application depends on and have them installed with rake gems:install
- # config.gem "bj"
- # config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
- # config.gem "sqlite3-ruby", :lib => "sqlite3"
- # config.gem "aws-s3", :lib => "aws/s3"
-
- config.gem "authlogic", :version => '>=2.0.11'
- config.gem "rubyzip", :lib => 'zip/zipfilesystem', :version => '>=0.9'
- config.gem "hpricot", :version => '>=0.8'
- config.gem "mime-types", :lib => 'mime/types', :version => '>=1.15'
+ # Gems needed for Contact
+ config.gem "authlogic", :version => '>=2.0.11'
+ config.gem "rubyzip", :lib => 'zip/zipfilesystem', :version => '>=0.9'
+ config.gem "hpricot", :version => '>=0.8'
+ config.gem "mime-types", :lib => 'mime/types', :version => '>=1.15'
+ config.gem 'rubyist-aasm', :lib => 'aasm', :version => '>=2.0', :source => "http://gems.github.com"
+ config.gem 'validatable', :lib => 'validatable'
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named
View
11 db/migrate/20090726030640_change_state_columns_to_aasm_states.rb
@@ -0,0 +1,11 @@
+class ChangeStateColumnsToAasmStates < ActiveRecord::Migration
+ def self.up
+ rename_column :messages, :state, :aasm_state
+ rename_column :recipients, :state, :aasm_state
+ end
+
+ def self.down
+ rename_column :recipients, :aasm_state, :state
+ rename_column :messages, :aasm_state, :state
+ end
+end
View
6 db/schema.rb
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20090610221548) do
+ActiveRecord::Schema.define(:version => 20090726030640) do
create_table "addressees", :force => true do |t|
t.integer "message_id"
@@ -50,7 +50,7 @@
create_table "messages", :force => true do |t|
t.string "title"
t.string "source"
- t.string "state"
+ t.string "aasm_state"
t.boolean "multipart"
t.integer "user_id"
t.integer "email_template_id"
@@ -78,7 +78,7 @@
t.datetime "created_at"
t.datetime "updated_at"
t.integer "organization_id"
- t.string "state", :limit => 10
+ t.string "aasm_state", :limit => 10
end
create_table "roles", :force => true do |t|
View
1 features/manage_email_templates.feature
@@ -25,6 +25,7 @@ Feature: Manage Email Templates
Given I am logged in
When I go to the homepage
And I follow "Email Templates"
+ And I follow "Make a new email template"
Then I should be on the new email templates page
Scenario: Making a new email template
View
8 features/manage_messages.feature
@@ -8,6 +8,7 @@ Feature: Managing Messages
When I follow "Mailouts"
And I follow "Make a new mailout"
Then I should be on the new message page
+ And I should see "Enter Title and Type of Mailout"
Scenario: Trying to make an email without a title
Given I am logged in
@@ -17,6 +18,7 @@ Feature: Managing Messages
And I press "Next"
Then I should see "Title can't be blank"
And I should see "Title"
+ And I should see "Enter Title and Type of Mailout"
Scenario: Making a new plain text email message directly
Given I am logged in
@@ -80,7 +82,7 @@ Feature: Managing Messages
When I follow "Mailouts"
And I follow "Make a new mailout"
And I fill in "Title" with "My email"
- And I choose "message_source_upload"
+ And I choose "message_source_html"
And I press "Next"
Then I should see "HTML File"
And I should see "Zip File"
@@ -91,7 +93,7 @@ Feature: Managing Messages
When I follow "Mailouts"
And I follow "Make a new mailout"
And I fill in "Title" with "My email"
- And I choose "message_source_upload"
+ And I choose "message_source_html"
And I press "Next"
And I attach the file at "simple_email/index.html" to "message_html_file_data"
And I press "Next"
@@ -107,7 +109,7 @@ Feature: Managing Messages
When I follow "Mailouts"
And I follow "Make a new mailout"
And I fill in "Title" with "My email"
- And I choose "message_source_upload"
+ And I choose "message_source_html"
And I press "Next"
And I attach the file at "simple_email/index.html" to "message_html_file_data"
And I attach the file at "simple_email/images.zip" to "message_zip_file_data"
View
11 features/step_definitions/manage_messages_steps.rb
@@ -1,29 +1,28 @@
When /^there is a message called "([^\"]*)" in the system with "([^\"]*)" as a recipient$/ do |title, recipient|
- message = Factory(:message, :title => title, :state => 'content_edited', :user => User.first)
+ message = Factory(:message, :title => title, :aasm_state => 'select_recipients', :user => User.first)
given, family = recipient.split(" ", 2)
recipient = Factory(:recipient, :given_name => given, :family_name => family, :organization => User.first.organization)
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', :user => User.first)
+ message = Factory(:message, :title => title, :aasm_state => 'select_recipients', :user => User.first)
group = Factory(:group, :name => group, :user => User.find(:first))
message.save!
message.groups << group
end
When /^there is a message called "([^\"]*)" in the system with "([^\"]*)" as a group that has been scheduled$/ do |title, group|
- message = Factory(:message, :title => title, :state => 'content_edited', :user => User.first)
+ message = Factory(:message, :title => title, :aasm_state => "confirm_mailout", :user => User.first,
+ :date_scheduled => 1.day.from_now)
group = Factory(:group, :name => group, :user => User.find(:first))
- message.date_scheduled = 1.day.from_now
- message.state = "date_scheduled"
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', :user => User.first)
+ message = Factory(:message, :title => title, :aasm_state => 'select_recipients', :user => User.first)
message.save!
end
View
2 features/step_definitions/manage_recipients_steps.rb
@@ -15,6 +15,6 @@
Then /^"([^\"]*)" should be black listed$/ do |name|
given, family = name.split
recipient = Recipient.find_by_given_name_and_family_name(given, family)
- recipient.black_list.should be_true
+ recipient.should be_black_listed
end
View
33 lib/tasks/bootstrap.rake
@@ -1,16 +1,23 @@
-
desc 'Bootstraps the system'
task :bootstrap => :environment do
- org = Organization.create!(:name => "Default")
- user = User.new(:password => 'contact',
- :password_confirmation => 'contact',
- :given_name => 'Default',
- :family_name => 'Admin',
- :email => 'admin@nowaythisisadomainname.org.au')
- user.organization = org
- user.login = 'admin'
- Role.create!(:name => 'admin')
- Membership.create!(:user_id => 1, :role_id => 1)
- user.admin = true
- user.save!
+ if Organization.find(:first) || User.find(:first)
+ STDERR.puts "\n================================================="
+ STDERR.puts "You have already bootstrapped the system or have"
+ STDERR.puts "user or org data entered in the database."
+ STDERR.puts "\nCan only bootstrap a new installation."
+ STDERR.puts "=================================================\n"
+ else
+ org = Organization.create!(:name => "Default")
+ user = User.new(:password => 'contact',
+ :password_confirmation => 'contact',
+ :given_name => 'Default',
+ :family_name => 'Admin',
+ :email => 'admin@nowaythisisadomainname.org.au')
+ user.organization = org
+ user.login = 'admin'
+ Role.create!(:name => 'admin')
+ Membership.create!(:user_id => 1, :role_id => 1)
+ user.admin = true
+ user.save!
+ end
end
View
263 spec/models/message_spec.rb
@@ -1,41 +1,24 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Message do
+
+ def valid_attributes
+ { :title => "New Mailout" }
+ end
+
+ def new_message(args = {})
+ Message.new(valid_attributes.merge!(args))
+ end
describe "validations" do
+
it "should be invalid without a title" do
- @message = Message.new
- @message.title = "Hello"
+ @message = new_message
@message.should be_valid
@message.title = nil
@message.should_not be_valid
end
- it "should be invalid without a recipient if the state is at 'recipients_selected'" do
- @message = Message.new
- @message.title = "Hello"
- @message.state = 'recipients_selected'
- @message.should_not be_valid
- end
-
- it "should be valid if the state is at 'recipients_selected' and it has a recipient" do
- @message = Message.new
- @message.title = "Hello"
- @message.state = 'recipients_selected'
- @recipient = Factory(:recipient, :email => "mikel@me.com")
- @message.add_recipient = @recipient.email
- @message.should be_valid
- end
-
- it "should be valid if the state is at 'recipients_selected' and it has a group" do
- @message = Message.new
- @message.title = "Hello"
- @message.state = 'recipients_selected'
- @group = Factory(:group)
- @message.add_group_id = @group
- @message.should be_valid
- end
-
end
describe "associations" do
@@ -160,20 +143,18 @@
end
it "should add an error to base if no recipient was found" do
- @message = Message.new
+ @message = new_message(:aasm_state => 'select_recipients')
Recipient.stub!(:find).and_return(nil)
@message.add_recipient = 'Mikel Lindsaar'
- @message.state = 'recipients_selected'
- @message.valid?
+ @message.next!
@message.errors.full_messages.should include("No recipient found with 'Mikel Lindsaar'")
end
it "should say 'Please select at least one recipient' if there are no recipients or groups selected already and nothing is passed in" do
- @message = Message.new
+ @message = new_message(:aasm_state => 'select_recipients')
Recipient.stub!(:find).and_return(nil)
@message.add_recipient = ''
- @message.state = 'recipients_selected'
- @message.valid?
+ @message.next!
@message.errors.full_messages.should include("Please select at least one recipient")
end
@@ -220,53 +201,15 @@
doing { @message.zip_file_data = file }.should change(Attachment, :count).by(1)
end
end
-
- end
- describe "step definitions" do
-
- it "should say it's next step is new if it's source is nil" do
- @message = Message.new
- @message.source = nil
- @message.next_step.should == 'new'
- end
-
- it "should say it's next step is direct_entry if it's source is edit" do
- @message = Message.new
- @message.state = 'new'
- @message.source = "edit"
- @message.next_step.should == 'edit_content'
- end
-
- it "should say what it's next step is select template if it's source is template" do
- @message = Message.new
- @message.state = 'new'
- @message.source = "template"
- @message.next_step.should == 'select_template'
- end
-
- it "should say what it's next step is select file if it's source is upload" do
- @message = Message.new
- @message.state = 'new'
- @message.source = "upload"
- @message.next_step.should == 'select_files'
- end
-
- it "should set return edit_content if the state is set to 'template_selected'" do
- @message = Message.new
- @email_template = Factory(:email_template)
- @message.email_template_id = @email_template.id
- @message.state = 'template_selected'
- @message.next_step.should == 'edit_content'
- end
-
- it "should set it's html_part and plain_part to that of the email template if the state is 'template_selected'" do
+
+ it "should set it's html_part and plain_part to that of the email template during the next transition if the state is 'select_template'" do
@message = Message.new
@email_template = Factory(:email_template)
@message.title = "Test Email"
@message.email_template_id = @email_template.id
- @message.state = 'template_selected'
- @message.save!
+ @message.aasm_state = 'select_template'
+ @message.next!
@message.html_part.should == @email_template.html_part
@message.plain_part.should == @email_template.plain_part
end
@@ -277,46 +220,168 @@
attachment = Factory(:attachment,
:filename => 'rails.png',
:directory => 'image',
- :data => data,
+ :data => data,
:content_type => 'image/png',
:message_id => @email_template.id)
@message = Message.new(:title => "New Email", :email_template => @email_template)
- @message.state = 'template_selected'
- @message.save
+ @message.aasm_state = 'select_template'
+ @message.next!
@message.attachments.count.should == 1
@message.attachments.first.filename.should == 'rails.png'
end
+
+ end
+
+
+
+
+
+ describe "state transitions using next!" do
+
+ it "should be initially in the new state" do
+ @message = new_message
+ @message.save
+ @message.state.should == "new"
+ end
+
+ it "should not transition from new without a source being defined" do
+ @message = new_message(:aasm_state => "new")
+ @message.next!.should be_false
+ end
- it "should return confirm if the state is set to date_scheduled" do
- @message = Message.new
- @message.state = 'date_scheduled'
- @message.next_step.should == 'confirm'
+ it "should transition from new to edit_content if source is plain" do
+ @message = new_message(:aasm_state => "new")
+ @message.source = "plain"
+ @message.next!
+ @message.state.should == "edit_content"
end
- it "should set return edit_content if the state is set to 'file_uploaded'" do
- @message = Message.new
- @message.state = 'file_uploaded'
- @message.next_step.should == 'edit_content'
+ it "should transition from new to select_html if source is html" do
+ @message = new_message(:aasm_state => "new")
+ @message.source = "html"
+ @message.next!
+ @message.state.should == "select_html"
end
-
- it "should set it's html_part and plain_part to that of the index.html and plain.txt files uploaded if the state is 'file_uploaded'" do
- @message = Message.new
- @message.state = 'file_uploaded'
- @message.next_step.should == 'edit_content'
+
+ it "should transition from new to select_template if source is template" do
+ @message = new_message(:aasm_state => "new")
+ @message.source = "template"
+ @message.next!
+ @message.state.should == "select_template"
end
- it "should return add_recipients if it's state is content_edited" do
- @message = Message.new
- @message.state = 'content_edited'
- @message.next_step.should == 'select_recipients'
+ it "should transition from select_html to edit_content" do
+ @message = new_message(:aasm_state => "select_html")
+ @message.next!
+ @message.state.should == "edit_content"
end
- it "should return 'ready_to_send' if it's state is 'confirmed' " do
- @message = Message.new
- @message.state = 'confirmed'
- @message.next_step.should == 'ready_to_send'
+ it "should transition from select_template to edit_content" do
+ @message = new_message(:aasm_state => "select_template")
+ @message.next!
+ @message.state.should == "edit_content"
+ end
+
+ it "should transition from edit_content to select_recipients" do
+ @message = new_message(:aasm_state => "edit_content", :source => 'plain')
+ @message.next!
+ @message.state.should == "select_recipients"
+ end
+
+ it "should transition from select_recipients to select_recipients once we add one recipient" do
+ @message = new_message
+ @message.aasm_state = 'select_recipients'
+ @recipient = Factory(:recipient, :email => "mikel@me.com")
+ @message.add_recipient = @recipient.email
+ @message.next!
+ @message.state.should == "select_recipients"
+ end
+
+ it "should transition from select_recipients to schedule_mailout only if we have finished selecting recipients" do
+ @message = new_message
+ @message.aasm_state = 'select_recipients'
+ @recipient = Factory(:recipient, :email => "mikel@me.com")
+ @message.add_recipient = @recipient.email
+ @message.next!
+ @message = Message.find(@message.id)
+ @message.next!
+ @message.state.should == "schedule_mailout"
+ end
+
+ it "should transition from schedule_mailout to confirm_mailout" do
+ @message = new_message(:aasm_state => "schedule_mailout")
+ @message.next!
+ @message.state.should == "confirm_mailout"
+ end
+
+ it "should transition from confirm_mailout to confirmed" do
+ @message = new_message(:aasm_state => "confirm_mailout")
+ @message.next!
+ @message.state.should == "confirmed"
end
end
+
+ describe "state transitions using previous!" do
+
+ it "should transition to confirm_mailout from confirmed" do
+ @message = new_message(:aasm_state => "confirmed")
+ @message.previous!
+ @message.state.should == "confirm_mailout"
+ end
+
+ it "should transition to schedule_mailout from confirm_mailout" do
+ @message = new_message(:aasm_state => "confirm_mailout")
+ @message.previous!
+ @message.state.should == "schedule_mailout"
+ end
+
+ it "should transition to select_recipients from schedule_mailout" do
+ @message = new_message(:aasm_state => "schedule_mailout")
+ @message.previous!
+ @message.state.should == "select_recipients"
+ end
+
+ it "should transition to edit_content from select_recipients" do
+ @message = new_message(:aasm_state => "select_recipients")
+ @message.previous!
+ @message.state.should == "edit_content"
+ end
+
+ it "should transition to select_html from edit_content if the source is html" do
+ @message = new_message(:aasm_state => "edit_content")
+ @message.source = 'html'
+ @message.previous!
+ @message.state.should == "select_html"
+ end
+
+ it "should transition to select_template from edit_content if the source is template" do
+ @message = new_message(:aasm_state => "edit_content")
+ @message.source = 'template'
+ @message.previous!
+ @message.state.should == "select_template"
+ end
+
+ it "should transition to new from edit_content if the source is plain" do
+ @message = new_message(:aasm_state => "edit_content")
+ @message.source = 'plain'
+ @message.previous!
+ @message.state.should == "new"
+ end
+
+ it "should transition to new from select_html" do
+ @message = new_message(:aasm_state => "select_html")
+ @message.previous!
+ @message.state.should == "new"
+ end
+
+ it "should transition to new from select_template" do
+ @message = new_message(:aasm_state => "select_template")
+ @message.previous!
+ @message.state.should == "new"
+ end
+
+ end
+
end
View
2 spec/models/recipient_spec.rb
@@ -11,7 +11,7 @@
it "should say it is black listed" do
recipient = Factory(:recipient)
recipient.black_list!
- recipient.black_list.should be_true
+ recipient.should be_black_listed
end
end

0 comments on commit a564e76

Please sign in to comment.