Skip to content

Commit

Permalink
my: groups: create: error if name already exists
Browse files Browse the repository at this point in the history
- controller: error if name already exists (JSON)
- js model: handle ajax errors
- js controller: render ajax errors
  • Loading branch information
eins78 committed Jul 15, 2015
1 parent 6804f5a commit a4eaad3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
16 changes: 10 additions & 6 deletions app/assets/javascripts/controllers/groups_controller_create.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions app/assets/javascripts/models/group.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 24 additions & 12 deletions spec/features/workgroups_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a4eaad3

Please sign in to comment.