Skip to content

Commit a6377c2

Browse files
committed
minor refactoring to ease testing
split rendering similar teams into a separate template refactored #creare and added specs refactored #exact_team_exists? and added specs added shared notification_bar partial and linked to product_description layout added showing notifications on team creation
1 parent 970713d commit a6377c2

11 files changed

+146
-41
lines changed

app/controllers/teams_controller.rb

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,34 @@ def new
7777
end
7878

7979
def create
80-
create_params = params.require(:team).permit(:selected, :slug, :name)
80+
team_params = params.require(:team).permit(:selected, :slug, :name)
8181

82-
@team, confirmed = selected_or_new(create_params)
83-
@teams = Team.any_of({ :name => /#{team_to_regex(@team)}.*/i }).limit(3).to_a unless confirmed
82+
if team_params.fetch(:selected, nil) == 'true'
83+
@team = Team.where(slug: team_params[:slug]).first
84+
render :create and return
85+
end
8486

85-
if @team.valid? and @teams.blank? and @team.new_record?
86-
@team.add_user(current_user)
87-
# @team.edited_by(current_user)
88-
@team.save
87+
@teams = Team.with_similar_names(team_params[:name])
88+
unless @teams.empty?
89+
@new_team_name = team_params[:name]
90+
render 'similar_teams' and return
91+
end
92+
93+
@team = Team.new(name: team_params[:name])
94+
if @team.save
95+
# TODO show flash on success or falure
8996
record_event('created team')
97+
@team.add_user(current_user)
9098
end
9199
end
92100

101+
def get_similar_teams(name)
102+
name.gsub!(/ \-\./, '.*')
103+
#TODO move to Team scope
104+
teams = Team.any_of({ :name => /#{name}.*/i }).limit(3).to_a
105+
teams.empty? ? nil : teams
106+
end
107+
93108
def edit
94109
edit_params = params.permit(:slug, :id)
95110

@@ -299,23 +314,6 @@ def previous_job(job)
299314
Opportunity.with_public_id(public_id) unless public_id.nil?
300315
end
301316

302-
def team_to_regex(team)
303-
team.name.gsub(/ \-\./, '.*')
304-
end
305-
306-
def selected_or_new(opts)
307-
team = Team.new(name: opts[:name])
308-
confirm = false
309-
310-
if opts[:selected]
311-
if opts[:selected] == "true"
312-
team = Team.where(:slug => opts[:slug]).first
313-
end
314-
confirm = true
315-
end
316-
[team, confirm]
317-
end
318-
319317
def ensure_analytics_access
320318
@team = Team.find(params[:id])
321319
return redirect_to team_path(@team) unless (@team.analytics? && @team.admin?(current_user)) || is_admin?

app/helpers/teams_helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ def change_resume_path
163163
settings_path(anchor: 'jobs')
164164
end
165165

166-
def exact_team_exists?(teams, team)
167-
teams.map { |team| Team.slugify(team.name) }.include? team.slug
166+
def exact_team_exists?(teams, name)
167+
teams.map { |team| Team.slugify(team.name) }.include? name
168168
end
169169

170170
def team_connections_links(team)
@@ -194,4 +194,4 @@ def team_twitter_link(team)
194194
end
195195

196196

197-
end
197+
end

app/models/team.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ def with_completed_section(section)
174174
empty = Team.new.send(section).is_a?(Array) ? [] : nil
175175
Team.where(section.to_sym.ne => empty)
176176
end
177+
178+
def with_similar_names(name)
179+
name.gsub!(/ \-\./, '.*')
180+
teams = Team.any_of({ :name => /#{name}.*/i }).limit(3).to_a
181+
end
177182
end
178183

179184
def relevancy
@@ -473,10 +478,11 @@ def sorted_team_members
473478
end
474479

475480
def add_user(user)
476-
user.update_attribute(:team_document_id, id.to_s)
477481
touch!
478-
user.save!
479-
user
482+
user.tap do |u|
483+
u.update_attribute(:team_document_id, id.to_s)
484+
u.save!
485+
end
480486
end
481487

482488
def remove_user(user)

app/views/layouts/product_description.html.haml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
= yield :head
2525
2626
%body#product-description
27+
= render partial: 'shared/notification_bar'
2728
= yield
2829
= render partial: 'shared/footer'
29-
30-
<!-- product_description.html.haml -->
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#main-content
2+
.notification-bar.hidden
3+
.notification-bar-inside{ class: (flash[:error].blank? ? 'notice' : 'error') }
4+
%p= flash[:notice] || flash[:error]
5+
%a.close-notification{ onclick: "$(this).parent().parent().hide();" }
6+
%span Close
7+

app/views/teams/_similar_teams.html.haml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
.team-avatar
88
=image_tag(team.avatar_url)
99
%h4= link_to team.name, teamname_path(:slug => team.slug), :target => :new
10-
= link_to 'Select', teams_path(Team.new, :team => { :slug=> team.slug, :selected => true }), :remote => true, :method => :post, :class => "select"
10+
= link_to 'Select', teams_path(Team.new, :team => { :slug=> team.slug, selected: true }), :remote => true, :method => :post, :class => "select"
1111

12-
- unless exact_team_exists?(@teams, @team)
12+
- unless exact_team_exists?(@teams, @new_team_name)
1313
.just-create-team
1414
%h3 None of the above are my team
15-
= link_to "Create team #{@team.name}", teams_path(@team, :team => { :name => @team.name, :selected => false }), :remote => true, :method => :post, :class => "create-team"
15+
= link_to "Create team #{@new_team_name}", teams_path(Team.new, :team => { :name => @new_team_name, selected: false }), :remote => true, :method => :post, :class => "create-team"

app/views/teams/create.js.erb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<% if @teams.blank? %>
1+
var notificationBar = $('#main-content').find('.notification-bar');
2+
notificationBar.find('.notification-bar-inside').find('p').html("<%= flash[:error] || flash[:notice] %>");
3+
notificationBar.show();
4+
25
$('section.feature:not(.payment)').hide();
36
$('section.feature.payment').append('<%= escape_javascript(render :partial => "payment", :locals => {:account => @team.account || @team.build_account, :plan => @team.account.try(:current_plan)}) %>');
47
$('section.feature.payment').show();
58
setupPayment();
6-
<% else %>
7-
$('section.feature .intro .results').html('<%= escape_javascript(render :partial => 'similar_teams') %>');
8-
$('section.feature:not(.payment)').addClass('find-team');
9-
<% end %>
9+

app/views/teams/similar_teams.js.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
$('section.feature .intro .results').html('<%= escape_javascript(render :partial => 'similar_teams') %>');
2+
$('section.feature:not(.payment)').addClass('find-team');

spec/controllers/teams_controller_spec.rb

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,63 @@
3939
end
4040
end
4141

42-
end
42+
describe "#create" , focus: true do
43+
let(:team) { Fabricate.build(:team) }
44+
45+
it 'renders an error message if action was unsuccessful' do
46+
47+
end
48+
49+
context 'a team is selected from a list of similar teams' do
50+
it 'renders a template with a choice of tariff plans when user selects a team' do
51+
post :create, :team => { selected: 'true', slug: 'team_name' }, format: :js
52+
expect(response).to render_template('create')
53+
end
54+
end
55+
56+
context 'a team does not exist' do
57+
let(:response) { post :create, :team => { name: 'team_name' }, format: :js }
58+
59+
before do
60+
allow(controller).to receive(:get_similar_teams).and_return(nil)
61+
62+
allow(Team).to receive(:new).and_return(team)
63+
allow(team).to receive(:save).and_return(true)
64+
allow(team).to receive(:add_user).and_return(true)
65+
end
66+
67+
it 'creates a new team' do
68+
expect(team).to receive(:save)
69+
response
70+
end
71+
72+
it 'adds current user to the team' do
73+
expect(team).to receive(:add_user).with(current_user)
74+
response
75+
end
76+
77+
it 'records an event "created team"' do
78+
expect(controller).to receive(:record_event).with('created team')
79+
response
80+
end
81+
82+
it 'renders template with option to join' do
83+
expect(response).to be_success
84+
expect(response).to render_template('create')
85+
end
86+
end
87+
88+
context 'a team with similar name already exists' do
89+
before do
90+
allow(Team).to receive(:new).and_return(team)
91+
allow(Team).to receive(:with_similar_names).and_return([ team ])
92+
end
93+
94+
it 'renders a template with a list of similar teams' do
95+
post :create, :team => { name: 'team_name' }, format: :js
96+
expect(response).to render_template('similar_teams')
97+
end
98+
end
99+
end
100+
101+
end

spec/helpers/teams_helper_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe TeamsHelper, :type => :helper do
4+
describe "#exact_team_exists?" do
5+
let(:teams) { Fabricate.build_times(3, :team) }
6+
7+
it "returns true if there is a team with exact matching name" do
8+
teams << Fabricate.build(:team, name: 'test_name', slug: 'test-name')
9+
expect(helper.exact_team_exists?(teams, 'test-name')).to be_truthy
10+
end
11+
12+
it "returns false if there is no team with exact mathcing name" do
13+
expect(helper.exact_team_exists?(teams, 'non-existent team name')).to be_falsey
14+
end
15+
16+
17+
end
18+
end

spec/models/team_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44
let(:team) { Fabricate(:team) }
55
let(:invitee) { Fabricate(:user) }
66

7+
describe '#with_similar_names' do
8+
let!(:team_1) { Fabricate(:team, name: 'dream_team') }
9+
let!(:team_2) { Fabricate(:team, name: 'dream_group') }
10+
let!(:team_3) { Fabricate(:team, name: 'test_team') }
11+
12+
it 'returns teams with similar names' do
13+
result = Team.with_similar_names('team')
14+
expect(result.count).to eq 2
15+
end
16+
17+
it 'returns teams using wildcards' do
18+
result = Team.with_similar_names('dr -.')
19+
expect(result).to include(team_1, team_2)
20+
end
21+
end
22+
723
it 'adds the team id to the user when they are added to a team' do
824
team.add_user(invitee)
925
expect(invitee.reload.team).to eq(team)

0 commit comments

Comments
 (0)