Skip to content

Commit 6f01958

Browse files
committed
minor refactoring
updated fabricators invoicer rendering refactoring
1 parent c8fe78a commit 6f01958

13 files changed

+120
-52
lines changed

app/controllers/accounts_controller.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,15 @@ def webhook
6262
end
6363

6464
def send_invoice
65-
@team = Team.find(params[:team_id])
66-
@team.account.send_invoice_for(1.month.ago)
67-
redirect_to teamname_path(slug: @team.slug), notice: "sent invoice for #{1.month.ago.strftime("%B")} to #{@team.account.admin.email}"
65+
team, period = Team.find(params[:team_id]), 1.month.ago
66+
67+
if team.account.send_invoice_for(period)
68+
flash[:notice] = "sent invoice for #{period.strftime("%B")} to #{team.account.admin.email}"
69+
else
70+
flash[:error] = 'There was an error in sending an invoice'
71+
end
72+
73+
redirect_to teamname_path(slug: team.slug)
6874
end
6975

7076
private

app/helpers/accounts_helper.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,17 @@ def monthly_plan_price(plan)
66
def purchased_plan(plan)
77
plan.nil? ? "Monthly" : plan.name
88
end
9+
10+
def card_for(customer)
11+
card = customer[:active_card] || customer[:cards].first
12+
end
13+
14+
def invoice_date(invoice)
15+
Time.at(invoice[:date]).to_date.to_formatted_s(:long_ordinal)
16+
end
17+
18+
def subscription_period_for(invoice, period)
19+
subscription_period = invoice[:lines][:data].first[:period][period]
20+
Time.at(subscription_period).to_date.to_formatted_s(:long_ordinal)
21+
end
922
end

app/mailers/notifier_mailer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class NotifierMailer < ActionMailer::Base
55
add_template_helper(UsersHelper)
66
add_template_helper(ProtipsHelper)
77
add_template_helper(ApplicationHelper)
8+
add_template_helper(AccountsHelper)
89

910
layout 'email', except: [:weekly_digest, :alert_admin]
1011

app/views/notifier_mailer/invoice.html.haml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
%tr
22
%td.main-content-grey{:style => "padding: 30px 60px; background:#ffffff;font-family:'Helvetica Neue','Helvetica','Arial','sans-serif';"}
33
%p{:style => "font-size: 16px; font-family:'Helvetica Neue','Helvetica','Arial','sans-serif'; margin-bottom: 10px;"}
4-
==Your card has ending in #{@customer[:active_card][:last4]} been charged
4+
="Your card ending in #{card_for(@customer)[:last4]} has been charged"
55
%table{:style => "width:600"}
66
%tr
77
%td
8-
== Bill Date: #{Time.at(@invoice[:date]).strftime("%B %d, %Y")}
8+
== Bill Date: #{invoice_date(@invoice)}
99
%tr
1010
%td
1111
%tr
@@ -20,7 +20,7 @@
2020
%tr
2121
%td
2222
Duration:
23-
==#{Time.at(@invoice[:lines][:subscriptions].first[:period][:start]).strftime("%B %d, %Y")}-#{Time.at(@invoice[:lines][:subscriptions].first[:period][:end]).strftime("%B %d, %Y")}
23+
==#{subscription_period_for(@invoice, :start)}-#{subscription_period_for(@invoice, :end)}
2424
%td
2525
Description:
2626
Enhanced Team Profile (4 job posts anytime)

app/views/subscription_mailer/team_upgrade.html.haml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,3 @@
1616
%p{:style => "font-size: 14px; margin: 0; font-family:'Helvetica Neue','Helvetica','Arial','sans-serif';"}
1717
If you have any questions or concerns about your account, please contact us:
1818
=mail_to('support@coderwall.com', nil, :style => "color:3D8DCC;")
19-
20-
21-

app/views/subscription_mailer/team_upgrade.text.erb

Lines changed: 0 additions & 13 deletions
This file was deleted.

app/views/teams/premium.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
%ul.legend
4646
%li.team-visitors
4747
=link_to 'Visitors', visitors_team_path(@team)
48-
-if is_admin?
48+
-if is_admin? && @team.account
4949
%li.send-invoice
5050
=link_to 'Send Invoice', send_invoice_team_account_path(@team), :method => :post
5151

spec/controllers/accounts_controller_spec.rb

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
RSpec.describe AccountsController, :type => :controller do
2-
let(:team) { Fabricate(:team) }
2+
let(:team) { Fabricate(:team, account: nil) }
33
let(:plan) { Plan.create(amount: 20000, interval: Plan::MONTHLY, name: 'Monthly') }
44
let(:current_user) { Fabricate(:user) }
55

@@ -30,4 +30,38 @@ def valid_params
3030

3131
end
3232
end
33+
34+
describe '#send_inovice' do
35+
before do
36+
team.account = Account.new
37+
38+
allow(Team).to receive(:find) { team }
39+
allow(team.account).to receive(:send_invoice_for) { true }
40+
allow(team.account).to receive(:admin) { current_user }
41+
42+
allow(Time).to receive(:current) { Date.parse('02/11/15').to_time } # so we do not bother with the time portion of the day
43+
end
44+
45+
it 'calls send_invoice for the last month' do
46+
expect(team.account).to receive(:send_invoice_for).with(Date.parse('02/10/15').to_time)
47+
get :send_invoice, id: '123'
48+
end
49+
50+
it 'displays success message' do
51+
get :send_invoice, id: '123'
52+
expect(flash[:notice]).to eq("sent invoice for October to #{current_user.email}")
53+
end
54+
55+
it 'redirects to team profile' do
56+
get :send_invoice, id: '123'
57+
expect(response).to redirect_to(teamname_path(slug: team.slug))
58+
end
59+
60+
it 'displays failure message' do
61+
allow(team.account).to receive(:send_invoice_for) { false }
62+
get :send_invoice, id: '123'
63+
expect(flash[:error]).to eq('There was an error in sending an invoice')
64+
end
65+
66+
end
3367
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
Fabricator(:account) do
2+
name 'test_account'
3+
admin_id 1
4+
stripe_card_token { "tok_14u7LDFs0zmMxCeEU3OGRUa0_#{rand(1000)}" }
5+
stripe_customer_token { "cus_54FsD2W2VkrKpW_#{rand(1000)}" }
26
end

spec/fabricators/plan_fabricator.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
Fabricator(:plan) do
2+
name { sequence(:name) { |i| "plan_no_#{i}" } }
3+
amount { rand * 100 }
24
end
35

46
# == Schema Information

spec/fabricators/team_fabricator.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Fabricator(:team) do
22
name { Faker::Company.name }
3-
end
3+
account { Fabricate.build(:account) }
4+
end

spec/helpers/accounts_helper_spec.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
require 'spec_helper'
22

33
RSpec.describe AccountsHelper, :type => :helper do
4+
it '#invoice_date formats inovoice date from unix time' do
5+
invoice = { date: 1068854400 }
6+
expect(helper.invoice_date(invoice)).to eq('November 15th, 2003')
7+
end
48

5-
end
9+
it '#card_for returns card for a customer' do
10+
customer = { cards: ['test'] }
11+
expect(helper.card_for(customer)).to eq('test')
12+
end
13+
14+
it '#subscription_period returns start and end dates for a subscription in the invoice' do
15+
invoice = {
16+
:lines => {
17+
:data => [ {
18+
:period => {
19+
start: 1005523200,
20+
end: 1351728000
21+
}
22+
}]
23+
}
24+
}
25+
expect(helper.subscription_period_for(invoice, :start)).to eq('November 12th, 2001')
26+
expect(helper.subscription_period_for(invoice, :end)).to eq('November 1st, 2012')
27+
end
28+
end

spec/models/account_spec.rb

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'vcr_helper'
22

33
RSpec.describe Account, :type => :model do
4-
let(:team) { Fabricate(:team) }
4+
let(:team) { Fabricate(:team, account: nil) }
55
let(:account) { { stripe_card_token: new_token } }
66

