From 34ed3419f6809b7c4d324e9b40eaa461b737b4a0 Mon Sep 17 00:00:00 2001 From: ifournight Date: Mon, 10 Apr 2017 14:10:28 +0800 Subject: [PATCH] Update feature, request specs --- app/controllers/activities_controller.rb | 39 ++++++------ .../api/v1/create_teams_controller.rb | 2 +- .../api/v1/create_todos_controller.rb | 2 +- app/controllers/create_todos_controller.rb | 10 +-- app/models/project.rb | 2 + app/views/activities/index.html.erb | 30 ++++++--- app/views/create_todos/_create_todo.html.erb | 1 + spec/features/user_create_todo_spec.rb | 27 +++++++- spec/features/user_sign_in_spec.rb | 19 ------ spec/features/view_activities_page_spec.rb | 61 ++++++++++++++++--- spec/features/view_home_page_spec.rb | 13 ---- spec/requests/api/v1/complete_todo_spec.rb | 6 +- spec/requests/api/v1/create_todo_spec.rb | 17 ++++-- spec/support/factory_helpers.rb | 18 +++++- 14 files changed, 159 insertions(+), 88 deletions(-) delete mode 100644 spec/features/user_sign_in_spec.rb delete mode 100644 spec/features/view_home_page_spec.rb diff --git a/app/controllers/activities_controller.rb b/app/controllers/activities_controller.rb index 5492581..8a609e0 100644 --- a/app/controllers/activities_controller.rb +++ b/app/controllers/activities_controller.rb @@ -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 diff --git a/app/controllers/api/v1/create_teams_controller.rb b/app/controllers/api/v1/create_teams_controller.rb index a275ab0..f53d665 100644 --- a/app/controllers/api/v1/create_teams_controller.rb +++ b/app/controllers/api/v1/create_teams_controller.rb @@ -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 diff --git a/app/controllers/api/v1/create_todos_controller.rb b/app/controllers/api/v1/create_todos_controller.rb index db31232..ab5aeec 100644 --- a/app/controllers/api/v1/create_todos_controller.rb +++ b/app/controllers/api/v1/create_todos_controller.rb @@ -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 diff --git a/app/controllers/create_todos_controller.rb b/app/controllers/create_todos_controller.rb index b1f8024..e6e3941 100644 --- a/app/controllers/create_todos_controller.rb +++ b/app/controllers/create_todos_controller.rb @@ -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 @@ -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 diff --git a/app/models/project.rb b/app/models/project.rb index c954ea0..615213e 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -11,6 +11,8 @@ def completed end end + has_many :activities + def collaborators Access .where( diff --git a/app/views/activities/index.html.erb b/app/views/activities/index.html.erb index 3c9ae5a..803abf8 100644 --- a/app/views/activities/index.html.erb +++ b/app/views/activities/index.html.erb @@ -1,19 +1,29 @@ -
+<%= content_tag :section, class: 'section', id: "activities-team_#{@team.id}" do %> <% if @activities.any? %> + <% @by_day_groups.each do |by_day_group| %> -
+ + <%= content_tag :section, id: "by-day_#{by_day_group[:day]}", class: 'section' do %> +

<%= by_day_group[:day] %>

<% by_day_group[:group].each do |project_id, by_project_group| %> <% project = Project.find(project_id) %> -

<%= link_to project.name, project %>

-
    - <% by_project_group.each do |activity| %> - <%= render activity %> + + <%= content_tag :section, id: "by-d_#{by_day_group[:day]}-p_#{project.id}", class: 'section' do %> +

    <%= link_to project.name, project %>

    +
      + <% by_project_group.each do |activity| %> + <%= render activity %> + <% end %> +
    <% end %> -
-
+ <% end %> -
+ + <% end %> + <% end %> + <% end %> -
+ +<% end %> diff --git a/app/views/create_todos/_create_todo.html.erb b/app/views/create_todos/_create_todo.html.erb index d277610..ce47a0b 100644 --- a/app/views/create_todos/_create_todo.html.erb +++ b/app/views/create_todos/_create_todo.html.erb @@ -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 %>

<% if create_todo.errors[:title].any? %> diff --git a/spec/features/user_create_todo_spec.rb b/spec/features/user_create_todo_spec.rb index e662968..e9c0608 100644 --- a/spec/features/user_create_todo_spec.rb +++ b/spec/features/user_create_todo_spec.rb @@ -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 diff --git a/spec/features/user_sign_in_spec.rb b/spec/features/user_sign_in_spec.rb deleted file mode 100644 index f588a47..0000000 --- a/spec/features/user_sign_in_spec.rb +++ /dev/null @@ -1,19 +0,0 @@ - -require 'rails_helper' - -RSpec.describe 'user sign in' do - scenario 'get redirect_to activities page' do - user = create(:user) - activity = create(:activity) - a = activity - - visit sign_in_url - within "#new_sign_in" do - fill_in 'sign_in_name', with: user.name - click_on 'Sign in!' - end - - expect(page).to have_css "#activity_#{a.id}", - text: "user #{a.user_id} #{a.action} #{a.subject_type} #{a.subject_id}" - end -end diff --git a/spec/features/view_activities_page_spec.rb b/spec/features/view_activities_page_spec.rb index 2c98fe7..32ef665 100644 --- a/spec/features/view_activities_page_spec.rb +++ b/spec/features/view_activities_page_spec.rb @@ -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 diff --git a/spec/features/view_home_page_spec.rb b/spec/features/view_home_page_spec.rb deleted file mode 100644 index d8ed668..0000000 --- a/spec/features/view_home_page_spec.rb +++ /dev/null @@ -1,13 +0,0 @@ -require 'rails_helper' - -RSpec.feature 'User create and submit a new todo' do - scenario 'he/she see the created todo' do - user = create(:user) - sign_in(user) - first_todo = create(:todo, title: 'First todo') - - visit root_url - - expect(page).to have_css "#todo_#{first_todo.id}" - end -end diff --git a/spec/requests/api/v1/complete_todo_spec.rb b/spec/requests/api/v1/complete_todo_spec.rb index c12875c..0d5658d 100644 --- a/spec/requests/api/v1/complete_todo_spec.rb +++ b/spec/requests/api/v1/complete_todo_spec.rb @@ -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 diff --git a/spec/requests/api/v1/create_todo_spec.rb b/spec/requests/api/v1/create_todo_spec.rb index 2f0017d..9768a43 100644 --- a/spec/requests/api/v1/create_todo_spec.rb +++ b/spec/requests/api/v1/create_todo_spec.rb @@ -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 diff --git a/spec/support/factory_helpers.rb b/spec/support/factory_helpers.rb index c430bdd..67f1842 100644 --- a/spec/support/factory_helpers.rb +++ b/spec/support/factory_helpers.rb @@ -10,6 +10,12 @@ 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, @@ -17,10 +23,9 @@ def create_logic_project(project_name: 'V coming', ).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, @@ -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|