Created
October 5, 2012 15:06
-
-
Save jondkinney/3840366 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InvestmentCreationService | |
def initialize(investment, current_user) | |
@investment = investment | |
@current_user = current_user | |
end | |
def save | |
if @investment.save | |
log_activity | |
add_milestone_to_activity_stream | |
send_mail | |
end | |
end | |
def log_activity | |
Activity.log('investment', @investment.campaign, @investment.investor, @investment) | |
end | |
def add_milestone_to_activity_stream | |
@investment.campaign.write_mile_stone_message_if_capital_hit_the_line | |
end | |
def send_mail | |
state = @investment.investor.state | |
if @investment.campaign.nearing_max_unaccredited_investors_for_state?(state, 5) | |
Mailer.nearing_max_unaccredited_investors_alert(@investment.campaign.id, state).deliver | |
end | |
Mailer.commitment_alert(@investment.id).deliver | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe InvestmentCreationService do | |
before do | |
@company = FactoryGirl.build_stubbed(:company) | |
@campaign = FactoryGirl.build_stubbed(:campaign, amount: 10, company: @company) | |
@investor_user = FactoryGirl.build_stubbed(:investor_user) | |
State.any_instance.stub(:max_unaccredited).and_return(2) | |
@investment = FactoryGirl.build_stubbed(:investment, capital: 2, campaign: @campaign, investor: @investor_user) | |
@inv_cre_ser = InvestmentCreationService.new(@investment, @investor_user) | |
Campaign.stub(:find).and_return(@campaign) | |
Investment.stub(:find).and_return(@investment) | |
@investment.investor.stub(:display_name).and_return("Stubby Investor") | |
end | |
context "the investment saves successfully" do | |
before do | |
@investment.stub(:save).and_return(true) | |
end | |
it "should log the investment activity" do | |
Activity.should_receive(:log).exactly(1).times | |
@inv_cre_ser.save | |
end | |
it "should add a milestone to the activity stream if the amount of capital crosses a funding percentage threshold" do | |
@campaign.stub(:amount).and_return(100) | |
@investment.stub(:sum).and_return(25) | |
@campaign.investments.stub(:active).and_return(@investment) | |
Activity.should_receive(:log).with('investment', @investment.campaign, @investment.investor, @investment).exactly(1).times | |
Activity.should_receive(:log).with('milestone_message_25', @campaign).exactly(1).times | |
@inv_cre_ser.save | |
end | |
before do | |
@mailer = mock('Mailer', deliver: true) | |
end | |
it "should send out a mail message when there is an investment" do | |
Mailer.should_receive(:commitment_alert).with(@investment.id).and_return(@mailer) | |
@inv_cre_ser.save | |
end | |
it "should send out a mail message when there is an investment that makes the number of unaccredited investors in a campaign be within 5 from the max for the given state" do | |
Mailer.should_receive(:nearing_max_unaccredited_investors_alert).with(@campaign.id, @investor_user.address.state).and_return(@mailer) | |
@inv_cre_ser.save | |
end | |
end | |
context "the investment doesn't save successfully" do | |
before do | |
@investment.stub(:save).and_return(false) | |
@campaign.stub(:amount).and_return(100) | |
@investment.stub(:sum).and_return(25) | |
@campaign.investments.stub(:active).and_return(@investment) | |
end | |
it "should not log a new investment or create any milestones" do | |
Activity.should_not_receive(:log) | |
@inv_cre_ser.save | |
end | |
it "should not send a notification about receiving a commitment" do | |
Mailer.should_not_receive(:commitment_alert) | |
@inv_cre_ser.save | |
end | |
it "should not send a notification about nearing the max unaccredited investor alert" do | |
Mailer.should_not_receive(:nearing_max_unaccredited_investors_alert) | |
@inv_cre_ser.save | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment