Skip to content

Commit

Permalink
Update feature, request specs
Browse files Browse the repository at this point in the history
  • Loading branch information
ifournight committed Apr 10, 2017
1 parent 993e3c4 commit 34ed341
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 88 deletions.
39 changes: 19 additions & 20 deletions app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
class ActivitiesController < ApplicationController
def index
if Team.exists?(session[:team_id])
team = Team.find session[:team_id]
projects = current_user.anticipated_projects_in_team(team)
@team = Team.find session[:team_id]
projects = current_user.anticipated_projects_in_team(@team)
project_ids = projects.map(&:id)
@activities = Activity.where(project_id: project_ids).order('created_at DESC').to_a
else
@activities = Activity.order('created_at DESC').to_a
end
by_day = @activities.group_by_day(&:created_at)
@activities_by_day = {}
by_day.each do |day, group|
@activities_by_day[day] = {}
by_project = group.group_by(&:project_id)
by_project.each do |id, subgroup|
@activities_by_day[day][id] = subgroup

by_day = @activities.group_by_day(&:created_at)
@activities_by_day = {}
by_day.each do |day, group|
@activities_by_day[day] = {}
by_project = group.group_by(&:project_id)
by_project.each do |id, subgroup|
@activities_by_day[day][id] = subgroup
end
end
end

@by_day_groups = []
@activities_by_day.each do |day, group|
@by_day_groups << { day: day, group: group }
end
@by_day_groups = []
@activities_by_day.each do |day, group|
@by_day_groups << { day: day, group: group }
end

@by_day_groups.sort do |a, b|
(a[:day] <=> b[:day])
@by_day_groups.sort do |a, b|
(a[:day] <=> b[:day])
end
@by_day_groups.reverse!
end
@by_day_groups.reverse!
end
end
2 changes: 1 addition & 1 deletion app/controllers/api/v1/create_teams_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def create
@team = @create_team.do

if @team
render json_api: @team, status: :created
render json: @team, status: :created
else
render json: { errors: @create_team.errors.full_messages },
status: @create_team.status_code
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/create_todos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ def create
private

def create_todo_params
params.require(:create_todo).permit([:title, :creator_id])
params.require(:create_todo).permit([:title, :creator_id, :project_id])
end
end
10 changes: 6 additions & 4 deletions app/controllers/create_todos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ def create

if @new_todo
respond_to do |format|
format.html { redirect_to root_url }
format.html { redirect_to project_path(@new_todo) }
format.js {}
end
else
respond_to do |format|
format.html do
@todos = Todo.order('created_at DESC')
render 'home/index'
project = Project.find(@create_todo.project_id)
@uncompleted_todos = project.todos.uncompleted
@completed_todos = project.todos.completed
render 'projects/show'
end
format.js {}
end
Expand All @@ -22,6 +24,6 @@ def create
private

def create_todo_params
params.require(:create_todo).permit([:title, :creator_id])
params.require(:create_todo).permit([:title, :creator_id, :project_id])
end
end
2 changes: 2 additions & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def completed
end
end

has_many :activities

