Skip to content

Commit

Permalink
Add teams page (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
canan8 authored Jun 21, 2020
1 parent 3379403 commit 835ea95
Show file tree
Hide file tree
Showing 39 changed files with 531 additions and 25 deletions.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/retros.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the retros controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
class ApplicationController < ActionController::Base
before_action :authenticate_user!
helper_method :current_user_teams

def current_user_teams
current_user.teams
end
end
29 changes: 29 additions & 0 deletions app/controllers/retros_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class RetrosController < ApplicationController
before_action :find_retro, only: [:show]

def new
end

def create
@retro = Retro.new(retro_params)
if @retro.save
redirect_to @retro
else
flash[:error] = 'Retro session could not be created.'
redirect_to new_retro_path
end
end

def show
end

private

def retro_params
params.require(:retro).permit(:team_id, :date)
end

def find_retro
@retro = Retro.find(params[:id])
end
end
86 changes: 85 additions & 1 deletion app/controllers/teams_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,89 @@
class TeamsController < ApplicationController
before_action :find_team, except: [:index]
before_action :team_users, only: [:show, :edit]

def index
@teams = Team.all
end

def show
end

def edit
end

def update
begin
@team.update!(team_params)
flash[:success] = 'Team was successfully updated.'
redirect_to team_path
rescue StandardError => e
flash[:error] = 'Oops, please try again.'
reload
end
end

def destroy
begin
@team.destroy!
flash[:success] = 'Team is gone forever.'
redirect_to teams_path
rescue StandardError => e
flash[:error] = 'Oops, please try again.'
reload
end
end

def add_user
company = @team.company

begin
user = company.users.find_by!(email: params[:user][:email])
raise UserAlreadyPresentError if user.already_present?(@team)

@team.users << user
flash[:success] = 'Say hi to your new team member!'
rescue ActiveRecord::RecordNotFound => e
flash[:error] = 'User could not been found in your company.'
rescue UserAlreadyPresentError
flash[:error] = "You can only have one #{user.name}."
rescue StandardError => e
flash[:error] = 'Oops, please try again.'
end

reload
end

def remove_user_from_team
begin
relation = UserTeam.find_by!(user_id: params[:user_id], team_id: @team.id)
relation.destroy!
flash[:success] = 'User was successfully removed. Farewell...'
rescue ActiveRecord::RecordNotFound => e
flash[:error] = 'User is not in the team anyway.'
rescue StandardError => e
flash[:error] = 'Oops, please try again.'
end

reload
end

private

class UserAlreadyPresentError < StandardError; end

def find_team
@team = Team.find(params[:id])
end

def team_users
@team_users = @team.users
end

def team_params
params.require(:team).permit(:name, :description)
end

def reload
redirect_back(fallback_location: edit_team_path)
end
end
2 changes: 2 additions & 0 deletions app/helpers/retros_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module RetrosHelper
end
3 changes: 3 additions & 0 deletions app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ require('jquery')

$(document).on('turbolinks:load', function () {
// $('.ui.dropdown').dropdown(); use this later on @canan
$('.message .close').on('click', function() {
$(this).closest('.message').hide();
});
})
5 changes: 5 additions & 0 deletions app/models/answer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Answer < ApplicationRecord
belongs_to :user
belongs_to :retro
has_many :entries, dependent: :destroy
end
4 changes: 4 additions & 0 deletions app/models/entry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Entry < ApplicationRecord
belongs_to :answer
validates :detail, presence: true, length: { minimum: 2, maximum: 255 }
end
2 changes: 2 additions & 0 deletions app/models/outcome.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Outcome < ApplicationRecord
end
11 changes: 11 additions & 0 deletions app/models/retro.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Retro < ApplicationRecord
# date and time validation
belongs_to :team
has_many :outcomes, dependent: :destroy
has_many :answers, dependent: :destroy

def active?
status == 'active'
end

end
2 changes: 2 additions & 0 deletions app/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ class Team < ApplicationRecord
validates :company_id, presence: true

belongs_to :company
has_many :user_teams, dependent: :destroy
has_many :users, through: :user_teams
has_many :retros
end
6 changes: 6 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ class User < ApplicationRecord
uniqueness: { case_sensitive: false },
format: { with: VALID_EMAIL_REGEX }

belongs_to :company, optional: true
has_many :user_teams, dependent: :destroy
has_many :teams, through: :user_teams

def already_present?(resource)
resource.users.include?(self)
end
end
2 changes: 2 additions & 0 deletions app/models/user_team.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class UserTeam < ApplicationRecord
belongs_to :team
belongs_to :user
validates :user_id, presence: true
validates :team_id, presence: true
end
4 changes: 0 additions & 4 deletions app/views/devise/registrations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,3 @@
<br />
</div>
</div>

<!-- <%= link_to "Back", :back %> -->


28 changes: 28 additions & 0 deletions app/views/layouts/_messages.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<% flash.each do |message_type, message_content| %>
<% if notice %>
<div class="ui container">
<div class="ui success message">
<i class="close icon"></i>
<div class="header">
<%= notice %>
</div>
</div>
</div>
<br/>
<% elsif alert %>
<div class="ui container">
<div class="ui error message">
<i class="close icon"></i>
<%= alert %>
</div>
</div>
<br/>
<% else %>
<div class="ui <%= message_type %> message transition">
<i class="close icon"></i>
<div class="header">
<%= message_content %>
</div>
</div>
<% end %>
<% end %>
11 changes: 4 additions & 7 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
<%= render 'layouts/navigation'%>
</div>
</div>
<% if notice %>
<p class="alert alert-success"><%= notice %></p>
<% end %>
<% if alert %>
<p class="alert alert-danger"><%= alert %></p>
<% end %>
<%= yield %>
<div class="ui container">
<%= render 'layouts/messages' %>
<%= yield %>
</div>
</body>
</html>
18 changes: 18 additions & 0 deletions app/views/retros/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div class="ui container">
<div class="column">
<%= form_for(:retro, url: retros_path, html: { method: :post, class: 'ui form'}) do |f| %>
<div class="field">
<%= f.label :team %>
<%= f.collection_select :team_id, current_user_teams, :id, :name %>
</div>
<div class="field">
<%= f.label :DateTime %>
<%= f.datetime_select :date %>
</div>
<div class="actions">
<%= f.button "Create", class: 'blue ui button' %>
<%= link_to "Cancel", root_path, class: 'ui button'%>
</div>
<% end %>
</div>
</div>
5 changes: 5 additions & 0 deletions app/views/retros/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="ui container">
<!-- will be replaced with answers -->
<h1>Retro show</h1>
<%= @retro.id %>
</div>
67 changes: 67 additions & 0 deletions app/views/teams/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<div class="ui container">
<h3 class="ui top attached header">
<i class="settings icon"></i>
<div class="content">
Team Settings
<div class="sub header">Manage your preferences</div>
</div>
</h3>
<div class="ui attached segment">
<div class="ui two column very relaxed grid">
<div class="column">
<%= form_for(@team, url: team_path(@team.id), html: { method: :put, class: 'ui form'}) do |f| %>
<div class="field">
<%= f.label :team_name %>
<%= f.text_field :name %>
</div>

<div class="field">
<%= f.label :team_description %>
<%= f.text_field :description %>
</div>

<div class="actions">
<%= f.button "Save", class: 'blue ui button' %>
<%= link_to "Cancel", @team, class: 'ui button'%>
</div>
<% end %>
</div>
<div class="column">
<div class="ui celled list">
<% @team_users.each do |user| %>
<div class="item">
<div class="right floated content">
<div class="ui buttons">
<button class="ui tiny basic blue button">Edit Permissions</button>
</div>
<%= link_to "Remove", remove_user_team_path(@team.id, user.id), data: { confirm: "Are you sure?" }, method: :delete, class: 'ui tiny red button' %>
</div>
<div class="content">
<div class="header">
<% if user.name? %>
<%= user.name %>
<% else %>
<%= user.email %>
<% end %>
</div>
<em><%= user.title %></em>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>
<div class="ui attached segment">
<%= form_for :user, url: add_user_team_path(@team.id), html: { method: :post, class: 'ui form'} do |f| %>
<div class="field">
<%= f.label :user_email %>
<%= f.email_field :email %>
</div>

<%= f.button "Add User", class: 'ui blue button' %>
<% end %>
</div>
<br />
<%= link_to "Delete Team", team_path(@team), data: { confirm: "Are you sure?" }, method: :delete, class: 'negative ui button' %>
</div>
29 changes: 24 additions & 5 deletions app/views/teams/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
<% @teams.each do |team| %>
<ul>
<li> <%= team.name %> </li>
</ul>
<% end %>
<div class="ui container">
<h3 class="ui top attached header">
<i class="users icon"></i>
<div class="content">
My Teams
</div>
</h3>
<div class="ui attached segment">
<div class="ui middle aligned animated selection list">
<% current_user_teams.each do |team| %>
<div class="item">
<div class="content">
<div class="header">
<%= link_to team.name, team %>
</div>
<div class="description">
<em><%= pluralize(team.users.count, 'member') %></em>
</div>
</div>
</div>
<% end %>
</div>
</div>
</div>
Loading

0 comments on commit 835ea95

Please sign in to comment.