Skip to content

Commit

Permalink
implement groups AJAX crud
Browse files Browse the repository at this point in the history
  • Loading branch information
rilian committed Oct 6, 2013
1 parent 9ffc590 commit 7bdf8b1
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 39 deletions.
19 changes: 10 additions & 9 deletions app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class GroupsController < ApplicationController
before_filter :load_groups

def index
@groups = Group.all
end

def new
Expand All @@ -11,9 +12,7 @@ def create
@group = Group.new(group_params)

if @group.save
redirect_to groups_path
else
render :new
@groups = Group.all
end
end

Expand All @@ -25,21 +24,23 @@ def update
@group = Group.find(params[:id])

if @group.update(group_params)
redirect_to groups_path
else
render :edit
@groups = Group.all
end
end

def destroy
Group.find(params[:id]).delete

redirect_to groups_path
@groups = Group.all
end

private

def group_params
params.require(:group).permit(:name, :surname, :email, :phone, :group_id)
end

def load_groups
@groups = Group.all
@group = Group.new
end
end
10 changes: 9 additions & 1 deletion app/views/groups/_form.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<%= form_for @group do |f| %>
<h2>
<%= @group.persisted? ? 'Update Group' : 'Create Group' %>
</h2>

<%= form_for @group, remote: true do |f| %>
<div id="group_errors">
<%= f.object.errors.full_messages.join('. ') if f.object && f.object.errors.any? %>
</div>

<div>
<label for="group_name">Name
<%= f.text_field :name %>
Expand Down
25 changes: 25 additions & 0 deletions app/views/groups/_groups.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h2>
All Groups
</h2>

<table>
<tbody>
<tr>
<th>#</th>
<th>Name</th>
<th>Actions</th>
</tr>
<% @groups.each do |group| %>
<tr>
<td><%= group.id %></td>
<td><%= group.name %></td>
<td>
<%= link_to 'Edit', edit_group_path(group), remote: true %>
<%= link_to 'Delete', group_path(group), method: :delete, remote: true, data: { confirm: 'Are you sure ?' } %>
</td>
</tr>
<% end %>
</tbody>
</table>

<%= link_to 'Create Group', new_group_path, remote: true %>
7 changes: 7 additions & 0 deletions app/views/groups/create.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<% if @group.errors.any? %>
$("#group_errors").html("<%= escape_javascript(@group.errors.full_messages.join('; '))%>");
<% else %>
$("#group_errors").hide();
$("#entity_form").hide();
$("#entities_list").html("<%= escape_javascript(render('groups')) %>");
<% end %>
1 change: 1 addition & 0 deletions app/views/groups/destroy.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$("#entities_list").html("<%= escape_javascript(render('groups')) %>");
2 changes: 2 additions & 0 deletions app/views/groups/edit.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$("#entity_form").show();
$("#entity_form").html("<%= escape_javascript(render('form'))%>");
26 changes: 6 additions & 20 deletions app/views/groups/index.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
<h1>Groups</h1>
<div id="entities_list">
<%= render 'groups' %>
</div>

<%= link_to 'Create Group', new_group_path %>

<table>
<tr>
<th>#</th>
<th>Name</th>
<th>Actions</th>
</tr>
<% @groups.each do |group| %>
<tr>
<td><%= group.id %></td>
<td><%= group.name %></td>
<td>
<%= link_to 'Edit', edit_group_path(group) %>
<%= link_to 'Delete', group_path(group), method: :delete %>
</td>
</tr>
<% end %>
</table>
<div id="entity_form">
<%= render 'form' %>
</div>
3 changes: 3 additions & 0 deletions app/views/groups/index.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$("#group_errors").hide();
$("#entity_form").hide();
$("#entities_list").html("<%= escape_javascript(render('groups')) %>");
2 changes: 2 additions & 0 deletions app/views/groups/new.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$("#entity_form").show();
$("#entity_form").html("<%= escape_javascript(render('form'))%>");
6 changes: 6 additions & 0 deletions app/views/groups/update.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<% if @group.errors.any? %>
$("#group_errors").html("<%= escape_javascript(@group.errors.full_messages.join('; '))%>");
<% else %>
$("#entity_form").hide();
$("#entities_list").html("<%= escape_javascript(render('groups')) %>");
<% end %>
18 changes: 9 additions & 9 deletions spec/controllers/groups_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
it 'renders existing groups' do
get :index

response.status.should == 200
expect(response.status).to eq 200
expect(assigns(:groups)).to match_array([@group_1, @group_2])
end
end
Expand All @@ -19,16 +19,16 @@
it 'assigns empty Group' do
get :new

response.status.should eq 200
expect(response.status).to eq 200
expect(assigns(:group).class).to eq Group
end
end

describe 'POST create' do
it 'creates Group' do
post :create, { group: { name: 'a' } }
xhr :post, :create, { group: { name: 'a' } }

response.should redirect_to groups_path
expect(response.status).to eq 200
expect(Group.count).to eq 1
expect(Group.first.name).to eq 'a'
end
Expand All @@ -42,7 +42,7 @@
it 'assigns existing Group' do
get :edit, id: @group.id

response.status.should eq 200
expect(response.status).to eq 200
expect(assigns(:group)).to eq @group
end
end
Expand All @@ -53,9 +53,9 @@
end

it 'updates Group' do
put :update, { id: @group.id, group: { name: 'b' } }
xhr :put, :update, { id: @group.id, group: { name: 'b' } }

response.should redirect_to groups_path
expect(response.status).to eq 200
expect(Group.count).to eq 1
expect(Group.first.name).to eq 'b'
end
Expand All @@ -67,9 +67,9 @@
end

it 'deletes Group' do
delete :destroy, { id: @group.id }
xhr :delete, :destroy, { id: @group.id }

response.should redirect_to groups_path
expect(response.status).to eq 200
expect(Group.count).to eq 0
end
end
Expand Down

0 comments on commit 7bdf8b1

Please sign in to comment.