def collaborators
Access
.where(
Expand Down
30 changes: 20 additions & 10 deletions app/views/activities/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
<section class='section' id="todo-list-section">
<%= content_tag :section, class: 'section', id: "activities-team_#{@team.id}" do %>
<% if @activities.any? %>

<% @by_day_groups.each do |by_day_group| %>
<section class='section'>

<%= content_tag :section, id: "by-day_#{by_day_group[:day]}", class: 'section' do %>

<h2 class='title is-h2'><%= by_day_group[:day] %></h2>
<% by_day_group[:group].each do |project_id, by_project_group| %>
<% project = Project.find(project_id) %>
<h3 class='title is-h3'><%= link_to project.name, project %></h3>
<ul>
<% by_project_group.each do |activity| %>
<%= render activity %>

<%= content_tag :section, id: "by-d_#{by_day_group[:day]}-p_#{project.id}", class: 'section' do %>
<h3 class='title is-h3'><%= link_to project.name, project %></h3>
<ul>
<% by_project_group.each do |activity| %>
<%= render activity %>
<% end %>
</ul>
<% end %>
</ul>
<br>

<% end %>
</section>

<% end %>

<% end %>

<% end %>
</section>

<% end %>
1 change: 1 addition & 0 deletions app/views/create_todos/_create_todo.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<%= form_for create_todo, remote: true do |f| %>
<%= f.hidden_field :creator_id, value: create_todo.creator_id %>
<%= f.hidden_field :project_id, value: create_todo.project_id %>
<div class='field'>
<p class='control'>
<% if create_todo.errors[:title].any? %>
Expand Down
27 changes: 24 additions & 3 deletions spec/features/user_create_todo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,43 @@
RSpec.feature 'User create a new todo' do
before :each do
@user = create(:user, name: 'ifournight')
@team_citizen_4 = create_logic_team(owner: @user, team_name: 'Citizen 4')
@journey = create_logic_project_from_team(team: @team_citizen_4, project_name: 'journey', creator: @user)

sign_in @user

visit project_path(@journey)

@todo_title = 'First todo'
end

visit root_url
scenario 'he/she see the created todo' do
within '#new_create_todo' do
fill_in 'create_todo_title', with: @todo_title
click_on '创建任务'
end
end

scenario 'he/she see the created todo' do
expect(page).to have_content, @todo_title
end

context 'he/she submit empty titled todo' do
scenario 'stayed in this page' do
within '#new_create_todo' do
fill_in 'create_todo_title', with: ''
click_on '创建任务'
end

within '#new_create_todo' do
expect(page).to have_content "can't be blank"
end
end
end

scenario 'he/she visit activities page, see the related activity' do
within '#new_create_todo' do
fill_in 'create_todo_title', with: @todo_title
click_on '创建任务'
end

visit activities_path

Expand Down
19 changes: 0 additions & 19 deletions spec/features/user_sign_in_spec.rb

This file was deleted.

61 changes: 54 additions & 7 deletions spec/features/view_activities_page_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,62 @@
require 'rails_helper'

RSpec.feature 'User view activities page' do
scenario 'they see exsiting activities' do
user = create(:user)
activity = create(:activity)
sign_in(user)
before :each do
@user = create(:user, name: 'ifournight')
@momo = create(:user, name: 'momo')

@team_citizen_4 = create_logic_team(owner: @user, team_name: 'Citizen 4')
@team_private = create_logic_team(owner: @user, team_name: 'ifournight private')

add_team_member(team: @team_citizen_4, member: @momo, authority: 'member')
add_team_member(team: @team_private, member: @momo, authority: 'member')

@journey = create_logic_project_from_team(team: @team_citizen_4, project_name: 'journey', creator: @user)
@make_momo_star = create_logic_project_from_team(team: @team_citizen_4, project_name: 'make_momo_star', creator: @user)

@v_coming = create_logic_project_from_team(team: @team_private, project_name: 'v_coming', creator: @user)
@homework = create_logic_project_from_team(team: @team_private, project_name: 'tower_homework', creator: @user)

make_user_project_collaborator(@momo, @make_momo_star)
make_user_project_collaborator(@momo, @v_coming)
make_user_project_collaborator(@momo, @homework)

10.times do |i|
create_logic_todo(creator: @user, project: @journey, title: "Todo #{i * 4 + 1}")
create_logic_todo(creator: @user, project: @make_momo_star, title: "Todo #{i * 4 + 2}")
create_logic_todo(creator: @user, project: @v_coming, title: "Todo #{i * 4 + 3}")
create_logic_todo(creator: @user, project: @homework, title: "Todo #{i * 4 + 4}")
end
end

scenario 'he/she only see exsiting activities belongs_to current working team' do
visit sign_in_url
visit activities_url
sign_in(@momo)

# let rails know user is working on this project
visit project_path(@journey)
visit activities_path

expect(page).to have_current_path(activities_path)
expect(page).to have_css "#activities-team_#{@journey.team.id}"
expect(page).to have_no_css "#activities-team_#{@team_private.id}"
end

scenario 'he/she see activities belongs to projects only he/she anticipated' do
sign_in(@momo)

# let rails know user is working on this project
visit project_path(@journey)
visit activities_path

expect(page).to have_css "#activities-team_#{@journey.team.id}"

@make_momo_star.activities.each do |activity|
expect(page).to have_css "#activity_#{activity.id}"
end

a = activity
expect(page).to have_css "#activity_#{a.id}"
@journey.activities.each do |activity|
expect(page).to have_no_css "#activity_#{activity.id}"
end
end
end
13 changes: 0 additions & 13 deletions spec/features/view_home_page_spec.rb

This file was deleted.

6 changes: 4 additions & 2 deletions spec/requests/api/v1/complete_todo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

RSpec.describe 'POST /api/v1/complete_todos' do
before :each do
@todo = create(:todo)
@user = @todo.creator
@user = create(:user)
sign_in @user
@project = create_logic_project(team_name: 'Citizen 4', project_name: 'journey', creator: @user)

@todo = create_logic_todo(title: 'First todo', creator: @user, project: @project)
end

it 'completes the todo' do
Expand Down
17 changes: 11 additions & 6 deletions spec/requests/api/v1/create_todo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@
before :each do
@user = create(:user)
sign_in @user
@project = create_logic_project(team_name: 'Citizen 4', project_name: 'journey', creator: @user)
@todo_params = {
creator_id: @user.id,
project_id: @project.id,
title: 'First todo'
}
end

it 'creates the link' do
todo_params = build(:todo).attributes
post '/api/v1/create_todos', params: { create_todo: todo_params }
post '/api/v1/create_todos', params: { create_todo: @todo_params }

expect(response.status).to eq 201
expect(Todo.last.title).to eq todo_params['title']
expect(json_body['todo']['title']).to eq todo_params['title']
expect(Todo.last.title).to eq @todo_params[:title]
expect(json_body['todo']['title']).to eq @todo_params[:title]
end

context 'when there are invalid attributes' do
it 'returns a 422, with errors' do
todo_params = attributes_for(:todo, :invalid)
@todo_params = attributes_for(:todo, :invalid)

post '/api/v1/create_todos', params: { create_todo: todo_params }
post '/api/v1/create_todos', params: { create_todo: @todo_params }

expect(response.status).to eq 422
expect(json_body.fetch('errors')).not_to be_empty
Expand Down
18 changes: 16 additions & 2 deletions spec/support/factory_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ def create_logic_project(project_name: 'V coming',
creator:,
team_name: 'Citizen 4')
team = create_logic_team(team_name: team_name, owner: creator)
create_logic_project_from_team(project_name: project_name, creator: creator, team: team)
end

def create_logic_project_from_team(project_name: 'V coming',
creator:,
team:)
CreateProject.new(
project_name: project_name,
creator_id: creator.id,
team_id: team.id
).do
end

def create_logic_todo(title: 'First Todo',
def create_logic_todo(title:,
creator:,
project:)

CreateTodo.new(
project_id: project.id,
creator_id: creator.id,
Expand All @@ -44,6 +49,15 @@ def give_user_write_project_access(user, project)
access_type: Access::ACCESS_TYPE[:WRITE_PROJECT]
)
end

def make_user_project_collaborator(user, project)
Access.create(
user_id: user.id,
subject_id: project.id,
subject_type: 'Project',
access_type: Access::ACCESS_TYPE[:PROJECT_COLLABORATOR]
)
end
end

RSpec.configure do |config|
Expand Down

0 comments on commit 34ed341

Please sign in to comment.