Skip to content

Commit

Permalink
added basic relations between user and group
Browse files Browse the repository at this point in the history
  • Loading branch information
slk187 committed May 1, 2013
1 parent e4755b5 commit c0677aa
Show file tree
Hide file tree
Showing 20 changed files with 85 additions and 70 deletions.
4 changes: 3 additions & 1 deletion app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def edit

def show
@group = Group.find(params[:id])

@gtr = Usertogroup.where("group_id = ?", params[:id]).select("user_id").first
@x = User.where("id = ?", @gtr.user_id).select("email").first

respond_to do |format|
format.html # show.html.erb
format.json { render json: @group }
Expand Down
5 changes: 1 addition & 4 deletions app/controllers/grouptorepos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ def create
render "new"
end
end

def show
end


def destroy
@grouptorepo = Grouptorepo.find(params[:repository_id])
@grouptorepo.destroy
Expand Down
46 changes: 46 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,50 @@ def create
render "new"
end
end

def index
@user = User.all

respond_to do |format|
format.html # index.html.erb
format.json { render json: @user }
end
end

def destroy
@user = User.find(params[:id])
@user.destroy

respond_to do |format|
format.html { redirect_to users_path }
format.json { head :no_content }
end
end
def edit
@user = User.find(params[:id])
end

def show
@user = User.find(params[:id])
@gtr = Usertogroup.where("user_id = ?", params[:id]).select("group_id").first
@x = Group.where("id = ?", @gtr.group_id).select("group_name").first
respond_to do |format|
format.html # show.html.erb
format.json { render json: @user }
end
end

def update
@user = User.find(params[:id])

respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to users_path, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
end
22 changes: 0 additions & 22 deletions app/controllers/usertogroup_controller.rb

This file was deleted.

3 changes: 3 additions & 0 deletions app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ class Group < ActiveRecord::Base
attr_accessible :group_name, :description
has_many :grouptorepos#, :dependent => :destroy
has_many :repositories, through: :grouptorepos

has_many :usertogroups#, :dependent => :destroy
has_many :users, through: :usertogroups
end
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ def encrypt_password
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end

has_many :usertogroups, :dependent => :destroy
has_many :groups, through: :usertogroups
end
1 change: 1 addition & 0 deletions app/views/groups/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</p>
<p>
<b>Members:</b><br/>
<%= @x.email %>
</p>

<%= link_to 'Edit', edit_group_path(@group) %> |
Expand Down
8 changes: 8 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,16 @@
groups_path %>
<ul>
<li><%= link_to "Create", new_group_path %></li>
<li><%= link_to "Asign user to group", new_usertogroup_path %></li>
</ul>
</li>
<li class='has-sub'><%= link_to "User",
users_path %>
<ul>
<li><%= link_to "Create", new_user_path %></li>
</ul>
</li>

<li class="last"><%= link_to "About", root_path %></li>
</ul>
</div>
Expand Down
17 changes: 7 additions & 10 deletions app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
<%= form_for(@group) do |f| %>
<% if @group.errors.any? %>
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@group.errors.count, "error") %> prohibited this repository from being saved:</h2>
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

<ul>
<% @group.errors.full_messages.each do |msg| %>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :group_name %><br />
<%= f.text_field :group_name %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_field :description, :rows => "5" %>
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<% #tu trzeba dopisac edytowanie hasla %>
<div class="actions">
<%= f.submit %>
</div>
Expand Down
6 changes: 3 additions & 3 deletions app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1>Editing group</h1>
<h1>Editing user</h1>

<%= render 'form' %>

<%= link_to 'Show', @group %> |
<%= link_to 'Back', groups_path %>
<%= link_to 'Show', @user %> |
<%= link_to 'Back', users_path %>
10 changes: 5 additions & 5 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<h1>Listing groups</h1>
<h1>Listing users</h1>

<table>
<tr>
<th>Name</th>
<th></th>
</tr>

<% @groups.each do |f| %>
<% @user.each do |f| %>
<tr>
<td><%= f.group_name %></td>
<td><%= f.email %></td>
<td><%= link_to 'Show', f %></td>
<td><%= link_to 'Edit', edit_group_path(f) %></td>
<td><%= link_to 'Edit', edit_user_path(f) %></td>
<td><%= link_to 'Destroy', f, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New Group', new_group_path %>
<%= link_to 'New User', new_user_path %>
15 changes: 4 additions & 11 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
<p id="notice"><%= notice %></p>

<p>
<b>Group name:</b><br/>
<%= @group.group_name %>
</p>
<p>
<b>Description</b><br/>
<%= @group.description %>
</p>
<p>
<b>Members:</b><br/>
<b>User name:</b><br/>
<%= @user.email %>
</p>

<%= link_to 'Edit', edit_group_path(@group) %> |
<%= link_to 'Back', groups_path %>
<%= link_to 'Edit', edit_user_path(@user) %> |
<%= link_to 'Back', users_path %>
2 changes: 0 additions & 2 deletions app/views/usertogroup/create.html.erb

This file was deleted.

2 changes: 0 additions & 2 deletions app/views/usertogroup/destroy.html.erb

This file was deleted.

2 changes: 0 additions & 2 deletions app/views/usertogroup/edit.html.erb

This file was deleted.

2 changes: 0 additions & 2 deletions app/views/usertogroup/index.html.erb

This file was deleted.

2 changes: 0 additions & 2 deletions app/views/usertogroup/new.html.erb

This file was deleted.

2 changes: 0 additions & 2 deletions app/views/usertogroup/show.html.erb

This file was deleted.

2 changes: 0 additions & 2 deletions app/views/usertogroup/update.html.erb

This file was deleted.

1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
resources :repositories
resources :groups
resources :grouptorepos
resources :usertogroups

# The priority is based upon order of creation:
# first created -> highest priority.
Expand Down

0 comments on commit c0677aa

Please sign in to comment.