Skip to content

Commit

Permalink
Create minimal home, project controller/ page
Browse files Browse the repository at this point in the history
  • Loading branch information
ifournight committed Apr 7, 2017
1 parent d44a9af commit 204d34a
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 31 deletions.
6 changes: 0 additions & 6 deletions app/controllers/activities_controller.rb

This file was deleted.

6 changes: 3 additions & 3 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class HomeController < ApplicationController
def index
@uncompleted_todos = Todo.where('completed = ?', false).order('edited_at DESC')
@completed_todos = Todo.where('completed = ?', true).order('edited_at DESC')
@create_todo = CreateTodo.new(creator_id: current_user.id)
@user = current_user
@owned_teams = current_user.owned_teams
@joined_teams = current_user.joined_teams
end
end
11 changes: 11 additions & 0 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ProjectsController < ApplicationController
def show
@project = Project.find(params[:id])
@uncompleted_todos = @project.todos.uncompleted
@completed_todos = @project.todos.completed

@create_todo = CreateTodo.new(creator_id: current_user.id, project_id: @project.id)

session[:team_id] = @project.team.id
end
end
10 changes: 9 additions & 1 deletion app/models/project.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
class Project < ApplicationRecord
belongs_to :team
belongs_to :creator, class_name: 'User'
has_many :todos
has_many :todos do
def uncompleted
where('completed = ?', false).order('edited_at DESC')
end

def completed
Todo.where('completed = ?', true).order('edited_at DESC')
end
end

def collaborators
Access
Expand Down
6 changes: 5 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ class User < ApplicationRecord
has_many :activities
has_many :team_memberships, foreign_key: 'member_id'
has_many :owned_teams, class_name: 'Team', foreign_key: 'owner_id'
has_many :joined_teams, through: :team_memberships, source: :member
has_many :joined_teams, through: :team_memberships, source: :team

validates :name, presence: true
validates :name, uniqueness: true

validates :name, length: 4..15

def anticipated_projects_in_team(team)
ProjectCollaboratorQuery.new.find_user_anticipated_projects_in_team(self, team)
end
end
16 changes: 15 additions & 1 deletion app/quries/project_collaborator_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,19 @@ def find_project_collaborators(project)
temp.map(&:user)
end
end
end

def find_user_anticipated_projects_in_team(user, team)
projects = team.projects.to_a.keep_if do |project|
is_user_a_collaborator(user, project)
end
end

def is_user_a_collaborator(user, project)
Access.has_access?(
user.id,
project.id,
project.class.name,
Access::ACCESS_TYPE[:PROJECT_COLLABORATOR]
)
end
end
54 changes: 35 additions & 19 deletions app/views/home/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
<section class='section' id="todo-list-section">
<h2 class='title is-2'>任务</h2>
<section class='section' id="team-list-section">

<% if @completed_todos.any? %>
<section>
<h3 class='title is-3'>未完成</h3>
<ul id="todo-list-uncompleted">
<%= render @uncompleted_todos %>
</ul>
</section>
<% if @owned_teams.any? %>
<section class='section'>
<h2 class='title is-2'>拥有的团队</h2>
<% @owned_teams.each do |team| %>
<h3 class='title is-3'><%= team.name.upcase %></h3>
<ul>
<% @user.anticipated_projects_in_team(team).each do |project| %>
<% if project.collaborators.include?(@user) %>
<%= content_tag_for :li, project do %>
<%= link_to project.name, project %>
<% end %>
<% end %>
<% end %>
</ul>
<% end %>
</section>
<% end %>
<% if @completed_todos.any? %>
<section>
<h3 class='title is-3'>已完成</h3>
<ul id="todo-list-completed">
<%= render @completed_todos %>
</ul>
</section>

<% if @joined_teams.any? %>
<section class='section'>
<h2 class='title is-2'>参与的团队</h2>
<% @joined_teams.each do |team| %>
<h3 class='title is-3'><%= team.name.upcase %></h3>
<ul>
<% @user.anticipated_projects_in_team(team).each do |project| %>
<% if project.collaborators.include?(@user) %>
<%= content_tag_for :li, project do %>
<%= link_to project.name, project %>
<% end %>
<% end %>
<% end %>
</ul>
<% end %>
</section>
<% end %>
<section id="todo-list-create">
<%= render @create_todo %>
</section>

</section>
23 changes: 23 additions & 0 deletions app/views/projects/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<section class='section' id="todo-list-section">

<% if @uncompleted_todos.any? %>
<section>
<h3 class='title is-3'>未完成</h3>
<ul id="todo-list-uncompleted">
<%= render @uncompleted_todos %>
</ul>
</section>
<% end %>
<% if @completed_todos.any? %>
<section>
<h3 class='title is-3'>已完成</h3>
<ul id="todo-list-completed">
<%= render @completed_todos %>
</ul>
</section>
<% end %>
<section id="todo-list-create">
<%= render @create_todo %>
</section>

</seciont>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

resources :reopen_todos, only: [:create]

resources :projects, only: [:show]

namespace :api do
namespace :v1 do
resources :create_todos, only: [:create]
Expand Down

0 comments on commit 204d34a

Please sign in to comment.