Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
js: fixed team name validation behaviour
Browse files Browse the repository at this point in the history
Previously the inline validation was being done only over the scoped
teams current user had access too without their hidden team.

This way when the user tried to create a team with their own hidden name
or another user's name, an error happened.

With this patch the error appears inline as we expect from validation.

I also changed the polyfills syntax import.
  • Loading branch information
vitoravelino committed Nov 22, 2017
1 parent a7375d9 commit 86e72f8
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/assets/javascripts/modules/teams/components/new-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default {
Alert.show(`Team '${team.name}' was created successfully`);
EventBus.$emit('teamCreated', team);
}).catch((response) => {
let errors = response.data;
let errors = response.data.errors || response.data.error;

if (Array.isArray(errors)) {
errors = errors.join('<br />');
Expand All @@ -65,7 +65,7 @@ export default {

return new Promise((resolve) => {
const searchTeam = () => {
const promise = TeamsService.exists(value);
const promise = TeamsService.exists(value, { unscoped: true });

promise.then((exists) => {
// leave it for the back-end
Expand Down
12 changes: 7 additions & 5 deletions app/assets/javascripts/modules/teams/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const customActions = {
},
};

const resource = Vue.resource('api/v1/teams{/id}', {}, customActions);
const resource = Vue.resource('api/v1/teams{/id}{?hidden}', {}, customActions);

function all(params = {}) {
return resource.get({}, params);
Expand All @@ -24,12 +24,14 @@ function save(team) {
return resource.save({}, team);
}

function searchTeam(teamName) {
return resource.teamTypeahead({ teamName });
function searchTeam(teamName, options = {}) {
const params = Object.assign({ teamName }, options);

return resource.teamTypeahead(params);
}

function exists(value) {
return searchTeam(value)
function exists(value, options) {
return searchTeam(value, options)
.then((response) => {
const collection = response.data;

Expand Down
7 changes: 4 additions & 3 deletions app/assets/javascripts/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('core-js/fn/array/some');
require('core-js/fn/array/from');
require('core-js/fn/array/find-index');
import 'core-js/fn/array/some';
import 'core-js/fn/array/from';
import 'core-js/fn/array/find-index';
import 'core-js/fn/object/assign';
3 changes: 2 additions & 1 deletion app/controllers/teams_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def typeahead
# GET /teams/typeahead/%QUERY
def all_with_query
query = "#{params[:query]}%"
teams = policy_scope(Team).where("name LIKE ?", query).pluck(:name)
teams = params[:unscoped] == "true" ? Team.all : policy_scope(Team)
teams = teams.where("name LIKE ?", query).pluck(:name)
matches = teams.map { |t| { name: ActionController::Base.helpers.sanitize(t) } }
respond_to do |format|
format.json { render json: matches.to_json }
Expand Down
2 changes: 1 addition & 1 deletion app/views/teams/components/_form.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ div ref="form" class="collapse"
span v-if="!$v.team.name.required"
| Name can't be blank
span v-if="!$v.team.name.available"
| Name has already been taken
| Name is reserved or has already been taken
.form-group
= f.label :description, {class: 'control-label col-md-2'}
.col-md-7
Expand Down
2 changes: 1 addition & 1 deletion spec/features/teams_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
fill_in "Name", with: Team.last.name
wait_for_ajax

expect(page).to have_content("Name has already been taken")
expect(page).to have_content("Name is reserved or has already been taken")
expect(page).to have_button("Add", disabled: true)
end

Expand Down

0 comments on commit 86e72f8

Please sign in to comment.