77
let(:admin) {
@@ -18,7 +18,7 @@
1818
end
1919

2020
def new_token
21-
Stripe::Token.create(card: { number: 4242424242424242, cvc: 224, exp_month: 12, exp_year: 14 }).try(:id)
21+
Stripe::Token.create(card: { number: 4242424242424242, cvc: 224, exp_month: 12, exp_year: 14 }).try(:id)
2222
end
2323

2424
def post_job_for(team)
@@ -29,7 +29,7 @@ def post_job_for(team)
2929

3030
it 'should create a valid account locally and on stripe' do
3131
# TODO: Refactor api calls to Sidekiq job
32-
VCR.use_cassette("Account") do
32+
VCR.use_cassette("Account") do
3333

3434
expect(team.account).to be_nil
3535
team.build_account(account)
@@ -45,8 +45,8 @@ def post_job_for(team)
4545

4646
it 'should still create an account if account admin not team admin' do
4747
# TODO: Refactor api calls to Sidekiq job
48-
VCR.use_cassette("Account") do
49-
48+
VCR.use_cassette("Account") do
49+
5050
team.build_account(account)
5151
some_random_user = Fabricate(:user)
5252
team.account.admin_id = some_random_user.id
@@ -56,7 +56,7 @@ def post_job_for(team)
5656

5757
end
5858
end
59-
59+
6060
# FIXME: This request does not produce the same results out of two calls, under the Account cassette.
6161
# Something is stomping its request.
6262
# 1st call, under record all will pass this test
@@ -77,16 +77,16 @@ def post_job_for(team)
7777

7878
it 'should not allow stripe_customer_token or admin to be set/updated' do
7979
# TODO: Refactor api calls to Sidekiq job
80-
VCR.use_cassette("Account") do
81-
80+
VCR.use_cassette("Account") do
81+
8282
some_random_user = Fabricate(:user)
8383
account[:stripe_customer_token] = "invalid_customer_token"
8484
account[:admin_id] = some_random_user.id
8585
team.build_account(account)
8686
team.account.save_with_payment
8787
team.reload
8888
expect(team.account).to be_nil
89-
89+
9090
end
9191
end
9292
end
@@ -99,15 +99,15 @@ def post_job_for(team)
9999
describe 'free subscription' do
100100
before(:each) do
101101
# TODO: Refactor api calls to Sidekiq job
102-
VCR.use_cassette("Account") do
103-
102+
VCR.use_cassette("Account") do
103+
104104
expect(team.account).to be_nil
105105
team.build_account(account)
106106
team.account.admin_id = admin.id
107107
team.account.save_with_payment
108108
team.account.subscribe_to!(free_plan)
109109
team.reload
110-
110+
111111
end
112112
end
113113

@@ -118,13 +118,13 @@ def post_job_for(team)
118118

119119
it 'should not allow any job posts' do
120120
# TODO: Refactor api calls to Sidekiq job
121-
VCR.use_cassette("Account") do
122-
121+
VCR.use_cassette("Account") do
122+
123123
expect(team.can_post_job?).to eq(false)
124124
expect(team.premium?).to eq(false)
125125
expect(team.valid_jobs?).to eq(false)
126126
expect { Fabricate(:opportunity, team_document_id: team.id) }.to raise_error(ActiveRecord::RecordNotSaved)
127-
127+
128128
end
129129
end
130130

@@ -166,8 +166,8 @@ def post_job_for(team)
166166
describe 'monthly paid subscription' do
167167
before(:each) do
168168
# TODO: Refactor api calls to Sidekiq job
169-
VCR.use_cassette("Account") do
170-
169+
VCR.use_cassette("Account") do
170+
171171
expect(team.account).to be_nil
172172
team.build_account(account)
173173
team.account.admin_id = admin.id
@@ -195,23 +195,23 @@ def post_job_for(team)
195195
Fabricate(:opportunity, team_document_id: team.id)
196196
end
197197
expect(team.can_post_job?).to eq(true)
198-
199-
end
198+
199+
end
200200
end
201201
end
202202

203203
describe 'one-time job post charge' do
204204
before(:each) do
205205
# TODO: Refactor api calls to Sidekiq job
206-
VCR.use_cassette("Account") do
206+
VCR.use_cassette("Account") do
207207

208208
expect(team.account).to be_nil
209209
team.build_account(account)
210210
team.account.admin_id = admin.id
211211
team.account.save_with_payment(onetime_plan)
212212
team.reload
213213

214-
end
214+
end
215215
end
216216
it 'should add a one-time job post charge' do
217217
expect(team.account.plan_ids).to include(onetime_plan.id)
@@ -231,14 +231,14 @@ def post_job_for(team)
231231
expect(team.paid_job_posts).to eq(0)
232232
expect(team.can_post_job?).to eq(false)
233233
expect { Fabricate(:opportunity, team_document_id: team.id) }.to raise_error(ActiveRecord::RecordNotSaved)
234-
234+
235235
end
236236
end
237237

238238
it 'should allow upgrade to monthly subscription' do
239239
# TODO: Refactor api calls to Sidekiq job
240-
VCR.use_cassette("Account") do
241-
240+
VCR.use_cassette("Account") do
241+
242242
team.account.update_attributes({stripe_card_token: new_token})
243243
team.account.save_with_payment(monthly_plan)
244244
team.reload
@@ -258,7 +258,7 @@ def post_job_for(team)
258258

259259
it 'should allow additional one time job post charges' do
260260
# TODO: Refactor api calls to Sidekiq job
261-
VCR.use_cassette("Account") do
261+
VCR.use_cassette("Account") do
262262

263263
team.account.update_attributes({stripe_card_token: new_token})
264264
team.account.save_with_payment(onetime_plan)
@@ -274,7 +274,7 @@ def post_job_for(team)
274274
expect(team.premium?).to eq(true)
275275
expect(team.valid_jobs?).to eq(true)
276276

277-
end
277+
end
278278
end
279279
end
280280
end

0 commit comments

Comments
 (0)