From a4eaad33d469e4bc18f04d75b922fbc71c46110a Mon Sep 17 00:00:00 2001 From: "Max F. Albrecht" <1@178.is> Date: Wed, 15 Jul 2015 19:41:26 +0200 Subject: [PATCH] my: groups: create: error if name already exists - controller: error if name already exists (JSON) - js model: handle ajax errors - js controller: render ajax errors --- .../groups_controller_create.coffee | 16 +++++---- app/assets/javascripts/models/group.coffee | 3 ++ app/controllers/groups_controller.rb | 10 +++++- spec/features/workgroups_spec.rb | 36 ++++++++++++------- 4 files changed, 46 insertions(+), 19 deletions(-) diff --git a/app/assets/javascripts/controllers/groups_controller_create.coffee b/app/assets/javascripts/controllers/groups_controller_create.coffee index a43942cbf6..b789baed7a 100644 --- a/app/assets/javascripts/controllers/groups_controller_create.coffee +++ b/app/assets/javascripts/controllers/groups_controller_create.coffee @@ -20,14 +20,18 @@ class GroupsController.Create e.preventDefault() group = new App.Group name: @form.find("[name='name']").val() - if group.validate() - @el.remove() - group.create => document.location.reload true - else - App.DialogErrors.set @form, group.errors + unless group.validate() + return App.DialogErrors.set @form, group.errors + + group.create (response)=> + if (err = response.error)? + App.DialogErrors.set(@form, [{text: err}]) + else + do document.location.reload + render: -> @el = App.render "groups/create" - + window.App.GroupsController = {} unless window.App.GroupsController window.App.GroupsController.Create = GroupsController.Create diff --git a/app/assets/javascripts/models/group.coffee b/app/assets/javascripts/models/group.coffee index e48f2b2b74..efe69b8ee8 100644 --- a/app/assets/javascripts/models/group.coffee +++ b/app/assets/javascripts/models/group.coffee @@ -41,6 +41,9 @@ class Group success: (data)=> @refreshData data callback(data) if callback? + error: (xhr, status)=> + err = xhr?.responseJSON?.error || 'Error' + callback({error: err}) if callback? delete: (callback)-> $.ajax diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 5da5137d93..d0e399eca5 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -36,9 +36,17 @@ def show end def create(name = params[:name] || raise("Name has to be present.")) - group = current_user.groups.create(:name => name) respond_to do |format| format.json { + + if Group.find_by(:name => name).present? + err = "Eine Gruppe mit diesem Namen existiert bereits!" + render(json: {:error => err}, :status => :bad_request) \ + and return + end + + group = current_user.groups.create!(:name => name) + if group.persisted? render json: view_context.json_for(group) else diff --git a/spec/features/workgroups_spec.rb b/spec/features/workgroups_spec.rb index 9b8f45655a..0358b711d5 100644 --- a/spec/features/workgroups_spec.rb +++ b/spec/features/workgroups_spec.rb @@ -7,20 +7,26 @@ @current_user = sign_in_as "normin" end - scenario "Create a new group", browser: :firefox do - - visit my_groups_path - - create_new_group_with_context_primary_action - - name = Faker::Name.last_name - find("input[name='name']").set name - - click_primary_action_of_modal + feature "Create a new group", browser: :firefox do + it "is OK with new name" do + name = Faker::Name.last_name + visit my_groups_path + + create_group(name) + + assert_modal_not_visible + expect(@current_user.groups.find_by_name(name)).to be + end + it "FAILS when name already exists" do + name = Faker::Name.last_name + visit my_groups_path - assert_modal_not_visible - expect(@current_user.groups.find_by_name(name)).to be + create_group(name) + assert_modal_not_visible + create_group(name) + assert_error_alert + end end scenario "Requiring name during group creation", browser: :firefox do @@ -119,6 +125,12 @@ def create_new_group_with_context_primary_action assert_modal_visible end + def create_group(name) + create_new_group_with_context_primary_action + find("input[name='name']").set name + click_primary_action_of_modal + end + def edit_one_group @group = @current_user.groups.first find(".ui-workgroups tr[data-id='#{@group.id}'] .button.edit-workgroup").click