Skip to content

[WIP] Join team error #391 #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 22 additions & 24 deletions app/controllers/teams_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,31 @@ def new
end

def create
create_params = params.require(:team).permit(:selected, :slug, :name)
team_params = params.require(:team).permit(:selected, :slug, :name)
selected = team_params.fetch(:selected, nil)
team_name = team_params.fetch(:name, '')

@team, confirmed = selected_or_new(create_params)
@teams = Team.any_of({ :name => /#{team_to_regex(@team)}.*/i }).limit(3).to_a unless confirmed
@teams = Team.with_similar_names(team_name)

if @team.valid? and @teams.blank? and @team.new_record?
@team.add_user(current_user)
# @team.edited_by(current_user)
@team.save
if selected == 'true'
@team = Team.where(slug: team_params[:slug]).first
render :create and return
end

unless selected == 'false' || @teams.empty?
@new_team_name = team_name
render 'similar_teams' and return
end

@team = Team.new(name: team_name)
if @team.save
record_event('created team')
@team.add_user(current_user)

flash.now[:notice] = "Successfully created a team #{@team.name}"
render :create
else
flash[:error] = "There was an error in creating a team #{@team.name}"
end
end

Expand Down Expand Up @@ -299,23 +314,6 @@ def previous_job(job)
Opportunity.with_public_id(public_id) unless public_id.nil?
end

def team_to_regex(team)
team.name.gsub(/ \-\./, '.*')
end

def selected_or_new(opts)
team = Team.new(name: opts[:name])
confirm = false

if opts[:selected]
if opts[:selected] == "true"
team = Team.where(:slug => opts[:slug]).first
end
confirm = true
end
[team, confirm]
end

def ensure_analytics_access
@team = Team.find(params[:id])
return redirect_to team_path(@team) unless (@team.analytics? && @team.admin?(current_user)) || is_admin?
Expand Down
6 changes: 3 additions & 3 deletions app/helpers/teams_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ def change_resume_path
settings_path(anchor: 'jobs')
end

def exact_team_exists?(teams, team)
teams.map { |team| Team.slugify(team.name) }.include? team.slug
def exact_team_exists?(teams, name)
teams.map { |team| Team.slugify(team.name) }.include? name
end

def team_connections_links(team)
Expand Down Expand Up @@ -194,4 +194,4 @@ def team_twitter_link(team)
end


end
end
12 changes: 9 additions & 3 deletions app/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ def with_completed_section(section)
empty = Team.new.send(section).is_a?(Array) ? [] : nil
Team.where(section.to_sym.ne => empty)
end

def with_similar_names(name)
name.gsub!(/ \-\./, '.*')
teams = Team.any_of({ :name => /#{name}.*/i }).limit(3).to_a
end
end

def relevancy
Expand Down Expand Up @@ -473,10 +478,11 @@ def sorted_team_members
end

def add_user(user)
user.update_attribute(:team_document_id, id.to_s)
touch!
user.save!
user
user.tap do |u|
u.update_attribute(:team_document_id, id.to_s)
u.save!
end
end

def remove_user(user)
Expand Down
3 changes: 1 addition & 2 deletions app/views/layouts/product_description.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
= yield :head

%body#product-description
= render partial: 'shared/notification_bar'
= yield
= render partial: 'shared/footer'

<!-- product_description.html.haml -->
7 changes: 7 additions & 0 deletions app/views/shared/_notification_bar.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#main-content
.notification-bar.hidden
.notification-bar-inside{ class: (flash[:error].blank? ? 'notice' : 'error') }
%p= flash[:notice] || flash[:error]
%a.close-notification{ onclick: "$(this).parent().parent().hide();" }
%span Close

6 changes: 3 additions & 3 deletions app/views/teams/_similar_teams.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
.team-avatar
=image_tag(team.avatar_url)
%h4= link_to team.name, teamname_path(:slug => team.slug), :target => :new
= link_to 'Select', teams_path(Team.new, :slug=> team.slug, :selected => true), :remote => true, :method => :post, :class => "select"
= link_to 'Select', teams_path(Team.new, :team => { :slug=> team.slug, selected: true }), :remote => true, :method => :post, :class => "select"

- unless exact_team_exists?(@teams, @team)
- unless exact_team_exists?(@teams, @new_team_name)
.just-create-team
%h3 None of the above are my team
= link_to "Create team #{@team.name}", teams_path(@team, :team => {:name => @team.name, :selected => false}), :remote => true, :method => :post, :class => "create-team"
= 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"
10 changes: 5 additions & 5 deletions app/views/teams/create.js.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<% if @teams.blank? %>
var notificationBar = $('#main-content').find('.notification-bar');
notificationBar.find('.notification-bar-inside').find('p').html("<%= flash[:error] || flash[:notice] %>");
notificationBar.show();

$('section.feature:not(.payment)').hide();
$('section.feature.payment').append('<%= escape_javascript(render :partial => "payment", :locals => {:account => @team.account || @team.build_account, :plan => @team.account.try(:current_plan)}) %>');
$('section.feature.payment').show();
setupPayment();
<% else %>
$('section.feature .intro .results').html('<%= escape_javascript(render :partial => 'similar_teams') %>');
$('section.feature:not(.payment)').addClass('find-team');
<% end %>

2 changes: 2 additions & 0 deletions app/views/teams/similar_teams.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$('section.feature .intro .results').html('<%= escape_javascript(render :partial => 'similar_teams') %>');
$('section.feature:not(.payment)').addClass('find-team');
76 changes: 75 additions & 1 deletion spec/controllers/teams_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,78 @@
end
end

end
describe "#create" do
let(:team) { Fabricate.build(:team, name: 'team_name') }

before do
allow(Team).to receive(:with_similar_names).and_return([])
end

context 'a team is selected from a list of similar teams' do
it 'renders a template with a choice of tariff plans when user picks a name from existing names' do
allow(Team).to receive(:where).and_return(['team_1', 'team_2'])
post :create, :team => { selected: 'true', slug: 'team_name' }, format: :js

expect(assigns[:team]).to eq('team_1')
expect(response).to render_template('create')
end

it 'renders a template with a choice of tariff plans if user picks his own team name' do
post :create, :team => { name: 'team_name', selected: 'false' }, format: :js
expect(response).to render_template('create')
end
end

context 'a team does not exist' do
let(:response) { post :create, :team => { name: 'team_name' }, format: :js }

before do
allow(Team).to receive(:new).and_return(team)
allow(team).to receive(:save).and_return(true)
allow(team).to receive(:add_user).and_return(true)
end

it 'creates a new team' do
expect(team).to receive(:save)
response
end

it 'adds current user to the team' do
expect(team).to receive(:add_user).with(current_user)
response
end

it 'records an event "created team"' do
expect(controller).to receive(:record_event).with('created team')
response
end

it 'renders template with option to join' do
expect(response).to be_success
expect(response).to render_template('create')
expect(flash[:notice]).to eq("Successfully created a team team_name")
end

it 'renders failure notice' do
allow(team).to receive(:save).and_return(false)
response
expect(flash[:error]).to eq("There was an error in creating a team team_name")
end
end

context 'a team with similar name already exists' do
before do
allow(Team).to receive(:new).and_return(team)
allow(Team).to receive(:with_similar_names).and_return([ team ])
end

it 'renders a template with a list of similar teams' do
post :create, :team => { name: 'team_name' }, format: :js

expect(assigns[:new_team_name]).to eq('team_name')
expect(response).to render_template('similar_teams')
end
end
end

end
18 changes: 18 additions & 0 deletions spec/helpers/teams_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'spec_helper'

RSpec.describe TeamsHelper, :type => :helper do
describe "#exact_team_exists?" do
let(:teams) { Fabricate.build_times(3, :team) }

it "returns true if there is a team with exact matching name" do
teams << Fabricate.build(:team, name: 'test_name', slug: 'test-name')
expect(helper.exact_team_exists?(teams, 'test-name')).to be_truthy
end

it "returns false if there is no team with exact mathcing name" do
expect(helper.exact_team_exists?(teams, 'non-existent team name')).to be_falsey
end


end
end
16 changes: 16 additions & 0 deletions spec/models/team_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@
let(:team) { Fabricate(:team) }
let(:invitee) { Fabricate(:user) }

describe '#with_similar_names' do
let!(:team_1) { Fabricate(:team, name: 'dream_team') }
let!(:team_2) { Fabricate(:team, name: 'dream_group') }
let!(:team_3) { Fabricate(:team, name: 'test_team') }

it 'returns teams with similar names' do
result = Team.with_similar_names('team')
expect(result.count).to eq 2
end

it 'returns teams using wildcards' do
result = Team.with_similar_names('dr -.')
expect(result).to include(team_1, team_2)
end
end

it 'adds the team id to the user when they are added to a team' do
team.add_user(invitee)
expect(invitee.reload.team).to eq(team)
Expand Down