Skip to content

Commit ccc3c03

Browse files
YaroSpacejust3ws
authored andcommitted
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 6bb8e12 commit ccc3c03

File tree

2 files changed

+20
-35
lines changed

2 files changed

+20
-35
lines changed

app/controllers/teams_controller.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,33 +79,33 @@ def new
7979

8080
def create
8181
team_params = params.require(:team).permit(:selected, :slug, :name)
82-
selected = team_params.fetch(:selected, nil)
83-
team_name = team_params.fetch(:name, '')
8482

85-
@teams = Team.with_similar_names(team_name)
86-
87-
if selected == 'true'
83+
if team_params.fetch(:selected, nil) == 'true'
8884
@team = Team.where(slug: team_params[:slug]).first
8985
render :create and return
9086
end
9187

92-
unless selected == 'false' || @teams.empty?
93-
@new_team_name = team_name
88+
@teams = Team.with_similar_names(team_params[:name])
89+
unless @teams.empty?
90+
@new_team_name = team_params[:name]
9491
render 'similar_teams' and return
9592
end
9693

97-
@team = Team.new(name: team_name)
94+
@team = Team.new(name: team_params[:name])
9895
if @team.save
96+
# TODO show flash on success or falure
9997
record_event('created team')
10098
@team.add_user(current_user)
101-
102-
flash.now[:notice] = "Successfully created a team #{@team.name}"
103-
render :create
104-
else
105-
flash[:error] = "There was an error in creating a team #{@team.name}"
10699
end
107100
end
108101

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

spec/controllers/teams_controller_spec.rb

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,16 @@
3939
end
4040
end
4141

42-
describe "#create" do
43-
let(:team) { Fabricate.build(:team, name: 'team_name') }
42+
describe "#create" , focus: true do
43+
let(:team) { Fabricate.build(:team) }
44+
45+
it 'renders an error message if action was unsuccessful' do
4446

45-
before do
46-
allow(Team).to receive(:with_similar_names).and_return([])
4747
end
4848

4949
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 picks a name from existing names' do
51-
allow(Team).to receive(:where).and_return(['team_1', 'team_2'])
50+
it 'renders a template with a choice of tariff plans when user selects a team' do
5251
post :create, :team => { selected: 'true', slug: 'team_name' }, format: :js
53-
54-
expect(assigns[:team]).to eq('team_1')
55-
expect(response).to render_template('create')
56-
end
57-
58-
it 'renders a template with a choice of tariff plans if user picks his own team name' do
59-
post :create, :team => { name: 'team_name', selected: 'false' }, format: :js
6052
expect(response).to render_template('create')
6153
end
6254
end
@@ -65,6 +57,8 @@
6557
let(:response) { post :create, :team => { name: 'team_name' }, format: :js }
6658

6759
before do
60+
allow(controller).to receive(:get_similar_teams).and_return(nil)
61+
6862
allow(Team).to receive(:new).and_return(team)
6963
allow(team).to receive(:save).and_return(true)
7064
allow(team).to receive(:add_user).and_return(true)
@@ -88,13 +82,6 @@
8882
it 'renders template with option to join' do
8983
expect(response).to be_success
9084
expect(response).to render_template('create')
91-
expect(flash[:notice]).to eq("Successfully created a team team_name")
92-
end
93-
94-
it 'renders failure notice' do
95-
allow(team).to receive(:save).and_return(false)
96-
response
97-
expect(flash[:error]).to eq("There was an error in creating a team team_name")
9885
end
9986
end
10087

@@ -106,8 +93,6 @@
10693

10794
it 'renders a template with a list of similar teams' do
10895
post :create, :team => { name: 'team_name' }, format: :js
109-
110-
expect(assigns[:new_team_name]).to eq('team_name')
11196
expect(response).to render_template('similar_teams')
11297
end
11398
end

0 commit comments

Comments
 (0)